if (in.readableBytes() < 3) {
return;
}
// Get the pipeline so we can dynamically adjust it and fire events
final ChannelPipeline pipeline = ctx.pipeline();
// Pull out the magic header
int readerIndex = in.readerIndex();
final int magic1 = in.getUnsignedByte(readerIndex);
final int magic2 = in.getUnsignedByte(readerIndex + 1);
final int magic3 = in.getUnsignedByte(readerIndex + 2);
// String-based Command?
if (this.isStringCommand(magic1, magic2, magic3)) {
// Write a line break into the buffer so we mark the frame
in.writeBytes(Delimiters.lineDelimiter()[0]);
// Adjust the pipeline such that we use the command handler
pipeline.addLast(NAME_CHANNEL_HANDLER_FRAME_DECODER,
new DelimiterBasedFrameDecoder(2000, Delimiters.lineDelimiter()));
pipeline.addLast(NAME_CHANNEL_HANDLER_STRING_DECODER,
new StringDecoder(Charset.forName(WireProtocol.CHARSET)));
pipeline.addLast(NAME_CHANNEL_HANDLER_COMMAND, new StringCommandHandler());
pipeline.remove(NAME_CHANNEL_HANDLER_ACTION_CONTROLLER);
pipeline.remove(NAME_CHANNEL_HANDLER_EOF);
}
// Deploy command?
else if (this.isDeployCommand(magic1, magic2, magic3)) {
// Set the reader index so we strip out the command portion, leaving only the bytes containing the
// archive (the frame decoder will strip off the EOF delimiter)
in.readerIndex(in.readerIndex() + WireProtocol.COMMAND_DEPLOY_PREFIX.length());
// Adjust the pipeline such that we use the deploy handler only
pipeline.addLast(NAME_CHANNEL_HANDLER_DEPLOY_HANDLER, new DeployHandlerAdapter());
pipeline.remove(NAME_CHANNEL_HANDLER_ACTION_CONTROLLER);
pipeline.remove(NAME_CHANNEL_HANDLER_EOF);
} else {
// Unknown command/protocol
NettyServer.sendResponse(ctx, ctx.nextOutboundByteBuffer(), WireProtocol.RESPONSE_ERROR_PREFIX
+ "Unsupported Command");
in.clear();
ctx.close();
return;
}
// Write the bytes to the next inbound buffer and re-fire so the updated handlers in the pipeline can have a
// go at it
final ByteBuf nextInboundByteBuffer = ctx.nextInboundByteBuffer();
nextInboundByteBuffer.writeBytes(in);
pipeline.fireInboundBufferUpdated();
}