-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerThread.java
More file actions
302 lines (275 loc) · 6.95 KB
/
Copy pathServerThread.java
File metadata and controls
302 lines (275 loc) · 6.95 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
import java.net.Socket;
import java.io.IOException;
public class ServerThread implements Runnable
{
private Socket socket = null;
private Server server = null;
//In and out are of sender socket of server
private Reader in = null;
private Writer out = null;
private int mode = -1;
ServerThread(Socket s, Server server, int mode)
{
socket = s;
this.server = server;
try
{
in = new Reader(socket.getInputStream());
out = new Writer(socket.getOutputStream());
this.mode = mode;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
String username = null;
boolean rcv = false;
//First loop is for registration
while(true)
{
String s = in.readLine();
if(s==null)
continue;
in.readLine();
if(s.startsWith("REGISTER TORECV"))
{
rcv = true;
username = s.substring(16, s.lastIndexOf(32));
int m = Integer.parseInt(s.substring(s.lastIndexOf(32)).trim());
if(m!=mode)
{
out.print("ERROR 107 INVALID MODE\n\n");
return;
}
if(isValidUsername(username))
out.print("REGISTERED TORECV "+username+"\n\n");
else
{
out.print("ERROR 100 Malformed Username\n\n");
continue;
}
}
else if(s.startsWith("REGISTER TOSEND"))
{
username = s.substring(16, s.lastIndexOf(32));
int m = Integer.parseInt(s.substring(s.lastIndexOf(32)).trim());
if(m!=mode)
{
out.print("ERROR 107 INVALID MODE\n\n");
return;
}
if(isValidUsername(username))
out.print("REGISTERED TOSEND "+username+"\n\n");
else
{
out.print("ERROR 100 Malformed Username\n\n");
continue;
}
}
else
System.out.println(s);
break;
}
//If receiver thread insert socket and delete
if(rcv)
{
server.insert(username, socket);
server.updateCount(username);
System.out.println(username+" recv reg done");
return;
}
System.out.println(username+" send reg done");
//To ensure count is 2 for fully
//registering username
server.updateCount(username);
boolean recv = true, hash = false;
//br and pw are of the recipient thread of server
Reader br = null;
Writer pw = null;
String recip = null;
while(true)
{
//When receiving comm from sender thread of sender
if(recv)
{
recip = null;
String s = null, header1=null, header2 = null, message = null, signature=null;
int l1 = 0, l2 = 0;
br = null;
pw = null;
s = in.readLine();
// blocking read handle
if(s==null)
{
server.delete(username);
System.out.println(username+" Abruptly exited");
return;
}
//Sending message
if(s.startsWith("SEND"))
{
recip = s.substring(5);
header1 = in.readLine();
if(header1==null)
{
out.print("ERROR 103 Header incomplete\n\n");
continue;
}
l1 = Integer.parseInt(header1.substring(16));
if(mode==2)
{
header2 = in.readLine();
if(header2==null)
{
out.print("ERROR 103 Header incomplete\n\n");
continue;
}
l2 = Integer.parseInt(header2.substring(18));
}
if(!in.readLine().equals(""))
{
out.print("ERROR 103 Header incomplete\n\n");
continue;
}
message = in.read(l1);
if(mode==2)
signature = in.read(l2);
}
//Sending fetchkey to recipient for encryption
else if(s.startsWith("FETCHKEY"))
{
recip = s.substring(9);
in.readLine();
}
else if(s.startsWith("DEREGISTER"))
{
server.delete(username);
in.readLine();
System.out.println(username+" deregistered");
return;
}
//Checking if recipient exists
if(!server.contains(recip))
{
out.print("ERROR 101 No User Registered as "+recip+"\n\n");
continue;
}
Socket recipSocket = server.get(recip);
try
{
br = new Reader(recipSocket.getInputStream());
pw = new Writer(recipSocket.getOutputStream());
}
catch(IOException e){e.printStackTrace();}
if(br==null||pw ==null)
continue;
//Sending message
if(s.startsWith("SEND"))
{
if(mode==2)
pw.print("FORWARD "+username+"\n"+header1+"\n"+header2+"\n\n"+message+signature);
else
pw.print("FORWARD "+username+"\n"+header1+"\n\n"+message);
try
{
System.out.println(username + " TO "+recip+"\n"+(new String(message.getBytes(), "UTF-8"))+"\n");
System.out.println();
}catch(Exception e){}
}
//Sending fetchkey to recipient for encryption
else if(s.startsWith("FETCHKEY"))
{
pw.print("FETCHKEY"+"\n\n");
}
recv = false;
}
//When receiving comm from receiver thread of recipient
else
{
//Sending public key of sender to recipient
if(hash)
{
try
{
//Reading from receiver thread of this username for public key
Reader r = new Reader(server.get(username).getInputStream());
String s = r.readLine();
if(s==null)
continue;
if(s.startsWith("FETCHEDKEY"))
{
int l = Integer.parseInt(r.readLine());
pw.println("FETCHEDKEY\n"+l+"\n"+r.read(l)+"\n");
r.readLine();r.readLine();
hash = false;
}
}
catch(IOException e){e.printStackTrace();}
}
String s = br.readLine();
if(s==null)
continue;
//Acknowledgement message
if(s.startsWith("RECEIVED"))
{
br.readLine();
out.print("SENT "+recip+"\n\n");
}
//For encryption of message
else if(s.startsWith("FETCHEDKEY"))
{
s = br.readLine();
String key = br.read(Integer.parseInt(s));
br.readLine();
br.readLine();
out.print("FETCHEDKEY\n"+s+"\n"+key+"\n\n");
}
else if(s.startsWith("ERROR 102"))
{
br.readLine();
out.print(s+"\n\n");
}
else if(s.startsWith("ERROR 103"))
{
br.readLine();
out.print("ERROR 102 Recipient Server communication failed"+"\n\n");
}
else if(s.startsWith("ERROR 106"))
{
br.readLine();
out.print(s+"\n\n");
}
recv = true;
//If recipient wants public key of sender
//for signature check
if(s.startsWith("FETCHKEY"))
{
br.readLine();
try
{
Writer w = new Writer(server.get(username).getOutputStream());
w.print("FETCHKEY\n\n");
}
catch(IOException e){e.printStackTrace();}
recv = false;
hash = true;
}
}
}
}
public boolean isValidUsername(String s)
{
if(server.contains(s))
return false;
for(int i=0;i<s.length();++i)
{
if(!Character.isLetterOrDigit(s.charAt(i)))
return false;
}
return true;
}
}