-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
289 lines (248 loc) · 8.12 KB
/
Client.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
package com.kd.chat;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;
import com.kd.chat.messaging.AbstractMessage;
import com.kd.chat.messaging.ChatMessage;
import com.kd.chat.messaging.ClientExchangeMessage;
import com.kd.chat.messaging.HandshakeMessage;
import com.kd.chat.messaging.MessageListener;
/*
* Representation of a remote chat client connected to this client.
* Handles reading messages from the remote client, which are passed to any
* registered MessageListener interfaces.
*
* @author Robert Moore
*
*/
public class Client extends Thread{
//The socket connected to the remote client
protected final Socket socket;
//username of this client
protected String username;
//IP address of this client
protected final String ipAddress;
//listen port of this client the value may not be known until after the
//handshake is recieved
protected int port = -1;
//the username of the local client. Used for generating handshake message
protected final String localUsername;
//listen port of the local client. used for generating handshake messages
protected final int localPort;
//Collection of MessageListener interfaces that should be notified of
//recieved messages
protected final Collection<MessageListener> listeners = new ConcurrentLinkedQueue<MessageListener>();
protected boolean keepRunning = true;
/*
* Creates a new Client with the specified parameters. Does not connect to the remote
* client until {@link #connect()} is called
*
* @param ipAddress
* the IP address or hostname of this client.
* @param port
* the listen port of this client
* @param username
* the username of this client, may be null if its not known
* @param localUsername
* the username of the local client
* @param localPort
* the listen port of the local client
*/
public Client(final String ipAddress, final int port,
final String username, final String localUsername,
final int localPort){
this.ipAddress = ipAddress;
this.port = port;
this.username = username;
this.localUsername = localUsername;
this.localPort = localPort;
this.socket = new Socket();
}
public Client(final Socket socket, final String localUsername,
final int localPort){
this.socket = socket;
this.localUsername = localUsername;
this.localPort = localPort;
//Grab the actual address in case a hostname was provided
this.ipAddress = this.socket.getInetAddress().getHostAddress();
}
public void connect() throws IOException{
if(this.socket != null && !this.socket.isConnected()){
this.socket.connect(new InetSocketAddress(this.ipAddress, this.port));
}
}
public synchronized boolean performHandshake(){
HandshakeMessage sentMessage = null;
try{
sentMessage = new HandshakeMessage(this.localUsername, this.localPort);
}catch(UnsupportedEncodingException uee){
System.err.println("Unable to encode handshake");
System.err.println(uee.getMessage());
uee.printStackTrace(System.err);
return false;
}
AbstractMessage recievedMessage = null;
//keep reading messages until a HandshakeMessage is recieved
do{
try{
recievedMessage = AbstractMessage.decodeMessage(this.socket.getInputStream());
if(recievedMessage == null){
//Allow other threads to issue before trying again
Thread.yield();
continue;
}
}catch(IOException e){
System.err.println("Unable to read handshake from remote client");
System.err.println(e.getMessage());
e.printStackTrace();
return false;
}
}
//Keep looping until we've recieved a handshake from a client
while(recievedMessage == null);
//Didn't know the username (probably created from a socket) so just assign it
if(this.username == null){
this.username = ((HandshakeMessage) recievedMessage).getUsername();
}
//Verify that the username matches the expected value
else{
if(!this.username.equals(((HandshakeMessage) recievedMessage).getUsername())){
System.err.println("Handshake username did not match " + this.username + " <->" + ((HandshakeMessage) recievedMessage).getUsername());
return false;
}
}
if(this.port < 0){
this.port = ((HandshakeMessage) recievedMessage).getListenPort();
}
else if(this.port != ((HandshakeMessage) recievedMessage).getListenPort()){
System.err.println("Handshake ports don't match " + this.port +
"<->" + ((HandshakeMessage) recievedMessage).getListenPort());
return false;
}
return true;
}
public void disconnect(){
this.keepRunning = false;
if(this.socket != null && !this.socket.isClosed()){
try{
this.socket.close();
}
catch(IOException e){
}
}
else{
System.err.println("Already disconnected");
}
}
public synchronized void sendMessage(final String message) throws IOException{
ChatMessage cMessage = new ChatMessage(System.currentTimeMillis(),
this.localUsername, message);
AbstractMessage.encodeMessage(cMessage, this.socket.getOutputStream());
}
public synchronized void sendClient(final Client otherClient) throws IOException{
ClientExchangeMessage cMessage = new ClientExchangeMessage(otherClient.getIpAddress(), otherClient.getPort(), otherClient.getUsername());
AbstractMessage.encodeMessage(cMessage, this.socket.getOutputStream());
}
public synchronized void sendDisconnectMessage() throws IOException {
AbstractMessage.encodeMessage(AbstractMessage.DISCONNECT_MESSAGE,
this.socket.getOutputStream());
}
public synchronized void sendKeepAliveMessage() throws IOException{
AbstractMessage.encodeMessage(AbstractMessage.KEEPALIVE_MESSAGE,
this.socket.getOutputStream());
}
public void addMessageListener(final MessageListener listener){
this.listeners.add(listener);
}
public void removeMessageListener(final MessageListener listener){
this.listeners.remove(listener);
}
@Override
public void run(){
while(this.keepRunning){
try{
final AbstractMessage message = AbstractMessage.decodeMessage(this.socket.getInputStream());
if(message == null){
try{
Thread.sleep(5);
}
catch(InterruptedException ie){
}
continue;
}
if(message.getType() == AbstractMessage.TYPE_CHAT_MESSAGE){
for(MessageListener listener: Client.this.listeners){
listener.chatMessageArrived(Client.this,
(ChatMessage) message);
}
}
else if(message.getType() == AbstractMessage.TYPE_CLIENT_EXCHANGE_MESSAGE){
for(MessageListener listener : Client.this.listeners){
listener.clientMessageArrived(Client.this,
(ClientExchangeMessage) message);
}
}
else if(message.getType() == AbstractMessage.TYPE_DISCONNECT_MESSAGE){
for(MessageListener listener: Client.this.listeners){
listener.disconnectMessageArrived(Client.this);
}
}
}
catch(Exception e){
this.keepRunning = false;
System.err.println(this + ": Caught exception while reading from client.");
System.err.println(e.getMessage());
e.printStackTrace(System.err);
for(MessageListener listener: Client.this.listeners){
listener.disconnectMessageArrived(this);
}
}
}
}
public String getUsername(){
return this.username;
}
public Socket getSocket(){
return this.socket;
}
public String getIpAddress(){
return this.ipAddress;
}
public int getPort(){
return this.port;
}
@Override
public boolean equals(Object o){
if(o instanceof Client){
return this.equals((Client) o);
}
return super.equals(o);
}
public boolean equals(Client client){
try{
InetAddress myAddress = InetAddress.getByName(this.ipAddress);
InetAddress otherAddress = InetAddress.getByName(client.ipAddress);
if(myAddress.equals(otherAddress) && this.port == client.port){
return true;
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
return false;
}
@Override
public int hashCode(){
//a hacky hack to stope compiler from complaining
return this.socket.hashCode() ^ this.port;
}
@Override
public String toString(){
return (this.username == null ? "Unknown client" : this.username) + "@"
+ this.ipAddress + ":" + this.port;
}
}