mina2.0编码过滤器

    mina2.0是性能非常强悍的socket框架,它是基于nio开发的.下面的代码给出了如何开发自己的编码过滤器.本文中,只给出了部分代码,主要实现IoBuffer到十六进制字符串的转换.如朋友们有问题,可以沟通交流.
    下面的代码,采用了内部匿名类来实现,因为这个类只在初始化的时候使用,所以没必要专门为这个功能写个类并继承.祝朋友们学习愉快^-^
package test.binary;

import java.net.InetSocketAddress;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import code.Hex;

public class Server {
	public static void main(String[] args) throws Exception {
		NioSocketAcceptor acceptor = new NioSocketAcceptor();
		DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
		//日志过滤器
		chain.addLast("logger", new LoggingFilter()); 
		//编码过滤器
		chain.addLast("codec", new ProtocolCodecFilter(new ProtocolCodecFactory() {

			@Override
			public ProtocolEncoder getEncoder(IoSession session) throws Exception {
				ProtocolEncoder pe = new ProtocolEncoder() {
					@Override
					public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {	
						out.write(Hex.hexString2IoBuffer(message.toString().replace(" ", "")));
					}

					@Override
					public void dispose(IoSession session) throws Exception {
					}
				};
				return pe;
			}

			@Override
			public ProtocolDecoder getDecoder(IoSession session) throws Exception {
				ProtocolDecoder pd = new ProtocolDecoder(){

					@Override
					public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
						out.write(Hex.ioBuffer2hexString(in));
					}

					@Override
					public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
						
					}

					@Override
					public void dispose(IoSession session) throws Exception {
						
					}};
				return pd;
			}
		}));

		acceptor.setHandler(new IoHandler() {
			
			@Override
			public void sessionOpened(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void sessionCreated(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void sessionClosed(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void messageSent(IoSession session, Object message) throws Exception {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void messageReceived(IoSession session, Object message) throws Exception {
				System.out.println(message.toString());
				session.write("AA FF DD");
				
			}
			
			@Override
			public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
				// TODO Auto-generated method stub
				
			}
		});

		acceptor.bind(new InetSocketAddress("192.168.8.199", 8888));
	}

}

你可能感兴趣的:(apache,框架,.net,socket,Mina)