use of org.nustaq.kontraktor.Promise 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;
}
use of org.nustaq.kontraktor.Promise 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;
}
use of org.nustaq.kontraktor.Promise 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;
}
use of org.nustaq.kontraktor.Promise 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;
}
use of org.nustaq.kontraktor.Promise 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);
}
Aggregations