use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.
the class AsyncRequestHandler method initRequestHandler.
/**
* Returns true if the request should continue.
*
* @return
*/
private boolean initRequestHandler(SelectionKey selectionKey) {
ByteBuffer inputBuffer = inputStream.getBuffer();
int remaining = inputBuffer.remaining();
// Don't have enough bytes to determine the protocol yet...
if (remaining < 3)
return true;
byte[] protoBytes = { inputBuffer.get(0), inputBuffer.get(1), inputBuffer.get(2) };
try {
String proto = ByteUtils.getString(protoBytes, "UTF-8");
inputBuffer.clear();
RequestFormatType requestFormatType = RequestFormatType.fromCode(proto);
requestHandler = requestHandlerFactory.getRequestHandler(requestFormatType);
if (logger.isInfoEnabled())
logger.info("Protocol negotiated for " + socketChannel.socket() + ": " + requestFormatType.getDisplayName());
// The protocol negotiation is the first request, so respond by
// sticking the bytes in the output buffer, signaling the Selector,
// and returning false to denote no further processing is needed.
outputStream.getBuffer().put(ByteUtils.getBytes("ok", "UTF-8"));
prepForWrite(selectionKey);
return false;
} catch (IllegalArgumentException e) {
// okay we got some nonsense. For backwards compatibility,
// assume this is an old client who does not know how to negotiate
RequestFormatType requestFormatType = RequestFormatType.VOLDEMORT_V0;
requestHandler = requestHandlerFactory.getRequestHandler(requestFormatType);
if (logger.isInfoEnabled())
logger.info("No protocol proposal given for " + socketChannel.socket() + ", assuming " + requestFormatType.getDisplayName());
return true;
}
}
use of voldemort.client.protocol.RequestFormatType 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);
}
}
use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.
the class SocketServerSession method negotiateProtocol.
private RequestFormatType negotiateProtocol(InputStream input, OutputStream output) throws IOException {
input.mark(3);
byte[] protoBytes = new byte[3];
ByteUtils.read(input, protoBytes);
RequestFormatType requestFormat;
try {
String proto = ByteUtils.getString(protoBytes, "UTF-8");
requestFormat = RequestFormatType.fromCode(proto);
output.write(ByteUtils.getBytes("ok", "UTF-8"));
output.flush();
} catch (IllegalArgumentException e) {
// okay we got some nonsense. For backwards compatibility,
// assume this is an old client who does not know how to negotiate
requestFormat = RequestFormatType.VOLDEMORT_V0;
// reset input stream so we don't interfere with request format
input.reset();
logger.info("No protocol proposal given, assuming " + RequestFormatType.VOLDEMORT_V0.getDisplayName());
}
return requestFormat;
}
use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.
the class SocketPoolTest method testVariousProtocols.
@Test
public void testVariousProtocols() throws Exception {
for (RequestFormatType type : RequestFormatType.values()) {
SocketDestination dest = new SocketDestination("localhost", port, type);
SocketAndStreams sas = pool.checkout(dest);
assertEquals(type, sas.getRequestFormatType());
}
}
use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.
the class VoldemortNativeSocketStoreTest method configs.
@Parameters
public static Collection<Object[]> configs() {
RequestFormatType[] types = new RequestFormatType[] { RequestFormatType.VOLDEMORT_V1, RequestFormatType.VOLDEMORT_V2, RequestFormatType.VOLDEMORT_V3 };
List<Object[]> options = new ArrayList<Object[]>();
boolean[] nioOptions = { true, false };
for (RequestFormatType type : types) {
for (boolean nio : nioOptions) {
options.add(new Object[] { type, nio });
}
}
return options;
}
Aggregations