Now that I have narrowed by server down to using JBoss Netty, it’s time to define protocols. This involves two things. First, we need to select the actual protocol itself and second, we need to define how to construct the handlers. Let’s start by defining the handlers. Netty is built upon the principle of handlers, which are more or less the same idea as filters in web applications. This allows you to define functional handlers that perform specific tasks such as compression, security, and business logic. In this manner, one handler does not need to concern itself with another handlers and handlers can be added/removed without affecting the system. However, order and dependency does matter in certain cases. For example, if you have a handler that depends on a translation of bytes into a POJO, then the handler performing the translation must be in the pipeline and occur prior to the application logic. Generally, a semi-complex, yet real world application, for a protocol stack would consist of:
- Encryption Handlers via SSL processing, handshakes, etc. These handlers take the data stream, decrypt it, and pass the decrypted bytes down the stream
- Compression Handlers via GZip processing for instance. These handlers take the data stream, uncompress it, and pass the uncompressed bytes down the stream
- Transport Protocol Handlers for converting transport protocol data in protocol messages such as HTTP. Subsequent handlers use the protocol-specific messages (ie: HTTP message) for interacting with the protoocol data
- Security Handler for performing security in terms of authentication and authorization checks. These handlers read a header or portions of the data stream and validate the data as being both a valid, authentication user as well as being permissible or authorized or view/modify the associated data
- Application Protocol Handlers for controlling an actual underlying application/business protocol. These differs from the transport protocols in that they specify the actual business data and commands to perform on the business data. Often times, these handlers parse out a given business command, convert to a business message or POJO, and invoke a business delegate object or handler to process the data
- Application/Business Handlers for processing business logic related to a specific protocol message.
This type of stack allows easy extensibility and customization. Future handlers can be added in as needed and future application messages can be added in through custom delegates. It also allows the business logic code to only contain business logic without concern for security, compression, etc. The business logic deals solely with business objects and the business protocol. Further, because of this abstraction and because of the abstraction of the transport layers in Netty, supporting other transport protocols such as UDP, serial, USB, etc is an easy task.
