Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ClientActivity extends Activity {
- private static final String HOSTNAME = "192.168.2.145";
- private static final int PORT = 9123;
- private static final long CONNECT_TIMEOUT = 30 * 1000L; // 30 seconds
- private IoSession session;
- private NioSocketConnector connector;
- private ConnectFuture future;
- // Set this to false to use object serialization instead of custom codec.
- private static final boolean USE_CUSTOM_CODEC = true;
- public class ClientSessionHandler extends IoHandlerAdapter {
- private final Logger LOGGER = LoggerFactory.getLogger(ClientSessionHandler.class);
- private boolean finished;
- public ClientSessionHandler() {
- }
- public boolean isFinished() {
- return finished;
- }
- public void sessionOpened(IoSession s) {
- session = s;
- }
- public void messageReceived(IoSession session, Object message) {
- // server only sends ResultMessage. otherwise, we will have to identify
- // its type using instanceof operator.
- ResultMessage rm = (ResultMessage) message;
- if (rm.isOk()) {
- EditText et = (EditText)findViewById(R.id.messageBoard);
- et.append(rm.getValue());
- } else {
- // seever returned error code because of overflow, etc.
- Log.d("***************************", "Server error, disconnecting...");
- session.close(true);
- finished = true;
- }
- }
- public void exceptionCaught(IoSession session, Throwable cause) {
- session.close(true);
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_client);
- }
- public void connectToServerButtonClicked(View v){
- connector = new NioSocketConnector();
- connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
- if (USE_CUSTOM_CODEC) {
- connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MessageProtocolCodecFactory(false)));
- } else {
- connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
- }
- connector.getFilterChain().addLast("logger", new LoggingFilter());
- connector.setHandler(new ClientSessionHandler());
- for (; ; ) {
- try {
- future = connector.connect(new InetSocketAddress(HOSTNAME, PORT));
- future.awaitUninterruptibly();
- session = future.getSession();
- break;
- } catch (RuntimeIoException e) {
- Log.d("***************************", "Failed to connect.");
- e.printStackTrace();
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- }
- }
- // wait until the summation is done
- session.getCloseFuture().awaitUninterruptibly();
- connector.dispose();
- }
- public void sendMessageButtonClicked(View v){
- if (session == null){
- Log.d("***************************", "Session is null");
- return;
- }
- AddMessage am = new AddMessage();
- am.setSequence(1); // copy sequence
- EditText et = (EditText)findViewById(R.id.message);
- am.setValue(et.getText().toString());
- session.write(am);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement