use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.
the class AsyncHttpActor method postWithContext.
public IPromise<HttpResponse> postWithContext(String url, String postData, HttpContext ctx, String... headers) {
Promise res = new Promise();
if (url == null) {
int debug = 1;
}
try {
HttpPost req = new HttpPost(url);
setHeaders(req, headers);
beChrome(req);
req.setEntity(new StringEntity(postData));
getClient().execute(req, ctx, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse result) {
// switch to actor thread
execute(() -> res.resolve(result));
}
@Override
public void failed(Exception ex) {
// switch to actor thread
execute(() -> res.reject(ex));
}
@Override
public void cancelled() {
// switch to actor thread
execute(() -> res.reject("cancelled"));
}
});
} catch (Throwable th) {
Log.Warn(this, "get fail " + th + " " + url);
res.reject(th);
}
return res;
}
use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.
the class HttpConnectable method connect.
@Override
public <T extends Actor> IPromise<T> connect(Callback<ActorClientConnector> disconnectCallback, Consumer<Actor> actorDisconnecCB) {
HttpClientConnector con = new HttpClientConnector(this);
con.disconnectCallback = disconnectCallback;
ActorClient actorClient = new ActorClient(con, actorClz, coding);
Promise p = new Promise();
con.getRefPollActor().execute(() -> {
Thread.currentThread().setName("Http Ref Polling");
actorClient.connect(inboundQueueSize, null).then(p);
});
return p;
}
use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.
the class HttpPublisher method publish.
@Override
public IPromise<ActorServer> publish(Consumer<Actor> disconnectCallback) {
ActorServer actorServer;
try {
Pair<PathHandler, Undertow> serverPair = Http4K.get().getServer(port, hostName);
UndertowHttpServerConnector con = new UndertowHttpServerConnector(facade);
con.setConnectionVerifier(connectionVerifier);
con.setSessionTimeout(sessionTimeout);
actorServer = new ActorServer(con, facade, coding == null ? new Coding(SerializerType.FSTSer) : coding);
con.setActorServer(actorServer);
actorServer.start(disconnectCallback);
serverPair.getFirst().addPrefixPath(urlPath, con);
} catch (Exception e) {
Log.Warn(null, e);
return new Promise<>(null, e);
}
return new Promise<>(actorServer);
}
use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.
the class JNPM method getVersions.
protected IPromise<List<String>> getVersions(String module) {
Promise res = new Promise();
http.getContent(config.getRepo() + "/" + module).then((cont, err) -> {
if (cont != null) {
JsonObject parse = Json.parse(cont).asObject().get("versions").asObject();
List<String> versions = new ArrayList<>();
parse.forEach(mem -> versions.add(mem.getName()));
res.resolve(versions);
} else {
res.reject(err);
}
});
return res;
}
use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.
the class JNPM method downLoadAndInstall.
private IPromise downLoadAndInstall(String tarUrl, String moduleName, String resolvedVersion, File targetDir) {
Promise p = new Promise();
String moduleKey = createModuleKey(moduleName, targetDir);
List<Promise> promlist = packagesUnderway.get(moduleKey);
if (promlist != null) {
if (promlist.size() == 0) {
// timing: has already arrived and proms resolved
p.resolve(true);
} else {
packagesUnderway.get(moduleKey).add(p);
}
return p;
}
Log.Info(this, String.format("installing '%s' in %s.", moduleName + "@" + resolvedVersion, targetDir.getAbsolutePath()));
ArrayList list = new ArrayList();
packagesUnderway.put(moduleKey, list);
list.add(p);
checkThread();
http.getContentBytes(tarUrl).then((resp, err) -> {
execInThreadPool(() -> {
// multithread unpacking (java io blocks, so lets mass multithread)
byte[] b = resp;
try {
b = AsyncHttpActor.unGZip(b, b.length * 10);
File outputDir = new File(targetDir, moduleName);
unTar(new ByteArrayInputStream(b), outputDir);
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (ArchiveException e) {
e.printStackTrace();
return false;
}
return true;
}).then(() -> {
checkThread();
// Log.Info(this,String.format("installed '%s' in %s.", moduleName+"@"+ resolvedVersion, nodeModulesDir.getAbsolutePath()));
packagesUnderway.get(moduleKey).forEach(prom -> prom.resolve(true));
packagesUnderway.get(moduleKey).clear();
});
});
return p;
}
Aggregations