Search in sources :

Example 1 with StreamRequestHandler

use of voldemort.server.protocol.StreamRequestHandler in project voldemort by voldemort.

the class SocketServerSession method run.

public void run() {
    try {
        activeSessions.put(sessionId, this);
        DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream(), 64000));
        DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream(), 64000));
        RequestFormatType protocol = negotiateProtocol(inputStream, outputStream);
        RequestHandler handler = handlerFactory.getRequestHandler(protocol);
        logger.info("Client " + socket.getRemoteSocketAddress() + " connected successfully with protocol " + protocol.getCode());
        while (!isInterrupted() && !socket.isClosed() && !isClosed) {
            StreamRequestHandler srh = handler.handleRequest(inputStream, outputStream);
            if (srh != null) {
                if (logger.isTraceEnabled())
                    logger.trace("Request is streaming");
                StreamRequestHandlerState srhs = null;
                try {
                    do {
                        if (logger.isTraceEnabled())
                            logger.trace("About to enter streaming request handler");
                        srhs = srh.handleRequest(inputStream, outputStream);
                        if (logger.isTraceEnabled())
                            logger.trace("Finished invocation of streaming request handler, result is " + srhs);
                    } while (srhs != StreamRequestHandlerState.COMPLETE);
                } catch (VoldemortException e) {
                    srh.handleError(outputStream, e);
                    outputStream.flush();
                    break;
                } finally {
                    srh.close(outputStream);
                }
            }
            outputStream.flush();
        }
        if (isInterrupted())
            logger.info(Thread.currentThread().getName() + " has been interrupted, closing session.");
    } catch (EOFException e) {
        logger.info("Client " + socket.getRemoteSocketAddress() + " disconnected.");
    } catch (IOException e) {
        // if this is an unexpected
        if (!isClosed)
            logger.error(e);
    } finally {
        try {
            if (!socket.isClosed())
                socket.close();
        } catch (Exception e) {
            logger.error("Error while closing socket", e);
        }
        // now remove ourselves from the set of active sessions
        this.activeSessions.remove(sessionId);
    }
}
Also used : StreamRequestHandler(voldemort.server.protocol.StreamRequestHandler) StreamRequestHandler(voldemort.server.protocol.StreamRequestHandler) RequestHandler(voldemort.server.protocol.RequestHandler) BufferedInputStream(java.io.BufferedInputStream) DataOutputStream(java.io.DataOutputStream) EOFException(java.io.EOFException) RequestFormatType(voldemort.client.protocol.RequestFormatType) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BufferedOutputStream(java.io.BufferedOutputStream) VoldemortException(voldemort.VoldemortException) VoldemortException(voldemort.VoldemortException) IOException(java.io.IOException) EOFException(java.io.EOFException) StreamRequestHandlerState(voldemort.server.protocol.StreamRequestHandler.StreamRequestHandlerState)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 VoldemortException (voldemort.VoldemortException)1 RequestFormatType (voldemort.client.protocol.RequestFormatType)1 RequestHandler (voldemort.server.protocol.RequestHandler)1 StreamRequestHandler (voldemort.server.protocol.StreamRequestHandler)1 StreamRequestHandlerState (voldemort.server.protocol.StreamRequestHandler.StreamRequestHandlerState)1