use of org.nustaq.kontraktor.remoting.base.ObjectSink in project kontraktor by RuedigerMoeller.
the class JSR356ClientConnector method connect.
@Override
public IPromise connect(Function<ObjectSocket, ObjectSink> factory) throws Exception {
endpoint = new WSClientEndpoint(uri, null);
ObjectSink sink = factory.apply(endpoint);
endpoint.setSink(sink);
return new Promise<>(null);
}
use of org.nustaq.kontraktor.remoting.base.ObjectSink in project kontraktor by RuedigerMoeller.
the class _JSR356ServerConnector method onOpen.
@Override
public void onOpen(Session session, EndpointConfig config) {
Log.Info(this, "onClose " + session + " " + config);
MySocket objectsocket = new MySocket(session);
ObjectSink sink = factory.apply(objectsocket);
session.addMessageHandler(new MessageHandler.Whole<byte[]>() {
@Override
public void onMessage(byte[] msg) {
System.out.println("MESSAGE:" + new String(msg, 0));
sink.receiveObject(msg, null, null);
}
});
}
use of org.nustaq.kontraktor.remoting.base.ObjectSink in project kontraktor by RuedigerMoeller.
the class MyWebSocketConnector method onOpen.
@Override
public void onOpen(Session session, EndpointConfig config) {
// System.out.println(session.getId() + " has opened a connection");
// never received in tcat 8.5 ??
// session.addMessageHandler(byte[].class, message -> {
// System.out.println("msg byte[] "+new String(message,0));
// }
// );
facade.execute(() -> {
ServletWebObjectSocket socket = new ServletWebObjectSocket(session);
ObjectSink sink = sinkFactory.apply(socket);
socket.setSink(sink);
sessions.put(session.getId(), socket);
session.addMessageHandler(String.class, message -> {
// System.out.println("msg String "+message);
try {
Object o = socket.getConf().asObject(message.getBytes("UTF-8"));
socket.getSink().receiveObject(o, null, null);
} catch (UnsupportedEncodingException e) {
Log.Warn(this, e);
}
});
});
}
use of org.nustaq.kontraktor.remoting.base.ObjectSink in project kontraktor by RuedigerMoeller.
the class TCPServerConnector method acceptLoop.
protected Promise acceptLoop(Actor facade, int port, Function<ObjectSocket, ObjectSink> factory, Promise p) {
try {
numberOfThreads.incrementAndGet();
acceptSocket = new ServerSocket(port);
p.resolve();
while (!acceptSocket.isClosed()) {
Socket clientSocket = acceptSocket.accept();
clientSockets.add(clientSocket);
MyTCPSocket objectSocket = new MyTCPSocket(clientSocket);
facade.execute(() -> {
ObjectSink sink = factory.apply(objectSocket);
new Thread(() -> {
try {
numberOfThreads.incrementAndGet();
while (!clientSocket.isClosed()) {
try {
Object o = objectSocket.readObject();
sink.receiveObject(o, null, null);
} catch (Exception e) {
if (e instanceof EOFException == false && e instanceof SocketException == false)
Log.Warn(this, e);
try {
clientSocket.close();
} catch (IOException e1) {
Log.Warn(this, e1);
}
}
}
sink.sinkClosed();
} finally {
clientSockets.remove(clientSocket);
numberOfThreads.decrementAndGet();
}
}, "tcp receiver").start();
});
}
} catch (Exception e) {
Log.Info(this, e.getMessage());
if (!p.isSettled())
p.reject(e);
} finally {
if (!p.isSettled())
p.reject("conneciton failed");
try {
acceptSocket.close();
} catch (IOException e) {
Log.Warn(this, e);
}
numberOfThreads.decrementAndGet();
}
return p;
}
use of org.nustaq.kontraktor.remoting.base.ObjectSink in project kontraktor by RuedigerMoeller.
the class HttpClientConnector method connect.
@Override
public IPromise connect(Function<ObjectSocket, ObjectSink> factory) throws Exception {
Promise res = new Promise();
byte[] message = authConf.asByteArray(authData);
if (HttpClientConnector.DumpProtocol) {
try {
System.out.println("auth-req:" + new String(message, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
HttpPost req = new HttpPost(cfg.getUrl());
req.addHeader(NO_CACHE);
req.setEntity(new ByteArrayEntity(message));
Executor actor = Actor.current();
getClient().execute(req, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse result) {
if (result.getStatusLine().getStatusCode() != 200) {
closeClient();
res.reject(result.getStatusLine().getStatusCode());
return;
}
String cl = result.getFirstHeader("Content-Length").getValue();
int len = Integer.parseInt(cl);
if (len > 0) {
byte[] resp = new byte[len];
try {
result.getEntity().getContent().read(resp);
if (HttpClientConnector.DumpProtocol) {
try {
System.out.println("auth-resp:" + new String(resp, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
actor.execute(() -> {
sessionId = (String) authConf.asObject(resp);
MyHttpWS myHttpWS = new MyHttpWS(cfg.getUrl() + "/" + sessionId);
ObjectSink sink = factory.apply(myHttpWS);
myHttpWS.setSink(sink);
startLongPoll(myHttpWS);
res.resolve();
});
} catch (Exception e) {
Log.Warn(this, e);
res.reject(e);
}
} else {
res.reject("connection rejected, no connection id");
}
}
@Override
public void failed(Exception ex) {
res.reject(ex);
}
@Override
public void cancelled() {
res.reject("canceled");
}
});
return res;
}
Aggregations