forked from TimLethbridge/Lloseng
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatClient.java
More file actions
210 lines (179 loc) · 5.34 KB
/
ChatClient.java
File metadata and controls
210 lines (179 loc) · 5.34 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
// 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
package client;
import ocsf.client.*;
import common.*;
import java.io.*;
import java.util.Arrays;
import java.util.List;
/**
* This class overrides some of the methods defined in the abstract
* superclass in order to give more functionality to the client.
*
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganiè
* @author François Bélanger
* @version July 2000
*/
public class ChatClient extends AbstractClient
{
//Instance variables **********************************************
/**
* The interface type variable. It allows the implementation of
* the display method in the client.
*/
ChatIF clientUI;
//Constructors ****************************************************
/**
* Constructs an instance of the chat client.
*
* @param host The server to connect to.
* @param port The port number to connect on.
* @param clientUI The interface type variable.
*/
public ChatClient(String host, int port, ChatIF clientUI)
throws IOException
{
super(host, port); //Call the superclass constructor
this.clientUI = clientUI;
openConnection();
}
//Instance methods ************************************************
/**
* This method handles all data that comes in from the server.
*
* @param msg The message from the server.
*/
public void handleMessageFromServer(Object msg)
{
clientUI.display(msg.toString());
}
/**
* This method handles all data coming from the UI
*
* @param message The message from the UI.
*/
public void handleMessageFromClientUI(String message)
{
try
{
sendToServer(message);
}
catch(IOException e)
{
clientUI.display
("Could not send message to server. Terminating client.");
quit();
}
}
/**
* This method terminates the client.
*/
public void quit()
{
try
{
closeConnection();
}
catch(IOException e) {}
System.exit(0);
}
/**
* Hook method called after the connection has been closed. The default
* implementation does nothing. The method may be overriden by subclasses to
* perform special processing such as cleaning up and terminating, or
* attempting to reconnect.
*/
protected void connectionClosed() {
System.out.println("SYSTEM ::: Connection to the Server Terminated");
}
/**
* Hook method called each time an exception is thrown by the client's
* thread that is waiting for messages from the server. The method may be
* overridden by subclasses.
*
* @param exception
* the exception raised.
*/
protected void connectionException(Exception exception) {
System.out.println("SYSTEM ::: Server Shutdown");
quit();
}
/**
* This method contains all the client commands.
* Every commands begin with '#'.
*
* @param command word that needs to be actioned.
*/
public void clientCommand(String message){
//Make the message received into an array (necessary for setHost/setPort)
String[] messageArray = message.split(" ");
String[] commandArray = new String[]{"#quit","#login","#logoff","#gethost","#sethost","#getport","#setport"};
//If the command isn't in the 'known' list, give error message and command list.
if (!Arrays.asList(commandArray).contains(messageArray[0])){
System.out.println("The command line you entered is 'INVALID', here is a list of all current commands.");
System.out.println("#quit // #login // #logoff // #gethost // #sethost // #getport // #setport");
}
switch(messageArray[0]) {
case "#quit":
quit();
case "#login":
//what happens in #login
if (isConnected()){
System.out.println("SYSTEM ::: The client is already connected");
return;
}
else {
try
{
openConnection();
System.out.println("SYSTEM ::: Client connected");
}
catch (IOException ex){System.out.println("SYSTEM ::: Failed to connect to Server");}
return;
}
case "#logoff":
try
{
System.out.println("SYSTEM ::: Shutting down client");
closeConnection();
return;
}
catch(IOException e) {}
return;
case "#gethost":
System.out.println("SYSTEM ::: The Host name is: " + getHost());
return;
case "#sethost":
try
{
setHost(messageArray[1]);
System.out.println("SYSTEM ::: The new Host has been set to: " + getHost());
} catch (Exception ex2)
{
System.out.println("SYSTEM ::: Array Out of Bound: Please try again with the new Host name.");
} return;
case "#getport":
try
{
System.out.println("SYSTEM ::: The current Port is: " + getPort());
}
catch (Exception a)
{
System.out.println("SYSTEM ::: There was an error in the getPort attempt");
}
return;
case "#setport":
try {
int newPort = Integer.parseInt(messageArray[1]);
setPort(newPort);
System.out.println("SYSTEM ::: The new Port has been set to: " + getPort());
} catch (Exception ex2)
{
System.out.println("SYSTEM ::: Array Out-of-Bound or your Port was not a valid Integer between 1 and 65535.");
} return;
}
}
}
//End of ChatClient class