Search in sources :

Example 1 with Actor

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

the class UndertowWebsocketServerConnector method connect.

@Override
public void connect(Actor facade, Function<ObjectSocket, ObjectSink> factory) throws Exception {
    PathHandler server = getServer(port).getFirst();
    server.addExactPath(path, Handlers.websocket((exchange, channel) -> {
        // connection callback
        final CountDownLatch latch = new CountDownLatch(1);
        Runnable runnable = () -> {
            UTWebObjectSocket objectSocket = new UTWebObjectSocket(exchange, channel, sendStrings, sendSid);
            ObjectSink sink = factory.apply(objectSocket);
            objectSocket.setSink(sink);
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onClose(WebSocketChannel webSocketChannel, StreamSourceFrameChannel channel) throws IOException {
                    onCloseMessage(null, webSocketChannel);
                }

                @Override
                protected void onCloseMessage(CloseMessage cm, WebSocketChannel channel) {
                    try {
                        channel.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    sink.sinkClosed();
                    try {
                        objectSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    channel.getReceiveSetter().set(null);
                }

                @Override
                protected void onError(WebSocketChannel channel, Throwable error) {
                    Log.Debug(this, error);
                    sink.sinkClosed();
                }

                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
                    String data = message.getData();
                    byte[] bytez = data.getBytes("UTF-8");
                    sink.receiveObject(objectSocket.getConf().asObject(bytez), null, null);
                }

                @Override
                protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
                    ByteBuffer[] data = message.getData().getResource();
                    byte[] bytez = Buffers.take(data, 0, data.length);
                    sink.receiveObject(objectSocket.getConf().asObject(bytez), null, null);
                }
            });
            latch.countDown();
        };
        facade.execute(runnable);
        try {
            // fix provided by billtob, race condition during connection init
            latch.await(3000, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        channel.resumeReceives();
    }));
}
Also used : Handlers(io.undertow.Handlers) ChannelListener(org.xnio.ChannelListener) Promise(org.nustaq.kontraktor.Promise) IOException(java.io.IOException) io.undertow.websockets.core(io.undertow.websockets.core) UUID(java.util.UUID) Pair(org.nustaq.kontraktor.util.Pair) Function(java.util.function.Function) org.nustaq.kontraktor.remoting.base(org.nustaq.kontraktor.remoting.base) ByteBuffer(java.nio.ByteBuffer) Undertow(io.undertow.Undertow) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) PathHandler(io.undertow.server.handlers.PathHandler) java.lang.ref(java.lang.ref) IPromise(org.nustaq.kontraktor.IPromise) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) FSTUtil(org.nustaq.serialization.util.FSTUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Actor(org.nustaq.kontraktor.Actor) Log(org.nustaq.kontraktor.util.Log) Buffers(org.xnio.Buffers) WebObjectSocket(org.nustaq.kontraktor.remoting.websockets.WebObjectSocket) PathHandler(io.undertow.server.handlers.PathHandler) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Example 2 with Actor

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

the class MyKontraktorServlet method createAndInitFacadeApp.

@Override
protected Actor createAndInitFacadeApp(ServletConfig config) {
    Actor facade = Actors.AsActor(ServletApp.class);
    ((ServletApp) facade).init(4);
    return facade;
}
Also used : Actor(org.nustaq.kontraktor.Actor)

Example 3 with Actor

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

the class ActorsImpl method makeProxy.

public Actor makeProxy(Actor realActor, Class<? extends Actor> clz, DispatcherThread disp, int qs) {
    try {
        if (realActor == null) {
            if (Modifier.isAbstract(clz.getModifiers())) {
                // pseudo interface/abstract
                realActor = getFactory().instantiateProxy(clz, realActor);
                Field target = realActor.getClass().getField("__target");
                target.setAccessible(true);
                target.set(realActor, realActor);
            } else
                realActor = clz.newInstance();
        }
        if (qs <= 100)
            qs = disp.getScheduler().getDefaultQSize();
        qs = FSTUtil.nextPow2(qs);
        realActor.__mailbox = createQueue(qs);
        realActor.__mailboxCapacity = qs;
        // wtf
        realActor.__mbCapacity = realActor.__mailboxCapacity;
        realActor.__cbQueue = createQueue(qs);
        Actor selfproxy = getFactory().instantiateProxy(clz, realActor);
        realActor.__self = selfproxy;
        selfproxy.__self = selfproxy;
        selfproxy.__mailbox = realActor.__mailbox;
        selfproxy.__mailboxCapacity = qs;
        selfproxy.__mbCapacity = realActor.__mbCapacity;
        selfproxy.__cbQueue = realActor.__cbQueue;
        realActor.__scheduler = disp.getScheduler();
        selfproxy.__scheduler = disp.getScheduler();
        realActor.__currentDispatcher = disp;
        selfproxy.__currentDispatcher = disp;
        disp.addActor(realActor);
        return selfproxy;
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) Actor(org.nustaq.kontraktor.Actor)

Example 4 with Actor

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

the class RemotingPlayground method main.

public static void main(String[] args) {
    AServ server = Actors.AsActor(AServ.class);
    TCPNIOPublisher publisher = new TCPNIOPublisher(server, 5678);
    publisher.publish(actor -> System.out.println("actor " + actor + " disconnected")).await();
    AServ client = (AServ) new TCPConnectable(AServ.class, "localhost", 5678).connect().await();
    client.pingme();
}
Also used : TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) Actor(org.nustaq.kontraktor.Actor) Actors(org.nustaq.kontraktor.Actors) TCPClientConnector(org.nustaq.kontraktor.remoting.tcp.TCPClientConnector) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher)

Example 5 with Actor

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

the class UntypedGreeterClient method main.

// NOTE: due to a temporary bug in 4.03, untyped java clients DO NOT WORK
// with Java-implemented Servers. Use typed clients meanwhile
public static void main(String[] args) {
    Actor remote = (Actor) new WebSocketConnectable().actorClass(Actor.class).url("ws://localhost:3999").serType(SerializerType.JsonNoRef).connect((x, y) -> System.out.println("disconnect " + x + " " + y)).await();
    remote.ask("greet", "Kontraktor", 1000).then((r, e) -> System.out.println(r));
}
Also used : Actor(org.nustaq.kontraktor.Actor) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)

Aggregations

Actor (org.nustaq.kontraktor.Actor)18 IPromise (org.nustaq.kontraktor.IPromise)8 Promise (org.nustaq.kontraktor.Promise)8 ByteBuffer (java.nio.ByteBuffer)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Actors (org.nustaq.kontraktor.Actors)2 TCPNIOPublisher (org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher)2 WebSocketConnectable (org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)2 Pair (org.nustaq.kontraktor.util.Pair)2 Handlers (io.undertow.Handlers)1 Undertow (io.undertow.Undertow)1 PathHandler (io.undertow.server.handlers.PathHandler)1 io.undertow.websockets.core (io.undertow.websockets.core)1 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)1 EOFException (java.io.EOFException)1 Serializable (java.io.Serializable)1 java.lang.ref (java.lang.ref)1