forked from husseinalosman/simpleChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientConsole.java
More file actions
228 lines (179 loc) · 5.94 KB
/
ClientConsole.java
File metadata and controls
228 lines (179 loc) · 5.94 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
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
// 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;
String loginID;
//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);
client.sendToServer("#loginID " + loginID);
}
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();
if (message.toString().charAt(0) == '#') { // if the first character of the input is a #
if (!message.contains(" ")) { // only one word input
String command = message.substring(1); // command is the second part of the input (after #)
switch(command) {
case "quit":
System.out.println("Closing program.");
client.quit(); // close client
break;
case "logoff":
client.closeConnection(); // closeConnection
break;
case "gethost":
System.out.println(client.getHost());
break;
case "getport":
System.out.println(Integer.toString(client.getPort()));
break;
case "login":
System.out.println("Error: missing login ID.");
default:
System.out.println("Not a valid command.");
break;
}
}
else if (message.contains(" ")) { // two word input
String command = message.split(" ")[0];
String para = message.split(" ")[1];
switch (command) {
case "#sethost":
if (client.isConnected()) { // error message if client is already connected
System.out.println("Error: Already connected to server.");
} else {
client.setHost(para);
}
break;
case "#setport":
if (client.isConnected()) { // error message if client is already connected
System.out.println("Error: Already connected to server.");
} else {
client.setPort(Integer.parseInt(para));
}
break;
case "#login":
if (client.isConnected()) {
System.out.println("Error: Already connected to server.");
} else {
client.setLoginID(message.split(" ")[1]);
}
client.openConnection();
client.sendToServer("#loginID " + client.getLoginID());
client.setLoginID(message.split(" ")[1]);
break;
default:
System.out.println("Not a valid command."); // if used # but not a valid command
break;
}
}
}
else { // if the input isn't a command
client.handleMessageFromClientUI(message);
}
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}
}
/**
* 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 host to connect to.
*/
public static void main(String[] args)
{
String host = "localhost"; // initialize host
int port = DEFAULT_PORT; // initialize port number to default
String loginID = null;
try
{
loginID = (String)args[0];
}
catch(Throwable t) {
System.out.println("ERROR - No login ID specified. Connection aborted.");
System.exit(1);
}
try
{
port = Integer.parseInt(args[1]); //get port from command line
}
catch(Throwable t)
{
System.out.println("Didn't specify port or wrong input. Using default port.");
}
ClientConsole chat= new ClientConsole(loginID, host, port);
chat.accept(); //Wait for console data
}
}
//End of ConsoleChat class