Advertisement
olcayertas

Apache Mina

Dec 19th, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. public class ClientActivity extends Activity {
  2.  
  3.     private static final String HOSTNAME = "192.168.2.145";
  4.     private static final int PORT = 9123;
  5.     private static final long CONNECT_TIMEOUT = 30 * 1000L; // 30 seconds
  6.     private IoSession session;
  7.     private NioSocketConnector connector;
  8.     private ConnectFuture future;
  9.  
  10.     // Set this to false to use object serialization instead of custom codec.
  11.     private static final boolean USE_CUSTOM_CODEC = true;
  12.  
  13.     public class ClientSessionHandler extends IoHandlerAdapter {
  14.         private final Logger LOGGER = LoggerFactory.getLogger(ClientSessionHandler.class);
  15.         private boolean finished;
  16.  
  17.         public ClientSessionHandler() {
  18.         }
  19.  
  20.         public boolean isFinished() {
  21.             return finished;
  22.         }
  23.  
  24.         public void sessionOpened(IoSession s) {
  25.             session = s;
  26.         }
  27.  
  28.         public void messageReceived(IoSession session, Object message) {
  29.             // server only sends ResultMessage. otherwise, we will have to identify
  30.             // its type using instanceof operator.
  31.             ResultMessage rm = (ResultMessage) message;
  32.  
  33.             if (rm.isOk()) {
  34.                 EditText et = (EditText)findViewById(R.id.messageBoard);
  35.                 et.append(rm.getValue());
  36.             } else {
  37.                 // seever returned error code because of overflow, etc.
  38.                 Log.d("***************************", "Server error, disconnecting...");
  39.                 session.close(true);
  40.                 finished = true;
  41.             }
  42.         }
  43.  
  44.         public void exceptionCaught(IoSession session, Throwable cause) {
  45.             session.close(true);
  46.         }
  47.     }
  48.  
  49.     @Override
  50.     protected void onCreate(Bundle savedInstanceState) {
  51.         super.onCreate(savedInstanceState);
  52.         setContentView(R.layout.activity_client);
  53.     }
  54.  
  55.     public void connectToServerButtonClicked(View v){
  56.         connector = new NioSocketConnector();
  57.         connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
  58.  
  59.         if (USE_CUSTOM_CODEC) {
  60.             connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MessageProtocolCodecFactory(false)));
  61.         } else {
  62.             connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
  63.         }
  64.  
  65.         connector.getFilterChain().addLast("logger", new LoggingFilter());
  66.         connector.setHandler(new ClientSessionHandler());
  67.  
  68.         for (; ; ) {
  69.             try {
  70.                 future = connector.connect(new InetSocketAddress(HOSTNAME, PORT));
  71.                 future.awaitUninterruptibly();
  72.                 session = future.getSession();
  73.                 break;
  74.             } catch (RuntimeIoException e) {
  75.                 Log.d("***************************", "Failed to connect.");
  76.                 e.printStackTrace();
  77.                 try {
  78.                     Thread.sleep(5000);
  79.                 } catch (InterruptedException e1) {
  80.                     e1.printStackTrace();
  81.                 }
  82.             }
  83.         }
  84.  
  85.         // wait until the summation is done
  86.         session.getCloseFuture().awaitUninterruptibly();
  87.         connector.dispose();
  88.     }
  89.  
  90.     public void sendMessageButtonClicked(View v){
  91.  
  92.         if (session == null){
  93.             Log.d("***************************", "Session is null");
  94.             return;
  95.         }
  96.  
  97.         AddMessage am = new AddMessage();
  98.         am.setSequence(1); // copy sequence
  99.         EditText et = (EditText)findViewById(R.id.message);
  100.         am.setValue(et.getText().toString());
  101.         session.write(am);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement