-
Notifications
You must be signed in to change notification settings - Fork 4
/
EchoServer.java
310 lines (282 loc) · 7.34 KB
/
EchoServer.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com
import java.io.*;
import ocsf.server.*;
import common.*;
/**
* This class overrides some of the methods in the abstract
* superclass in order to give more functionality to the server.
*
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganière
* @author François Bélanger
* @author Paul Holden
* @version July 2000
*/
public class EchoServer extends AbstractServer
{
//Class variables *************************************************
/**
* The default port to listen on.
*/
final public static int DEFAULT_PORT =5555;
/**
* The interface type variable. It allows the implementation of
* the display method in the client.
*/
ChatIF serverUI;
//Constructors ****************************************************
/**
* Constructs an instance of the echo server.
*
* @param port The port number to connect on.
*/
public EchoServer(int port)
{
super(port);
}
/**
* Constructs an instance of the echo server.
*
* @param port The port number to connect on.
* @param serverUI The interface type variable.
*/
public EchoServer(int port, ChatIF serverUI) throws IOException
{
super(port);
this.serverUI = serverUI;
}
//Instance methods ************************************************
/**
* This method handles any messages received from the client.
*
* @param msg The message received from the client.
* @param client The connection from which the message originated.
*/
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
if (msg.toString().startsWith("#login "))
{
if (client.getInfo("loginID") != null)
{
try
{
client.sendToClient("You are already logged in.");
}
catch (IOException e)
{
}
return;
}
client.setInfo("loginID", msg.toString().substring(7));
}
else
{
if (client.getInfo("loginID") == null)
{
try
{
client.sendToClient("You need to login before you can chat.");
client.close();
}
catch (IOException e) {}
return;
}
System.out.println("Message received: " + msg + " from \"" +
client.getInfo("loginID") + "\" " + client);
this.sendToAllClients(client.getInfo("loginID") + "> " + msg);
}
}
/**
* This method handles all data coming from the UI
*
* @param message The message from the UI
*/
public void handleMessageFromServerUI(String message)
{
if (message.charAt(0) == '#')
{
runCommand(message);
}
else
{
// send message to clients
serverUI.display(message);
this.sendToAllClients("SERVER MSG> " + message);
}
}
/**
* This method executes server commands.
*
* @param message String from the server console.
*/
private void runCommand(String message)
{
// run commands
// a series of if statements
if (message.equalsIgnoreCase("#quit"))
{
quit();
}
else if (message.equalsIgnoreCase("#stop"))
{
stopListening();
}
else if (message.equalsIgnoreCase("#close"))
{
try
{
close();
}
catch(IOException e) {}
}
else if (message.toLowerCase().startsWith("#setport"))
{
if (getNumberOfClients() == 0 && !isListening())
{
// If there are no connected clients and we are not
// listening for new ones, assume server closed.
// A more exact way to determine this was not obvious and
// time was limited.
int newPort = Integer.parseInt(message.substring(9));
setPort(newPort);
//error checking should be added
serverUI.display
("Server port changed to " + getPort());
}
else
{
serverUI.display
("The server is not closed. Port cannot be changed.");
}
}
else if (message.equalsIgnoreCase("#start"))
{
if (!isListening())
{
try
{
listen();
}
catch(Exception ex)
{
serverUI.display("Error - Could not listen for clients!");
}
}
else
{
serverUI.display
("The server is already listening for clients.");
}
}
else if (message.equalsIgnoreCase("#getport"))
{
serverUI.display("Currently port: " + Integer.toString(getPort()));
}
}
/**
* This method overrides the one in the superclass. Called
* when the server starts listening for connections.
*/
protected void serverStarted()
{
System.out.println
("Server listening for connections on port " + getPort());
}
/**
* This method overrides the one in the superclass. Called
* when the server stops listening for connections.
*/
protected void serverStopped()
{
System.out.println
("Server has stopped listening for connections.");
}
/**
* Run when new clients are connected. Implemented by Benjamin Bergman,
* Oct 22, 2009.
*
* @param client the connection connected to the client
*/
protected void clientConnected(ConnectionToClient client)
{
// display on server and clients that the client has connected.
String msg = "A Client has connected";
System.out.println(msg);
this.sendToAllClients(msg);
}
/**
* Run when clients disconnect. Implemented by Benjamin Bergman,
* Oct 22, 2009
*
* @param client the connection with the client
*/
synchronized protected void clientDisconnected(
ConnectionToClient client)
{
// display on server and clients when a user disconnects
String msg = client.getInfo("loginID").toString() + " has disconnected";
System.out.println(msg);
this.sendToAllClients(msg);
}
/**
* Run when a client suddenly disconnects. Implemented by Benjamin
* Bergman, Oct 22, 2009
*
* @param client the client that raised the exception
* @param Throwable the exception thrown
*/
synchronized protected void clientException(
ConnectionToClient client, Throwable exception)
{
String msg = client.getInfo("loginID").toString() + " has disconnected";
System.out.println(msg);
this.sendToAllClients(msg);
}
/**
* This method terminates the server.
*/
public void quit()
{
try
{
close();
}
catch(IOException e)
{
}
System.exit(0);
}
//Class methods ***************************************************
/**
* This method is responsible for the creation of
* the server instance (there is no UI in this phase).
*
* @param args[0] The port number to listen on. Defaults to 5555
* if no argument is entered.
*/
public static void main(String[] args)
{
int port = 0; //Port to listen on
try
{
port = Integer.parseInt(args[0]); //Get port from command line
}
catch(Throwable t)
{
port = DEFAULT_PORT; //Set port to 5555
}
EchoServer sv = new EchoServer(port);
try
{
sv.listen(); //Start listening for connections
}
catch (Exception ex)
{
System.out.println("ERROR - Could not listen for clients!");
}
}
}
//End of EchoServer class