📄 tennisplayer.java
字号:
/*
* @(#) $Id: TennisPlayer.java 357871 2005-12-20 01:56:40Z trustin $
*/
package org.apache.mina.examples.tennis;
import org.apache.mina.protocol.ProtocolCodecFactory;
import org.apache.mina.protocol.ProtocolHandler;
import org.apache.mina.protocol.ProtocolHandlerAdapter;
import org.apache.mina.protocol.ProtocolProvider;
import org.apache.mina.protocol.ProtocolSession;
/**
* A {@link ProtocolHandler} implementation which plays a tennis game.
*
* @author The Apache Directory Project (dev@directory.apache.org)
* @version $Rev: 357871 $, $Date: 2005-12-20 10:56:40 +0900 (Tue, 20 Dec 2005) $
*/
public class TennisPlayer implements ProtocolProvider {
private final ProtocolHandler HANDLER = new TennisPlayerHandler();
public ProtocolCodecFactory getCodecFactory() {
throw new UnsupportedOperationException();
}
public ProtocolHandler getHandler() {
return HANDLER;
}
private static class TennisPlayerHandler extends ProtocolHandlerAdapter {
private static int nextId = 0;
/** Player ID * */
private final int id = nextId++;
public void sessionOpened(ProtocolSession session) {
System.out.println("Player-" + id + ": READY");
}
public void sessionClosed(ProtocolSession session) {
System.out.println("Player-" + id + ": QUIT");
}
public void messageReceived(ProtocolSession session, Object message) {
System.out.println("Player-" + id + ": RCVD " + message);
TennisBall ball = (TennisBall) message;
// Stroke: TTL decreases and PING/PONG state changes.
ball = ball.stroke();
if (ball.getTTL() > 0) {
// If the ball is still alive, pass it back to peer.
session.write(ball);
} else {
// If the ball is dead, this player loses.
System.out.println("Player-" + id + ": LOSE");
session.close();
}
}
public void messageSent(ProtocolSession session, Object message) {
System.out.println("Player-" + id + ": SENT " + message);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -