-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOMachine.java
497 lines (369 loc) · 13.8 KB
/
IOMachine.java
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
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
/**
* IOMachine
*
* This class was made to easily execute IO commands. Initialize it with an ObjectOutputStream and an ObjectInputStream
* and then use commands like findProfile (returns a profile object) and deleteProfile (erases a profile from the
* server).
*
* @author Team 60, Section 11
* @version 5/03/2021
*
*/
public class IOMachine extends ObjectOutputStream {
ObjectOutputStream dos;
ObjectInputStream dis;
//Constructs an IO machine from an ObjectOutputStream and an ObjectInputStream
public IOMachine(ObjectOutputStream dos, ObjectInputStream dis) throws IOException, ClassNotFoundException {
this.dos = dos;
this.dis = dis;
}
/**
* Searches for a Profile in the server by username, returns that profile (Or null if it is not found).
*
* @param username - the username of a profile to be searched for
* @return Profile or null
*/
public Profile findProfile(String username) {
try {
String result; //result returned from server
//Write command and username to server
dos.writeObject("SendProfile");
dos.writeObject(username);
//read whether the profile was found or not
result = (String) dis.readObject();
if (result.equals("True")) {
//read and return profile
return (Profile) dis.readObject();
}
//returns null if no profile matching the username is found
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* adds a profile to the server based on the associated account
*
* @param profile - the profile to added
* @return whether the profile was added or not
*/
public boolean addProfile(Profile profile, String email) {
try {
String result; //result returned from server
//Write command, email, and profile to add to server
dos.writeObject("AddProfile");
dos.writeObject(email);
dos.writeObject(profile);
//read whether the profile was found or not
result = (String) dis.readObject();
return result.equals("True");
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* removes a profile from the server
*
* @param username - username of the profile to be deleted
* @return a boolean that is true if the profile was found and deleted
*/
public boolean deleteProfile(String username) {
try {
String result; //result returned from server
//Write command and username to server
dos.writeObject("DeleteProfile");
dos.writeObject(username);
//read whether the profile was found or not
result = (String) dis.readObject();
return result.equals("True");
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* adds an account to the server
*
* @param account - the account to added
* @return whether the account was added or not
*/
public boolean addAccount(Account account) {
try {
String result; //result returned from server
dos.writeObject("AddAccount");
dos.writeObject(account.getEmail());
dos.writeObject(account.getPassword());
ArrayList<Profile> profiles = account.getProfiles();
dos.writeObject(profiles);
result = (String) dis.readObject();
return result.equals("True");
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Searches for an Account in the server by email, returns that Account (Or null if it is not found).
*
* @param email - the email of a account to be searched for
*
* @return an Account or null
*/
public Account findAccount(String email) {
try {
String result; //result returned from server
String password; //password returned from server
ArrayList<Profile> profiles; //profiles returned from the server
dos.writeObject("SendAccount");
dos.writeObject(email);
result = (String) dis.readObject();
if (result.equals("True")) {
password = (String) dis.readObject();
profiles = (ArrayList<Profile>) dis.readObject();
return (new Account(email, password, profiles));
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* removes an Account from the server
*
* @param account - Account to be deleted
* @return whether the account was deleted or not
*/
public boolean deleteAccount(Account account) {
try {
String result; //result returned from server
dos.writeObject("DeleteAccount");
dos.writeObject(account.getEmail());
result = (String) dis.readObject();
return result.equals("True");
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* edits a non list field for a profile
*
* @param username - username of the profile to be edited
* @param parameter - the name of the field to be edited, capitalized
* @param parameterValue - the new value of the field
*
* @return whether field was edited or not
*/
public boolean editProfile(String username, String parameter, String parameterValue) {
try {
String result; //result returned from server
dos.writeObject("Edit" + parameter);
dos.writeObject(username);
dos.writeObject(parameterValue);
result = (String) dis.readObject();
return result.equals("True");
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* edits a list field for a profile
*
* @param action - whether the list is being added to or removed from. Should either be "Add" or "Remove"
* @param username - username of the profile to be edited
* @param parameter - the name of the field to be edited, capitalized
* @param parameterValue - the value of the field being added/removed
*
* @return whether field was edited or not
*/
public boolean editProfileList(String action, String username, String parameter, String parameterValue ) {
try {
String result; //result returned from server
dos.writeObject(action + parameter);
dos.writeObject(username);
dos.writeObject(parameterValue);
result = (String) dis.readObject();
return result.equals("True");
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* sends a friend request and updates fields appropriately
*
* @param username - the username receiving the request
* @param requester - the username sending the request
* @return whether the friend was added to the friends list and removed from the pending requests list
* */
public boolean sendRequest(String username, String requester) {
try {
String result; //result returned from the server
dos.writeObject("AddRequestsSent");
dos.writeObject(username);
dos.writeObject(requester);
result = (String) dis.readObject();
if (result.equals("True")) {
dos.writeObject("AddRequestsReceived");
dos.writeObject(requester);
dos.writeObject(username);
result = (String) dis.readObject();
return result.equals("True");
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* rescinds an already sent friend request
*
* @param username - the username receiving the request
* @param requester - the username sending the request
* @return whether the friend request was successfully rescinded
* */
public boolean rescindRequest(String username, String requester) {
try {
String result; //result returned from the server
dos.writeObject("RemoveRequestsSent");
dos.writeObject(username);
dos.writeObject(requester);
result = (String) dis.readObject();
if (result.equals("True")) {
dos.writeObject("RemoveRequestsReceived");
dos.writeObject(requester);
dos.writeObject(username);
result = (String) dis.readObject();
return result.equals("True");
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false; }
}
/**
* accepts a friend request
*
* @param username - the person accepting the request
* @param requester - the person whose request is accepted
*
* @return whether the friend was added to the friends list and removed from the pending requests list
* */
public boolean acceptFriend(String username, String requester) {
try {
String result; //result returned from the server
dos.writeObject("AddFriend");
dos.writeObject(username);
dos.writeObject(requester);
result = (String) dis.readObject();
if (result.equals("True")) {
dos.writeObject("AddFriend");
dos.writeObject(requester);
dos.writeObject(username);
result = (String) dis.readObject();
} else {
return false;
}
dos.writeObject("RemoveRequestsSent");
dos.writeObject(requester);
dos.writeObject(username);
result = (String) dis.readObject();
if (result.equals("True")) {
dos.writeObject("RemoveRequestsReceived");
dos.writeObject(username);
dos.writeObject(requester);
result = (String) dis.readObject();
return result.equals("True");
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* declines a friend request
*
* @param username - the username of the profile that is declining the request
* @param requester - the username of the profile that sent the request
*
* @return whether the friend request was denied
*/
public boolean declineFriend(String username, String requester) {
try {
String result; //result returned from the server
dos.writeObject("RemoveRequestsSent");
dos.writeObject(username);
dos.writeObject(requester);
result = (String) dis.readObject();
if (result.equals("True")) {
dos.writeObject("RemoveRequestsReceived");
dos.writeObject(requester);
dos.writeObject(username);
result = (String) dis.readObject();
return result.equals("True");
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false; }
}
/**
* unfriends another user
*
* @param username - the username of the profile being unfriended
* @param requester - the username of the profile that initiated the unfriending
*
* @return whether the user was unfriended
* */
public boolean unfriend(String username, String requester) {
try {
String result; //result returned from the server
dos.writeObject("RemoveFriend");
dos.writeObject(username);
dos.writeObject(requester);
result = (String) dis.readObject();
if (result.equals("True")) {
dos.writeObject("RemoveFriend");
dos.writeObject(requester);
dos.writeObject(username);
result = (String) dis.readObject();
return result.equals("True");
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public ArrayList<String> viewAllProfiles() {
try {
String result; //result returned from the server
dos.writeObject("SendAllProfiles");
result = (String) dis.readObject();
if (result.equals("True")) {
return (ArrayList<String>) dis.readObject();
} else {
return new ArrayList<String>();
}
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<String>();
}
}
}