Search in sources :

Example 51 with IPromise

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;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 52 with IPromise

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;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) ActorClient(org.nustaq.kontraktor.remoting.base.ActorClient)

Example 53 with IPromise

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);
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) ActorServer(org.nustaq.kontraktor.remoting.base.ActorServer) Coding(org.nustaq.kontraktor.remoting.encoding.Coding) PathHandler(io.undertow.server.handlers.PathHandler) Undertow(io.undertow.Undertow)

Example 54 with IPromise

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;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) JsonObject(com.eclipsesource.json.JsonObject)

Example 55 with IPromise

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;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) ArchiveException(org.apache.commons.compress.archivers.ArchiveException)

Aggregations

IPromise (org.nustaq.kontraktor.IPromise)58 Promise (org.nustaq.kontraktor.Promise)56 Remoted (org.nustaq.kontraktor.annotations.Remoted)6 Actor (org.nustaq.kontraktor.Actor)5 JsonObject (com.eclipsesource.json.JsonObject)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 ByteBuffer (java.nio.ByteBuffer)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3 TableSpaceActor (org.nustaq.reallive.impl.tablespace.TableSpaceActor)3 JsonValue (com.eclipsesource.json.JsonValue)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2