-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
362 lines (303 loc) · 16.3 KB
/
Server.java
File metadata and controls
362 lines (303 loc) · 16.3 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
import java.io.*;
import java.util.*;
import java.net.*;
public class Server{
public static HashMap<String, Socket> user2Socket = new HashMap<String, Socket>();
public static HashMap<String, String> user2ChatRoom = new HashMap<String, String>();
public static HashMap<String,List<String> > chatRoom2Users = new HashMap<String,List<String> >();
public static void main(String[] args) throws IOException{
int clients = Integer.parseInt(args[0]);
String server_ip = args[1];
int server_port = Integer.parseInt(args[2]);
ServerSocket ss = new ServerSocket(server_port);
System.out.println("Server online...");
while(true){
Socket s = null;
try{
if(clients >= 0){
s = ss.accept();
clients--;
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
Scanner scn = new Scanner(System.in);
Thread t = new ClientHandler(s,dis,dos,scn,user2Socket,user2ChatRoom,chatRoom2Users,clients);
t.start();
}
}
catch(Exception e){
s.close();
e.printStackTrace();
}
}
}
}
class ClientHandler extends Thread{
final DataInputStream dis;
final DataOutputStream dos;
final Scanner scn;
final Socket s;
public static HashMap<String, Socket> user2Socket;
public static HashMap<String, String> user2ChatRoom;
public static HashMap<String,List<String> > chatRoom2Users;
private int i,l;
private String strRcvd,username = "",chatroom = "";
public static int clients;
public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos,Scanner scn,HashMap<String, Socket> user2Socket,HashMap<String, String> user2ChatRoom,HashMap<String,List<String> > chatRoom2Users,int clients){
this.s = s;
this.dis = dis;
this.dos = dos;
this.scn = scn;
this.user2Socket = user2Socket;
this.user2ChatRoom = user2ChatRoom;
this.chatRoom2Users = chatRoom2Users;
this.clients = clients;
}
public void run(){
try{
if(clients < 0) {
dos.writeUTF("No more clients can be accepted...");
this.s.close();
return;
}
}
catch (IOException e) {
e.printStackTrace();
}
while(true){
try{
strRcvd = dis.readUTF();
System.out.println("client: => " + strRcvd);
String[] splitedMessage = strRcvd.split("\\s+");
//---------------------------- get username from client------------------------------//
if(splitedMessage[0].equals("username:")){
username = splitedMessage[1];
if(!user2Socket.containsKey(username)){
user2Socket.put(username,s);
dos.writeUTF(splitedMessage[1] + " registered...");
}
else
dos.writeUTF("Username: " + username + " already exists...");
}
//--------------------------------------- create chatroom -------------------------------------//
else if(splitedMessage[0].equals("create") && splitedMessage[1].equals("chatroom")){
if(user2ChatRoom.containsKey(username)){
dos.writeUTF("The user is already in a chatroom...");
continue;
}
chatroom = splitedMessage[2];
List<String> userlist = chatRoom2Users.get(chatroom);
if(userlist == null) {
userlist = new ArrayList<String>();
chatRoom2Users.put(chatroom, userlist);
userlist.add(username);
user2ChatRoom.put(username,chatroom);
dos.writeUTF(chatroom + " is successfully created and you (" + username + ") are added as the first member...");
}
else
dos.writeUTF(chatroom + " already exists...");
}
//----------------------------------------- list chatrooms -----------------------------------------//
else if(splitedMessage[0].equals("list") && splitedMessage[1].equals("chatrooms")){
String listChatRooms = "";
for(String key : chatRoom2Users.keySet())
listChatRooms += key + ", ";
if(!listChatRooms.equals(""))
listChatRooms = listChatRooms.substring(0, listChatRooms.length() - 2);
dos.writeUTF("List of chat rooms: " + listChatRooms);
}
//------------------------------------------ join chatroom -----------------------------------------//
else if(splitedMessage[0].equals("join")){
chatroom = splitedMessage[1];
if(user2ChatRoom.containsKey(username)){
dos.writeUTF(username + " is already in an existing chatroom...");
continue;
}
if(chatRoom2Users.containsKey(chatroom)){
List<String> userlist = chatRoom2Users.get(chatroom);
userlist.add(username);
user2ChatRoom.put(username,chatroom);
dos.writeUTF("You are now a member of "+ chatroom + "...");
//------- broadcast in the group that a new user has been added to the chatroom-------//
l = userlist.size();
DataOutputStream dosNew = null;
for(i = 0;i<l;++i){
String member = userlist.get(i);
if(member == username)
continue;
Socket sockInfo = user2Socket.get(member);
dosNew = new DataOutputStream(sockInfo.getOutputStream());
dosNew.writeUTF(username + " is now a member of " + chatroom + "...");
dosNew.flush();
}
}
else
dos.writeUTF(chatroom + " does not exist...");
}
//------------------------------ Leave chatroom ------------------------------//
else if(splitedMessage[0].equals("leave")){
if(!user2ChatRoom.containsKey(username)){
dos.writeUTF(username + " is not part of any chatroom...");
continue;
}
chatroom = user2ChatRoom.get(username);
if(chatRoom2Users.containsKey(chatroom)){
List<String> userlist = chatRoom2Users.get(chatroom);
userlist.remove(username);
if(userlist.size() == 0)
chatRoom2Users.remove(chatroom);
user2ChatRoom.remove(username);
dos.writeUTF("You have left " + chatroom + "...");
//------------ broadcast in the group that the user has left the chatroom ------------//
DataOutputStream dosNew = null;
l = userlist.size();
for(i = 0;i<l;++i){
String member = userlist.get(i);
Socket sockInfo = user2Socket.get(member);
dosNew = new DataOutputStream(sockInfo.getOutputStream());
dosNew.writeUTF(username + " has left " + chatroom + "...");
dosNew.flush();
}
}
else
dos.writeUTF(chatroom + "does not exist...");
}
//----------------------------------- List users in the chatroom -------------------------------//
else if(splitedMessage[0].equals("list") && splitedMessage[1].equals("users")){
chatroom = user2ChatRoom.get(username);
if(chatroom == null){
dos.writeUTF("You are not a member of any chatroom...");
continue;
}
String userlist = "";
List<String> temp = chatRoom2Users.get(chatroom);
for(i = 0;i<temp.size();++i)
userlist += temp.get(i) + ", ";
userlist = userlist.substring(0, userlist.length() - 2);
dos.writeUTF("List of users in " + chatroom + " : " + userlist);
}
//------------------------------------- Add new user to chatroom -------------------------------//
else if(splitedMessage[0].equals("add")){
if(user2ChatRoom.get(username) == null){
dos.writeUTF("You cannot add a member unless you are a member of some group...");
continue;
}
String newuser = splitedMessage[1];
if(!user2Socket.containsKey(newuser)){
dos.writeUTF(newuser + " is offline...");
continue;
}
if(user2ChatRoom.containsKey(newuser)){
dos.writeUTF(newuser + " already a member of a group...");
continue;
}
chatroom = user2ChatRoom.get(username);
if(chatroom != null){
user2ChatRoom.put(newuser,chatroom);
List<String> userlist = chatRoom2Users.get(chatroom);
userlist.add(newuser);
dos.writeUTF(newuser + " is now a member of " + chatroom);
Socket newSocket = user2Socket.get(newuser);
DataOutputStream dosNew = new DataOutputStream(newSocket.getOutputStream());
dosNew.writeUTF("You are now a member of " + chatroom);
dosNew.flush();
}
}
//----------------------------------- File transfer will take place ------------------------------//
else if(splitedMessage[0].equals("FileAttributes:")){
int filesize = Integer.parseInt(splitedMessage[1]);
String filename = splitedMessage[2];
String protocol = splitedMessage[3];
int current = 0, tillNow = 0;
String destFile = System.getProperty("user.dir") + "/" + filename;
byte [] mybytearray = new byte [6022386];
InputStream is = s.getInputStream();
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
while ((current = is.read(mybytearray)) > 0) {
bos.write(mybytearray, 0, current);
tillNow += current;
if(tillNow >= filesize)
break;
}
bos.flush();
bos.close();
List<Socket> memberSockets = new ArrayList<Socket>();
List<String> userlist = chatRoom2Users.get(chatroom);
l = userlist.size();
for(i = 0;i < l;i++){
String member = userlist.get(i);
if(member == username)
continue;
memberSockets.add(user2Socket.get(member));
}
l--;
String filepath = System.getProperty("user.dir") + "/" + filename;
File myFile = new File (filepath);
DataOutputStream dosNew = null;
String fileAttributes = "FileAttributes: " + Long.toString(myFile.length()) + " " + filename + " " +protocol + " " + username;
for(i = 0;i<l;++i){
Socket sockInfo = memberSockets.get(i);
dosNew = new DataOutputStream(sockInfo.getOutputStream());
dosNew.writeUTF(fileAttributes);
dosNew.flush();
mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sockInfo.getOutputStream();
os.write(mybytearray,0,mybytearray.length);
os.flush();
bis.close();
}
myFile.delete();
}
//------------------------------- valid message to be broadcasted in the group --------------------------//
else if(splitedMessage[0].equals("reply")){
int msgLen = splitedMessage.length;
String protocol = splitedMessage[msgLen-1];
chatroom = user2ChatRoom.get(username);
if(chatroom == null){
dos.writeUTF("You are not member of any chatroom...");
continue;
}
List<Socket> memberSockets = new ArrayList<Socket>();
List<String> userlist = chatRoom2Users.get(chatroom);
l = userlist.size();
for(i = 0;i < l;i++){
String member = userlist.get(i);
if(member == username)
continue;
memberSockets.add(user2Socket.get(member));
}
l--;
// simple message to be broadcasted in the chatroom
if(!protocol.equals("tcp") && !protocol.equals("udp")){
splitedMessage[0] = username + ":";
String msg2All = String.join(" ", splitedMessage);
DataOutputStream dosNew = null;
for(i = 0;i < l;++i){
Socket sockInfo = memberSockets.get(i);
dosNew = new DataOutputStream(sockInfo.getOutputStream());
dosNew.writeUTF(msg2All);
dosNew.flush();
}
}
}
//--------------------------------- client exiting -------------------------------------//
else if(strRcvd.equals("exit")){
user2Socket.remove(username);
this.s.close();
clients += 1;
System.out.println("$$ " + clients);
break;
}
//--------------------------------- Invalid Command ---------------------------------//
else
dos.writeUTF("Invalid command...");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}