I. Learning materials
Mina Introduction Example (i)
Http://www.cnblogs.com/juepei/p/3939119.html
Mina Getting Started Tutorial (ii)----SPRING4 integrated Mina
Http://www.cnblogs.com/juepei/p/3940396.html
Apache Mina Getting Started instance--creating a Mina Time service
Http://loftor.com/archives/apache-mina-quick-start-guide.html
MINA2.0 User manual Chinese version--series documents
Http://blog.csdn.net/lxlzhn/article/category/1272213/2
Mina Heartbeat mechanism
Source: http://wandejun1012.iteye.com/blog/2065941
Mina Framework Detailed
Http://www.cnblogs.com/gw811/p/4279230.html
Mina Encoder Detailed
Http://wenku.baidu.com/link?url= Pk11evhi-8nevx6wcaxzrqja1yjumnxbjwts5s2nenhje1sx16ljnjnv-7mkas5z4rihrotgu5dx-dgdfdnzk-i2w4vmlfbssay1nrd33ak
Mina Series Article index (decoding parsing, etc., good)
http://my.oschina.net/ielts0909/blog/92716
Second, code example
(1) Mina service-side creation steps
1. Setting up Listener objects
Ioacceptor acceptor=new niosocketacceptor ();
2. Add filter (log, codec)
Acceptor.getfilterchain (). AddLast ("Logger", New Loggingfilter ());
Acceptor.getfilterchain (). AddLast ("Codec", new Protocolcodecfilter (New Textlinecodecfactory (Charset.forname (" UTF-8 ")));
3. Add handler Processing
Acceptor.sethandler (New Timeserverhandler ());
4. Set the reply parameter "input buffer buffer size, how long to enter idle time"
Acceptor.getSessionConfig.setReadBufferSize (2048);
Acceptor.getessionConfig.setIdleTime (idlestatus.both_idle,10);
5. Bind the port number and start listening
Acceptor.bind (New Inetsocketaddress (PORT));
Handler Processing class development
Inheriting the Iohandler class or implementing the Iohandler interface
Client handler, server-side handler use the same Iohandler inheritance implementation.
Public Timeserverhandler extends Iohandleradapter
{
@Override
public void Exceptioncaught (Iosession session,throwable cause) throws Exception
{
Cause.printstacktrace ();
}
@Override
public void messagereceived (iosession session,object message) throws Exception
{
String str=message.tostring ();
if (Str.trim (). Equalsignorecase ("Quit"))
{
Session.close ();
return;
}
Date Date=new date ();
Session.write (Date.tostring ());
System.out.println ("Message written ...");
}
@Override
public void Sessionidle (iosession session,idlestatus status) throws Exception
{
System.out.println ("IDLE" +session.getidlecount (status));
}
}
Exceptioncaught method: Catch errors, simply print the wrong stack trace and close the session.
Messagereceived method: To process the data received from the client, here is the current time to return to the client. Depending on the protocol codec used, the type of object passed differently, and the Session.write (object) is different when the data is returned. If you do not specify a codec for the protocol, you will receive an object of type Iobuffer, and the returned data is also required to be iobuffer.
Sessionidle Method: A session is called periodically when idle state
There are several ways to rewrite the
@Override
public void Messagesent (iosession arg0, Object message) throws Exception {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Client Send Message" +message.tostring ());
}
@Override
public void Sessionclosed (Iosession session) throws Exception {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Client and:" +session.getremoteaddress (). toString () + "Disconnected");
}
@Override
public void Sessioncreated (Iosession session) throws Exception {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Client and:" +session.getremoteaddress (). toString () + "establish connection");
}
@Override
public void sessionopened (Iosession arg0) throws Exception {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Open Connection");
}
(2) Mina Client Creation
1. Create a client Connection object
Ioconnector connector=new niosocketconnector (); or niosocketconnector connector = new Niosocketconnector ();
2. Set the filter responsibility chain (log, codec "multiple codecs")
Connector.getfilterchain (). AddLast ("Logger", New Loggingfilter ());
Connector.getfilterchain (). AddLast ("Codec", new Protocolcodecfilter (New Prefixedstringcodecfactory ( Charset.forname ("UTF-8")));
You can also use
Ioacceptor.getfilterchain (). AddLast ("Codec", new Protocolcodecfilter (New Textlinecodecfactory (Charset.forname (" UTF-8 ")));
3. Set handler processing
Connector.sethandler (New Timeclienthandler ());
4. Establish Connect connection
Connectfuture Connectfuture=connector.connect (New inetsocketaddress ("127.0.0.1", Baseconfig.port));
Connectfuture.awaituninterruptibly ();
5. Get Session Back
Iosession session=connectfuture.getsession ();
6. Input/output operation
The following is the input to the receiving console, which is then sent to the server
Scanner sc = new Scanner (system.in);
Boolean quit = false;
while (!quit) {
String str = Sc.next ();
if (Str.equalsignorecase ("Quit")) {
Quit = true;
}
Session.write (str);
}
7. Close session and connector
if (session!=null) {
if (session.isconnected ()) {
Session.getclosefuture (). awaituninterruptibly ();
}
Connector.dispose (TRUE);
}
Ioconnector connector = new Niosocketconnector ();
Connector.getfilterchain (). AddLast ("Logger", New Loggingfilter ());
Connector.getfilterchain (). AddLast ("Codec", new Protocolcodecfilter (New Prefixedstringcodecfactory ( Charset.forname ("UTF-8")));
Connector.sethandler (New Timeclienthander ());
Connectfuture connectfuture = connector.connect (New inetsocketaddress ("127.0.0.1", Baseconfig.port));
Waiting to establish a connection
Connectfuture.awaituninterruptibly ();
SYSTEM.OUT.PRINTLN ("Connection succeeded");
Iosession session = Connectfuture.getsession ();
Scanner sc = new Scanner (system.in);
Boolean quit = false;
while (!quit) {
String str = Sc.next ();
if (str.equalsignorecase ("Quit")) {
quit = true;
}
session.write (str);
}
Shut down
if (session!=null) {
if (session.isconnected ()) {
Session.getclosefuture (). awaituninterruptibly ();
}
Connector.dispose (TRUE);
}
Three, the adhesive packet subcontracting processing
There are three common ways to separate a stream of bytes into messages:
Use a fixed-length message.
Uses a fixed-length header to indicate the length of the message content body.
Use separators, for example, many text-based messages append a newline (or CR LF combination) after a message (http://www.faqs.org/rfcs/rfc977.html)
Java Communication Series II: Mina Primer Summary