Advertisement
olcayertas

Apache Mina Server

Dec 19th, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1.  
  2. public class ServerActivity extends Activity {
  3.  
  4.     private NioSocketAcceptor acceptor;
  5.     private static final int SERVER_PORT = 9123;
  6.     private IoSession session;
  7.  
  8.     // Set this to false to use object serialization instead of custom codec.
  9.     private static final boolean USE_CUSTOM_CODEC = true;
  10.  
  11.     private class ServerSessionHandler extends IoHandlerAdapter {
  12.  
  13.         private final Logger LOGGER = LoggerFactory.getLogger(ServerSessionHandler.class);
  14.  
  15.         @Override
  16.         public void sessionOpened(IoSession s) {
  17.             session = s;
  18.             // set idle time to 60 seconds
  19.             session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);
  20.         }
  21.  
  22.         @Override
  23.         public void messageReceived(IoSession session, Object message) {
  24.             // client only sends AddMessage. otherwise, we will have to identify
  25.             // its type using instanceof operator.
  26.             AddMessage am = (AddMessage) message;
  27.             String value = am.getValue();
  28.             EditText et = (EditText)findViewById(R.id.messageBoard);
  29.             et.append(value);
  30.         }
  31.  
  32.         @Override
  33.         public void sessionIdle(IoSession session, IdleStatus status) {
  34.             //LOGGER.info("Disconnecting the idle.");
  35.             // disconnect an idle client
  36.             session.close(true);
  37.         }
  38.  
  39.         @Override
  40.         public void exceptionCaught(IoSession session, Throwable cause) {
  41.             // close the connection on exceptional situation
  42.             session.close(true);
  43.         }
  44.     }
  45.  
  46.     @Override
  47.     protected void onCreate(Bundle savedInstanceState) {
  48.         super.onCreate(savedInstanceState);
  49.         setContentView(R.layout.activity_server);
  50.     }
  51.  
  52.     public void startServerButtonClicked(View v){
  53.         acceptor = new NioSocketAcceptor();
  54.         acceptor.getSessionConfig().setReadBufferSize(2048);
  55.  
  56.         // Prepare the service configuration.
  57.         if (USE_CUSTOM_CODEC) {
  58.             acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MessageProtocolCodecFactory(true)));
  59.         } else {
  60.             acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
  61.         }
  62.  
  63.         acceptor.getFilterChain().addLast("logger", new LoggingFilter());
  64.         acceptor.setHandler(new ServerSessionHandler());
  65.  
  66.         try {
  67.             acceptor.bind(new InetSocketAddress(SERVER_PORT));
  68.         } catch (IOException e) {
  69.             e.printStackTrace();
  70.         }
  71.  
  72.         Log.d("***************************", "Listening on port " + SERVER_PORT);
  73.     }
  74.  
  75.     public void sendMessageButtonClicked(View v){
  76.  
  77.         if (session == null){
  78.             Log.d("***************************", "Session is null");
  79.             return;
  80.         }
  81.  
  82.         ResultMessage rm = new ResultMessage();
  83.         rm.setSequence(1); // copy sequence
  84.         EditText et = (EditText)findViewById(R.id.message);
  85.         rm.setValue(et.getText().toString());
  86.         rm.setOk(true);
  87.         session.write(rm);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement