MINA客户端和服务端

IoService(Inf)

     |

     |==AbstractIoService       --IoAcceptor                              -----IoConnector

     |                       |                 |                                                    |

     |                       |-----------|==AbstractIoAcceptor                |==AbstractIoConnector

                                                       |

                                                       |==NioSocketAcceptor                |==NioSocketConnector

                                                       |==NioDatagramAcceptor           |==NiooDatagramConnector

                                                       |==VmPipeAcceptor                    |==VmPipeConnector

 

 

 

图例:

 

=== 表示: implements

---- 表示:   extends

 

一个非常好的入门的例子:http://mina.apache.org/tutorial-on-protocolcodecfilter-for-mina-2x.html

 

out-of-box examples:

out-of-box examples

 

例题一、Reverser         Text protocol based on a protocol codec          Server

 

 

 
public class Main { 
private static final int PORT = 8080; public static void main(String[] args) throws Exception { NioSocketAcceptor acceptor = new NioSocketAcceptor(); // Prepare the configuration acceptor.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset .forName("UTF-8")))); // Bind acceptor.setHandler(new ReverseProtocolHandler()); acceptor.bind(new InetSocketAddress(PORT)); System.out.println("Listening on port " + PORT); } } 

 

 

public class ReverseProtocolHandler extends IoHandlerAdapter { @Override public void exceptionCaught(IoSession session, Throwable cause) { // Close connection when unexpected exception is caught. session.close(); } @Override public void messageReceived(IoSession session, Object message) { // Reverse reveiced string String str = message.toString(); StringBuffer buf = new StringBuffer(str.length()); for (int i = str.length() - 1; i >= 0; i--) { buf.append(str.charAt(i)); } // and write it back. session.write(buf.toString()); } }

 

 

 

运行测试:(建议使用SocketTestv2.0.0作为客户端进行测试)

 

 socketTest作为客户端

 

运行结果:

Listening on port 8080
12924 [NioProcessor-1] INFO org.apache.mina.filter.logging.LoggingFilter - CREATED
12924 [NioProcessor-1] INFO org.apache.mina.filter.logging.LoggingFilter - OPENED
42696 [NioProcessor-2] INFO org.apache.mina.filter.logging.LoggingFilter - CREATED
42696 [NioProcessor-2] INFO org.apache.mina.filter.logging.LoggingFilter - OPENED
42696 [NioProcessor-2] INFO org.apache.mina.filter.logging.LoggingFilter - RECEIVED: HeapBuffer[pos=0 lim=298 cap=2048: 47 45 54 20 68 74 74 70 3A 2F 2F 77 77 77 2E 73...]
42727 [NioProcessor-2] INFO org.apache.mina.filter.logging.LoggingFilter - SENT: HeapBuffer[pos=0 lim=146 cap=147: 30 2E 31 2F 50 54 54 48 20 30 30 46 30 35 32 39...]
42727 [NioProcessor-2] INFO org.apache.mina.filter.logging.LoggingFilter - SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]

 

 

例题二、SumUp Server

需求:客户端不断发送AddMessage,服务端把这些AddMessage中传递过来的数据进行累加,并客户端每发一个AddMessage请求,服务端都把最后的累加和返回给客户端,服务端给客户端会送的报文格式是ResultMessage。由于两者都有个报文序号所以被抽象了一个公共基类。

 

AddMessage(seqnum,addValue);

 

ResultMessage(seqnum,retcode,sumValue)

 

另外,报文序列化方面我们同时支持两种,由用户进行配置,一种是采用java的对象流;另一种是用户自定义的codec.

 

public abstract class AbstractMessage implements Serializable {//以支持对象序列化 private int sequence;//报文序列号 public int getSequence() { return sequence; } public void setSequence(int sequence) { this.sequence = sequence; } }

 

 

public class AddMessage extends AbstractMessage { private static final long serialVersionUID = -940833727168119141L; private int value; public AddMessage() { } public int getValue() { return value; } public void setValue(int value) { this.value = value; } @Override public String toString() { // it is a good practice to create toString() method on message classes. return getSequence() + ":ADD(" + value + ')'; } }

 

 

public class ResultMessage extends AbstractMessage { 
private static final long serialVersionUID = 7371210248110219946L; private boolean ok; private int value; public ResultMessage() { } public boolean isOk() { return ok; } public void setOk(boolean ok) { this.ok = ok; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } @Override public String toString() { if (ok) { return getSequence() + ":RESULT(" + value + ')'; } else { return getSequence() + ":RESULT(ERROR)"; } } }
 

 

 

 用户自定义的codec上,我们可以采用:公共包头+不同包体的方式。

 

你可能感兴趣的:(apache,Mina)