void like_msg(int like, int ul) { int ret; struct chat_packet *c = (struct chat_packet *)calloc(1,sizeof(struct chat_packet)); struct node *i = chatroom_start->next; char server_group[1]; if (like > line_number) { printf("Nonexistent line\n> "); return; } sprintf(server_group, "%d", connected); /*Convert server id to string for spread groupname*/ while (i->sequence != like) (i=i->next); if (strlen(chatroom) > 1) { if (ul == 1) { c->type = 1;//Chat like type } else c->type = 7; // Unlike c->server_id = connected; strncpy(c->name, username, strlen(username)); strncpy(c->group, chatroom, strlen(chatroom)); c->lts = i->data->sequence; /* Send Message */ ret = SP_multicast(Mbox, AGREED_MESS, server_group, 2, sizeof(struct chat_packet), (char *) c); if( ret < 0 ) { SP_error( ret ); } } else { printf("Sorry, you must join a chat room to like a message.\n"); } }
void main() { int ret; E_init(); system("clear"); sp_time test_timeout; test_timeout.sec = 5; test_timeout.usec = 0; strncpy(Spread_name, "10080", 5); ret = SP_connect_timeout( Spread_name, User, 0, 1, &Mbox, Private_group, test_timeout ); if( ret != ACCEPT_SESSION ) { SP_error( ret ); Bye(); } printf("Connected to %s with private group %s\n", Spread_name, Private_group ); E_attach_fd( 0, READ_FD, User_command, 0, NULL, LOW_PRIORITY ); E_attach_fd( Mbox, READ_FD, Read_message, 0, NULL, HIGH_PRIORITY ); show_menu(); for(;;) { E_handle_events(); User_command(); } exit (0); }
static void Turn_mature(int __code, void *__data) { int ret; ret = SP_multicast( Mbox, AGREED_MESS, Spread_group, MATURING_MESS, 0, 0 ); if( ret < 0 ) { SP_error( ret ); Spread_reconnect(ret); } wack_alarm(LOGIC, "Turn_mature"); }
void Spread_reconnect(int ret){ if(ret != -8 && ret != -11 && ret != -2) wack_alarm(EXIT, "Spread_reconnnect: Unexpected Error (%d)", ret); wack_alarm(PRINT,"connecting to %s", Spread_name); Clean_up(); if(Mbox >= 0) { SP_disconnect(Mbox); E_detach_fd( Mbox, READ_FD ); Mbox = -1; } /* connecting to the relevant Spread daemon, asking for group info */ if(spread_lock) { handle_reconnect(0, NULL); return; } ret = SP_connect( Spread_name, User, 0, 1, &Mbox, Private_group ) ; if(ret == ACCEPT_SESSION) { ret = SP_join( Mbox, Spread_group ); if( ret < 0 ) { SP_error( ret ); SP_disconnect( Mbox ); Mbox = -1; wack_alarm(PRINT, "Spread join on reconnect failed [%d].", ret); handle_reconnect(0, NULL); return; } } else { wack_alarm(PRINT, "Spread connect failed [%d].", ret); handle_reconnect(0, NULL); return; } /* State initializations */ State = BOOT; Old_maturity = 0; Maturity = 0; E_queue( Turn_mature, 0, 0, Maturity_timeout ); My.num_allocated = 0; strcpy(My.private_group_name, Private_group); E_attach_fd( Mbox, READ_FD, Handle_network, 0, NULL, HIGH_PRIORITY ); E_set_active_threshold( HIGH_PRIORITY ); }
void show_servers() { int ret; struct chat_packet *c; char server_group[1]; c = (struct chat_packet *)calloc(1,sizeof(struct chat_packet)); c->type = 8;//Request online server list. c->server_id = connected; strncpy(c->name, username, strlen(username)); strncpy(c->client_group, Private_group, MAX_GROUP_NAME); sprintf(server_group, "%d", connected); /*Convert server id to string for spread groupname*/ c->sequence = 0; //Server updates this /* Send Message */ ret = SP_multicast(Mbox, AGREED_MESS, server_group, 2, sizeof(struct chat_packet), (char *) c); if( ret < 0 ) { SP_error( ret ); } }
void join_server(char *server_id) { int ret; struct chat_packet *c; struct chat_packet *r; 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=0; int16 mess_type; int endian_mismatch; char group[80]; c = (struct chat_packet *)calloc(1,sizeof(struct chat_packet)); r = (struct chat_packet *)calloc(1,sizeof(struct chat_packet)); c->type = 2;//Request to join server strncpy(c->text, Private_group, strlen(Private_group)); c->server_id = atoi(server_id); //printf("Sending private grup %s to server", Private_group); strncpy(c->client_group, Private_group, MAX_GROUP_NAME); /* Send Message */ if (connected > 0) { /*Leave current group*/ sprintf(group, "c%d", connected); SP_leave(Mbox, group); } /* ret = SP_multicast(Mbox, AGREED_MESS, server_id, 2, sizeof(struct chat_packet), (char *)c); */ /* That was the old way. New way is join the server group */ sprintf(group, "c%d", atoi(server_id)); ret = SP_join(Mbox, group); connected = atoi(server_id); /*Set connected. Note we still need response from server, but we need to pass this to the recv connect */ if( ret < 0 ) { SP_error( ret ); } }
void join_room(char *group) { int ret; struct chat_packet *c; char server_group[1]; sprintf(server_group, "%d", connected); /*Convert server id to string for spread groupname*/ //c = malloc(sizeof(struct chat_packet)); // //bzero(c, sizeof(struct chat_packet)); c = (struct chat_packet *) calloc(1, sizeof(struct chat_packet)); /*better than malloc, zeros out memory*/ if ( group == "1" || group == "2" || group == "3" || group == "4" || group == "5") { printf("Chatrooms cannot be 1-5. These are reserved\n>"); } else if (strlen(username) < 1) { printf("Please set your username before joining a chat room\n>"); } else { /*Disjoin from current group */ if (strlen(chatroom) > 0) { printf("leaving chatroom, %s\n", chatroom); sprintf(chatroom, "%s%d",chatroom, connected); SP_leave(Mbox, chatroom); } //printf("chatroom length = %d\n", strlen(chatroom)); /* Send request to server to join a group */ strncpy(c->client_group, Private_group, MAX_GROUP_NAME); strncpy(c->name, username, strlen(username)); //printf("Sending join with %s", Private_group); strncpy(c->group, group, strlen(group)); c->type = 5; ret = SP_multicast(Mbox, AGREED_MESS, server_group, 2, sizeof(struct chat_packet), (char *)c); if( ret < 0 ) { SP_error( ret ); } } }
int main( int argc, char *argv[] ) { int ret; int mver, miver, pver; sp_time test_timeout; test_timeout.sec = 5; test_timeout.usec = 0; server_num = 0; set_max_msgs(25); 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, NULL, 0, 1, &Mbox, Private_group, test_timeout ); if( ret != ACCEPT_SESSION ) { SP_error( ret ); Bye(); } printf("Client: 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("\n > "); fflush(stdout); E_handle_events(); return( 0 ); }
void send_msg(char *mtext) { int ret; struct chat_packet *c; char group[80]; c = (struct chat_packet *)calloc(1,sizeof(struct chat_packet)); char server_group[1]; sprintf(server_group, "%d", connected); /*Convert server id to string for spread groupname*/ if (strlen(chatroom) > 1 && connected > 0 && strlen(username) >0) { //printf("Sending %s to group %s\n", mtext, chatroom); c->type = 0;//Chat message type strncpy(c->text, mtext, strlen(mtext)); c->server_id = connected; strncpy(c->group, chatroom, strlen(chatroom)); strncpy(c->name, username, strlen(username)); c->sequence = 0; //Server updates this //c->resend = 0; //Not sure we need this anymore /* Send Message */ //printf("Sending to group %s\n", server_group); ret = SP_multicast(Mbox, AGREED_MESS, server_group, 2, sizeof(struct chat_packet), (char *)c); if( ret < 0 ) { SP_error( ret ); } } else { if (strlen(chatroom) <= 1) { printf("Sorry, you must join a chat room to send a message.\n"); } else if (strlen(username) < 1) { printf("Please set your username before sending a message.\n"); } else { printf("Please connect to a server before trying to send a message.\n"); } } }
static void Send_local_arp_cache(int __code, void *__data) { int ret, num_bytes=0; arp_entry *addresses, *s; arp_entry *d = (arp_entry *)Mess; sample_arp_cache(); s = addresses = reference_private_arp_cache(); while(s->ip && (char *)d < &Mess[MAX_MESS_LEN-sizeof(arp_entry)]) { memcpy(d, s, sizeof(arp_entry)); d++; s++; num_bytes += sizeof(arp_entry); } wack_alarm(WACK_DEBUG, "Sending %d local arp entries", num_bytes/sizeof(arp_entry)); ret = SP_multicast( Mbox, RELIABLE_MESS, Spread_group, ARPCACHE_MESS, num_bytes, Mess ); if( ret < 0 ) { SP_error( ret ); Spread_reconnect(ret); } }
void Handle_network(int __unused1, int __unused2, void *__unused3) { int ret; ret = SP_receive( Mbox, &Service_type, Sender, MAX_PSEUDO, &Num_groups, Target_groups, &Mess_type, &Endian_mismatch, MAX_MESS_LEN, Mess ); if( ret < 0 ) { SP_error( ret ); Spread_reconnect(ret); } if( Is_membership_mess( Service_type ) ) { if( Is_reg_memb_mess( Service_type ) ) { Handle_membership(); Send_local_arp_cache(0, NULL); }else{ /* Ignore Transitional membership */ } }else if( Is_regular_mess( Service_type ) ){ /* Handle regular messages */ if ( Mess_type == STATE_MESS ) Handle_state(); else if ( Mess_type == MATURING_MESS ) Handle_mature(); else if ( Mess_type == BALANCE_MESS ) Handle_balance(); else if ( Mess_type == OPERATOR_MESS ) Handle_operator(); else if ( Mess_type == ARPCACHE_MESS ) Handle_arp_cache((arp_entry *)Mess,ret); /* Ignore messages of other types */ }else{ wack_alarm(EXIT,"Error: received message of unknown message type %d with ret %d", Service_type, ret ); } }
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 ); #ifdef _REENTRANT #ifdef __bsdi__ /* bsdi bug - doing a close when another thread blocks on the socket causes a seg fault */ ret = send( Mbox, mess, 0, 0 ); if( ret < 0 ) { SP_error( CONNECTION_CLOSED ); Bye(); } #endif /* __bsdi__ */ #endif /* _REENTRANT */ 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 ); #ifdef _REENTRANT #ifdef __bsdi__ /* bsdi bug - doing a close when another thread blocks on the socket causes a seg fault */ ret = send( Mbox, mess, 0,0 ); if( ret < 0 ) { SP_error( CONNECTION_CLOSED ); Bye(); } #endif /* __bsdi__ */ #endif /* _REENTRANT */ 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; #ifndef _REENTRANT 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; #endif /* _REENTRANT */ case 'q': Bye(); break; default: printf("\nUnknown commnad\n"); Print_menu(); break; } printf("\nUser> "); fflush(stdout); }
int main( int argc, char *argv[] ) { int ret; #ifdef SPREAD_VERSION int mver, miver, pver; #endif sp_time test_timeout; test_timeout.sec = 5; test_timeout.usec = 0; Usage( argc, argv ); #ifdef SPREAD_VERSION 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); #else printf("Spread library version is %1.2f\n", SP_version() ); #endif #ifdef ENABLE_PASSWORD if (Use_Pword) { strncpy(Pword_user.username, Pword_username, 32); Pword_user.username[32] = '\0'; strncpy(Pword_user.password, Pword_password, 8); Pword_user.password[8] = '\0'; SP_set_auth_method("PWORD", pword_authenticate, &Pword_user); } #endif 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 ); #ifndef _REENTRANT 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 ); #endif /* _REENTRANT */ Print_menu(); printf("\nUser> "); fflush(stdout); Num_sent = 0; #ifdef _REENTRANT #ifndef ARCH_PC_WIN95 ret = pthread_create( &Read_pthread, NULL, Read_thread_routine, 0 ); #else /* ARCH_PC_WIN95 */ Read_pthread = CreateThread( NULL, 0, Read_thread_routine, NULL, 0, &ret ); #endif /* ARCH_PC_WIN95 */ for(;;) { User_command(); #ifdef __bsdi__ /* bug in BSDI */ sched_yield(); #endif } #else /* _REENTRANT */ #ifndef ARCH_PC_WIN95 E_handle_events(); #else /* ARCH_PC_WIN95 */ for(;;) User_command(); #endif /* ARCH_PC_WIN95 */ #endif /* _REENTRANT */ return( 0 ); }
int main(int argc, char **argv) { int c; char port[6] = SPLOGGER_PORT; char *group_name = NULL; int16 code = 0; opterr = 0; while ((c = getopt(argc, argv, "hg:p:c:")) != -1) { switch (c) { case 'g': group_name = malloc(MAX_GROUP_NAME); strncpy(group_name, optarg, MAX_GROUP_NAME - 1); break; case 'p': strncpy(port, optarg, 5); break; case 'c': code = (int16) atoi(optarg); break; case '?': if (optopt == 'g') fprintf(stderr, "Option -g requires an argument.\n"); else if (optopt == 'p') fprintf(stderr, "Option -p requires an argument.\n"); else if (isprint(optopt)) fprintf(stderr, "Unknown option `-%c'.\n", optopt); else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); exit(EXIT_FAILURE); default: abort(); } } if (group_name == NULL) { fprintf(stderr, "Error: you must specify a group with -g.\n"); exit(EXIT_FAILURE); } int ret; mailbox mbox; char pgroupname[MAX_GROUP_NAME]; /* Connect on 127.0.0.1:4803 */ ret = SP_connect(port, NULL, 0, 0, &mbox, pgroupname); if (ret != ACCEPT_SESSION) { SP_error(ret); return -1; } /* Open the file */ int input_fd = 0; /* Default is stdin */ if (optind < argc) { input_fd = open(argv[optind], O_RDONLY); if (input_fd < 0) { perror("open()"); exit(EXIT_FAILURE); } } /* Read in the buffer */ int bytes_read = 0; char buf[BUFSIZE]; while (1) { ret = read(input_fd, buf + bytes_read, BUFSIZE - bytes_read); if (ret == 0) { close(input_fd); break; } bytes_read += ret; } /* Send out the message */ ret = SP_multicast(mbox, FIFO_MESS | SELF_DISCARD, group_name, code, bytes_read, buf); /* Disconnect */ ret = SP_disconnect(mbox); if (ret) { SP_error(ret); return -1; } return 0; }
int main( int argc, char *argv[] ) { int ret; int service_type, num_groups; char sender[MAX_GROUP_NAME]; int16 mess_type; int dummy_endian_mismatch; int joined_members; int sender_index; int i,j; Usage( argc, argv ); if (Num_members == 0) { /* connecting to the relevant Spread daemon, no need for group info */ printf("flooder: connecting to %s\n", Spread_name ); ret = SP_connect( Spread_name, User, 0, 0, &Mbox, Private_group ) ; } else { /* connecting to the relevant Spread daemon, DO need group info */ printf("flooder: connecting to %s with group membership\n", Spread_name ); ret = SP_connect( Spread_name, User, 0, 1, &Mbox, Private_group ) ; } if(ret < 0) { SP_error( ret ); exit(1) ; } /* * Joining the process group. * * Note that this is not necessary in order to multicast the * messages, but only to demonstrate end-to-end behaviour. */ if( Read_only ) { printf("flooder: Only receiving messages\n"); SP_join( Mbox, "flooder" ); }else if( Write_only ) { printf("flooder: starting multicast of %d messages, %d bytes each (self discarding).\n", Num_messages, Num_bytes); }else{ SP_join( Mbox, "flooder" ); printf("flooder: starting multicast of %d messages, %d bytes each.\n", Num_messages, Num_bytes); } /* Wait for all members to join */ joined_members = 0; while( joined_members < Num_members) { service_type = 0; ret = SP_receive( Mbox, &service_type, sender, FLOODER_MAX_GROUPS, &num_groups, ret_groups, &mess_type, &dummy_endian_mismatch, sizeof(recv_mess), recv_mess ); if( ret < 0 ) { if ( (ret == GROUPS_TOO_SHORT) || (ret == BUFFER_TOO_SHORT) ) { printf("\n========Buffers or Groups too Short=======\n"); printf("Should NOT happen in wait for members! Program quitting\n"); exit(1); } } 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 during wait for members, from %s, of type %d, (endian %d) to %d groups \n(%d bytes): %s\n", sender, mess_type, dummy_endian_mismatch, num_groups, ret, recv_mess ); }else if( Is_membership_mess( service_type ) ) { 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", &ret_groups[i][0] ); /* update count of joined members */ joined_members = num_groups; }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, dummy_endian_mismatch, num_groups, ret, recv_mess ); }else printf("received message of unknown message type 0x%x with ret %d\n", service_type, ret); } /* joined_members < Num_members */ /* Update My_Counter_index field based on location of my name in last membership message */ if (Num_members) { My_Counter_index = mess_type; memcpy(&mess[0], &My_Counter_index, sizeof(int)); } for( i=1; i <= Num_messages; i++ ) { /* multicast a message unless Read_only */ if( !Read_only ) { if (Num_members) { ret = SP_multicast( Mbox, FIFO_MESS, "flooder", 0, Num_bytes, mess ); Send_Counter++; } else { ret = SP_multicast( Mbox, RELIABLE_MESS, "flooder", 0, Num_bytes, mess ); } if( ret != Num_bytes ) { if( ret < 0 ) { SP_error( ret ); exit(1); } printf("sent a different message %d -> %d\n", Num_bytes, ret ); } } /* receive a message (Read_only) or one of my messages */ if( Read_only || ( i > 200 && !Write_only ) ) { int notdone; do{ service_type = 0; ret = SP_receive( Mbox, &service_type, sender, FLOODER_MAX_GROUPS, &num_groups, ret_groups, &mess_type, &dummy_endian_mismatch, sizeof(recv_mess), recv_mess ); 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, FLOODER_MAX_GROUPS, &num_groups, ret_groups, &mess_type, &dummy_endian_mismatch, sizeof(recv_mess), recv_mess ); } } if( ret < 0 ) { SP_error( ret ); exit(1); } if (Num_members) { /* update counters of received messages */ memcpy(&sender_index, &recv_mess[0], sizeof(int)); Recv_Counters[sender_index]++; /* printf("DEBUG: updated counter %d to value %d\n", sender_index, Recv_Counters[sender_index]); */ if (Recv_Counters[sender_index] == (Lowest_Recv_Counter + 1)) { /* Update Lowest_Recv_Counter */ Lowest_Recv_Counter = INT_MAX; for (j=0; j < Num_members; j++) { if (Recv_Counters[j] == 0) continue; if (Recv_Counters[j] < Lowest_Recv_Counter) Lowest_Recv_Counter = Recv_Counters[j]; } if (Lowest_Recv_Counter == INT_MAX) Lowest_Recv_Counter = 0; } /* Read loop is done if we send messages and we have received all * other senders messages upto 100 less then our current send count */ notdone = ( Lowest_Recv_Counter < (Send_Counter - 200) && !Read_only ); } else { notdone = (strcmp( sender, Private_group ) != 0 && !Read_only); } } while( notdone ); } /* report some progress... */ if( i%1000 == 0 ) printf("flooder: completed %6d messages of %d bytes\n",i, ret); } printf("flooder: completed multicast of %d messages, %d bytes each.\n", Num_messages, Num_bytes); return 0; }
static void Balance(int __code, void *__data) { int ret; int num_bytes; int i, j, k; int min_index; int max_index; static member *tempMembers = NULL; static entry *tempTable = NULL; wack_alarm(LOGIC, "Balance called." ); if(!tempMembers) { tempMembers = (member *)malloc(sizeof(member) * MAX_PSEUDO); } if(!tempTable) { tempTable = (entry *)malloc(sizeof(entry) * MAX_PSEUDO); } if( My_index ) goto leave; if( State != RUN ) return; State = BALANCE; num_bytes = sizeof( int ); min_index = 0; max_index = 0; memcpy( tempMembers, Members, MAX_PSEUDO * sizeof( member ) ); memcpy( tempTable, Allocation_table, MAX_PSEUDO * sizeof( entry ) ); for ( k = 0; k < Balance_rate; k++ ) { for ( i = 0; i < Num_members; i++ ) { if( tempMembers[i].num_allocated < tempMembers[min_index].num_allocated ) min_index = i; if( tempMembers[i].num_allocated > tempMembers[max_index].num_allocated ) max_index = i; } if( tempMembers[max_index].num_allocated - tempMembers[min_index].num_allocated > 1 ) { for( j = 0; j < Num_pseudo; j++ ) { if( _rif_ip_s(tempTable[j]) == _rif_ip_s(tempMembers[max_index]) && tempTable[j].claim_priority < PREFER ) { memcpy( &Mess[num_bytes], &(_rif_ip_s(tempMembers[min_index])), sizeof( address ) ); num_bytes += sizeof( address ); memcpy( &Mess[num_bytes], &(_pif_ip_s(tempTable[j])), sizeof( address ) ); num_bytes += sizeof( address ); tempMembers[min_index].num_allocated++; tempMembers[max_index].num_allocated--; _rif_ip_s(tempTable[j]) = _rif_ip_s(tempMembers[min_index]); j = Num_pseudo; } else if ( j == Num_pseudo ) { if( k == 0 ) { State = RUN; goto leave; } memcpy( &Mess[0], &k, sizeof( int ) ); k = Balance_rate + 1; } } } else { if( k == 0 ) { State = RUN; goto leave; } memcpy( &Mess[0], &k, sizeof( int ) ); k = Balance_rate + 1; } } if ( k == Balance_rate ) memcpy( &Mess[0], &k, sizeof( int ) ); ret = SP_multicast( Mbox, AGREED_MESS, Spread_group, BALANCE_MESS, num_bytes, Mess ); if( ret < 0 ) { SP_error( ret ); Spread_reconnect(ret); } State = RUN; wack_alarm(LOGIC, "Shifting to RUN in balance" ); leave: if( !Complete_balance ) E_queue( Balance, 0, 0, Balance_timer ); return; }
void recv_server_msg(struct chat_packet *c, int16 mess_type) { int ret; struct node *temp, *temp2; if (mess_type == 0) { // If the mess_type is 0, that means we already have the chat_packet, we just want to update the likes. temp = chatroom_start->next; while (temp != NULL) { if (temp->data->sequence == c->sequence) { temp->data->num_likes = c->num_likes; break; } temp = temp->next; } refresh_display(); } else if (mess_type == 13) { temp = chatroom_start; int count= 0; /* We need to put the merged chats in the correct order */ while (temp->next != NULL) { temp->next->sequence = count;/* for resequencing*/ if (c->sequence == temp->next->data->sequence) { /*already received this LTS, so don't add it. */ break; } else if (c->sequence < temp->next->data->sequence) { temp2 = temp->next; temp->next = (struct node *)calloc(1,sizeof(struct node)); temp->next->data = (struct chat_packet *)calloc(1, sizeof(struct chat_packet)); memcpy(temp->next->data, c, sizeof(struct chat_packet)); temp->next->sequence = count; temp->next->previous = temp; /*Double link list added for traversal on 25 chat lines */ temp2->previous = temp->next; temp->next->next = temp2; break; } temp = temp->next; } if (temp->next == NULL) /*We are at the end, so add the message */ { temp2 = temp->next; temp->next = (struct node *)calloc(1,sizeof(struct node)); temp->next->data = (struct chat_packet *)calloc(1,sizeof(struct chat_packet)); memcpy(temp->next->data, c, sizeof(struct chat_packet)); temp->next->sequence = count; temp->next->previous = temp; /*Double link list added for traversal on 25 chat lines */ temp->next->next = temp2; chatroom_latest = temp->next; /*set latest to this new packet */ } /*Renumber */ temp = chatroom_start; while (temp->next !=NULL) { count++; temp->next->sequence = count; temp = temp->next; } line_number = count; refresh_display(); //print_after(c->sequence); /*Print out where we added the packet (for dispaly consistency)*/ } else if (c->type == 0 || c->type == 3) /*Message packet */ { if (c->type == 0) line_number++; /*Only update line number on text message */ chatroom_latest->next = (struct node *)calloc(1,sizeof(struct node)); chatroom_latest->next->previous = chatroom_latest; chatroom_latest = chatroom_latest->next; /*Advance the pointer */ chatroom_latest->data = (struct chat_packet *)calloc(1,sizeof(struct chat_packet)); chatroom_latest->sequence = line_number; memcpy(chatroom_latest->data, c, sizeof(struct chat_packet)); if (c->type == 0) /* Live message, display it */ { refresh_display(); } } else if (c->type == 2) { printf("Successful connection to server %d\n>", c->server_id); connected = c->server_id; } else if (c->type == 5) /* Received response to join group */ { ret = SP_join( Mbox, c->group ); if( ret < 0 ) SP_error( ret ); bzero(chatroom, MAX_GROUP_NAME); strncpy(chatroom, c->group, strlen(c->group)-1); /*Remove server id from chatroom group name */ chatroom_start = (struct node *)calloc(1, sizeof(struct node)); chatroom_latest = chatroom_start; printf("Successfully joined group %s\n>", chatroom); /* don't display server index at end of group */ line_number = 0; /*Refresh line number */ } else if (c->type == 6) /*Refresh screen all after lamport timestamp */ /*No longer necessary*/ { struct node *i; i = chatroom_start->next; /* Setup iterator */ line_number = 0; while ((i !=NULL) && ((i->data->sequence != c->sequence) || (i->data->server_id != c->server_id))) { line_number++; i = i->next; } while (i != NULL) { line_number++; printf("%d:%s> %s",line_number, i->data->name, i->data->text); } } else if (c->type == 8) /*Display servers online */ { printf("Servers that are online are: %s\n>", c->text); } else if (c->type == 9) /*Updated name list for chatroom */ { bzero(attendees, 80); strncpy(attendees, c->text, strlen(c->text)); //printf("Members in chatroom are now: %s\n>", c->text); Moved to display. refresh_display(); } }
static void User_command() { char command[130]; char client_group[13]; char mess[SPREAD_MESS_LEN]; char group[MAX_ROOM_LEN - strlen("CR#_")]; char groups[10][MAX_GROUP_NAME]; int num_groups; unsigned int mess_len; int ret; int srv; int i; 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", u_id); if (ret < 1 ) printf("invalid username\n"); break; case 'c': ret = sscanf( &command[2], "%d", &srv); if ( ret < 1 || isValidServer(srv) != 1) { //isValid sets server_num printf("invalid server index\n"); break; } ret = sprintf( client_group, "client_group%d",server_num); if (ret < 1) { printf("Error connecting to server-client group\n"); break; } ret = SP_join(Mbox, client_group); if (ret < 0) SP_error( ret ); sprintf( server_priv_group, "server%d", server_num); break; case 'j': //check if already in room if (strlen(chatroom) > 0) { clear_room(chatroom); } else { ret = sscanf( &command[2], "%s", chatroom ); if( ret < 1 ) { printf(" invalid chatroom name \n"); break;} add_room(chatroom); } if (strlen(u_id) > 0 && isValidServer(server_num)) { //need valid username + server for chatroom ret = sprintf ( group, "%s%d_%s","CR",server_num,chatroom); if (ret < 1) { printf("Error creating chatroom name <%s> from group <%s>\n",chatroom,group); break; } ret = SP_join( Mbox, group ); //join CR#_<group> spread group if( ret < 0 ) SP_error( ret ); } else { printf("Invalid username or server number, cannot join chatroom\n"); break;} join_chatroom* send = (join_chatroom*)mess; send->type = TYPE_JOIN_CHATROOM; send->source = SOURCE_CLIENT; strncpy(send->u_id,u_id,MAX_NAME_LEN); strncpy(send->chatroom,chatroom,MAX_ROOM_LEN); //send <group> as chatroom spread name ret = SP_multicast( Mbox, AGREED_MESS, server_priv_group, 1, sizeof(join_chatroom), mess ); if( ret < 0 ) { SP_error( ret ); Bye(); } break; /* case 'x':{ char ms[6]; add_room(chatroom); set_max_msgs(25); int maxi = 40; for (int i = 1; i <= maxi;i++) { struct lamport* ts = malloc(sizeof(lamport)); ts->server_id = 1; ts->index = i; sprintf(ms,"%s%d","asdfd",i); add_message(ts,u_id,chatroom,ms); if (i % 3 == 0 || i % 4 == 1 || i % 6 == 2) { i++; maxi++; struct lamport* ls = malloc(sizeof(lamport)); ls->server_id = 1; ls->index = i; add_like(LIKE,ls,u_id,chatroom,ts); if (i % 6 == 2){ i++;maxi++; char newu[MAX_NAME_LEN]; sprintf(newu,"%sx",u_id); struct lamport* qs = malloc(sizeof(lamport)); qs->server_id = 1; qs->index = i; add_like(LIKE,qs,newu,chatroom,ts); } } if (i % 4 == 1) { i++; maxi++; struct lamport* ls = malloc(sizeof(lamport)); ls->server_id = 1; ls->index = i; add_like(UNLIKE,ls,u_id,chatroom,ts); } } printf("== %s ==\n",chatroom); print_room(chatroom); break;} */ case 'a': { char text[MAX_MESS_LEN]; printf("enter message: "); if (fgets(text, MAX_MESS_LEN, stdin) == NULL) Bye(); if (strlen(chatroom) < 1){ printf("Please join a chatroom\n"); break; } message* dat = (message*)mess; dat->type = TYPE_SEND_MSG; dat->source = SOURCE_CLIENT; strncpy(dat->u_id,u_id,MAX_NAME_LEN); strncpy(dat->chatroom,chatroom,MAX_ROOM_LEN); strncpy(dat->mess,text,MAX_MESS_LEN); ret= SP_multicast( Mbox, AGREED_MESS, server_priv_group, 1, sizeof(message), mess ); if( ret < 0 ) { SP_error( ret ); Bye(); } break; } case 'l': { if (strlen(chatroom) < 1){ printf("Please join a chatroom\n"); break;} int linenum; ret = sscanf( &command[2], "%d", &linenum ); printf("Liking line %d.....\n",linenum); if( ret < 1 ) { printf(" invalid linenum \n"); break;} lamport* stamp = get_msg_stamp(linenum); printf("%d. index %d?\n",linenum,stamp->index); like* dat = (like*)mess; dat->type = TYPE_LIKE_MSG; dat->source = SOURCE_CLIENT; dat->msg_timestamp.server_id = stamp->server_id; dat->msg_timestamp.index = stamp->index; strncpy(dat->u_id,u_id,MAX_NAME_LEN); strncpy(dat->chatroom,chatroom,MAX_ROOM_LEN); dat->like_state = LIKE; ret = SP_multicast( Mbox, AGREED_MESS, server_priv_group, 1, sizeof(like), mess); if (ret < 0) { SP_error(ret); Bye; } break; } case 'r': { if (strlen(chatroom) < 1){ printf("Please join a chatroom\n"); break;} int linenum; ret = sscanf( &command[2], "%d", linenum ); if( ret < 1 ) { printf(" invalid linenum \n"); break;} lamport* stamp = get_msg_stamp(linenum); printf("%d. index %d?\n",linenum,stamp->index); like* dat = (like*)mess; dat->type = TYPE_LIKE_MSG; dat->source = SOURCE_CLIENT; dat->msg_timestamp.server_id = stamp->server_id; dat->msg_timestamp.index = stamp->index; strncpy(dat->u_id,u_id,MAX_NAME_LEN); strncpy(dat->chatroom,chatroom,MAX_ROOM_LEN); dat->like_state = UNLIKE; ret = SP_multicast( Mbox, AGREED_MESS, server_priv_group, 1, sizeof(like), mess); if (ret < 0) { SP_error(ret); Bye; } break; } case 'h': printf("history not implemented\n"); break; case 'v': printf("network view not implemented\n"); break; case 'q': Bye(); break; default: printf("\nUnknown commnad\n"); Print_menu(); break; } printf(" \n > "); fflush(stdout); }
static void Read_message() { static char mess[SPREAD_MESS_LEN]; 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 ); 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 ) ) { //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 ); typecheck* type = (typecheck*)mess; if (type->type == TYPE_JOIN_CHATROOM) { join_chatroom* dat = (join_chatroom*)mess; ret = add_user(dat->u_id,dat->sp_id,dat->chatroom); } else if (type->type == TYPE_LEAVE_CHATROOM) { leave_chatroom* dat = (leave_chatroom*)mess; rm_user(dat->sp_id); } else if (type->type == TYPE_SEND_MSG) { message* dat = (message*)mess; if (strlen(chatroom) == 0) add_room(chatroom); lamport* ts = malloc(sizeof(lamport)); ts->server_id = dat->timestamp.server_id; ts->index = dat->timestamp.index; printf("[%d,%d]\n",ts->server_id,ts->index); add_message(ts, dat->u_id, dat->chatroom, dat->mess); } else if (type->type == TYPE_LIKE_MSG) { like* dat = (like*)mess; lamport* lts = malloc(sizeof(lamport)); lamport* mts = malloc(sizeof(lamport)); lts->server_id = dat->timestamp.server_id; lts->index = dat->timestamp.index; mts->server_id = dat->timestamp.server_id; mts->index = dat->timestamp.index; ret = add_like(dat->like_state, lts, dat->u_id, chatroom, mts); printf("~~~~~~~~ %d:[%d,%d | %d,%d] %d %s %s \n",ret, lts->server_id,lts->index,mts->server_id,mts->index,dat->like_state,dat->u_id,chatroom); } master_print(); }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"); fflush(stdout); }
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 Send_state_message() { int num_bytes; int ret; int i; /* * A State message looks like this: * Gid * real_address * index * maturity * number of pseudo * pseudo * claim_priority * number of preferred pseudo * pseudo */ wack_alarm(LOGIC, "sending state");/*debug*/ Print_alloc(); num_bytes = 0; memcpy( &Mess[num_bytes], &Gid, sizeof( group_id ) ); num_bytes += sizeof( group_id ); memcpy( &Mess[num_bytes], &(_rif_ip_s(My)), sizeof( address ) ); num_bytes += sizeof( address ); memcpy( &Mess[num_bytes], &My_index, sizeof( int ) ); num_bytes += sizeof( int ); memcpy( &Mess[num_bytes], &Maturity, sizeof( int32 ) ); num_bytes += sizeof( int32 ); memcpy( &Mess[num_bytes], &My.num_allocated, sizeof( int32 ) ); num_bytes += sizeof( int ); ret=0; for ( i = 0; i < Num_pseudo; i++ ) { if ( _rif_ip_s(Old_table[i]) == _rif_ip_s(My) ) { memcpy( &Mess[num_bytes], &(_pif_ip_s(Old_table[i])), sizeof( address ) ); num_bytes += sizeof( address ); memcpy( &Mess[num_bytes], &(Old_table[i].claim_priority), sizeof( int ) ); num_bytes += sizeof( int ); ret++; } } assert(ret == My.num_allocated); memcpy( &Mess[num_bytes], &Num_prefer, sizeof( int ) ); num_bytes += sizeof( int ); for ( i = 0; i < Num_prefer; i++ ) { memcpy( &Mess[num_bytes], &Prefer_address[i], sizeof( address ) ); num_bytes += sizeof( address ); } ret = SP_multicast( Mbox, AGREED_MESS, Spread_group, STATE_MESS, num_bytes, Mess ); if( ret < 0 ) { SP_error( ret ); Spread_reconnect(ret); } }
void setup(struct initializers *i) { /* Sets up all ports */ /* and waits for the start_mcast message to start the actual process */ int mcast_addr; int start = 0; int bytes; int num, c, num_groups, r, ret; int service_type = 0; int responded=0; int16 mess_type; int endian_mismatch=0; int response[10]; struct timeval timeout; unsigned char ttl_val; char target_groups[MAX_MEMBERS][MAX_GROUP_NAME]; char logfilename[10]; char sender[MAX_GROUP_NAME]; char groups[10][MAX_GROUP_NAME]; char mess_buf[MAX_MESS_LEN]; sp_time test_timeout; struct packet_structure *p=malloc(sizeof(struct packet_structure)); snprintf(logfilename, 10, "%d.out", machine_index); logfile = fopen(logfilename, "w"); 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(); ret = SP_join(Mbox, group); printf("Join group %s:%d\n", group, ret); if (machine_index == 1) { for (c=1; c <= total_machines; c++) { response[c] = 0; completed[c] = 0; } /* Collect up the users, and send start message when everyone is in the group */ response[1]=1; while (responded < 1) { ret = SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups, &mess_type, &endian_mismatch, sizeof(mess_buf), mess_buf ); p = (struct packet_structure *)mess_buf; if (p->type == 4) { printf("ret = %d Got machine id %d\n", ret, p->machine_index); /* Add this machine to the array and check to see if we are done */ response[p->machine_index] = 1; printf("Got response from %d\n", p->machine_index); r = 1; for (c=1; c <= total_machines; c++) { if (response[c] == 0) r =0; } if (r==1) responded = 1; } else if (p->type == 3 && machine_index == 1) { completed[p->machine_index] = 1; } } /* Send start sending message to everyone */ if (r=1) /*All ready */ { p->type= 2; printf("Ready to go..\n"); ret= SP_multicast( Mbox, AGREED_MESS, group, 1, sizeof(struct packet_structure), (char *)p ); } } else { /*We are not machine index 1*/ /*Send ready to begin message */ p->type = 4; p->machine_index = machine_index; ret= SP_multicast( Mbox, AGREED_MESS, group, 1, sizeof(struct packet_structure), (char *)p ); printf("Join=%d, group %s\n", ret, group); if( ret < 0 ) { SP_error( ret ); Bye(); } else { printf("Sent: %d\n", ret); } } };
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, success =0; char server_group[1]; char *uname; service_type = 0; ret = SP_receive( Mbox, &service_type, sender, 100, &num_groups, target_groups, &mess_type, &endian_mismatch, sizeof(mess), mess ); 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; //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 ); //printf("private group is %s\n", Private_group); if (strncmp(Private_group, sender, strlen(Private_group)) != 0) { recv_server_msg((struct chat_packet *) mess, mess_type); } }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] ); sprintf(server_group, "c%d", connected); if (strncmp(sender, server_group, 2) == 0) { //printf("Server connection message\n"); for (i=0; i< num_groups; i++) { if (target_groups[i][1] == server_group[1]) { printf("Successfully connected to server %d\n>", connected); success = 1; show_menu(); } } if (!success) { printf("Server unavailable. Please try again later.\n>"); connected = 0; SP_leave(Mbox, sender); } } else { /*Membership message from chatroom. Show users in chat */ /*No longer here*/ } 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 ); if (strncmp(sender, server_group, 2) == 0 && memb_info.changed_member[1] == server_group[1]) { printf("Disconnected from server %s\n", server_group); SP_leave(Mbox, sender); /*Disconnect us from the server group */ if (chatroom != NULL) { sprintf(chatroom, "%s%d",chatroom, connected); SP_leave(Mbox, chatroom); chatroom[0]='\0'; chatroom_start = (struct node *)calloc(1,sizeof(struct node)); } connected = 0; /*we are no longer connected */ } }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 < (int) 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("Incorrect mess type"); } else if (Is_reject_mess( service_type)) { printf("Rejcted"); } else printf("Unknown"); fflush(stdout); }