-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwnClient.java
580 lines (449 loc) · 12.2 KB
/
OwnClient.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class OwnClient
extends Thread
{
protected Socket socket;
protected DataInputStream istream;
protected DataOutputStream ostream;
protected boolean stop = false;
protected OwnWindow parentWindow;
protected Vector userList = new Vector();
public OwnClient(String host, String name, int portnumber,
OwnWindow mainWindow)
throws UnknownHostException, IOException, Exception
{
super("jntu Chat client thread");
System.out.println("entered client socket");
parentWindow = mainWindow;
// set up the client socket
socket = new Socket(host, portnumber);
// Get the output stream
ostream = new DataOutputStream(socket.getOutputStream());
// Get an input stream to correspond to the client socket
istream = new DataInputStream(socket.getInputStream());
// Start listening for stuff from the server
start();
// Now send the server some information about this user
sendUserInfo();
System.out.println("back after sending user info");
}
public void run()
{
System.out.println("starting client thread");
while (!stop)
try {
parseCommand();
}
catch (IOException e) {
lostConnection();
return;
}
return;
}
void parseCommand()
throws IOException
{
short commandType = 0;
synchronized (istream) {
commandType = istream.readShort();
System.out.println("client command"+commandType);
if (stop)
return;
switch (commandType) {
case OwnCommand.NOOP:
{
// Do nothing
break;
}
case OwnCommand.PING:
{
// The server is sending us a ping request. Just
// send one back
sendPing();
break;
}
case OwnCommand.CONNECT:
{
// The server is telling us that a new user
// connected
receiveConnect();
break;
}
case OwnCommand.USERINFO:
{
// The server is sending info about a user
// (maybe us)
receiveUserInfo();
break;
}
case OwnCommand.SERVERMESS:
{
// The server is sending us a 'dialog box' message
receiveServerMess();
break;
}
case OwnCommand.DISCONNECT:
{
// Somebody is disconnecting. Maybe us :)
receiveDisconnect();
break;
}
case OwnCommand.ACTIVITY:
{
// Someone is typing, drawing, etc.
receiveActivity();
break;
}
case OwnCommand.CHATTEXT:
{
// There's incoming chat text from another user
System.out.println("caught chat text");
receiveChatText();
break;
}
default:
{
System.out.println("client: unknown command "
+ commandType);
break;
}
}
}
}
protected OwnUser findUser(int userId)
{
// Find a user in the list
OwnUser tmpUser = null;
OwnUser returnUser = null;
if (userId == 0)
return (null);
for (int count = 0; count < userList.size(); count ++)
{
tmpUser = (OwnUser) userList.elementAt(count);
System.out.println("find user is "+tmpUser.name);
if (tmpUser.id == userId)
{
returnUser = tmpUser;
break;
}
}
return (returnUser);
}
protected OwnUser readUser()
throws IOException
{
int userId = 0;
userId = istream.readInt();
System.out.println("user id of sender is"+userId);
return (findUser(userId));
}
protected void sendRecipients()
throws IOException
{
// This function will construct a recipient list and send it
// down the pipe based on which users are selected in the 'send to'
// list of the parent window
String[] selectedUsers;
int numberUsers = 0;
if (parentWindow.sendToAll.getState())
{
ostream.writeInt(0);
}
else
{
// Get the list of selected user names
selectedUsers = parentWindow.sendTo.getSelectedItems();
numberUsers = selectedUsers.length;
// Write out how many
ostream.writeInt(numberUsers);
// Loop for each
for (int count1 = 0; count1 < numberUsers; count1 ++)
{
for (int count2 = 0; count2 < userList.size();
count2++)
{
OwnUser tmp = (OwnUser)
userList.elementAt(count2);
if (selectedUsers[count1].equals(tmp.name))
ostream.writeInt(tmp.id);
}
}
}
}
protected void sendPing()
throws IOException
{
// Send a ping reply back to the server
synchronized (ostream)
{
ostream.writeShort(OwnCommand.PING);
System.out.println("i ");
return;
}
}
protected void receiveConnect()
throws IOException
{
// A new user has connected. We only use this to output a message
// saying that the user has connected; we get a USERINFO command
// that will actually tell us about the user later
String userName = istream.readUTF();
parentWindow.messages
.append("<<New user \"" + userName + "\" connected>>\n");
parentWindow.sendTo.add(userName);
}
protected void sendUserInfo()
throws IOException
{
// Send the server some information about this user
synchronized (ostream)
{
ostream.writeShort(OwnCommand.USERINFO);
ostream.writeInt(parentWindow.id); // We don't know our id
ostream.writeUTF(parentWindow.name);
ostream.writeUTF(parentWindow.plainPassword);
System.out.println("two"+OwnCommand.USERINFO);
System.out.println("client id"+parentWindow.id);
System.out.println("client name"+parentWindow.name);
System.out.println("client pasword"+parentWindow.plainPassword);
}
}
protected void receiveUserInfo()
throws IOException
{
int tmpId = 0;
String tmpName = "";
OwnUser newUser;
// The server is sending new information about some user.
tmpId = istream.readInt();
tmpName = istream.readUTF();
System.out.println("temp name is"+tmpName);
System.out.println("temp id is" +tmpId);
// Password field will be empty
istream.readUTF();
// Is the user name ours? If so, the server is telling us our
// own user id number.
if (tmpName.equals(parentWindow.name))
{
parentWindow.id = tmpId;
}
else
{
// Some new user has connected. Create a new user object
newUser = new OwnUser(tmpId, tmpName, "");
// Add the user to our collection of users
userList.addElement(newUser);
System.out.println("new user is connected"+newUser.name);
parentWindow.sendTo.add(newUser.name);
}
}
protected void receiveServerMess()
throws IOException
{
String message = "";
// The server is sending us a message. Get the message.
message = istream.readUTF();
// Make a dialog box with the message
new OwnInfoDialog(parentWindow, "Server message", true,
message);
}
protected void sendDisconnect()
throws IOException
{
// Tell the server that we're disconnecting
synchronized (ostream)
{
ostream.writeShort(OwnCommand.DISCONNECT);
ostream.writeInt(parentWindow.id);
ostream.writeUTF("");
}
}
protected void receiveDisconnect()
throws IOException
{
int tmpId = 0;
OwnUser tmpUser;
String disconnectMess = "";
java.awt.List list = parentWindow.sendTo;
tmpId = istream.readInt();
disconnectMess = istream.readUTF();
istream.readFully(new byte[istream.available()]);
// Who is it? Is it us?
if ((tmpId == parentWindow.id) || (tmpId == 0))
{
// If it's us, make a dialog box with the disconnection
// message
if (disconnectMess.equals(""))
disconnectMess = "(no reason given)";
new OwnInfoDialog(parentWindow, "Disconnected", true,
disconnectMess);
shutdown(false);
parentWindow.offline();
}
else
{
// Some other user disconnected. Output a message that this
// user has left the chat.
tmpUser = findUser(tmpId);
if (tmpUser == null)
// Don't know who this is. Ignore.
return;
parentWindow.messages.append("<<" + tmpUser.name
+ " is disconnecting>>\n");
// Remove this name from our 'currently sending to' list
synchronized (list)
{
for (int count2 = 0; count2 < list.getItemCount();
count2 ++)
{
if (list.getItem(count2).equals(tmpUser.name))
{
if (list.isIndexSelected(count2))
list.select(0);
list.remove(count2);
break;
}
}
// If there's nothing left in the list, make sure
// the 'send to all' checkbox is checked
if (list.getSelectedItems().length == 0)
parentWindow.sendToAll.setState(true);
}
userList.removeElement((Object) tmpUser);
userList.trimToSize();
}
}
protected void sendActivity(short activity)
throws IOException
{
// This tells our selected recipients that we're in the middle
// of typing something.
synchronized (ostream)
{
ostream.writeShort(OwnCommand.ACTIVITY);
ostream.writeInt(parentWindow.id);
ostream.writeShort(activity);
sendRecipients();
}
}
protected void receiveActivity()
throws IOException
{
// Some user is doing some activity, such as typing or drawing.
OwnUser fromUser = null;
short activity = 0;
int numForUsers = 0;
fromUser = readUser();
// Read the activity and discard the recipient list, if there is one
activity = istream.readShort();
numForUsers = istream.readInt();
for (int count = 0; count < numForUsers; count ++)
istream.readInt();
if (fromUser == null)
// Ack. No such user. It can happen if someone logs out at
// just the right moment
return;
String tmpString = "";
if (activity == OwnCommand.ACTIVITY_TYPING)
tmpString = "typing: " + fromUser.name;
if (!parentWindow.activity.getText().equals(tmpString))
parentWindow.activity.setText(tmpString);
}
protected void sendChatText(String data)
throws IOException
{
// This sends a line of chat text to the selected recipients
synchronized (ostream)
{
ostream.writeShort(OwnCommand.CHATTEXT);
ostream.writeInt(parentWindow.id);
if (parentWindow.sendToAll.getState())
// Public
ostream.writeBoolean(false);
else
// Private
ostream.writeBoolean(true);
ostream.writeUTF(data);
sendRecipients();
}
}
protected void receiveChatText()
throws IOException
{
OwnUser fromUser = null;
boolean priv = false;
short colour = 0;
String data = "";
String output = "";
int numForUsers = 0;
// There is incoming chat text.
// From whom is this message?
fromUser = readUser();
System.out.println("from user is"+fromUser);
// Is this message private
priv = istream.readBoolean();
data = istream.readUTF();
// Discard the recipient list, if there is one
numForUsers = istream.readInt();
for (int count = 0; count < numForUsers; count ++)
istream.readInt();
if (fromUser != null)
{
if (priv)
output += "*private from " + fromUser.name +
"*> ";
else
output += fromUser.name + "> ";
}
// Append the actual message
output += data;
parentWindow.activity.setText("");
parentWindow.messages.append(output);
}
public void lostConnection()
{
System.out.println("in lost connection");
if (!stop)
{
shutdown(true);
parentWindow.offline();
}
return;
}
public synchronized void shutdown(boolean notifyUser)
{
// Shut down the reader thread
stop = true;
// Close my input and output data streams. Don't do this
// synchronized, since the client reader will be sitting there
// blocking, waiting for data.
try {
istream.close();
}
catch (IOException e) {}
try {
synchronized (ostream)
{
ostream.flush();
ostream.close();
}
// close up my socket
socket.close();
}
catch (IOException e) {}
// Empty out our user list
userList.removeAllElements();
new OwnInfoDialog(parentWindow, "Disconnected", true,
("Disconnected from "
+ parentWindow.host));
parentWindow.theClient = null;
// Force garbage collection, since some clients seem to hold on
// to the connection somehow
System.gc();
return;
}
}