Why use a ProtocolCodecFilter

mina 自己带的FilterChain codec是非常好用,但是在实际网络应用中还是有他的局限性,如编写基于CMPP、SGIP的短信系统。 下面我编写的一个自己FilterChain例子,方便以后查看和其他的人查阅(哎,网上的mina资料少的可怜) 1.mina服务器类 package Minatest; import java.net.InetSocketAddress; import org.apache.mina.common.DefaultIoFilterChainBuilder; import org.apache.mina.common.IdleStatus; import org.apache.mina.common.IoAcceptor; import org.apache.mina.common.IoAcceptorConfig; import org.apache.mina.common.IoHandler; import org.apache.mina.common.IoSession; import org.apache.mina.filter.LoggingFilter; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.transport.socket.nio.SocketAcceptor; import org.apache.mina.transport.socket.nio.SocketAcceptorConfig; public class MinaServer { /** * @param args * @throws Exception * zxp * */ public static void main(String[] args) throws Exception { IoAcceptor acceptor=new SocketAcceptor(); IoAcceptorConfig config=new SocketAcceptorConfig(); //config.setDisconnectOnUnbind(true); DefaultIoFilterChainBuilder d= config.getFilterChain(); //d.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory( Charset.forName("UTF-8")))); //d.addFirst("a", ); InetSocketAddress address=new InetSocketAddress(9000); acceptor.bind(address, new MinaServer().new server(), config); } public class server implements IoHandler { public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception { // TODO Auto-generated method stub System.out.println("异常:"+arg1); arg1.fillInStackTrace(); } public void messageReceived(IoSession arg0, Object arg1) throws Exception { // TODO Auto-generated method stub System.out.println("收到信息"); SendMessages send=(SendMessages)arg1;/*********Object*****************/ System.out.println(send.msg); //收到客户端的消息 为0表示刚刚发送的测试消息发送成功 (这个流程是针对CMPP协议,对于普通的来说没意义) if(send.msg.equals("0")){ this.sessionClosed(arg0); } } //信息发送成功后触发的事件 public void messageSent(IoSession arg0, Object arg1) throws Exception { // TODO Auto-generated method stub System.out.println("发送信息"); } public void sessionClosed(IoSession arg0) throws Exception { // TODO Auto-generated method stub System.out.println("session 关闭"); arg0.close(); } public void sessionCreated(IoSession arg0) throws Exception { // TODO Auto-generated method stub System.out.println("session 建立"); } public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception { // TODO Auto-generated method stub System.out.println("session 空闲"); if(arg1==IdleStatus.BOTH_IDLE){ // //自定义发送类 // SendMessages send= new SendMessages(); // send.setMsg("Hi!客户端你空闲咯!"); // this.sendMessage(arg0, send); } } public void sessionOpened(IoSession arg0) throws Exception { // TODO Auto-generated method stub System.out.println("session 打开"); //arg0.setIdleTime(IdleStatus.BOTH_IDLE, 10); arg0.getFilterChain().addFirst("a", new ProtocolCodecFilter(new TestProtocolCodecFactory())); /************************************************** Why use a ProtocolCodecFilter? TCP guarantess delivery of all packets in the correct order. But there is no guarantee that one write operation on the sender-side will result in one read event on the receiving side. TCP保证收发的数据包遵照正确的次序。 但并不保证发送方的写操作会在接收方产生读事件。 In MINA terminology: without a ProtocolCodecFilter one call of IoSession.write(Object message) by the sender can result in multiple messageReceived(IoSession session, Object message) events on the receiver; and multiple calls of IoSession.write(Object message) can lead to a single messageReceived event. You might not encounter this behavior when client and server are running on the same host (or an a local network) but your applications should be able to cope with this. MINA技术:不用ProtocolCodecFilter的话,发送方的一次写调用IoSession.write(Object message)可能会在接收方产生多个接收事件;多次写调用IoSession.write(Object message)也可能会在接收方导致一个接收事件。当客户端和服务器运行在同一台主机或局域网时,你可能会遇到上述情况,但你的应用程序应该能够处理。 Most network applications need a way to find out where the current message ends and where the next message starts. 大多数网络应用程序需要识别出当前消息在那儿结束,以及下一个消息在那儿开始。 You could implement all this logic in your IoHandler, but adding a ProtocolCodecFilter will make your code much cleaner and easier to maintain. It allows you to separate your protocol logic from your business logic (IoHandler). 你可以将所有的逻辑在IoHandler中实现,但如果加入ProtocolCodecFilter的话,将会使你的代码更清晰,更易维护。 ProtocolCodecFilter允许你将协议逻辑从业务逻辑(IoHandler)中分离出来。 ***************************************************/ arg0.getFilterChain().addLast("logger", new LoggingFilter()); //发送条测试消息 SendMessages send= new SendMessages(); send.setMsg("Hi!客户端你好!"); this.sendMessage(arg0, send); } private void sendMessage(IoSession arg0, Object arg1){ arg0.write(arg1); } } } 2.mina客户端类 package Minatest; import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.apache.mina.common.DefaultIoFilterChainBuilder; import org.apache.mina.common.IdleStatus; import org.apache.mina.common.IoConnector; import org.apache.mina.common.IoConnectorConfig; import org.apache.mina.common.IoHandler; import org.apache.mina.common.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFactory; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.transport.socket.nio.SocketConnector; import org.apache.mina.transport.socket.nio.SocketConnectorConfig; public class MinaClient { /** * @param args * zxp * */ public static void main(String[] args) { // TODO Auto-generated method stub IoConnector connector=new SocketConnector(); IoConnectorConfig config=new SocketConnectorConfig(); //config.setConnectTimeout(1); //DefaultIoFilterChainBuilder d= config.getFilterChain(); //d.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); InetSocketAddress address=new InetSocketAddress("127.0.0.1",9000); connector.connect(address, new MinaClient().new client(), config); } public class client implements IoHandler{ public void exceptionCaught(IoSession arg0, Throwable arg1) throws Exception { // TODO Auto-generated method stub System.out.println("客户端异常:"+arg1); arg0.close(); } public void messageReceived(IoSession arg0, Object arg1) throws Exception { // TODO Auto-generated method stub System.out.println("客户端收到消息"); SendMessages send=(SendMessages) arg1; System.out.println(send.getMsg()); if(send.msg!=null){ send.setMsg("0"); this.sendMessages(arg0,send); } } public void messageSent(IoSession arg0, Object arg1) throws Exception { // TODO Auto-generated method stub //不能用这个方法发送信息,要不死循环 //arg0.getw } public void sessionClosed(IoSession arg0) throws Exception { // TODO Auto-generated method stub System.out.println("客户端关闭"); arg0.close(); } public void sessionCreated(IoSession arg0) throws Exception { // TODO Auto-generated method stub System.out.println("客户端建立"); //arg0.getFilterChain() } public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception { // TODO Auto-generated method stub System.out.println("客户端空闲"); if(arg1==IdleStatus.BOTH_IDLE){ this.messageSent(arg0, "空闲测试"); SendMessages send=new SendMessages(); send.setMsg("aaaaa"); this.sendMessages(arg0, send); } } public void sessionOpened(IoSession arg0) throws Exception { // TODO Auto-generated method stub System.out.println("客户端打开"); //10秒内没有读写就设置为空闲通道 arg0.setIdleTime(IdleStatus.BOTH_IDLE, 10); //自定义包解析 //ProtocolCodecFactory codec=new TestProtocolCodecFactory(); //arg0.getFilterChain().addFirst("test", new ProtocolCodecFilter(codec)); arg0.getFilterChain().addLast("a", new ProtocolCodecFilter(new TestProtocolCodecFactory())); } private void sendMessages(IoSession arg0, Object arg1){ System.out.println("sendMessages 发送"); arg0.write(arg1); } } } 3.工厂类 package Minatest; import org.apache.mina.filter.codec.demux.DemuxingProtocolCodecFactory; public class TestProtocolCodecFactory extends DemuxingProtocolCodecFactory { public TestProtocolCodecFactory(){ super.register(TestDecoder.class); super.register(TestEncoder.class); } } 4.编码类 package Minatest; import java.util.Collections; import java.util.HashSet; import java.util.Hashtable; import java.util.Set; import org.apache.mina.common.ByteBuffer; import org.apache.mina.common.IoSession; import org.apache.mina.filter.codec.ProtocolEncoderOutput; import org.apache.mina.filter.codec.demux.MessageEncoder; public class TestEncoder implements MessageEncoder { /** * 编码消息 * zxp * */ public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { SendMessages msg=(SendMessages)message; ByteBuffer b=ByteBuffer.allocate(msg.getmsgleng()); b.setAutoExpand(true); //编码消息 msg.encoder(b); //b.putInt(97); //b.put("a".getBytes()); b.flip(); out.write(b); } /** * 获取编码自定义消息类型(多个) * */ public Set > getMessageTypes() { Set > set=new HashSet >(); set.add(SendMessages.class); //返回指定映射的不可修改视图 即只读 return Collections.unmodifiableSet(set); } } 5.解码类 package Minatest; import org.apache.mina.common.ByteBuffer; import org.apache.mina.common.IoSession; import org.apache.mina.filter.codec.ProtocolDecoderOutput; import org.apache.mina.filter.codec.demux.MessageDecoder; import org.apache.mina.filter.codec.demux.MessageDecoderResult; public class TestDecoder implements MessageDecoder { /** * 检查指定的缓冲区decodable解码器 * zxp */ public MessageDecoderResult decodable(IoSession arg0, ByteBuffer arg1) { //包的长度是否大于0 if(arg1.remaining()<=0){ return MessageDecoderResult.NOT_OK; } return MessageDecoderResult.OK; } /** * 解码二进制或协议的具体内容到更高级别的消息对象 * (non-Javadoc) * @see org.apache.mina.filter.codec.demux.MessageDecoder#decode(org.apache.mina.common.IoSession, org.apache.mina.common.ByteBuffer, org.apache.mina.filter.codec.ProtocolDecoderOutput) */ public MessageDecoderResult decode(IoSession arg0, ByteBuffer arg1, ProtocolDecoderOutput arg2) throws Exception { //解码 byte[] b=new byte[arg1.limit()]; arg1.get(b); System.out.println("decode:"+arg1.position()); SendMessages send=new SendMessages(); String s=new String(b); send.setMsg(s); arg2.write(send); return MessageDecoderResult.OK; } /** * 解码完成后调用的方法 * */ public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1) throws Exception { } } 6.自定义消息类 package Minatest; import org.apache.mina.common.ByteBuffer; public class SendMessages implements java.io.Serializable { public String msg="0"; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getmsgleng(){ return msg.getBytes().length; } public void encoder(ByteBuffer b){ b.put(msg.getBytes()); //b.putInt(1); } }

你可能感兴趣的:(exception,object,session,String,Mina,Codec)