-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
537 lines (454 loc) · 15.6 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
#include "sp.h"
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <stdbool.h>
#include "include.h"
static char User[80];
static char Spread_name[80];
static mailbox Mbox;
static char private_group[MAX_GROUP_NAME];
static char *username_addr;
static char username_sscanf[MAX_NAME_LEN];
static char username[MAX_NAME_LEN];
static char client_server_group[MAX_GROUP_NAME];
static char *receiver_name;
static char current_group[MAX_NAME_LEN] = {'\0'};
static int server_index = -1;
static bool isConnected = NULL;
static char server_private_group[MAX_GROUP_NAME];
static int isLoggedWithUser = 0;
static int current_server_index = -1;
static update *list = NULL;
static update *mail = NULL;
static update *delete = NULL;
static update *read = NULL;
static update *view = NULL;
/*parameters of read message()*/
static int list_index = 1;
static char Is_read[10] = "unread";
static update* reply = NULL;
static void Usage(int argc, char const *argv[]);
static void Bye();
static void Print_menu();
static void User_command();
static void Read_message();
int main(int argc, char const *argv[])
{
int ret;
int mver, miver, pver;
sp_time test_timeout;
test_timeout.sec = 5;
test_timeout.usec = 0;
Usage( argc, argv );
if (!SP_version( &mver, &miver, &pver))
{
printf("main: Illegal variables passed to SP_version()\n");
Bye();
}
printf("Spread library version is %d.%d.%d\n", mver, miver, pver);
ret = SP_connect_timeout( Spread_name, User, 0, 1, &Mbox, private_group, test_timeout );
if( ret != ACCEPT_SESSION )
{
SP_error( ret );
exit(1);
}
printf("User: connected to %s with private group %s\n", Spread_name, private_group );
Print_menu();
printf("\nUser> ");
E_init();
E_attach_fd( 0, READ_FD, User_command, 0, NULL, LOW_PRIORITY );
E_attach_fd( Mbox, READ_FD, Read_message, 0, NULL, HIGH_PRIORITY );
E_handle_events();
return 0;
}
static void User_command()
{
int ret;
char command[100];
char receiver_fgets[MAX_NAME_LEN];
char subject[MAX_SUB_LEN];
char message[MAX_MSG_LEN];
int delete_index;
int read_index;
for (int i = 0; i < sizeof(command); i++) command[i] = '\0';
if (fgets(command, sizeof(command), stdin) == NULL)
{
Bye();
}
switch(command[0]){
case 'u':
if(current_group[0] != '\0'){
SP_leave(Mbox, current_group);
}
for (int i = 0; i < sizeof(username_sscanf); i++) username_sscanf[i] = '\0';
ret = sscanf(&command[2], "%s", username_sscanf);
if (ret <= 0)
{
printf("Invalid username.\n");
break;
}else
{
username_addr = strtok(username_sscanf, "\n");
strcpy(username, username_addr);
printf("Current user [%s]\n", username);
sprintf(private_group, "client_%s", username);
SP_join(Mbox, private_group);
strcpy(current_group, private_group);
isLoggedWithUser = 1;
isConnected = 0;
current_server_index = -1;
}
break;
case 'c':
if (isLoggedWithUser == 0)
{
printf("Please login with an username\n");
break;
}
ret = sscanf(&command[2], "%d", &server_index);
if (ret <= 0)
{
printf("Invalid server index\n");
break;
}else if (server_index < 1 || server_index > 5)
{
printf("Invalid server_index, plz choose from [1,5] \n");
break;
}
if (current_server_index != -1)
{
if (current_server_index == server_index)
{
printf("Already connected to server %d\n", server_index );
break;
}else{
SP_leave(Mbox, client_server_group);
current_server_index = server_index;
}
}
sprintf(client_server_group,"client_server%d", server_index);
SP_join(Mbox, client_server_group);
printf("Connected to the new server%d\n", server_index);
current_server_index = server_index;
sprintf(server_private_group, "server%d", server_index);
isConnected = 1;
break;
case 'l':
if (isLoggedWithUser == 0)
{
printf("Please login with an username\n");
break;
}
if (isConnected == 0)
{
printf("Please connect to a server\n");
break;
}
list = malloc(sizeof(update));
list->type = LIST_TYPE;
list->source = FROM_CLIENT;
strncpy(list->receiver, username, MAX_NAME_LEN);
strncpy(list->subject, private_group, MAX_GROUP_NAME);
ret = SP_multicast( Mbox, AGREED_MESS, server_private_group, 1, sizeof(update),(char *)list);
if( ret <= 0 )
{
SP_error( ret );
exit(1);
}
break;
case 'm':
if (isLoggedWithUser == 0)
{
printf("Please login with an username\n");
break;
}
if (isConnected == 0)
{
printf("Please connect to a server\n");
break;
}
printf("to:\n");
if (fgets(receiver_fgets, MAX_NAME_LEN, stdin) == NULL)
{
printf("Invalid receiver_name.\n");
break;
}
receiver_name = strtok(receiver_fgets, "\n");
printf("subject:\n");
if (fgets(subject, MAX_SUB_LEN, stdin) == NULL)
{
printf("Invalid subject.\n");
break;
}
printf("to: %s\n", receiver_name);
printf("subject: %s\n", subject);
printf("enter message:\n");
if (fgets(message, MAX_MSG_LEN, stdin) == NULL)
{
printf("Invalid subject.\n");
break;
}
mail = (update*)malloc(sizeof(update));
mail->type = MAIL_TYPE;
mail->source = FROM_CLIENT;
strncpy(mail->sender, username, MAX_NAME_LEN);
strncpy(mail->receiver, receiver_name, MAX_NAME_LEN);
strncpy(mail->subject, subject, MAX_SUB_LEN);
strncpy(mail->msg, message, MAX_MSG_LEN);
ret = SP_multicast(Mbox, AGREED_MESS, server_private_group, 1, sizeof(update),(char *)mail);
if( ret <= 0 )
{
SP_error( ret );
exit(1);
}
break;
case 'd':
if (isLoggedWithUser == 0)
{
printf("Please login with an username\n");
break;
}
if (isConnected == 0)
{
printf("Please connect to a server\n");
break;
}
ret = sscanf( &command[2], "%d", &delete_index);
if (ret <= 0)
{
printf("Invalid delete_index.\n");
break;
}
delete = malloc(sizeof(update));
delete->type = DELETE_TYPE;
delete->source = FROM_CLIENT;
delete->update_index = delete_index;
strncpy(delete->receiver, username, MAX_NAME_LEN);
strncpy(delete->subject, private_group, MAX_GROUP_NAME);
ret = SP_multicast(Mbox, AGREED_MESS, server_private_group, 1, sizeof(update),(char *)delete);
if( ret <= 0 )
{
SP_error( ret );
exit(1);
}
break;
case 'r':
if (isLoggedWithUser == 0)
{
printf("Please login with an username\n");
break;
}
if (isConnected == 0)
{
printf("Please connect to a server\n");
break;
}
ret = sscanf(&command[2], "%d", &read_index);
if (ret <= 0)
{
printf("Invalid read_index.\n");
break;
}
read = malloc(sizeof(update));
read->type = READ_TYPE;
read->source = FROM_CLIENT;
read->update_index = read_index;
strncpy(read->receiver, username, MAX_NAME_LEN);
strncpy(read->subject, private_group, MAX_GROUP_NAME);
ret = SP_multicast(Mbox, AGREED_MESS, server_private_group,1,sizeof(update),(char *)read);
if( ret <= 0 )
{
SP_error( ret );
exit(1);
}
break;
case 'v':
if (isLoggedWithUser == 0)
{
printf("Please login with an username\n");
break;
}
if (isConnected == 0)
{
printf("Please connect to a server\n");
break;
}
view = malloc(sizeof(update));
view->type = VIEW_TYPE;
view->source = FROM_CLIENT;
strncpy(view->receiver, username, MAX_NAME_LEN);
strncpy(view->subject, private_group, MAX_GROUP_NAME);
// printf("subject for view%s\n", view->subject);
ret = SP_multicast(Mbox, AGREED_MESS, server_private_group, 1, sizeof(update),(char *)view);
if( ret <= 0 )
{
SP_error( ret );
exit(1);
}
break;
default:
Print_menu();
break;
}
fflush(stdout);
}
static void Read_message(){
int ret;
int service_type;
char sender[MAX_GROUP_NAME];
int num_groups;
char target_groups[MAX_MEMBERS][MAX_GROUP_NAME];
int16 mess_type;
int endian_mismatch;
membership_info memb_info;
int num_vs_sets;
vs_set_info vssets[MAX_VSSETS];
unsigned int my_vsset_index;
char members[MAX_MEMBERS][MAX_GROUP_NAME];
int i,j;
service_type = 0;
reply = malloc(sizeof(update));
ret = SP_receive(Mbox, &service_type, sender, MAX_GROUPS, &num_groups, target_groups, &mess_type, &endian_mismatch, sizeof(update),(char *)reply);
if (Is_regular_mess(service_type))
{
//printf("reply type%d\n", reply->type);
if (reply->type == LIST_TYPE)
{
if (reply->read == 1)
{
strcpy(Is_read, "read");
}else
{
strcpy(Is_read, "unread");
}
if (list_index == 1)
{
printf("%s's inbox:\n", username);
}
printf("%d -%s -From: %s -Subject: %s\n", list_index++, Is_read, reply->sender, reply->subject);
printf("-------------------------------------------------------\n");
}else if (reply->type == FINAL_TYPE)
{
printf("Toal %d mails in the inbox\n", list_index - 1);
list_index = 1;
}else if (reply->type == DELETE_TYPE)
{
printf("Delete the %d mail.\n", reply->update_index);
}else if (reply->type == READ_TYPE)
{
printf("%d. :\n", reply->update_index);
printf("From: %s\n", reply->sender);
printf("Subject: %s\n", reply->subject);
printf("%s\n", reply->msg);
printf("--------------------------------------------------------\n");
}else if (reply->type == VIEW_TYPE)
{
printf("membership: \n");
for (i = 0; i < 5; i++)
{
if (reply->membership[i] != -1)
{
printf("server%d\n", reply->membership[i] );
}
}
}
}else if( Is_membership_mess( service_type ) )
{
ret = SP_get_memb_info((char *)reply, service_type, &memb_info );
if (ret < 0) {
printf("BUG: membership message does not have valid body\n");
SP_error( ret );
exit( 1 );
}
/* parse the membership message*/
if( Is_reg_memb_mess( service_type ) )
{
printf("Received REGULAR membership for group %s with %d members, where I am member %d:\n",
sender, num_groups, mess_type );
for(int i=0; i < num_groups; i++ )
printf("\t%s\n", &target_groups[i][0] );
printf("grp id is %d %d %d\n",memb_info.gid.id[0], memb_info.gid.id[1], memb_info.gid.id[2] );
if (sender[0] == 'c' && num_groups == 1)
{
printf("[PLEASE CHANGE ANOTHRE SERVER, this one is in the diff partition.^.^]\n");
}
if( Is_caused_join_mess( service_type ) )
{
printf("Due to the JOIN of %s\n", memb_info.changed_member );
printf( "\n================================\n");
}else if( Is_caused_leave_mess( service_type ) ){
printf("Due to the LEAVE of %s\n", memb_info.changed_member );
printf( "\n================================\n");
}else if( Is_caused_disconnect_mess( service_type ) ){
printf("Due to the DISCONNECT of %s\n", memb_info.changed_member );
printf("server_index: %d\n", server_index);
if ((memb_info.changed_member[1] - '0') == server_index)
{
printf("The connected server crashed, plz connect to a new one.\n");
}
printf( "\n================================\n");
}else if( Is_caused_network_mess( service_type ) ){
printf("Due to NETWORK change with %u VS sets\n", memb_info.num_vs_sets);
num_vs_sets = SP_get_vs_sets_info((char *)reply, &vssets[0], MAX_VSSETS, &my_vsset_index );
if (num_vs_sets < 0) {
printf("BUG: membership message has more then %d vs sets. Recompile with larger MAX_VSSETS\n", MAX_VSSETS);
SP_error( num_vs_sets );
exit( 1 );
}
for(i = 0; i < num_vs_sets; i++ )
{
printf("%s VS set %d has %u members:\n",
(i == my_vsset_index) ?
("LOCAL") : ("OTHER"), i, vssets[i].num_members );
ret = SP_get_vs_set_members((char *)reply, &vssets[i], members, MAX_MEMBERS);
if (ret < 0) {
printf("VS Set has more then %d members. Recompile with larger MAX_MEMBERS\n", MAX_MEMBERS);
SP_error( ret );
exit( 1 );
}
for( j = 0; j < vssets[i].num_members; j++ )
printf("\t%s\n", members[j] );
}
}
}else if( Is_transition_mess( service_type ) ) {
printf("received TRANSITIONAL membership for group %s\n", sender );
}else if( Is_caused_leave_mess( service_type ) ){
printf("received membership message that left group %s\n", sender );
}else
printf("received incorrecty membership message of type 0x%x\n", service_type );
} else if ( Is_reject_mess( service_type ) )
{
printf("REJECTED message from %s, of servicetype 0x%x messtype %d, (endian %d) to %d groups \n(%d bytes): %s\n",
sender, service_type, mess_type, endian_mismatch, num_groups, ret,(char *)reply );
}else
printf("received message of unknown message type 0x%x with ret %d\n", service_type, ret);
}
static void Usage(int argc, char const *argv[])
{
sprintf( User, "hbqy" );
sprintf( Spread_name, SPREAD_NAME);
}
static void Bye()
{
printf("\nBye.\n");
SP_disconnect( Mbox );
exit( 0 );
}
static void Print_menu()
{
printf( "\n================================\n");
printf( "Client Usage: \n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
"\t[-u <username>]: login with a username ",
"\t[-c <server>]: connect to a specific server",
"\t[-l]: list the received mails",
"\t[-m]: mail a massgae",
"\t[d <index>]: delete that mail in the current list",
"\t[r <index>]: read that mail in the current list",
"\t[v]: print the membership of servers in the current network");
printf("================================\n");
fflush(stdout);
}