package __deprecated.server;
import game.dice.Dice;
import game.gamesheet.GameSheet;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
class ClientSession extends Thread {
private Socket socket;
private OutboundObjects outQueue;
private ActiveSessions activeSessions;
private ObjectInputStream netIn;
private ObjectOutputStream netOut;
public ClientSession(Socket s, OutboundObjects out, ActiveSessions as) throws IOException {
socket = s;
outQueue = out;
activeSessions = as;
netOut = new ObjectOutputStream(socket.getOutputStream());
netIn = new ObjectInputStream(socket.getInputStream());
System.out.println("ClientSession started on " + this + ".");
start();
}
public void run() {
try {
activeSessions.addSession(this);
while (true) {
Object fromClient = null;
try {
fromClient = netIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(fromClient.getClass().getSimpleName().equals("Dice")){
Dice diceFromClient = (Dice) fromClient;
System.out.println(diceFromClient.toString());
outQueue.addObject(diceFromClient);
}else if(fromClient.getClass().getSimpleName().equals("GameSheet")){
GameSheet gamesheetFromClient = (GameSheet)fromClient;
outQueue.addObject(gamesheetFromClient);
}
}
} catch (IOException e) {
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
}
public void sendObject(Object object) {
try {
if (!socket.isClosed()) {
netOut.reset();
netOut.writeObject(object);
netOut.flush();
} else {
throw new IOException();
}
} catch (IOException eee) {
try {
socket.close();
} catch (IOException ee) {
ee.printStackTrace();
}
}
}
public String toString() {
return super.getName();
}
}