Search in sources :

Example 1 with ObjectSink

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);
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) ObjectSink(org.nustaq.kontraktor.remoting.base.ObjectSink)

Example 2 with ObjectSink

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);
        }
    });
}
Also used : ObjectSink(org.nustaq.kontraktor.remoting.base.ObjectSink)

Example 3 with ObjectSink

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);
            }
        });
    });
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) ObjectSink(org.nustaq.kontraktor.remoting.base.ObjectSink)

Example 4 with ObjectSink

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;
}
Also used : EOFException(java.io.EOFException) ObjectSink(org.nustaq.kontraktor.remoting.base.ObjectSink) IOException(java.io.IOException) ObjectSocket(org.nustaq.kontraktor.remoting.base.ObjectSocket) TCPObjectSocket(org.nustaq.net.TCPObjectSocket) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 5 with ObjectSink

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Executor(java.util.concurrent.Executor) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ObjectSink(org.nustaq.kontraktor.remoting.base.ObjectSink)

Aggregations

ObjectSink (org.nustaq.kontraktor.remoting.base.ObjectSink)5 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Executor (java.util.concurrent.Executor)1 HttpPost (org.apache.http.client.methods.HttpPost)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 IPromise (org.nustaq.kontraktor.IPromise)1 Promise (org.nustaq.kontraktor.Promise)1 ObjectSocket (org.nustaq.kontraktor.remoting.base.ObjectSocket)1 TCPObjectSocket (org.nustaq.net.TCPObjectSocket)1