-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
718 lines (660 loc) · 26.1 KB
/
Server.java
File metadata and controls
718 lines (660 loc) · 26.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.*;
/**
* This class implements the server-side of the instant messaging application.
* It opens a ServerSocket tied to a specified port and constantly listening
* for incoming TCP connection request from clients. It also facilitates
* data provision for clients that want to begin their own private P2P session.
*/
public class Server {
/**
* Constants for the server which are defined at startup
*/
public static String CREDENTIAL_FILE = "credentials.txt";
public static int BLOCK_DURATION;
public static int TIMEOUT;
/**
* State variables
*/
private List<String> activeClientNames;
private List<String[]> credentials;
private HashMap<String, User> users;
/**
* Creates a new server and loads the stored credentials for all users
*/
public Server() {
activeClientNames = new ArrayList<>();
credentials = new ArrayList<>();
users = new HashMap<>();
// retrieve credential data
File credentialFile = new File(Server.CREDENTIAL_FILE);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(credentialFile));
String line;
while ((line = reader.readLine()) != null) {
credentials.add(line.split(" "));
users.put(line.split(" ")[0], new User());
}
} catch (IOException e) {
//e.printStackTrace();
}
}
/**
* Adding a client entry to current collection of active handlers
* @param client the ClientHandler to be stored
* @param username the name of the user being handled by ClientHandler
*/
public synchronized void addClient(ClientHandler client, String username) {
activeClientNames.add(username);
users.get(username).setThread(client);
users.get(username).setOnline();
}
/**
* Removes a client entry from the current collection of active handlers
* @param username the name of the user to be removed
*/
public synchronized void removeClient(String username) {
if (users.containsKey(username) && users.get(username).isOnline()) {
activeClientNames.remove(username);
users.get(username).setThread(null);
users.get(username).setOffline();
}
}
/**
* @return the list of user credentials
*/
public List<String[]> getCredentials() {
return credentials;
}
/**
* @return a mapping of usernames to User structures
*/
public HashMap<String, User> getUser() {
return users;
}
/**
* @return the list of users currently online
*/
public List<String> getActiveUsers() {
return activeClientNames;
}
/**
* @param username name of the target user to set the block time of
*/
public synchronized void setBlockTime(String username) {
long startTime = System.currentTimeMillis();
long endTime = startTime + Server.BLOCK_DURATION*1000;
users.get(username).setBlockTime(endTime);
}
public static void main(String[] args) throws Exception {
// retrieve input parameters for the server
if (args.length != 3) {
System.out.println("Required arguments: server_port block_duration timeout");
return;
}
BLOCK_DURATION = Integer.parseInt(args[1]);
TIMEOUT = Integer.parseInt(args[2]);
// set up the server's socket
int serverPort = Integer.parseInt(args[0]);
ServerSocket serverSocket = new ServerSocket(serverPort);
Server server = new Server();
// have server constantly run
try {
while (true) {
// constantly listen for TCP connection requests and accept them
Socket clientSocket = serverSocket.accept();
ClientHandler handler = new ClientHandler(server, clientSocket);
handler.start();
}
} catch (SocketException e) {
//e.printStackTrace();
serverSocket.close();
}
}
}
/**
* This class implements handling of clients on a separate thread. This
* includes authentication handling, message redirecting between users,
* timeout handling among other functions.
*/
class ClientHandler extends Thread {
/**
* State variables
*/
private Socket socket;
private String username;
private DataInputStream inputStream;
private DataOutputStream outputStream;
private Server server;
private Timer timeoutTimer;
/**
* Initialises a new ClientHandler to facilitate communication between the
* server and a client.
* @param server the currently running server
* @param socket the socket connecting the server to a client to be handled
*/
public ClientHandler (Server server, Socket socket) {
this.server = server;
this.socket = socket;
this.username = null;
try {
this.outputStream = new DataOutputStream(socket.getOutputStream());
this.inputStream = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
//e.printStackTrace();
}
}
/**
* Handles user login and then constantly listens for incoming messages
* from the user and processes each request.
*/
public void run() {
try {
// user authentication phase
if (loginUser()) {
server.addClient(this, username);
sendBroadcast(username + " logged in", username);
startTimeoutTimer();
} else {
close();
return;
}
// once user has logged in, load up their message queue to check
// for messages sent to them when offline
if (!server.getUser().get(username).isMessageQueueEmpty()) {
sendMessage(server.getUser().get(username).readMessageQueue());
}
// constantly check for incoming requests
while (!Thread.currentThread().isInterrupted()) {
String data = readMessage();
if (data.isEmpty()) continue;
String[] splitData = data.split(" ");
switch (splitData[0]) {
case "logout":
if (data.equals("logout")) {
logoutUser();
} else {
sendMessage("Invalid command. Use format: logout");
}
break;
case "whoelse":
if (data.equals("whoelse")) {
displayActiveUsersSince(-1);
} else {
sendMessage("Invalid command. Use format: whoelse");
}
break;
case "whoelsesince":
// length 2 for whoelsesince and the time
if (splitData.length == 2 && isNumeric(splitData[1])) {
displayActiveUsersSince(Integer.parseInt(splitData[1]));
} else {
sendMessage("Invalid command. Use format: whoelsesince <time>");
}
break;
case "broadcast":
// length 2 for broadcast and at least 1 word message
if (splitData.length >= 2) {
String message = username + ":" + data.substring(splitData[0].length());
if (!sendBroadcast(message, username)) {
sendMessage("Your message could not be delivered to some recipients");
}
} else {
sendMessage("Invalid command. Use format: broadcast <message>");
}
break;
case "message":
// length 3 for message, user and at least 1 word message
if (splitData.length >= 3) {
String user = splitData[1];
if (user.equals(username)) {
sendMessage("Error: You can't message yourself");
} else if (server.getUser().containsKey(user)) {
List<String> messageData = Arrays.asList(splitData).subList(2, splitData.length);
String message = username + ": " + String.join(" ", messageData);
if (!sendMessageToUser(message, username, user)) {
sendMessage("Your message could not be delivered as the recipient has blocked you");
}
} else {
sendMessage("Error: Invalid user");
}
} else {
sendMessage("Invalid command. User format: message <user> <message>");
}
break;
case "block":
// length 2 for block and the user's name
if (splitData.length == 2) {
String user = splitData[1];
User currUser = server.getUser().get(username);
if (user.equals(username)) {
sendMessage("Error: Cannot block yourself");
} else if (!server.getUser().containsKey(user)) {
sendMessage("Error: invalid user");
} else if (currUser.hasBlacklisted(user)) {
sendMessage(user + " is already blocked");
} else {
currUser.addToBlacklist(user);
sendMessage(user + " is blocked");
}
} else {
sendMessage("Invalid command. User format: block <user>");
}
break;
case "unblock":
// length 2 for unblock and the user's name
if (splitData.length == 2) {
String user = splitData[1];
User currUser = server.getUser().get(username);
if (user.equals(username)) {
sendMessage("Error: You are not blocked");
} else if (!server.getUser().containsKey(user)) {
sendMessage("Error: invalid user");
} else if (!currUser.hasBlacklisted(user)) {
sendMessage(user + " was not blocked");
} else {
currUser.removeFromBlacklist(user);
sendMessage(user + " is unblocked");
}
} else {
sendMessage("Invalid command. User format: unblock <user>");
}
break;
case "setupprivate":
// length 2 for setupprivate and the user's name
if (splitData.length == 2) {
String user = splitData[1];
if (user.equals(username)) {
// notify user they are trying to start P2P with self
sendMessage("startprivate,self");
} else if (server.getUser().containsKey(user)) {
User userData = server.getUser().get(user);
if (userData.hasBlacklisted(username)) {
sendMessage("startprivate,blocked");
} else if (userData.isOnline()) {
// target client's listening socket for P2P will have the same
// address and port as the socket used for communication with
// this server. This is allowed since TCP sockets are defined
// but source address/port as well as destination
String ipAddress = userData.getSocket().getInetAddress().getHostAddress();
int port = userData.getSocket().getPort();
// send port and address data back to the user, so they can
// start a P2P session
String[] message = {"startprivate", user, ipAddress, Integer.toString(port)};
sendMessage(String.join(",", message));
} else {
// notify that target user is offline
sendMessage("startprivate,offline");
}
} else {
// notify that the target user doesn't exist
sendMessage("startprivate,invalid");
}
}
break;
default:
sendMessage("That is not a valid command.");
}
restartTimer();
}
} catch(Exception e) {
//e.printStackTrace();
close();
}
}
/**
* Handles user login.
* This includes username/password authentication and blocking.
* @return whether or not the user was successfully logged in
*/
private boolean loginUser() {
boolean loggedIn = false;
short attemptsLeft = 3;
boolean usernameEntered = false;
String username = "";
String expectedPassword = "";
// request a username to be entered and verify it is valid
while (!usernameEntered) {
sendMessage("Username: ");
username = readMessage();
if (username.isEmpty()) continue;
for (String[] credential : server.getCredentials()) {
if (username.equals(credential[0])) {
usernameEntered = true;
// keep record of the password for later
expectedPassword = credential[1];
break;
}
}
if (usernameEntered) {
long userTimeout = server.getUser().getOrDefault(username, new User()).getBlockTime();
if (server.getActiveUsers().contains(username)) {
sendMessage("This user is already logged in.");
usernameEntered = false;
} else if (System.currentTimeMillis() < userTimeout) {
sendMessage("Your account is blocked due to multiple login failures. Please try again later.");
usernameEntered = false;
}
} else {
sendMessage("This username doesn't exist. Please try again");
}
}
// handle password checking for the entered username
// the user is given 3 sequential attempts
while (attemptsLeft > 0) {
sendMessage("Password: ");
String password = readMessage();
if (password.isEmpty()) continue;
if (password.equals(expectedPassword)) {
this.username = username;
loggedIn = true;
break;
} else {
if (--attemptsLeft > 0)
sendMessage("Invalid password. Please try again.");
}
}
// check for 3 failed attempts
if (attemptsLeft == 0) {
sendMessage("Invalid password. Your account has been blocked. Please try again later.");
server.setBlockTime(username);
}
// login success message
if (loggedIn) {
sendMessage("Successfully logged in! Welcome to the chat server!");
// send the username of the user logged in to the client as a form
// of "handshaking" or confirmation
sendMessage("name " + username);
}
return loggedIn;
}
/**
* Logs the user out of their current session and closes the thread
*/
public void logoutUser() {
sendMessage("logout");
sendBroadcast(username + " logged out", username);
close();
}
/**
* Sends a UTF string message to the user
* @param message the message to send to the user
*/
public void sendMessage(String message) {
try {
outputStream.writeUTF(message);
} catch (IOException e) {
//e.printStackTrace();
close();
}
}
/**
* @return the incoming message sent from the user to the server
*/
public String readMessage() {
String message = "";
try {
message = inputStream.readUTF();
} catch (IOException e) {
//e.printStackTrace();
close();
}
return message;
}
/**
* Sends a broadcast message to all online users
* @param message the message to broadcast to online users
* @param senderName the name of the user sending the broadcast
* @return whether or not the broadcast message was successfully send
to all users
*/
public boolean sendBroadcast(String message, String senderName) {
List<String> users = new ArrayList<>(server.getActiveUsers());
users.remove(senderName);
int numMsgToSend = users.size();
for (String username : users) {
numMsgToSend -= (sendMessageToUser(message, senderName, username) ? 1 : 0);
}
return (numMsgToSend == 0);
}
/**
* Sends a message from this user to another
* @param message the message to be sent
* @param sender the name of the user sending the message
* @param receiverName the name of the user to send the message to
* @return whether or not the messages was successfully sent to the user
*/
public boolean sendMessageToUser(String message, String sender, String receiverName) {
User user = server.getUser().get(receiverName);
if (!user.hasBlacklisted(sender)) {
if (user.isOnline()) {
ClientHandler handler = user.getThread();
handler.sendMessage(message);
} else {
user.addToMessageQueue(message);
}
return true;
} else {
return false;
}
}
/**
* Sends a message to the user with a list of all the users that have been
* active since a given time
* @param secondsBack the time to check whether users have been active since
*/
public void displayActiveUsersSince(long secondsBack) {
List<String> users;
// negative parameter means show all active users
if (secondsBack < 0) {
users = new ArrayList<>(server.getActiveUsers());
users.remove(username);
} else {
// otherwise we account for the specified "time since"
long time = System.currentTimeMillis() / 1000 - secondsBack;
users = new ArrayList<>(server.getUser().keySet());
users.remove(username);
for (String name : server.getUser().keySet()) {
User user = server.getUser().get(name);
if (!user.isOnline() && user.getLastOnline() < time) {
users.remove(name);
}
}
}
sendMessage(String.join("\n", users));
}
/**
* @return the name of the user this handler is handling
*/
public String getUsername() {
return this.username;
}
/**
* Starts a timer for the duration of the server specified timeout
* duration. Once the time is over, the user is logged out.
*/
public void startTimeoutTimer() {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
sendMessage("Your session has timed out and you have been logged out.");
logoutUser();
}
};
timeoutTimer = new Timer();
timeoutTimer.schedule(timerTask, server.TIMEOUT * 1000);
}
/**
* Restarts the timer for when to logout a user due to inactivity.
* This function is called after the user sends a message to the server.
*/
public void restartTimer() {
timeoutTimer.cancel();
startTimeoutTimer();
}
/**
* @return the socket this handler is handling
*/
public Socket getSocket() {
return this.socket;
}
/**
* Clean up the ClientHandler.
* Closes socket and its stream threads.
* Also removes the client and interrupts the thread
*/
public void close() {
try {
server.removeClient(username);
inputStream.close();
outputStream.close();
socket.close();
Thread.currentThread().interrupt();
} catch (IOException e) {
//e.printStackTrace();
}
}
/**
* @param number the "number" string to check
* @return whether or not the string is actually a number
*/
private boolean isNumeric(String number) {
try {
int num = Integer.parseInt(number);
} catch (NumberFormatException | NullPointerException e) {
return false;
}
return true;
}
}
/**
* This class is used for handling and tracking the state of a user
*/
class User {
/**
* This user's state variables
*/
private long blockTime;
private HashSet<String> blacklist;
private ClientHandler thread;
private boolean isOnline;
private long lastOnline;
private List<String> messageQueue;
/**
* Initialises a new User with dummy data
*/
public User() {
this.blacklist = new HashSet<>();
this.isOnline = false;
this.blockTime = -1;
this.thread = null;
this.lastOnline = System.currentTimeMillis() / 1000;
this.messageQueue = new ArrayList<>();
}
/**
* @return the block time for 3 incorrect password attempts
*/
public long getBlockTime() {
return blockTime;
}
/**
* @param blockTime the block time for 3 incorrect passwords
* as specified at server startup
*/
public synchronized void setBlockTime(long blockTime) {
this.blockTime = blockTime;
}
/**
* @param user the user to enquire about
* @return whether or not the current user has blacklisted the specified user
*/
public boolean hasBlacklisted(String user) {
return blacklist.contains(user);
}
/**
* @param user the user to be added to the current user's blacklist
*/
public synchronized void addToBlacklist(String user) {
blacklist.add(user);
}
/**
* @param user the user to be removed from the current user's blacklist
*/
public synchronized void removeFromBlacklist(String user) {
blacklist.remove(user);
}
/**
* @return the thread responsible for handling messages to/from this user
*/
public ClientHandler getThread() {
return thread;
}
/**
* @param thread the thread responsible for handling messages to/from this user
*/
public synchronized void setThread(ClientHandler thread) {
this.thread = thread;
}
/**
* @return whether or not this user is currently online
*/
public boolean isOnline() {
return isOnline;
}
/**
* Set this user's status to online
*/
public synchronized void setOnline() {
isOnline = true;
setLastOnline();
}
/**
* Set this user's status to offline
*/
public synchronized void setOffline() {
isOnline = false;
setLastOnline();
}
/**
* @return the time this user was last online
*/
public long getLastOnline() {
return lastOnline;
}
/**
* Updates the time this user was last online (current time)
*/
public synchronized void setLastOnline() {
this.lastOnline = System.currentTimeMillis() / 1000;
}
/**
* @param message the message to be added to this user's offline messaging queue
*/
public synchronized void addToMessageQueue(String message) {
messageQueue.add(message);
}
/**
* @return the messages sent to this user while they were offline
*/
public synchronized String readMessageQueue() {
String messages = String.join("\n", messageQueue);
messageQueue.clear();
return messages;
}
/**
* @return whether or not the offline message queue for this user is empty
*/
public boolean isMessageQueueEmpty() {
return messageQueue.isEmpty();
}
/**
* @return the socket the server uses to communicate with this user
*/
public Socket getSocket() {
return thread.getSocket();
}
}