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();
}));
}
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;
}
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);
}
}
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();
}
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));
}
Aggregations