Search in sources :

Example 1 with WrappedProcessor

use of net.morimekta.providence.server.WrappedProcessor in project providence by morimekta.

the class SocketServer method process.

@SuppressWarnings("unchecked")
private void process(long startTime, Socket socket) {
    try (Socket ignore = socket;
        BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
        IOMessageReader reader = new IOMessageReader(in, serializer);
        IOMessageWriter writer = new IOMessageWriter(out, serializer)) {
        while (socket.isConnected()) {
            AtomicReference<PServiceCall> callRef = new AtomicReference<>();
            AtomicReference<PServiceCall> responseRef = new AtomicReference<>();
            try {
                DefaultProcessorHandler handler = new DefaultProcessorHandler(new WrappedProcessor(processor, (c, p) -> {
                    callRef.set(c);
                    responseRef.set(p.handleCall(c));
                    return responseRef.get();
                }));
                handler.process(reader, writer);
                out.flush();
                long endTime = System.nanoTime();
                double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
                try {
                    instrumentation.onComplete(duration, callRef.get(), responseRef.get());
                } catch (Throwable th) {
                    LOGGER.error("Exception in service instrumentation", th);
                }
            } catch (IOException e) {
                long endTime = System.nanoTime();
                double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
                try {
                    instrumentation.onTransportException(e, duration, callRef.get(), responseRef.get());
                } catch (Throwable th) {
                    LOGGER.error("Exception in service instrumentation", th);
                }
                throw new UncheckedIOException(e.getMessage(), e);
            }
            in.mark(1);
            if (in.read() < 0) {
                return;
            }
            in.reset();
            startTime = System.nanoTime();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e.getMessage(), e);
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Socket(java.net.Socket) BufferedInputStream(java.io.BufferedInputStream) DefaultProcessorHandler(net.morimekta.providence.server.DefaultProcessorHandler) LoggerFactory(org.slf4j.LoggerFactory) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) BufferedOutputStream(java.io.BufferedOutputStream) ServerSocket(java.net.ServerSocket) PProcessor(net.morimekta.providence.PProcessor) SocketTimeoutException(java.net.SocketTimeoutException) NS_IN_MILLIS(net.morimekta.providence.util.ServiceCallInstrumentation.NS_IN_MILLIS) WrappedProcessor(net.morimekta.providence.server.WrappedProcessor) ThreadFactory(java.util.concurrent.ThreadFactory) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) Serializer(net.morimekta.providence.serializer.Serializer) Logger(org.slf4j.Logger) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) ServiceCallInstrumentation(net.morimekta.providence.util.ServiceCallInstrumentation) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) PServiceCall(net.morimekta.providence.PServiceCall) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) DefaultProcessorHandler(net.morimekta.providence.server.DefaultProcessorHandler) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) BufferedInputStream(java.io.BufferedInputStream) WrappedProcessor(net.morimekta.providence.server.WrappedProcessor) PServiceCall(net.morimekta.providence.PServiceCall) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Aggregations

ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nonnull (javax.annotation.Nonnull)1 PProcessor (net.morimekta.providence.PProcessor)1 PServiceCall (net.morimekta.providence.PServiceCall)1 IOMessageReader (net.morimekta.providence.mio.IOMessageReader)1 IOMessageWriter (net.morimekta.providence.mio.IOMessageWriter)1 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)1