-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_user.c
451 lines (388 loc) · 13.2 KB
/
class_user.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
/*
* The Spread Toolkit.
*
* The contents of this file are subject to the Spread Open-Source
* License, Version 1.0 (the ``License''); you may not use
* this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.spread.org/license/
*
* or in the file ``license.txt'' found in this distribution.
*
* Software distributed under the License is distributed on an AS IS basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Creators of Spread are:
* Yair Amir, Michal Miskin-Amir, Jonathan Stanton, John Schultz.
*
* Copyright (C) 1993-2009 Spread Concepts LLC <[email protected]>
*
* All Rights Reserved.
*
* Major Contributor(s):
* ---------------
* Ryan Caudy [email protected] - contributions to process groups.
* Claudiu Danilov [email protected] - scalable wide area support.
* Cristina Nita-Rotaru [email protected] - group communication security.
* Theo Schlossnagle [email protected] - Perl, autoconf, old skiplist.
* Dan Schoenblum [email protected] - Java interface.
*
*/
#include "sp.h"
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define int32u unsigned int
static char User[80];
static char Spread_name[80];
static char Private_group[MAX_GROUP_NAME];
static mailbox Mbox;
static int Num_sent;
static unsigned int Previous_len;
static int To_exit = 0;
#define MAX_MESSLEN 102400
#define MAX_VSSETS 10
#define MAX_MEMBERS 100
static void Print_menu();
static void User_command();
static void Read_message();
static void Usage( int argc, char *argv[] );
static void Print_help();
static void Bye();
int main( int argc, char *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 );
Bye();
}
printf("User: connected 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 );
E_attach_fd( Mbox, READ_FD, Read_message, 0, NULL, HIGH_PRIORITY );
Print_menu();
printf("\nUser> ");
fflush(stdout);
Num_sent = 0;
E_handle_events();
return( 0 );
}
static void User_command()
{
char command[130];
char mess[MAX_MESSLEN];
char group[80];
char groups[10][MAX_GROUP_NAME];
int num_groups;
unsigned int mess_len;
int ret;
int i;
for( i=0; i < sizeof(command); i++ ) command[i] = 0;
if( fgets( command, 130, stdin ) == NULL )
Bye();
switch( command[0] )
{
case 'j':
ret = sscanf( &command[2], "%s", group );
if( ret < 1 )
{
printf(" invalid group \n");
break;
}
ret = SP_join( Mbox, group );
if( ret < 0 ) SP_error( ret );
break;
case 'l':
ret = sscanf( &command[2], "%s", group );
if( ret < 1 )
{
printf(" invalid group \n");
break;
}
ret = SP_leave( Mbox, group );
if( ret < 0 ) SP_error( ret );
break;
case 's':
num_groups = sscanf(&command[2], "%s%s%s%s%s%s%s%s%s%s",
groups[0], groups[1], groups[2], groups[3], groups[4],
groups[5], groups[6], groups[7], groups[8], groups[9] );
if( num_groups < 1 )
{
printf(" invalid group \n");
break;
}
printf("enter message: ");
if (fgets(mess, 200, stdin) == NULL)
Bye();
mess_len = strlen( mess );
ret= SP_multigroup_multicast( Mbox, SAFE_MESS, num_groups, (const char (*)[MAX_GROUP_NAME]) groups, 1, mess_len, mess );
if( ret < 0 )
{
SP_error( ret );
Bye();
}
Num_sent++;
break;
case 'm':
num_groups = sscanf(&command[2], "%s%s%s%s%s%s%s%s%s%s",
groups[0], groups[1], groups[2], groups[3], groups[4],
groups[5], groups[6], groups[7], groups[8], groups[9] );
if( num_groups < 1 )
{
printf(" invalid group \n");
break;
}
printf("enter message: ");
mess_len = 0;
while ( mess_len < MAX_MESSLEN) {
if (fgets(&mess[mess_len], 200, stdin) == NULL)
Bye();
if (mess[mess_len] == '\n')
break;
mess_len += strlen( &mess[mess_len] );
}
ret= SP_multigroup_multicast( Mbox, SAFE_MESS, num_groups, (const char (*)[MAX_GROUP_NAME]) groups, 1, mess_len, mess );
if( ret < 0 )
{
SP_error( ret );
Bye();
}
Num_sent++;
break;
case 'b':
ret=sscanf( &command[2], "%s", group );
if( ret != 1 ) strcpy( group, "dummy_group_name" );
printf("enter size of each message: ");
if (fgets(mess, 200, stdin) == NULL)
Bye();
ret=sscanf(mess, "%u", &mess_len );
if( ret !=1 ) mess_len = Previous_len;
if( mess_len > MAX_MESSLEN ) mess_len = MAX_MESSLEN;
Previous_len = mess_len;
printf("sending 10 messages of %u bytes\n", mess_len );
for( i=0; i<10; i++ )
{
Num_sent++;
sprintf( mess, "mess num %d ", Num_sent );
ret= SP_multicast( Mbox, FIFO_MESS, group, 2, mess_len, mess );
if( ret < 0 )
{
SP_error( ret );
Bye();
}
printf("sent message %d (total %d)\n", i+1, Num_sent );
}
break;
case 'r':
Read_message();
break;
case 'p':
ret = SP_poll( Mbox );
printf("Polling sais: %d\n", ret );
break;
case 'e':
E_attach_fd( Mbox, READ_FD, Read_message, 0, NULL, HIGH_PRIORITY );
break;
case 'd':
E_detach_fd( Mbox, READ_FD );
break;
case 'q':
Bye();
break;
default:
printf("\nUnknown commnad\n");
Print_menu();
break;
}
printf("\nUser> ");
fflush(stdout);
}
static void Print_menu()
{
printf("\n");
printf("==========\n");
printf("User Menu:\n");
printf("----------\n");
printf("\n");
printf("\tj <group> -- join a group\n");
printf("\tl <group> -- leave a group\n");
printf("\n");
printf("\ts <group> -- send a message\n");
printf("\tm <group> -- send a multiline message to group. Terminate with empty line\n");
printf("\tb <group> -- send a burst of messages\n");
printf("\n");
printf("\tr -- receive a message (stuck) \n");
printf("\tp -- poll for a message \n");
printf("\te -- enable asynchonous read (default)\n");
printf("\td -- disable asynchronous read \n");
printf("\n");
printf("\tq -- quit\n");
fflush(stdout);
}
/* FIXME: The user.c code does not use memcpy()s to avoid bus errors when
* dereferencing a pointer into a potentially misaligned buffer */
static void Read_message()
{
static char mess[MAX_MESSLEN];
char sender[MAX_GROUP_NAME];
char target_groups[MAX_MEMBERS][MAX_GROUP_NAME];
membership_info memb_info;
vs_set_info vssets[MAX_VSSETS];
unsigned int my_vsset_index;
int num_vs_sets;
char members[MAX_MEMBERS][MAX_GROUP_NAME];
int num_groups;
int service_type;
int16 mess_type;
int endian_mismatch;
int i,j;
int ret;
service_type = 0;
ret = SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups,
&mess_type, &endian_mismatch, sizeof(mess), mess );
printf("\n============================\n");
if( ret < 0 )
{
if ( (ret == GROUPS_TOO_SHORT) || (ret == BUFFER_TOO_SHORT) ) {
service_type = DROP_RECV;
printf("\n========Buffers or Groups too Short=======\n");
ret = SP_receive( Mbox, &service_type, sender, MAX_MEMBERS, &num_groups, target_groups,
&mess_type, &endian_mismatch, sizeof(mess), mess );
}
}
if (ret < 0 )
{
if( ! To_exit )
{
SP_error( ret );
printf("\n============================\n");
printf("\nBye.\n");
}
exit( 0 );
}
if( Is_regular_mess( service_type ) )
{
mess[ret] = 0;
if ( Is_unreliable_mess( service_type ) ) printf("received UNRELIABLE ");
else if( Is_reliable_mess( service_type ) ) printf("received RELIABLE ");
else if( Is_fifo_mess( service_type ) ) printf("received FIFO ");
else if( Is_causal_mess( service_type ) ) printf("received CAUSAL ");
else if( Is_agreed_mess( service_type ) ) printf("received AGREED ");
else if( Is_safe_mess( service_type ) ) printf("received SAFE ");
printf("message from %s, of type %d, (endian %d) to %d groups \n(%d bytes): %s\n",
sender, mess_type, endian_mismatch, num_groups, ret, mess );
}else if( Is_membership_mess( service_type ) )
{
ret = SP_get_memb_info( mess, service_type, &memb_info );
if (ret < 0) {
printf("BUG: membership message does not have valid body\n");
SP_error( ret );
exit( 1 );
}
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( 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( Is_caused_join_mess( service_type ) )
{
printf("Due to the JOIN of %s\n", memb_info.changed_member );
}else if( Is_caused_leave_mess( service_type ) ){
printf("Due to the LEAVE of %s\n", memb_info.changed_member );
}else if( Is_caused_disconnect_mess( service_type ) ){
printf("Due to the DISCONNECT of %s\n", memb_info.changed_member );
}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( mess, &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(mess, &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, mess );
}else printf("received message of unknown message type 0x%x with ret %d\n", service_type, ret);
printf("\n");
printf("User> ");
fflush(stdout);
}
static void Usage(int argc, char *argv[])
{
sprintf( User, "user" );
sprintf( Spread_name, "10330");
while( --argc > 0 )
{
argv++;
if( !strncmp( *argv, "-u", 2 ) )
{
if (argc < 2) Print_help();
strcpy( User, argv[1] );
argc--; argv++;
}else if( !strncmp( *argv, "-r", 2 ) )
{
strcpy( User, "" );
}else if( !strncmp( *argv, "-s", 2 ) ){
if (argc < 2) Print_help();
strcpy( Spread_name, argv[1] );
argc--; argv++;
}else{
Print_help();
}
}
}
static void Print_help()
{
printf( "Usage: spuser\n%s\n%s\n%s\n",
"\t[-u <user name>] : unique (in this machine) user name",
"\t[-s <address>] : either port or port@machine",
"\t[-r ] : use random user name");
exit( 0 );
}
static void Bye()
{
To_exit = 1;
printf("\nBye.\n");
SP_disconnect( Mbox );
exit( 0 );
}