Search in sources :

Example 1 with ObjectSocket

use of org.nustaq.kontraktor.remoting.base.ObjectSocket 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)

Aggregations

EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ObjectSink (org.nustaq.kontraktor.remoting.base.ObjectSink)1 ObjectSocket (org.nustaq.kontraktor.remoting.base.ObjectSocket)1 TCPObjectSocket (org.nustaq.net.TCPObjectSocket)1