-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRequestHandler.java
119 lines (105 loc) · 4.32 KB
/
RequestHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*****************************************************
* This class runs on the server side portion of the game.
* It creates a new thread for each client that is connected
* to the server.
*****************************************************/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.HashMap;
class RequestHandler extends Thread {
private Socket socket;
private GameHandler gameHandler;
Message returnedMessage;
ObjectInputStream ois;
ObjectOutputStream oos;
Player player;
RequestHandler(Socket socket, GameHandler gameHandler) {
this.socket = socket;
this.gameHandler = gameHandler;
try {
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
System.out.println("New client connected.");
//Print the thread id
System.out.println("Thread ID is: " + getId());
//Create a new player and assign its id from the thread ID.
player = new Player(getId());
//add the new player to the gamestate
gameHandler.addPlayerToGame(getId(), player);
//increase the number of players and display amount
gameHandler.incrementAmountOfPlayers();
System.out.println("Player amount: " + gameHandler.getNumberOfCurrentPlayers());
// sleep gives time to client to set up input & output streams
Thread.sleep(100);
while (socket.isConnected()) {
/*listen for a message from the client and handle
the message logic. Also return new message to be sent
to the client */
returnedMessage = listenForMessageFromClient();
//Send the new messaget to the client.
sendMessageToClient(returnedMessage);
}
} catch (Exception e) {
System.out.println("\nException handled in Request Handler run(), client is disconnected");
try {
socket.close(); //close the socket
Server.removeSocket(socket); //remove socket from clientSocketList
} catch (IOException e1) {
System.out.println("Error closing connection");
}
System.out.println("Connection closed for player: " + this.getId());
System.out.println("Server listening for more connectoins...");
/* TODO add function to set character to available
TODO to add function to decrement player count */
}
}
synchronized private void sendMessageToClient(Message gameObj) throws IOException {
//Send message to the client
this.oos.writeUnshared(gameObj);
oos.flush();
}
synchronized private Message listenForMessageFromClient() {
try {
Message gameObject = (Message) ois.readUnshared();
Message returnObject = gameHandler.parseMessage(gameObject, getId());
return returnObject;
} catch (SocketException e) {
Message errorMessage = new Message(ClueGameConstants.ERROR_CODE, new String("Error!!!"));
try {
ois.close();
} catch (IOException e1) {
System.out.println("Error in RequestHandler listenForMessageFromClient");
e1.printStackTrace();
}
return errorMessage;
} catch (IOException e2){
System.out.println("Error in RequestHandler listenForMessageFromClient");
Message errorMessage = null;
try {
ois.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return errorMessage;
} catch(ClassNotFoundException e3) {
System.out.println("Error in RequestHandler listenForMessageFromClient");
Message errorMessage = null;
try {
ois.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return errorMessage;
}
}
} // Class RequestHandler