Search in sources :

Example 1 with CallbackWrapper

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();
}
Also used : java.util(java.util) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) BiFunction(java.util.function.BiFunction) CallbackRefSerializer(org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer) Log(org.nustaq.kontraktor.util.Log) org.nustaq.kontraktor(org.nustaq.kontraktor) Local(org.nustaq.kontraktor.annotations.Local) org.nustaq.kontraktor.remoting.base(org.nustaq.kontraktor.remoting.base) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 2 with CallbackWrapper

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();
}
Also used : java.util(java.util) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) BiFunction(java.util.function.BiFunction) CallbackRefSerializer(org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer) Log(org.nustaq.kontraktor.util.Log) org.nustaq.kontraktor(org.nustaq.kontraktor) Local(org.nustaq.kontraktor.annotations.Local) org.nustaq.kontraktor.remoting.base(org.nustaq.kontraktor.remoting.base) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallbackRefSerializer(org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper)

Aggregations

java.util (java.util)2 BiFunction (java.util.function.BiFunction)2 org.nustaq.kontraktor (org.nustaq.kontraktor)2 CallerSideMethod (org.nustaq.kontraktor.annotations.CallerSideMethod)2 Local (org.nustaq.kontraktor.annotations.Local)2 CallbackWrapper (org.nustaq.kontraktor.impl.CallbackWrapper)2 org.nustaq.kontraktor.remoting.base (org.nustaq.kontraktor.remoting.base)2 CallbackRefSerializer (org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer)2 RemoteCallEntry (org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry)2 Log (org.nustaq.kontraktor.util.Log)2