1 20 package org.apache.mina.example.sumup; 21 22 import org.apache.mina.common.IoHandler; 23 import org.apache.mina.common.IoHandlerAdapter; 24 import org.apache.mina.common.IoSession; 25 import org.apache.mina.example.sumup.message.AddMessage; 26 import org.apache.mina.example.sumup.message.ResultMessage; 27 import org.apache.mina.util.SessionLog; 28 29 35 public class ClientSessionHandler extends IoHandlerAdapter { 36 private final int[] values; 37 38 private boolean finished; 39 40 public ClientSessionHandler(int[] values) { 41 this.values = values; 42 } 43 44 public boolean isFinished() { 45 return finished; 46 } 47 48 public void sessionOpened(IoSession session) { 49 for (int i = 0; i < values.length; i++) { 51 AddMessage m = new AddMessage(); 52 m.setSequence(i); 53 m.setValue(values[i]); 54 session.write(m); 55 } 56 } 57 58 public void messageReceived(IoSession session, Object message) { 59 ResultMessage rm = (ResultMessage) message; 62 if (rm.isOk()) { 63 if (rm.getSequence() == values.length - 1) { 68 SessionLog.info(session, "The sum: " + rm.getValue()); 70 session.close(); 71 finished = true; 72 } 73 } else { 74 SessionLog.warn(session, "Server error, disconnecting..."); 76 session.close(); 77 finished = true; 78 } 79 } 80 81 public void exceptionCaught(IoSession session, Throwable cause) { 82 session.close(); 83 } 84 } | Popular Tags |