1 20 package org.apache.mina.example.tennis; 21 22 import org.apache.mina.common.IoHandler; 23 import org.apache.mina.common.IoHandlerAdapter; 24 import org.apache.mina.common.IoSession; 25 26 32 public class TennisPlayer extends IoHandlerAdapter { 33 private static int nextId = 0; 34 35 36 private final int id = nextId++; 37 38 public void sessionOpened(IoSession session) { 39 System.out.println("Player-" + id + ": READY"); 40 } 41 42 public void sessionClosed(IoSession session) { 43 System.out.println("Player-" + id + ": QUIT"); 44 } 45 46 public void messageReceived(IoSession session, Object message) { 47 System.out.println("Player-" + id + ": RCVD " + message); 48 49 TennisBall ball = (TennisBall) message; 50 51 ball = ball.stroke(); 53 54 if (ball.getTTL() > 0) { 55 session.write(ball); 57 } else { 58 System.out.println("Player-" + id + ": LOSE"); 60 session.close(); 61 } 62 } 63 64 public void messageSent(IoSession session, Object message) { 65 System.out.println("Player-" + id + ": SENT " + message); 66 } 67 68 public void exceptionCaught(IoSession session, Throwable cause) { 69 cause.printStackTrace(); 70 session.close(); 71 } 72 } | Popular Tags |