-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
585 lines (520 loc) · 18.2 KB
/
client.c
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
//Jiayao Wu, Suyi Liu
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sp.h"
#include "net_include.h"
#define MAX_MEMBERS 10
static char User[80];
static char Spread_name[80];
static char username[80];
static char server_group_name[80];
static int server = -1;
static int temp_server;
static int delete_mail; //d 10
static int read_mail; //r 10
static char Private_group[MAX_GROUP_NAME];
static mailbox Mbox;
static void Print_menu();
static int To_exit = 0;
static void Bye();
static void User_command();
static void Read_message();
char choice;
static int currupdatesize;
static Update *currupdate;
static Mail *currmail;
static int currmailsize;
static int service_type;
static int sender[MAX_GROUP_NAME];
static char target_groups[MAX_MEMBERS][MAX_GROUP_NAME];
static int num_groups;
static int mess_type;
static int endian_mismatch;
//static Mail_list *mail_list;
static Update_l_p *update_l_p;
static Update_r_d *update_r_d;
static Update_add *update_add;
static Update_k *m_info;
Mail_node* form_mail_node(Mail *m);
void add_Mail_node_to_list(Mail_list* ml, Mail_node* mn);
int main( int argc, char *argv[])
{
int ret;
m_info = malloc(sizeof(Update_k));
//client connect to a private group
sprintf(User, "heirenwenhao123");
sprintf(Spread_name, "10330");
ret = SP_connect(Spread_name, User, 0 ,1, &Mbox, Private_group);
if (ret != ACCEPT_SESSION)
{
SP_error(ret);
Bye();
}
printf("User: connect to %s with private group %s\n",Spread_name,Private_group);
E_init();
E_attach_fd( 0, READ_FD, User_command, 0, NULL, LOW_PRIORITY );
Print_menu();
fflush(stdout);
E_handle_events();
return (0);
}
static void User_command() {
int senderM;
int ret;
int i;
int num_groups;
char receiver[80];
char subject[80];
char content[1000];
char command[130];
for( i=0; i < sizeof(command); i++ ) command[i] = 0;
if( fgets( command, 130, stdin ) == NULL )
Bye();
switch (command[0])
{
case 'u':
ret = sscanf( &command[2],"%s",username);
if (ret < 1)
{
printf("invalid username.\n");
Print_menu();
break;
} else {
printf("Logged in as %s.\n", username);
server = -1;
}
Print_menu();
break;
case 'c':
ret = sscanf( &command[2],"%d",&temp_server);
if (ret < 1)
{
printf("invalid server number.\n");
Print_menu();
break;
}
if (username[0] == '\0') {
printf("You have to specify username first!\n");
Print_menu();
break;
}
//handle cases not in 1-5
if (temp_server < 1 || temp_server >5) {
printf("Invalid server index. Please try again.\n");
Print_menu();
break;
}
if (temp_server == server) {
printf("You have already connected to this server!\n");
Print_menu();
break;
}
if (temp_server != -1)
{
server = temp_server;
}
sprintf(server_group_name,"server_%d",server);
printf("Connected to server: %s\n",server_group_name);
Print_menu();
break;
case 'l':
if (server == -1) {
printf("You have to connect to a server first!\n");
Print_menu();
break;
}
update_l_p = malloc(sizeof(Update_l_p));
update_l_p->type = 5;
update_l_p->server_index = server;
strcpy(update_l_p->client, username);
ret = SP_multicast(Mbox, AGREED_MESS, server_group_name,0,sizeof(Update_l_p),update_l_p);
if ( ret < 0) {
SP_error(ret);
Bye();
}
//no need here: initialize a fresh mail list
//wait to receive the msg
int all_received_flag = 0;
int index = 0;
printf("\n~~~User name: %s~~~\n", username);
printf("~~~Mail server index: %d~~~\n", server);
printf("~~~Listing of headers~~~\n");
currmail = malloc(sizeof(Mail));
while (all_received_flag == 0) {
ret = SP_receive(Mbox, &service_type,sender, MAX_MEMBERS,&num_groups,target_groups,&mess_type,&endian_mismatch,sizeof(Mail), currmail);
if ( ret < 0) {
SP_error(ret);
Bye();
}
if (currmail->read == 2) {
all_received_flag = 1;
printf("~~~End of listing~~~\n");
break;
}
Mail *m = malloc(sizeof(Mail));
//copying fields
m->stamp = currmail->stamp;
m->server = currmail->server;
m->read = currmail->read;
strcpy(m->sender, currmail->sender);
strcpy(m->receiver, currmail->receiver);
strcpy(m->subject, currmail->subject);
strcpy(m->msg, currmail->msg);
//printing
index++;
printf("Mail %d: \n", index);
printf(" sender: %s", m->sender);
printf(" subject: %s \n", m->subject);
if (m->read == 1) {
printf(" R\n \n");
} else {
printf(" N\n \n");
}
free(m);
}
Print_menu();
break;
case 'm':
printf("Mailing a message\n");
if (server == -1) {
printf("You have to connect to a server first!\n");
Print_menu();
break;
}
currmail = malloc(sizeof(Mail));
printf("to: ");
//fgets (&receiver,sizeof(stdin), stdin);
scanf("%[^\n]%*c", &receiver);
printf("subject: ");
//fgets (&subject,sizeof(stdin), stdin);
scanf("%[^\n]%*c",subject);
printf("msg: ");
//fgets (&content, 1000, stdin);
scanf("%[^\n]%*c", content);
currmail->read = 0;
currmail->server = server;
strcpy(currmail->sender, username);
strcpy(currmail->receiver,receiver);
strcpy(currmail->subject,subject);
strcpy(currmail->msg,content);
update_add = malloc(sizeof(Update_add));
update_add->type = 2;
strcpy(update_add->client, receiver);
update_add->server_index = server;
update_add->mail = *currmail;
ret = SP_multicast(Mbox, AGREED_MESS, server_group_name,0,sizeof(Update_add),update_add );
if ( ret < 0) {
SP_error(ret);
Bye();
}
free(currmail);
Print_menu();
break;
case 'd':
if (server == -1) {
printf("You have to connect to a server first!\n");
Print_menu();
break;
}
update_l_p = malloc(sizeof(Update_l_p));
update_l_p->type = 5;
update_l_p->server_index = server;
strcpy(update_l_p->client, username);
//send the request for listing (operation = 4)
ret = SP_multicast(Mbox, AGREED_MESS, server_group_name,0,sizeof(Update_l_p),update_l_p);
if ( ret < 0) {
SP_error(ret);
Bye();
}
//temporary mail list
Mail_list *temp_mail_list = malloc(sizeof(Mail_list));
temp_mail_list->dummyhead.next = NULL;
temp_mail_list->tail = &(temp_mail_list->dummyhead);
temp_mail_list->count = 0;
//wait to receive the msg
int all_received_flag2 = 0;
int index2 = 0;
printf("~~~User name: %s~~~\n", username);
printf("~~~Mail server index: %d~~~\n", server);
printf("~~~Listing of headers~~~\n");
currmail = malloc(sizeof(Mail));
while (all_received_flag2 == 0) {
ret = SP_receive(Mbox, &service_type,sender, MAX_MEMBERS,&num_groups,target_groups,&mess_type,&endian_mismatch,sizeof(Mail), currmail);
if ( ret < 0) {
SP_error(ret);
Bye();
}
if (currmail->read == 2) {
all_received_flag2 = 1;
printf("~~~End of listing~~~\n");
break;
}
Mail *m = malloc(sizeof(Mail));
//copying fields
m->stamp = currmail->stamp;
m->server = currmail->server;
m->read = currmail->read;
strcpy(m->sender, currmail->sender);
strcpy(m->receiver, currmail->receiver);
strcpy(m->subject, currmail->subject);
strcpy(m->msg, currmail->msg);
//adding to local list is not needed here
Mail_node* mn = form_mail_node(m);
add_Mail_node_to_list(temp_mail_list, mn);
//printing
index2++;
printf("Mail %d: \n", index2);
printf(" sender: %s", m->sender);
printf(" subject: %s \n", m->subject);
if (m->read == 1) {
printf(" R\n \n");
} else {
printf(" N\n \n");
}
}
printf("Please select which message you want to delete: ");
char delete_mail_s[80];
scanf("%s", delete_mail_s);
delete_mail = atoi(delete_mail_s);
if (delete_mail < 1) {
printf("Index should be greater than or equal to 1\n");
Print_menu();
break;
}
Mail_node *temp = &(temp_mail_list->dummyhead);
int breakflag2 = 0;
for (int i = 0; i < delete_mail;i++) {
if (temp->next != NULL) {
temp = temp->next;
} else {
printf("invalid number. \n");
breakflag2 = 1;
break;
}
}
if (breakflag2 == 1) {
Print_menu();
break;
}
update_r_d = malloc(sizeof(Update_r_d));
update_r_d->type = 4;
strcpy(update_r_d->client, username);
update_r_d->server_index = temp->mail->server;
update_r_d->server_stamp = temp->mail->stamp;
ret = SP_multicast(Mbox, AGREED_MESS, server_group_name,0,sizeof(Update_r_d), update_r_d);
if ( ret < 0) {
SP_error(ret);
Bye();
}
printf("Deleting a mail\n");
free(temp_mail_list);
free(currmail);
Print_menu();
break;
case 'r':
if (server == -1) {
printf("You have to connect to a server first!\n");
Print_menu();
break;
}
update_l_p = malloc(sizeof(Update_l_p));
update_l_p->type = 5;
update_l_p->server_index = server;
strcpy(update_l_p->client, username);
//send the request for reading (operation = 5)
ret = SP_multicast(Mbox, AGREED_MESS, server_group_name,0,sizeof(Update_l_p),update_l_p);
if ( ret < 0) {
SP_error(ret);
Bye();
}
//temporary mail list
Mail_list *temp_mail_list2 = malloc(sizeof(Mail_list));
temp_mail_list2->dummyhead.next = NULL;
temp_mail_list2->tail = &(temp_mail_list2->dummyhead);
temp_mail_list2->count = 0;
//wait to receive the msg
int all_received_flag3 = 0;
int index3 = 0;
printf("~~~User name: %s~~~\n", username);
printf("~~~Mail server index: %d~~~\n", server);
printf("~~~Listing of headers~~~\n");
currmail = malloc(sizeof(Mail));
while (all_received_flag3 == 0) {
ret = SP_receive(Mbox, &service_type,sender, MAX_MEMBERS,&num_groups,target_groups,&mess_type,&endian_mismatch,sizeof(Mail), currmail);
if ( ret < 0) {
SP_error(ret);
Bye();
}
if (currmail->read == 2) {
all_received_flag3 = 1;
printf("~~~End of listing~~~\n");
break;
}
Mail *m = malloc(sizeof(Mail));
//copying fields
m->stamp = currmail->stamp;
m->server = currmail->server;
m->read = currmail->read;
strcpy(m->sender, currmail->sender);
strcpy(m->receiver, currmail->receiver);
strcpy(m->subject, currmail->subject);
strcpy(m->msg, currmail->msg);
//adding to local list is not needed here
Mail_node* mn = form_mail_node(m);
add_Mail_node_to_list(temp_mail_list2, mn);
//printing
index3++;
printf("Mail %d: \n", index3);
printf(" sender: %s", m->sender);
printf(" subject: %s \n", m->subject);
if (m->read == 1) {
printf(" R\n \n");
} else {
printf(" N\n \n");
}
}
printf("Please select which message you want to read: ");
char read_mail_s[80];
scanf("%s", read_mail_s);
read_mail = atoi(read_mail_s);
if (read_mail < 1) {
printf("Index should be greater than or equal to 1\n");
Print_menu();
break;
}
//printf("%d",read_mail);
//read_mail = command[0];
Mail_node *temp2 = &(temp_mail_list2->dummyhead);
int breakflag = 0;
for (int i = 0; i < read_mail;i++) {
if (temp2->next != NULL) {
temp2 = temp2->next;
} else {
printf("invalid number \n");
breakflag = 1;
break;
}
}
if (breakflag == 1) {
Print_menu();
break;
}
update_r_d = malloc(sizeof(Update_r_d));
update_r_d->type = 3;
strcpy(update_r_d->client, username);
update_r_d->server_index = temp2->mail->server;
update_r_d->server_stamp = temp2->mail->stamp;
ret = SP_multicast(Mbox, AGREED_MESS, server_group_name,0,sizeof(Update_r_d), update_r_d);
if ( ret < 0) {
SP_error(ret);
Bye();
}
//SP_receive() 等待
printf("Here is the mail:\n\n");
ret = SP_receive(Mbox, &service_type,sender, MAX_MEMBERS,&num_groups,target_groups,&mess_type,&endian_mismatch,sizeof(Mail),currmail);
if ( ret < 0) {
printf("sp_error when recieve msg for read (mail)\n");
SP_error(ret);
Bye();
}
if (currmail->stamp == -1) {
printf("Sorry, the mail has already been deleted, or it is out of the range\n");
Print_menu();
break;
}
printf("sender: %s\n", currmail->sender);
printf("receiver: %s\n",currmail->receiver);
printf("subject: %s\n", currmail->subject);
printf("msg: %s\n", currmail->msg);
free(currmail);
//free(temp_mail_list2);
Print_menu();
break;
case 'v':
if (server == -1) {
printf("You have to connect to a server first!\n");
Print_menu();
break;
}
update_l_p = malloc(sizeof(Update_l_p));
update_l_p->type = 6;
update_l_p->server_index = server;
strcpy(update_l_p->client, username);
//send the request for printing membership (operation = 6)
ret = SP_multicast(Mbox, AGREED_MESS, server_group_name,0,sizeof(Update_l_p),update_l_p);
if ( ret < 0) {
SP_error(ret);
Bye();
}
//SP_receive to wait
printf("here is the membership info: \n");
ret = SP_receive(Mbox, &service_type,sender, MAX_MEMBERS,&num_groups,target_groups,&mess_type,&endian_mismatch,sizeof(Update_k),m_info);
if ( ret < 0) {
printf("sp_error when recieve msg for membership info\n");
SP_error(ret);
Bye();
}
if (m_info->type != 8) {
printf("Weird, should be 8, but it is %d instead\n", m_info->type);
}
for (int member = 0; member < 6; member++) {
if (m_info->array[member] == 1) {
printf("server: %d\n",member);
}
}
printf("\n");
Print_menu();
break;
case 'q':
Bye();
break;
default:
printf("\nUnknown commnad\n");
Print_menu();
break;
}
}
static void Print_menu()
{
printf("\n");
printf("==========\n");
printf("Client Menu:\n");
printf("\tu <name> -- login with a user name\n");
printf("\tc <number> -- connect with a mail server\n");
printf("\tl -- list the headers of received mail\n");
printf("\tm -- mail a message to a user\n");
printf("\td <number> -- delete a mail message\n");
printf("\tr <number> -- read a received message\n");
printf("\tv -- print the membership\n");
printf("\tq -- quit\n");
printf("User > ");
fflush(stdout);
}
static void Bye()
{
To_exit = 1;
printf("\nBye.\n");
SP_disconnect( Mbox );
exit( 0 );
}
Mail_node* form_mail_node(Mail *m) {
Mail_node* mn = malloc(sizeof(Mail_node));
mn->mail = m;
mn->next = NULL;
return mn;
}
void add_Mail_node_to_list(Mail_list* ml, Mail_node* mn) {
if (ml->count == 0) {
ml->dummyhead.next = mn;
ml->tail = mn;
ml->tail->next = NULL;
ml->count++;
} else {
ml->tail->next = mn;
ml->tail = mn;
ml->count++;
}
}