-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.java
More file actions
408 lines (404 loc) · 19.9 KB
/
server.java
File metadata and controls
408 lines (404 loc) · 19.9 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
import java.util.* ;
import java.io.* ;
import java.nio.file.Files;
import java.nio.file.*;
import java.net.* ;
import java.awt.* ;
public class server {
public static Vector<Socket> ClientSockets;
public static Vector<String> LoginNames;
public static Vector<Chatroom> Chatrooms;
public static DatagramSocket SocUDP;
public static Map<String,Chatroom> ConnectedChatroom;
public static Vector<Integer> Ports;
public static int max_no_clients;
server(int max_no_clients_) {
try {
LoginNames = new Vector<String>();
ClientSockets = new Vector<Socket>();
Chatrooms = new Vector<Chatroom>();
Ports = new Vector<Integer>();
ServerSocket Soc = new ServerSocket(6666) ;
DatagramSocket SocUDP = new DatagramSocket(6661);
ConnectedChatroom = new HashMap<String,Chatroom>();
max_no_clients=max_no_clients_;
System.out.println("Server is running on Port-6666(TCP), 6661(UDP)");
while(1==1) {
Socket CSoc = Soc.accept();
AcceptClient client_ = new AcceptClient(CSoc,SocUDP) ;
}
}
catch(Exception e) {
e.printStackTrace(System.out);
System.exit(0);
}
}
public static void main(String args[]) throws Exception {
if (args.length < 1) {
System.out.println("Maximum number of Users for the Server not given, taking 5 by default.");
server server = new server(5) ;
} else {
server server = new server(Integer.parseInt(args[0])) ;
}
}
}
class AcceptClient extends Thread {
DatagramPacket recieve_inital;
DatagramSocket SocUDP;
DataInputStream din;
DataOutputStream dout;
Socket ClientSocket;
String LoginName;
AcceptClient (Socket CSoc, DatagramSocket SocUDP_) throws Exception {
ClientSocket = CSoc;
dout = new DataOutputStream(ClientSocket.getOutputStream()) ;
din = new DataInputStream(ClientSocket.getInputStream());
byte[] intial = new byte[1000];
recieve_inital = new DatagramPacket(intial, intial.length);
SocUDP = SocUDP_;
SocUDP.receive(recieve_inital);
// LoginName = din.readUTF() ;
// // if (server.LoginNames.size()==server.max_no_clients)
// // {
// // System.out.println("Cannot login user: Server's maximum limit reached");
// // dout.writeUTF("Cannot connect: Reached Server's maximum capacity");
// // ClientSocket.close() ; din.close() ; dout.close() ; return;
// // }
// System.out.println("User "+LoginName+" logged in");
// int port = recieve_inital.getPort();
// server.Ports.add(port);
// server.LoginNames.add(LoginName) ; server.ClientSockets.add(ClientSocket) ; server.ConnectedChatroom.put(LoginName,null);
start();
}
public void run() {
while(1==1) {
try {
String commandfromClient = new String() ;
commandfromClient = din.readUTF() ;
StringTokenizer tokenedcommand = new StringTokenizer(commandfromClient);
String command=tokenedcommand.nextToken();
if (command.equals("create_user")) {
LoginName = tokenedcommand.nextToken();
if (server.LoginNames.size()==server.max_no_clients) {
System.out.println("Cannot login user: Server's maximum limit reached");
dout.writeUTF("Cannot connect: Reached Server's maximum capacity");
dout.close();
din.close();
ClientSocket.close();
return;
} else {
File theDir = new File(LoginName);
if (theDir.exists() == false) {
theDir.mkdir();
}
System.out.println("User "+LoginName+" is created");
int port = recieve_inital.getPort();
server.ClientSockets.add(ClientSocket);
server.LoginNames.add(LoginName);
server.Ports.add(port);
server.ConnectedChatroom.put(LoginName, null);
}
} else if (command.equals("upload")) {
StringTokenizer st = new StringTokenizer(commandfromClient);
String cmd=st.nextToken(),fl=st.nextToken();
String st_ = din.readUTF();
StringTokenizer stt = new StringTokenizer(st_);
stt.nextToken();
int fileLength = Integer.parseInt(stt.nextToken());
StringTokenizer fileName = new StringTokenizer(fl,"/");
while(fileName.hasMoreTokens()) {
fl=fileName.nextToken();
}
byte[] file_contents = new byte[1000];
FileOutputStream fpout = new FileOutputStream(LoginName + "/" + fl);
BufferedOutputStream bpout = new BufferedOutputStream(fpout);
int size=1000;
if (size > fileLength) {
size=fileLength;
}
while(din.read(file_contents,0,size) != -1 && fileLength != 0) {
bpout.write(file_contents,0,size);
fileLength -= size;
if (size > fileLength) {
size=fileLength;
}
}
System.out.println("File Recieved");
bpout.flush();
} else if (command.equals("upload_udp")) {
StringTokenizer st = new StringTokenizer(commandfromClient);
String cmd = st.nextToken();
String fl = st.nextToken();
String st_ = din.readUTF();
StringTokenizer stt = new StringTokenizer(st_);
stt.nextToken();
int fileLength = Integer.parseInt(stt.nextToken());
StringTokenizer fileName = new StringTokenizer(fl, "/");
while(fileName.hasMoreTokens()) {
fl = fileName.nextToken();
}
byte[] file_contents = new byte[1000];
FileOutputStream fpout = new FileOutputStream(LoginName + "/" + fl);
BufferedOutputStream bpout = new BufferedOutputStream(fpout);
DatagramPacket receivePacket;
int size = 1024;
file_contents = new byte[size];
if (size > fileLength) {
size=fileLength;
}
System.out.println("Length of file is: " + fileLength);
while(fileLength > 0) {
receivePacket = new DatagramPacket(file_contents, size);
SocUDP.receive(receivePacket);
bpout.write(file_contents,0,size);
fileLength -= size;
if (size > fileLength) {
size=fileLength;
}
}
bpout.flush();
System.out.println("File is uploaded successfully");
} else if (command.equals("create_folder")) {
String folder = tokenedcommand.nextToken();
File theDir = new File(LoginName + "/" + folder);
if (!theDir.exists()) {
try {
theDir.mkdir();
System.out.println("Folder Created");
} catch (Exception e) {
System.out.println(e);
System.out.println("Folder Creation unsuccessful");
}
} else {
System.out.println("Folder Already Exists");
}
} else if (command.equals("move_file")){
String pathsrc = LoginName + "/" + tokenedcommand.nextToken();
String pathdst = LoginName + "/" + tokenedcommand.nextToken();
try {
Path temp = Files.move(Paths.get(pathsrc), Paths.get(pathdst));
System.out.println("File moved successfully");
dout.writeUTF("File moved successfully");
} catch (Exception e) {
System.out.println(e);
System.out.println("Failed to move the file");
dout.writeUTF("Failed to move the file");
}
} else if (command.equals("create_group")) {
String chatroomName=tokenedcommand.nextToken();
if (server.Chatrooms.indexOf(chatroomName) == -1) {
Chatroom chatR = new Chatroom(chatroomName, LoginName);
server.Chatrooms.add(chatR);
dout.writeUTF("Group "+chatroomName+" created");
}
} else if (command.equals("list_groups")) {
if (server.Chatrooms.size() == 0) {
dout.writeUTF("No Groups exist currently.");
}
else {
String outp = "";
for (int i = 0; i < server.Chatrooms.size(); i++) {
outp = outp + server.Chatrooms.elementAt(i).name + "\n";
}
dout.writeUTF(outp);
}
} else if (command.equals("join_group")) {
int i;
String chatroomName = tokenedcommand.nextToken();
for (i = 0; i < server.Chatrooms.size(); i++) {
if (server.Chatrooms.elementAt(i).name.equals(chatroomName)) {
String outp=server.Chatrooms.elementAt(i).Join(LoginName);
dout.writeUTF(outp);
server.Chatrooms.elementAt(i).Notify(LoginName+" joined the group",LoginName);
break;
}
}
if (i == server.Chatrooms.size()) {
dout.writeUTF(chatroomName+" doesn't exist");
}
} else if (command.equals("leave_group")) {
int i;
String chatroomName=tokenedcommand.nextToken();
for (i = 0; i < server.Chatrooms.size(); i++) {
if (server.Chatrooms.elementAt(i).name.equals(chatroomName)) {
String outp=server.Chatrooms.elementAt(i).Leave(LoginName);
server.Chatrooms.elementAt(i).Notify(LoginName+" left the group",LoginName);
if (outp.equals("DEL")) {
server.Chatrooms.remove(server.Chatrooms.elementAt(i));
dout.writeUTF("You left Group"+'\n'+chatroomName+" deleted");
} else {
dout.writeUTF(outp);
}
break;
}
}
if (i == server.Chatrooms.size()) {
dout.writeUTF(chatroomName+" doesn't exist");
}
} else if (command.equals("share_msg")) {
int i = 0;
String msg = "";
while(tokenedcommand.hasMoreTokens()) {
msg = msg + " " + tokenedcommand.nextToken();
}
while (i < server.Chatrooms.size() ) {
Chatroom C = server.Chatrooms.elementAt(i);
if (C.ListUsers().indexOf(LoginName) != -1) {
String msgfromClient = "Message From " + LoginName + "@" + C.name + ":";
msgfromClient += msg;
C.Notify(msgfromClient, LoginName);
}
i++;
}
} else if (command.equals("list_detail")) {
int i;
String outp="";
String chatroomName=tokenedcommand.nextToken();
for (i=0;i<server.Chatrooms.size();i++){
if (server.Chatrooms.elementAt(i).name.equals(chatroomName)){
Chatroom C = server.Chatrooms.elementAt(i);
Vector<String> outpl=C.ListUsers();
int j = 0;
while (j<outpl.size()) {
String name = outpl.elementAt(j);
outp += "Username: ";
outp += name + "\n";
File folder = new File("./" + name);
File[] listOfFiles = folder.listFiles();
if (listOfFiles.length >= 1) {
outp += "Files:\n";
}
int k = 0;
while (k < listOfFiles.length) {
if (listOfFiles[k].isFile()) {
outp += listOfFiles[k].getName();
outp += "\n";
} else if (listOfFiles[k].isDirectory()) {
String subdir = listOfFiles[k].getName();
File sbfolder = new File(folder+"/"+subdir);
File[] sblistOfFiles = sbfolder.listFiles();
for (int p = 0; p < sblistOfFiles.length; p++) {
outp += subdir + "/";
outp += sblistOfFiles[p].getName() + "\n";
}
}
k++;
}
j++;
}
break;
}
}
if (i==server.Chatrooms.size()) dout.writeUTF(chatroomName+" doesn't exist");
dout.writeUTF(outp);
} else if(command.equals("get_file")) {
int i = 0;
String fl1 = tokenedcommand.nextToken();
String[] flSplit = fl1.split("/",3);
String groupname = flSplit[0];
String fileUser=flSplit[1];
// for(i = 0; i < server.Chatrooms.size(); i++){
while (i < server.Chatrooms.size()) {
Chatroom C = server.Chatrooms.elementAt(i);
if(C.name.equals(groupname)) {
if(C.ListUsers().indexOf(fileUser) == -1 || C.ListUsers().indexOf(LoginName) == -1){
String outputError = "Either you or the user: "+fileUser+" doesn't belong to this group: " + groupname;
dout.writeUTF(outputError);
break;
}
String fl = flSplit[1] + "/" + flSplit[2];
File file = new File(fl);
long fileLength = file.length();
FileInputStream fpin = new FileInputStream(file);
BufferedInputStream bpin = new BufferedInputStream(fpin);
String[] justnamear = fl.split("/",0);
String justname = justnamear[justnamear.length-1];
long current=0;
String outputAnswer = "FILE "+justname+" LENGTH " + fileLength;
dout.writeUTF(outputAnswer);
System.out.println(outputAnswer);
System.out.println(fileLength);
while (current != fileLength) {
int size;
if(fileLength - current >= 1000) {
size = 1000;
current = size + current;
} else {
size = (int)(fileLength-current);
current=fileLength;
}
byte[] file_contents = new byte[size];
bpin.read(file_contents, 0, size);
dout.write(file_contents);
String progress = "File sending "+(current*100/fileLength)+"% complete";
System.out.println(progress);
}
System.out.println("File Sent");
break;
}
i++;
}
System.out.println("Check");
if (i == server.Chatrooms.size()) {
dout.writeUTF(groupname+" doesn't exist");
}
} else {
dout.writeUTF("Unrecognised command, please read the reference PDF file.");
}
} catch(Exception e) {
e.printStackTrace(System.out);
break;
}
}
}
}
class Chatroom {
Vector<String> Members = new Vector<String>();
String name;
Chatroom (String name,String member) {
this.name = name;
this.Members.add(member);
server.ConnectedChatroom.put(member,this);
}
public String Join (String member) {
this.Members.add(member);
server.ConnectedChatroom.put(member,this);
return ("Joined Chatroom "+this.name);
}
public String Leave (String member) {
this.Members.remove(member);
server.ConnectedChatroom.put(member,null);
if (this.Members.isEmpty()) return ("DEL");
else return("You left chatroom "+this.name);
}
public Vector<String> ListUsers() {
return this.Members;
}
public String Add(String memberAdd) {
if (this.Members.contains(memberAdd)) return(memberAdd+" is already a part of "+this.name);
if (!server.LoginNames.contains(memberAdd)) return("The username "+memberAdd+" doesn't exist");
for (int c=0; c<server.Chatrooms.size();c++)
{
Chatroom C = server.Chatrooms.elementAt(c);
if (C.Members.contains(memberAdd)) return("Cannot add "+memberAdd+" to chatroom "+this.name+"\n"+memberAdd+" already a part of chatroom "+C.name);
}
this.Members.add(memberAdd);
server.ConnectedChatroom.put(memberAdd,this);
return(memberAdd+" added to chatroom "+this.name);
}
public void Notify(String msg,String no_notif) {
for (int i=0;i<this.Members.size();i++)
{
if (!this.Members.elementAt(i).equals(no_notif))
{
try {
Socket sendSoc = server.ClientSockets.elementAt(server.LoginNames.indexOf(this.Members.elementAt(i)));
DataOutputStream senddout = new DataOutputStream(sendSoc.getOutputStream());
senddout.writeUTF(msg);
} catch(Exception e) {
System.out.println(e);
}
}
}
}
}