1: package siege.net;
2:
3: import java.awt.Point;
4: import java.net.MalformedURLException;
5: import java.rmi.AlreadyBoundException;
6: import java.rmi.NoSuchObjectException;
7: import java.rmi.NotBoundException;
8: import java.rmi.RemoteException;
9: import java.util.LinkedList;
10: import siege.core.Game;
11: import siege.core.GameStatus;
12: import siege.util.Log;
13:
14: 15: 16: 17: 18: 19:
20: public class Session extends Thread
21: {
22: 23: 24:
25: public Server server;
26:
27: 28: 29:
30: public Client client;
31:
32: 33: 34:
35: public Game game;
36:
37: private Point lastMovement;
38: private LinkedList<String> messageQueue = new LinkedList<String>();
39:
40: public Session(Game game)
41: {
42: super();
43: setPriority(MIN_PRIORITY);
44: this.game = game;
45: }
46:
47: @Override
48: public void run()
49: {
50: try {
51: while(true) {
52: switch(game.getStatus()) {
53: case CONNECTING:
54: routineConnecting();
55: break;
56: case PLAYING:
57: case WAITING:
58: routineSendMessages();
59: break;
60: }
61: if(lastMovement != null) {
62: client.sendMovement(lastMovement.x, lastMovement.y);
63: lastMovement = null;
64: }
65: if(game.getStatus().equals(GameStatus.END)) {
66: close();
67: }
68: sleep(1000);
69: }
70: } catch (NotBoundException ex) {
71: Log.error(this, ex);
72: } catch (MalformedURLException ex) {
73: Log.error(this, ex);
74: } catch (RemoteException ex) {
75: ex.printStackTrace();
76: Log.error(this, ex);
77: } catch (AlreadyBoundException ex) {
78: Log.error(this, ex);
79: } catch (InterruptedException ex) {
80: Log.error(this, ex);
81: } finally {
82: close();
83: }
84: }
85:
86: private void routineConnecting() throws RemoteException, AlreadyBoundException, NotBoundException, MalformedURLException
87: {
88: if(server == null) {
89: server = new Server(this);
90: }
91: if(game.getLocalPlayer().isChallenger()) {
92: if(game.getRemotePlayer().getAddress() != null) {
93: client = new Client();
94: client.saveName(game.getLocalPlayer().getName());
95: game.connected();
96: }
97: } else {
98: client = new Client();
99: client.saveName(game.getLocalPlayer().getName());
100: client.saveAddress(game.getLocalPlayer().getAddress());
101: game.connected();
102: }
103: }
104:
105: private void routineSendMessages() throws RemoteException
106: {
107: while(messageQueue.isEmpty() == false) {
108: String message = messageQueue.pollFirst();
109: client.sendMessage(message);
110: }
111: }
112:
113: public void sendMovement(int x, int y)
114: {
115: lastMovement = new Point(x, y);
116: }
117:
118: public void sendMessage(String message)
119: {
120: messageQueue.add(message);
121: }
122:
123: public void retreat() throws RemoteException
124: {
125: client.retreat();
126: }
127:
128: public void close()
129: {
130: interrupt();
131: try {
132: Server.unexportObject(server, true);
133: Log.message(this, "close > successfully unexported previous session");
134: } catch (NoSuchObjectException ex) {
135: Log.error(this, "close > "+ex);
136: }
137: }
138: }
139: