1 20 package org.apache.mina.example.sumup; 21 22 import java.net.InetSocketAddress ; 23 24 import org.apache.mina.common.IoAcceptor; 25 import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory; 26 import org.apache.mina.filter.LoggingFilter; 27 import org.apache.mina.filter.codec.ProtocolCodecFilter; 28 import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory; 29 import org.apache.mina.transport.socket.nio.SocketAcceptor; 30 import org.apache.mina.transport.socket.nio.SocketAcceptorConfig; 31 32 38 public class Server { 39 private static final int SERVER_PORT = 8080; 40 41 private static final boolean USE_CUSTOM_CODEC = true; 43 44 public static void main(String [] args) throws Throwable { 45 IoAcceptor acceptor = new SocketAcceptor(); 46 47 SocketAcceptorConfig cfg = new SocketAcceptorConfig(); 49 cfg.setReuseAddress(true); 50 if (USE_CUSTOM_CODEC) { 51 cfg.getFilterChain() 52 .addLast( 53 "codec", 54 new ProtocolCodecFilter( 55 new SumUpProtocolCodecFactory(true))); 56 } else { 57 cfg.getFilterChain().addLast( 58 "codec", 59 new ProtocolCodecFilter( 60 new ObjectSerializationCodecFactory())); 61 } 62 cfg.getFilterChain().addLast("logger", new LoggingFilter()); 63 64 acceptor.bind(new InetSocketAddress (SERVER_PORT), 65 new ServerSessionHandler(), cfg); 66 67 System.out.println("Listening on port " + SERVER_PORT); 68 } 69 } 70 | Popular Tags |