package server;
import java.io.IOException;
public class Client extends Thread {
BaseGame game;
Communicator comm;
int id;
boolean isReady;
volatile boolean keepRunning;
Client(BaseGame game, Communicator c, int id) {
this.game = game;
comm = c;
this.id = id;
isReady = false;
keepRunning = false;
}
Client(BaseGame game, Connector conn, byte proto, int id) {
this(game, ctorHelper(conn, proto), id);
}
private static Communicator ctorHelper(Connector conn, byte proto) {
Communicator comm = null;
switch(proto) {
case ServerGame.PROTO_VIBE_DUMB:
comm = new VibeDumb(conn);
break;
default:
throw new InternalHanabiError(); // TODO
}
return comm;
}
void stopListening() { keepRunning = false; }
@Override
public void run() {
keepRunning = true;
boolean retry = true;
do {
try {
comm.establish();
break;
} catch(IOException ex) {
retry = game.handleIOException(ex);
}
} while(retry);
listen();
}
void listen() {
while(keepRunning) {
Move m = null;
boolean retry = true;
while(retry){
try {
m = comm.receiveMove();
break;
} catch(IOException ex) {
retry = game.handleIOException(ex);
if(!keepRunning) return;
}
}
game.processMove(m);
}
}
}