Search in sources :

Example 1 with RemoteScheduler

use of org.nustaq.kontraktor.impl.RemoteScheduler in project kontraktor by RuedigerMoeller.

the class ActorClient method connect.

public IPromise<T> connect(int qsiz, Consumer<Actor> discon) {
    Promise<T> result = new Promise<>();
    try {
        client.connect(writesocket -> {
            Actor facadeProxy = Actors.AsActor(facadeClass, new RemoteScheduler(qsiz));
            facadeProxy.__remoteId = 1;
            AtomicReference<ObjectSocket> socketRef = new AtomicReference<>(writesocket);
            ConnectionRegistry reg = new ConnectionRegistry(coding) {

                @Override
                public Actor getFacadeProxy() {
                    return facadeProxy;
                }

                @Override
                public AtomicReference<ObjectSocket> getWriteObjectSocket() {
                    return socketRef;
                }
            };
            reg.setDisconnectHandler(discon);
            if (coding.getCrossPlatformShortClazzNames() != null)
                reg.getConf().registerCrossPlatformClassMappingUseSimpleName(coding.getCrossPlatformShortClazzNames());
            writesocket.setConf(reg.getConf());
            // ensure running in actor thread
            Actor.current();
            ObjectSink objectSink = new ObjectSink() {

                @Override
                public void receiveObject(ObjectSink sink, Object received, List<IPromise> createdFutures, Object securityContext) {
                    try {
                        reg.receiveObject(socketRef.get(), sink, received, createdFutures, securityContext);
                    } catch (Exception e) {
                        FSTUtil.rethrow(e);
                    }
                }

                @Override
                public void sinkClosed() {
                    reg.disconnect();
                }
            };
            reg.registerRemoteRefDirect(facadeProxy);
            poller.get().scheduleSendLoop(reg).then(() -> {
                objectSink.sinkClosed();
            });
            result.resolve((T) facadeProxy);
            return objectSink;
        });
    } catch (Exception e) {
        if (!result.isSettled())
            result.reject(e);
        else
            e.printStackTrace();
    }
    return result;
}
Also used : RemoteScheduler(org.nustaq.kontraktor.impl.RemoteScheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) Actor(org.nustaq.kontraktor.Actor) List(java.util.List)

Aggregations

List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Actor (org.nustaq.kontraktor.Actor)1 IPromise (org.nustaq.kontraktor.IPromise)1 Promise (org.nustaq.kontraktor.Promise)1 RemoteScheduler (org.nustaq.kontraktor.impl.RemoteScheduler)1