forked from husseinalosman/simpleChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientConsole.java
307 lines (252 loc) · 11 KB
/
ClientConsole.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
// 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 java.util.Scanner;
import client.*;
import common.*;
/**
* This class constructs the UI for a chat client. It implements the chat
* interface in order to activate the display() method. Warning: Some of the
* code here is cloned in ServerConsole
*
* @author François Bélanger
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganière
* @version September 2020
*/
public class ClientConsole implements ChatIF {
// Class variables *************************************************
/**
* The default port to connect on.
*/
final public static int DEFAULT_PORT = 5555;
// Instance variables **********************************************
/**
* The instance of the client that created this ConsoleChat.
*/
ChatClient client;
/**
* Scanner to read from the console
*/
Scanner fromConsole;
// Constructors ****************************************************
/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ClientConsole(String loginID, String host, int port) {
try {
client = new ChatClient(loginID, host, port, this);
} catch (IOException exception) {
System.out.println("Cannot open connection. Awaiting command.");
}
// Create scanner object to read from console
fromConsole = new Scanner(System.in);
}
// Instance methods ************************************************
/**
* This method waits for input from the console. Once it is received, it sends
* it to the client's message handler.
*/
public void accept() {
try {
String message;
while (true) {
message = fromConsole.nextLine();
// Prevents the program from crashing
// by making sure the message is not null
if (message.equals("")) {
accept();
}
// First see if there is a command
if (message.charAt(0) == '#') {
// Looks a the fist word of the message
switch (message.split(" ")[0]) {
case "#quit":
// Tries to close normally
// If it can't, it force closes
try {
client.handleMessageFromClientUI(message);
client.quit();
} catch (NullPointerException e) {
System.exit(0);
}
break;
case "#logoff":
// Disconnects the user
try {
client.handleMessageFromClientUI(message);
client.closeConnection();
} catch (IOException e) {
display("The connection could not be closed.");
}
break;
case "#sethost":
try {
// Makes sure the client is not connected
try {
if (client.isConnected()) {
display("Make sure to disconnect using the command '#logoff' before "
+ "\nchanging your host name.");
break;
}
}
// If any problem occurs
catch (Exception e) {
// Do nothing
}
// Displays the second part of the message
// which is the future host name
display("Setting host name to '" + message.split(" ")[1] + "'");
// Sets the host name
client.setHost(message.split(" ")[1]);
// Shows success
display("Done! Your host name is now set to '" + message.split(" ")[1] + "'");
}
// If no host name was given
catch (ArrayIndexOutOfBoundsException e) {
display("Make sure to also include your desired host name at the end"
+ " of your command.");
}
// If it couldn't connect to the client
catch (NullPointerException en) {
display("Done! Your host name is now set to '" + message.split(" ")[1] + "'");
}
// Any other problem
catch (Exception ex) {
display("There was a problem with the host name you entered.");
}
break;
case "#setport":
try {
// Makes sure the client is not connected
try {
if (client.isConnected()) {
display("Make sure to disconnect using the command '#logoff' before "
+ "\nchanging your port number.");
break;
}
}
// If any problem occurs
catch (Exception e) {
// Do nothing
}
// Displays the second part of the message
// which is the future port number
display("Setting the port number to " + message.split(" ")[1]);
// Sets the port number
client.setPort(Integer.parseInt(message.split(" ")[1]));
// Shows success
display("Done! Your port is now set to " + message.split(" ")[1]);
}
// If no port number was given
catch (ArrayIndexOutOfBoundsException e) {
display("Make sure to also include your desired port at the end of your command.");
}
// If it couldn't connect to the client
catch (NullPointerException en) {
display("Done! Your port is now set to " + message.split(" ")[1]);
}
// Any other problem
catch (Exception ex) {
display("There was a problem with the port number you entered. Make sure its a number.");
}
break;
case "#login":
// If client is not connected
if (!client.isConnected()) {
try {
// If a user name was provided it changes the
// loginID so it can use the new user name provided
if (message.split(" ").length == 2) {
client.setLoginID(message.split(" ")[1]);
}
// Otherwise, it connects with the user name
// previously provided
client.openConnection();
} catch (IOException e) {
display("The server could not be reached.");
}
}
// If client is already connected
else {
display("You are already logged in. You can't login again.");
}
break;
case "#gethost":
display("Your current hostname is '" + client.getHost() + "'.");
break;
case "#getport":
display("Your current port is " + client.getPort() + ".");
break;
default:
display("'" + message + "' doesn't match any commands.");
}
}
// If no command was given, proceed to sending the message to the client
else {
try {
client.handleMessageFromClientUI(message);
} catch (NullPointerException e) {
display("Couldn't connect to server.\n"
+ "Enter '#login <loginID>', or if you already logged in '#login'\n"
+ "to try to connect to the server.");
}
}
}
}
// If it couldn't connect to the server/client
catch (Exception ex) {
display("Couldn't connect to server. Enter '#quit' to exit.");
accept();
}
}
/**
* This method overrides the method in the ChatIF interface. It displays a
* message onto the screen.
*
* @param message The string to be displayed.
*/
public void display(String message) {
System.out.println(message);
}
// Class methods ***************************************************
/**
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The loginID.
* @param args[1] The host name.
* @param args[2] The port number.
*/
public static void main(String[] args) {
String host = "";
String loginID = "";
int port;
// For the loginID
try {
loginID = args[0];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ERROR - No login ID specified. Connection aborted.");
System.exit(0);
}
// For the host name
try {
host = args[1];
} catch (ArrayIndexOutOfBoundsException e) {
host = "localhost";
}
// For the port
try {
port = Integer.parseInt(args[2]);
} catch (ArrayIndexOutOfBoundsException ex) {
port = DEFAULT_PORT;
}
// Creates the chat
ClientConsole chat = new ClientConsole(loginID, host, port);
chat.accept(); // Wait for console data
}
}
//End of ConsoleChat class