-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerReceiver.java
More file actions
172 lines (155 loc) · 5.46 KB
/
ServerReceiver.java
File metadata and controls
172 lines (155 loc) · 5.46 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.io.*;
// Gets messages from client and puts them in a queue, for another
// thread to forward to the appropriate client.
public class ServerReceiver extends Thread {
private String myClientsName;
private BufferedReader myClient;
private ClientTable clientTable;
private ScoreboardTable scoreboardTable;
/**
*
* @param myClientsName
* - the name of the user
* @param myClient
* - used to close the client when needed
* @param clientTable
* - table with users
* @param scoreboardTable
* - scoreboard
*/
public ServerReceiver(String myClientsName, BufferedReader myClient, ClientTable clientTable,
ScoreboardTable scoreboardTable) {
this.myClientsName = myClientsName;
this.myClient = myClient;
this.clientTable = clientTable;
this.scoreboardTable = scoreboardTable;
}
public void run() {
try {
while (true) {
// Reads the two lines that the user passed from the
// ClientSender
String opponent = myClient.readLine();
String cellActive = myClient.readLine();
/*
* if user passes Yes - the message is offered to the queue of
* the user opponent
*/
if (cellActive.equals("Yes")) {
Message msg = new Message(myClientsName, cellActive + " " + myClientsName);
MessageQueue opponentQueue = clientTable.getQueue(opponent);
if (opponentQueue != null)
opponentQueue.offer(msg);
else
System.err.println("Message for unexistent client " + opponent + ": " + cellActive);
}
/*
* if user passes No - the message is offered to the queue of
* the user opponent
*/
else if (cellActive.equals("No")) {
Message msg = new Message(myClientsName, cellActive);
MessageQueue opponentQueue = clientTable.getQueue(opponent);
if (opponentQueue != null)
opponentQueue.offer(msg);
else
System.err.println("Message for unexistent client " + opponent + ": " + cellActive);
}
/*
* if user passes GetUsers - the message is offered to his queue
*/
else if (cellActive.equals("GetUsers")) {
Message msg = new Message(myClientsName, cellActive + myClientsName);
MessageQueue opponentQueue = clientTable.getQueue(myClientsName);
if (opponentQueue != null)
opponentQueue.offer(msg);
else
System.err.println("Message for unexistent client " + myClientsName + ": " + cellActive);
}
/*
* if user passes Increase - method that increases the score of
* the user is being called
*/
else if (cellActive.equals("Increase")) {
scoreboardTable.updateScore(myClientsName);
}
/*
* if user passes Scoreboard - the message is offered to his
* queue
*/
else if (cellActive.equals("Scoreboard")) {
Message msg = new Message(myClientsName, cellActive);
MessageQueue scoreboardQueue = clientTable.getQueue(myClientsName);
if (scoreboardQueue != null)
scoreboardQueue.offer(msg);
else
System.err.println("Message for unexistent client " + myClientsName + ": " + cellActive);
}
/*
* if user passes Exit - the message is offered to the queue of
* the user opponent
*/
else if (cellActive.equals("Exit")) {
Message msg = new Message(myClientsName, cellActive);
MessageQueue opponentQueue = clientTable.getQueue(opponent);
if (opponentQueue != null)
opponentQueue.offer(msg);
else
System.err.println("Message for unexistent client " + opponent + ": " + cellActive);
}
/*
* if not a specific text is send - this means that the user is
* in game
*/
else {
// checks if user passes an integer
boolean number = true;
try {
int a = Integer.parseInt(cellActive);
} catch (NumberFormatException e) {
number = false;
}
/*
* if both opponent and number are passed correctly - the
* number is offered to the queue of the user opponent
*/
if (opponent != null && number) {
Message msg = new Message(myClientsName, cellActive);
MessageQueue opponentQueue = clientTable.getQueue(opponent);
if (opponentQueue != null)
opponentQueue.offer(msg);
else
System.err.println("Message for unexistent client " + opponent + ": " + cellActive);
}
/*
* if the passed number is not an Integer - but the opponent
* is correct - then it has been made a request for game -
* the question with the name of the user are passed
*/
else if (opponent != null) {
String question = "Do you want to play?" + myClientsName;
Message msg = new Message(myClientsName, question);
MessageQueue opponentQueue = clientTable.getQueue(opponent);
if (opponentQueue != null) {
opponentQueue.offer(msg);
} else {
System.err.println("Message for unexistent client " + opponent + ": " + question);
}
} else {
myClient.close(); // the client must close
return;
}
}
}
} catch (IOException e) {
System.err.println("Something went wrong with the client " + myClientsName + " " + e.getMessage());
clientTable.remove(myClientsName);
// No point in trying to close sockets. Just give up.
// We end this thread (we don't do System.exit(1)).
} catch (NullPointerException e) { // catch NullPointerException to
// remove the user from the table
System.err.println("Something went wrong with the client " + myClientsName + " " + e.getMessage());
clientTable.remove(myClientsName);
}
}
}