use of org.nustaq.kontraktor.impl.CallbackWrapper in project kontraktor by RuedigerMoeller.
the class AbstractKrouter method forwardMultiCallInternal.
// tweak handling such it can deal with double results, runs in caller thread (but single threaded => remoteref registry)
@CallerSideMethod
protected void forwardMultiCallInternal(RemoteCallEntry rceIn, Actor remoteRef, ConnectionRegistry clientRemoteRegistry, boolean[] done, Callback[] selected) {
RemoteCallEntry rce = rceIn.createCopy();
Runnable toRun = () -> {
ConnectionRegistry serviceRemoteReg = remoteRef.__self.__clientConnection;
if (// targets krouter facade
rce.getReceiverKey() == 1)
rce.setReceiverKey(remoteRef.__remoteId);
if (rce.getFutureKey() != 0) {
long prevFuturekey = rce.getFutureKey();
Promise p = new Promise();
long cbid = serviceRemoteReg.registerPublishedCallback(p);
rce.setFutureKey(-cbid);
p.then((r, e) -> {
self().execute(() -> {
serviceRemoteReg.removePublishedObject(cbid);
// websocket close is unreliable
if (serviceRemoteReg.isTerminated()) {
handleServiceDiscon(remoteRef);
return;
}
if (!done[0]) {
done[0] = true;
RemoteCallEntry cbrce = (RemoteCallEntry) r;
cbrce.setReceiverKey(prevFuturekey);
clientRemoteRegistry.forwardRemoteMessage(cbrce);
}
});
});
}
if (rce.getCB() != null) {
Callback cb = rce.getCB();
CallbackWrapper[] wrapperTrick = { null };
CallbackWrapper wrapper = wrapperTrick[0] = new CallbackWrapper(self(), (r, e) -> {
if (selected[0] == null) {
selected[0] = wrapperTrick[0];
}
if (selected[0] == wrapperTrick[0])
cb.complete(r, e);
});
rce.setCB(wrapper);
}
// websocket close is unreliable
if (!serviceRemoteReg.isTerminated())
serviceRemoteReg.forwardRemoteMessage(rce);
else {
handleServiceDiscon(remoteRef);
}
};
if (Thread.currentThread() != getCurrentDispatcher())
self().execute(toRun);
else
toRun.run();
}
use of org.nustaq.kontraktor.impl.CallbackWrapper in project kontraktor by RuedigerMoeller.
the class AbstractKrouter method forwardCallInternal.
// call from client to service
// mission: patch remotecall ids such everything is mappe via Krouter registry
protected void forwardCallInternal(RemoteCallEntry rce, Actor remoteRef, ConnectionRegistry clientRemoteRegistry) {
RemoteCallEntry finalRce = rce.createCopy();
Runnable toRun = () -> {
ConnectionRegistry serviceRemoteReg = (ConnectionRegistry) remoteRef.__self.__clientConnection;
if (// targets krouter facade
finalRce.getReceiverKey() == 1)
finalRce.setReceiverKey(remoteRef.__remoteId);
if (finalRce.getFutureKey() != 0) {
long prevFuturekey = finalRce.getFutureKey();
Promise p = new Promise();
long cbid = serviceRemoteReg.registerPublishedCallback(p);
finalRce.setFutureKey(-cbid);
p.then((r, e) -> {
serviceRemoteReg.removePublishedObject(cbid);
// websocket close is unreliable
if (serviceRemoteReg.isTerminated()) {
handleServiceDiscon(remoteRef);
return;
}
RemoteCallEntry cbrce = (RemoteCallEntry) r;
cbrce.setReceiverKey(prevFuturekey);
self().execute(() -> clientRemoteRegistry.forwardRemoteMessage(cbrce));
});
}
if (finalRce.getCB() != null) {
CallbackRefSerializer.MyRemotedCallback cb = (CallbackRefSerializer.MyRemotedCallback) finalRce.getCB();
long id = cb.getId();
finalRce.setCB(new CallbackWrapper(self(), (res, err) -> {
RemoteCallEntry rcerouted = (RemoteCallEntry) res;
rcerouted.setReceiverKey(id);
clientRemoteRegistry.forwardRemoteMessage(rcerouted);
}) {
@Override
public boolean isRouted() {
return true;
}
});
}
// websocket close is unreliable
if (!serviceRemoteReg.isTerminated())
serviceRemoteReg.forwardRemoteMessage(finalRce);
else {
handleServiceDiscon(remoteRef);
}
};
if (Thread.currentThread() != getCurrentDispatcher())
self().execute(toRun);
else
toRun.run();
}
Aggregations