byte[] protoBytes = { inputBuffer.get(0), inputBuffer.get(1), inputBuffer.get(2) };
try {
String proto = ByteUtils.getString(protoBytes, "UTF-8");
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;
}
}