-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
647 lines (610 loc) · 19.1 KB
/
Server.java
File metadata and controls
647 lines (610 loc) · 19.1 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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
// Server class
public class Server
{
public static void main(String[] args) throws IOException
{
// server is listening on port 5056
ServerSocket ss = new ServerSocket(Integer.parseInt(args[1]));
// running infinite loop for getting
// client request
while (true)
{
Socket s = null;
try
{
// socket object to receive incoming client requests
s = ss.accept();
System.out.println("A new client is connected : " + s);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Assigning new thread for this client");
// create a new thread object
Thread t = new ClientHandler(s, dis, dos);
// Invoking the start() method
t.start();
}
catch (Exception e){
s.close();
e.printStackTrace();
}
}
}
}
class Room {
public String name;
public String password;
public ArrayList<ClientHandler> participants;
public Room(String n,ClientHandler u){
name=n;
password="";
participants = new ArrayList<ClientHandler>();
participants.add(u);
}
public Room(String n,String p,ClientHandler u){
name=n;
password=p;
participants = new ArrayList<ClientHandler>();
participants.add(u);
}
}
// ClientHandler class
class ClientHandler extends Thread
{
final static String lock = "lck";
public final DataInputStream dis;
public final DataOutputStream dos;
final Socket s;
static volatile HashSet<Integer> randlist = new HashSet<Integer>();
static volatile ArrayList<String> nametakenlist = new ArrayList<String>();
static volatile HashMap<String,Room> roomlist = new HashMap<String,Room>();
static volatile HashMap<String,ClientHandler> clientlist = new HashMap<String,ClientHandler>();
public String name;
public String roomname;
// Constructor
public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos)
{
this.s = s;
this.dis = dis;
this.dos = dos;
name="";
roomname="";
}
@Override
public void run()
{
boolean cond = true;
while (cond)
{
try {
int len;
len = dis.readInt();
Short sh = dis.readShort();
String hex = Integer.toHexString(sh & 0xffff);
byte[] opcode = new byte[1];
dis.read(opcode);
int opcodeint = (int) opcode[0] & 0xff ;
System.out.println("The length is: "+len);
System.out.println("The hex is: "+hex);
System.out.println("Printing op code is "+ opcodeint);
if(opcodeint == 255) {
System.out.println("Handshake");
byte [] readmsg = new byte[len];
dis.read(readmsg);
String r = new String(readmsg);
System.out.println("Printing message sent to us is "+r);
// Sending response to client
int i=0;
for(i=0;randlist.contains(i);i++){
System.out.println("Looking for rand");
}
randlist.add(i);
String msgsend = ""+"rand"+i;
nametakenlist.add(msgsend);
dos.writeInt(msgsend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x00;
dos.writeByte(sendreturncode);
String tmp_name=""+"rand"+i;
this.name = tmp_name;
System.out.println("Name of the client is "+this.name);
synchronized(lock){
clientlist.put(tmp_name,this);
}
dos.writeBytes(msgsend);
dos.flush();
}
else if(opcodeint == 10){
System.out.println("Join");
String oldroomstring = this.roomname;
int errorflag=0;
byte [] roomnamelen = new byte[1];
dis.read(roomnamelen);
int x = roomnamelen[0];
byte[] roomname = new byte[x];
dis.read(roomname);
String roomnamestring=new String(roomname);
System.out.println("Room name is "+ roomnamestring);
byte [] passwordlen = new byte[1];
dis.read(passwordlen);
int c = passwordlen[0];
System.out.println("Printing all keys out: "+ roomlist.keySet());
if (c > 0 ){
byte[] password = new byte[c];
dis.read(password);
String passwordstring=new String(password);
if(this.roomname.equals(roomnamestring)){
// error condition client is in room
String msgend="You attempt to bend space and time to reenter where you already are. You fail..";
errorflag=1;
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x01;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
dos.flush();
}
else {
if (roomlist.containsKey(roomnamestring)){ // if room already exists
System.out.println("Room exists");
if(passwordstring.equals(roomlist.get(roomnamestring).password)){
this.roomname = roomnamestring;
roomlist.get(roomnamestring).participants.add(this);
}
else {
//error wrong password
errorflag=1;
String msgend="Invalid password. You shall not pass.";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x01;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
dos.flush();
}
}
else {
// create new room with password
System.out.println("room doesn't exist");
synchronized (lock){
Room tmp_room = new Room(roomnamestring,passwordstring,this);
roomlist.put(roomnamestring,tmp_room);
this.roomname = roomnamestring;
}
}
}
}
else { // no password case
System.out.println("no password case");
if(this.roomname.equals(roomnamestring)){
// error condition client is in room
System.out.println("Erroe, client is in room");
errorflag=1;
String msgend="You attempt to bend space and time to reenter where you already are. You fail..";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x01;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
dos.flush();
}
else {
if (roomlist.containsKey(roomnamestring)) {
System.out.println("Room exists");
this.roomname = roomnamestring;
roomlist.get(roomnamestring).participants.add(this);
}
else {
System.out.println("Room doesn't exists");
synchronized (lock){
Room tmp_room = new Room(roomnamestring,this);
System.out.println("Map before a put"+roomlist.keySet());
roomlist.put(roomnamestring,tmp_room);
System.out.println("Map after a put"+roomlist.keySet());
this.roomname = roomnamestring;
}
}
}
}
if(errorflag == 0){
// send success response
System.out.println("Sending succesful response");
String msgend="";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x00;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
dos.flush();
synchronized(lock) {
if(oldroomstring.length()>0){
roomlist.get(oldroomstring).participants.remove(this);
if(roomlist.get(oldroomstring).participants.size()==0){
roomlist.remove(oldroomstring);
}
}
}
}
}
else if(opcodeint==11){
String msgend="";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x00;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
dos.flush();
if(this.roomname.length()>0){
String tmp_name = this.roomname;
this.roomname ="";
synchronized(lock){
roomlist.get(tmp_name).participants.remove(this);
if (roomlist.get(tmp_name).participants.size()==0){
roomlist.remove(tmp_name);
}
}
}
else {
try
{
// closing resources
System.out.println("Leaving");
String oldname = this.name;
clientlist.remove(this.name);
nametakenlist.remove(this.name);
if(oldname.length()>=5){
String isRand = oldname.substring(0,4);
String rand = "rand";
if(isRand.equals(rand)) {
try {
int num = Integer.parseInt(oldname.substring(4));
System.out.println("Num is"+num);
synchronized (lock){
randlist.remove(num);
}
}
catch(Exception e){
;
}
}
}
this.dis.close();
this.dos.close();
cond =false;
break;
//
}catch(IOException e){
e.printStackTrace();
}
}
}
else if(opcodeint==12){
synchronized(lock){
HashMap<String,Integer> tmp_map = new HashMap<String,Integer>();
int len_counter=0;
for(String s:roomlist.keySet()){
len_counter = len_counter + s.length();
tmp_map.put(s,s.length());
}
if(roomlist.keySet().size()==0){
tmp_map.put("",0);
}
dos.writeInt(len_counter+tmp_map.size()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x00;
dos.writeByte(sendreturncode);
for(String i:tmp_map.keySet()){
byte len2 = (byte)((int)tmp_map.get(i));
dos.writeByte(len2);
dos.writeBytes(i);
}
}
}
else if(opcodeint==13){
synchronized(lock){
if(this.roomname.length()>0){
HashMap<String,Integer> tmp_map = new HashMap<String,Integer>();
int len_counter=0;
for(ClientHandler c: roomlist.get(this.roomname).participants){
String s = new String(c.name);
len_counter = len_counter + s.length();
tmp_map.put(s,s.length());
}
if(roomlist.get(this.roomname).participants.size()==0){
tmp_map.put("",0);
}
dos.writeInt(len_counter+tmp_map.size()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x00;
dos.writeByte(sendreturncode);
for(String i:tmp_map.keySet()){
byte len2 = (byte)((int)tmp_map.get(i));
dos.writeByte(len2);
dos.writeBytes(i);
}
}
else {
HashMap<String,Integer> tmp_map = new HashMap<String,Integer>();
int len_counter=0;
for(String s:clientlist.keySet()){
len_counter = len_counter + s.length();
tmp_map.put(s,s.length());
}
if(clientlist.keySet().size()==0){
tmp_map.put("",0);
}
dos.writeInt(len_counter+tmp_map.size()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x00;
dos.writeByte(sendreturncode);
for(String i:tmp_map.keySet()){
byte len3 = (byte)((int)tmp_map.get(i));
dos.writeByte(len3);
dos.writeBytes(i);
}
}
}
}
else if(opcodeint==14){
String oldname = this.name;
byte [] newnamelen = new byte[1];
dis.read(newnamelen);
int x = newnamelen[0];
byte[] newname = new byte[x];
dis.read(newname);
String newnamestring=new String(newname);
if(nametakenlist.contains(newnamestring) && (newnamestring.equals(oldname) == false) ){
//error, name's taken
String msgend="This nick has been nicked by someone else.";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x01;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
dos.flush();
}
else {
if(oldname.equals(newnamestring)){
// just send the empty respone
String msgend="";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x00;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
dos.flush();
}
else{
// check for if old name is a rand name,deal, then send the success empty respons ;
boolean randflag = false;
if(oldname.length()>=5){
String isRand = oldname.substring(0,4);
String rand = "rand";
if(isRand.equals(rand)) {
try {
int num = Integer.parseInt(oldname.substring(4));
synchronized (lock){
randlist.remove(num);
}
}
catch(Exception e){
randflag = false;
}
}
}
synchronized (lock){
nametakenlist.remove(oldname);
this.name = newnamestring;
nametakenlist.add(newnamestring);
clientlist.remove(oldname);
clientlist.put(newnamestring,this);
}
// send empty response
String msgend="";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x00;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
dos.flush();
}
}
}
else if(opcodeint == 15 ){
byte [] sendmsgtolen = new byte[1];
dis.read(sendmsgtolen);
int x = sendmsgtolen[0];
byte[] sendto= new byte[x];
dis.read(sendto);
String sendtostring=new String(sendto);
short msglen = dis.readShort();
int msglenint = msglen;
byte[] messagebytes= new byte[msglenint];
dis.read(messagebytes);
String message = new String(messagebytes);
if(!clientlist.containsKey(sendtostring)){
// error condition. guy who we're sending message do dne
String msgend="Nick not present";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x01;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
}
else{
// sending to recipient
int sendlength = message.length()+3+this.name.length();
clientlist.get(sendtostring).dos.writeInt(sendlength);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
clientlist.get(sendtostring).dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0x0f;
clientlist.get(sendtostring).dos.writeByte(sendOpcode);
byte sendernamelen = (byte)this.name.length();
clientlist.get(sendtostring).dos.writeByte(sendernamelen);
clientlist.get(sendtostring).dos.writeBytes(this.name);
clientlist.get(sendtostring).dos.writeShort(msglen);
clientlist.get(sendtostring).dos.writeBytes(message);
// sending success ack
String msgend="";
dos.writeInt(msgend.length()+1);
int sendopcodehexint1 = 0x417;
short sendopcodehex1 = (short) sendopcodehexint1;
dos.writeShort(sendopcodehex1);
byte sendOpcode1 = (byte)0xfe;
dos.writeByte(sendOpcode1);
byte sendreturncode1 = (byte)0x00;
dos.writeByte(sendreturncode1);
dos.writeBytes(msgend);
}
}
else if(opcodeint == 16){
byte [] sendmsgtolen = new byte[1];
dis.read(sendmsgtolen);
int x = sendmsgtolen[0];
byte[] sendto= new byte[x];
dis.read(sendto);
String sendtoroomstring=new String(sendto);
short msglen = dis.readShort();
int msglenint = msglen;
byte[] messagebytes= new byte[msglenint];
dis.read(messagebytes);
String message = new String(messagebytes);
if(!this.roomname.equals(sendtoroomstring)){
// error condition
String msgend="You shout into the void and hear nothing.";
dos.writeInt(msgend.length()+1);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0xfe;
dos.writeByte(sendOpcode);
byte sendreturncode = (byte)0x01;
dos.writeByte(sendreturncode);
dos.writeBytes(msgend);
}
else {
for(ClientHandler c : roomlist.get(sendtoroomstring).participants){
if(!c.name.equals(this.name)){
int sendlength = message.length()+4+this.roomname.length()+this.name.length();
c.dos.writeInt(sendlength);
int sendopcodehexint = 0x417;
short sendopcodehex = (short) sendopcodehexint;
c.dos.writeShort(sendopcodehex);
byte sendOpcode = (byte)0x10;
c.dos.writeByte(sendOpcode);
byte roomnamelen = (byte) this.roomname.length();
c.dos.writeByte(roomnamelen);
c.dos.writeBytes(this.roomname);
byte sendernamelen = (byte)this.name.length();
c.dos.writeByte(sendernamelen);
c.dos.writeBytes(this.name);
c.dos.writeShort(msglen);
c.dos.writeBytes(message);
}
}
String msgend="";
dos.writeInt(msgend.length()+1);
int sendopcodehexint1 = 0x417;
short sendopcodehex1 = (short) sendopcodehexint1;
dos.writeShort(sendopcodehex1);
byte sendOpcode1 = (byte)0xfe;
dos.writeByte(sendOpcode1);
byte sendreturncode1 = (byte)0x00;
dos.writeByte(sendreturncode1);
dos.writeBytes(msgend);
}
}
} catch (IOException e) {
//e.printStackTrace();
// remove all traces of this client
// take care of the rand situation
System.out.println("Quit");
String oldname = this.name;
clientlist.remove(this.name);
nametakenlist.remove(this.name);
if(this.roomname.length()>0){
roomlist.get(this.roomname).participants.remove(this);
if(roomlist.get(this.roomname).participants.size()==0){
roomlist.remove(this.roomname);
}
}
if(oldname.length()>=5){
String isRand = oldname.substring(0,4);
String rand = "rand";
if(isRand.equals(rand)) {
try {
int num = Integer.parseInt(oldname.substring(4));
System.out.println("Num we're removing is:"+num);
synchronized (lock){
randlist.remove(num);
}
}
catch(Exception e2){
;
}
}
}
cond=false;
//
break;
}
}
}
}