Apache Mina Development Manual IV
Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs
First, the main steps of Mina development
1. Create a class that implements the Ioservice interface
The Ioservice interface has two sub-interfaces:
1) Ioacceptor interface, for server
2) Ioconnection interface for the client
The purpose of the Ioservice interface is to provide services with several default implementations:
Niodatagramacceptor, Niodatagramconnector
Niosocketacceptor, Niosocketconnector
Vmpipeacceptor, Vmpipeconnector
Proxyconnector
2, set a filter, if you need to customize the filter, then this filter needs to implement the Iofilter interface
The Iofilter function is to create a layer of filter that acts like a filter in the servlet specification.
The default implementations of the Iofilter interface are many, some are for internal use only, some are for users, and the order of the filters can affect the operation of the program. Common implementation classes include the following:
1) Executorfilter: Any operation after this filter is working on this executor
2) Loggingfilter: Log action filter, logging
3) Protocolcodecfilter: Realize the separation between the protocol layer and the business layer.
4) Proxyfilter: Proxy filter, intercept request or response, forward to agent.
5) Sslfilter: For SSL communication
3. Create a processing class that implements the Iohandler interface for handling events
Handle business logic, especially the Iosession interface inside.
4. Bind a port to Ioservice to start working
Second, Mina Custom protocol
Examples in Mina:
Org.apache.mina.example.chat: Supports spring, JMX, custom protocols
Org.apache.mina.example.imagine: Support for JMX, custom protocols
Org.apache.mina.example.sumup: Support for custom protocols
Org.apache.mina.example.tapedeck: Demonstration of state machine, custom protocol
Third, the Protocol decoder
The protocol decoder is dependent on the Protocoldecoder interface:
public interface protocoldecoder{ //decode the contents of a binary protocol or a specific protocol into advanced message void Decode (iosession session, Iobuffer in, Protocoldecoderoutput out); This method is called when the specified session is closed , void Finishdecode (iosession session, protocoldecoderoutput out); Release all resources related to this decoder void Dispose (iosession session) throws Exception;}
Due to the asynchronous reasons, the Iobuffer received by the content is not fully determined, that is, in the Iobuffer for a receive, the storage is not necessarily a complete agreement, there may be multiple incomplete agreement, it may just be a complete agreement, these are uncertain.
Based on the above situation, the actual development is generally implemented Cumulativeprotocoldecoder class, the function of this abstract class is to provide some help for the resolution protocol, this abstract class implements the Decode interface in the Protocoldecoder interface, and abstract out a Dodecode method. The Dodecode method requires that if a protocol can be parsed, the protocol is parsed and placed into the Protocoldecoderoutput class, returning true, and vice versa, which returns false directly and requires manual rollback of the POS.
The Protocol decoder class has two ways of working:
1) completely disposed of by Dodecode
2) The logical part is disposed by the Dodecode method, and the Iobuffer is semi-automatic.
Four, the Protocol encoder
The Protocol encoder relies on the Protocolencoder interface.
public interface protocolencoder{ //Encode advanced message objects into binary or specific protocol data void encode (iosession session, Object message, Protocolencoderoutput out); Releases all resources related to this Protocol encoder void Dispose (iosession session) throws Exception;}
The Protocol encoder can be fully automatic, because the required content can be written to Iobuffer, and to Protocolencoderoutput, the background will be based on the size of the buffer, how much can write how much, Until all the protocols that need to be sent in the Protocolencoderoutput are sent out.
Apache Mina Development Manual IV