Search in sources :

Example 6 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class ReactAppSession method loadSessionData.

@Override
protected IPromise loadSessionData(String sessionId, ISessionStorage storage) {
    Promise res = new Promise();
    Log.Info(this, "loadSessionData " + sessionId);
    // let's cache the record of current user (take care => stale state in case of multiple clients from same user)
    storage.getUser(getUserKey()).then((user, err) -> {
        if (user != null) {
            userRecord = user;
            res.resolve(userRecord);
        } else {
            res.reject(err);
        }
    });
    return res;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise)

Example 7 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class ReactApp method register.

@Remoted
public IPromise register(String nick, String pwd, String text) {
    Promise p = new Promise();
    sessionStorage.putUserIfNotPresent(MapRecord.New(nick).put("pwd", pwd).put("text", text).put("verified", true)).then((r, e) -> {
        if (!r) {
            p.reject("user " + nick + " already exists");
        } else {
            p.resolve(true);
        }
    });
    return p;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) Remoted(org.nustaq.kontraktor.annotations.Remoted)

Example 8 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class ReactApp method getDirectRequestResponse.

@Override
protected IPromise<String> getDirectRequestResponse(String path) {
    String[] split = path.split("/");
    if (split.length == 0)
        return resolve("<html>Invalid Link</html>");
    Promise res = new Promise();
    sessionStorage.takeToken(split[split.length - 1], false).then((token, err) -> {
        if (token != null) {
            res.resolve("<html>User confirmed: '" + token.getUserId() + "' data:" + token.getData() + "</html>");
            // lets increment a count for that
            // note: not atomic
            sessionStorage.getUser(token.getUserId()).then(record -> {
                // increment
                record.put("count", record.getInt("count") + 1);
                // save
                sessionStorage.putUser(record);
                // session cached record is stale now => update
                // this is a bad example, should use events instead and ofc
                // default session storage is not a database replacement
                sessions.values().stream().filter(sess -> sess.getUserKey().equals(token.getUserId())).forEach(sess -> ((ReactAppSession) sess).updateUserRecord(record));
            });
        } else
            res.reject("<html>Expired or invalid Link</html>");
    });
    return res;
}
Also used : Secured(org.nustaq.kontraktor.annotations.Secured) MapRecord(org.nustaq.reallive.records.MapRecord) IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) BasicWebAppActor(org.nustaq.kontraktor.weblication.BasicWebAppActor) Remoted(org.nustaq.kontraktor.annotations.Remoted) IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise)

Example 9 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class Routing method connectClient.

public static IPromise<Object> connectClient(ConnectableActor connectable, Consumer<Actor> disconnectCallback) {
    Promise p = promise();
    connectable.connect(null, disconnectCallback).then((r, e) -> {
        if (r != null) {
            getPinger().cyclic(CLIENT_PING_INTERVAL_MS, () -> {
                long[] paids = null;
                if (r.__clientConnection != null)
                    paids = r.__clientConnection.getRemotedActorIds();
                // System.out.println("remoted ids:"+ Arrays.toString(paids));
                // System.out.println("published ids:"+ Arrays.toString(r.__clientConnection.getPublishedActorIds()));
                r.router$clientPing(System.currentTimeMillis(), paids);
                return true;
            });
        }
        p.complete(r, e);
    });
    return p;
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise)

Example 10 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class BasicTest method testTimeout.

@Test
public void testTimeout() throws InterruptedException {
    AtomicInteger count = new AtomicInteger(0);
    IPromise p = new Promise().timeoutIn(1000);
    Thread.sleep(2000);
    p.then(new Callback() {

        @Override
        public void complete(Object result, Object error) {
            System.out.println("res:" + result + " err:" + error);
            count.incrementAndGet();
        }
    }).onTimeout(new Consumer() {

        @Override
        public void accept(Object o) {
            count.addAndGet(8);
            System.out.println("the EXPECTED timout");
        }
    });
    Thread.sleep(500);
    assertTrue(count.get() == 9);
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) IPromise(org.nustaq.kontraktor.IPromise) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

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