forked from husseinalosman/simpleChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchoServer.java
199 lines (163 loc) · 6.58 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
// 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.*;
/**
* 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;
// Constructors ****************************************************
/**
* Constructs an instance of the echo server.
*
* @param port The port number to connect on.
*/
public EchoServer(int port) {
super(port);
}
// 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) {
// Displays the incoming message
System.out.println("Message received: " + msg + " from " + client.getInfo("loginID") + ".");
// Analyzes if a login was requested
switch (msg.toString().split(" ")[0]) {
case "#login":
// Analyzes if it is not the first time the loginID was used
if (client.getInfo("loginID") != null) {
try {
// If it wasn't the first time, it displays an error message
// and terminates the connection with the user
client.sendToClient(
"The '#login' command should only be used during the login.\nTerminating connection.");
client.close();
} catch (IOException e) {
System.out.println("Couln't kickout the client...");
}
}
// If it is the first time the command is used
else {
try {
// Sets the second part of the message to be the
// user's loginID
client.setInfo("loginID", msg.toString().split(" ")[1]);
// Displays the login message to the server console
System.out.println(client.getInfo("loginID") + " has logged on.");
// Displays the login message to all connected clients
this.sendToAllClients("> " + client.getInfo("loginID") + " has logged on.");
}
// If no loginID was provided
catch (ArrayIndexOutOfBoundsException e) {
try {
client.sendToClient("No login ID was provided. Terminating connection.");
client.close();
}
// Any other error
catch (IOException ex) {
System.out.println("An unknown error occured.");
}
}
}
break;
case "#logoff":
// Sends a message to all clients showing who disconnected
this.sendToAllClients("> " + client.getInfo("loginID") + " has disconnected.");
// Displays a message on the server console showing who disconnected
this.clientDisconnected(client);
break;
case "#quit":
// Sends a message to all clients showing who quit
this.sendToAllClients("> " + client.getInfo("loginID") + " has disconnected.");
// Displays a message on the server console showing who quit
this.clientDisconnected(client);
break;
default:
this.sendToAllClients("> " + client.getInfo("loginID") + ": " + msg);
}
}
/**
* 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.");
}
/**
* This method displays a message on the server console to show that a new
* client was connected
*/
@Override
protected void clientConnected(ConnectionToClient client) {
System.out.println("A new client is attempting to connect to the server.");
}
/**
* This method displays a message on the server console to show that a client
* was disconnected
*/
@Override
synchronized protected void clientDisconnected(ConnectionToClient client) {
System.out.println(client.getInfo("loginID") + " has disconnected.");
}
/**
* This method closes the connection with a client if there was an exception
*/
@Override
synchronized protected void clientException(ConnectionToClient client, Throwable exception) {
try {
client.close();
} catch (IOException e) {
System.out.println("An error occured with a client.");
}
}
// 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
}
// Creates the server
EchoServer sv = new EchoServer(port);
try {
sv.listen(); // Start listening for connections
ServerConsole serverConsole = new ServerConsole(sv); // Creates the server console
serverConsole.accept();
} catch (Exception ex) {
System.out.println("ERROR - Could not listen for clients!");
}
}
}
//End of EchoServer class