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 ); }
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 ); }
static void User_command() { char command[80]; int i; if( fgets( command,70,stdin ) == NULL ) { printf("Bye.\n"); exit( 0 ); } switch( command[0] ) { case '0': Activate_status(); printf("\n"); printf("Monitor> "); fflush(stdout); break; case '1': Define_partition(); Print_partition( Work_partition ); break; case '2': for( i=0; i < Conf_num_procs( &Cn ); i++ ) Partition[i] = Work_partition[i]; Mutex_lock( &Partition_mutex ); Partition_active = 1; Mutex_unlock( &Partition_mutex ); #ifndef _REENTRANT Send_partition(); #endif printf("\n"); printf("Monitor> "); fflush(stdout); break; case '3': Print_partition( Partition ); break; case '4': for( i=0; i < Conf_num_procs( &Cn ); i++ ) { Partition[i] = 0; Work_partition[i] = 0; } Mutex_lock( &Partition_mutex ); Partition_active = 0; Mutex_unlock( &Partition_mutex ); Send_partition(); #ifndef _REENTRANT E_dequeue( Send_partition, 0, NULL ); #endif printf("\n"); printf("Monitor> "); fflush(stdout); break; case '5': Define_flow_control(); Print_flow_control( Work_fc_buf ); break; case '6': for( i=0; i < Conf_num_procs( &Cn )+1; i++ ) { Fc_buf[i][0] = Work_fc_buf[i][0]; Fc_buf[i][1] = Work_fc_buf[i][1]; } Send_flow_control(); printf("\n"); printf("Monitor> "); fflush(stdout); break; case '7': Print_flow_control( Fc_buf ); break; case '8': Kill_spreads(); printf("\n"); printf("Monitor> "); fflush(stdout); break; case 'r': /* trigger reload of spread configuration */ Reload_Conf(); printf("Reload Membership. \n"); break; case '9': case 'q': printf("Bye.\n"); exit( 0 ); break; default: printf("\nUnknown commnad\n"); Print_menu(); break; } }
int main( int argc, char *argv[] ) { int i; #ifdef _REENTRANT int ret; #endif fclose(stderr); Alarm_set_types( NONE ); Alarmp( SPLOG_PRINT, SYSTEM, "/===========================================================================\\\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| The Spread Toolkit. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Copyright (c) 1993-2014 Spread Concepts LLC |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| All rights reserved. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| The Spread package is licensed under the Spread Open-Source License. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| You may only use this software in compliance with the License. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| A copy of the license can be found at http://www.spread.org/license |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| This product uses software developed by Spread Concepts LLC for use |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| in the Spread toolkit. For more information about Spread, |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| see http://www.spread.org |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| This software is distributed on an \"AS IS\" basis, WITHOUT WARRANTY OF |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| ANY KIND, either express or implied. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Creators: |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Yair Amir [email protected] |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Michal Miskin-Amir [email protected] |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Jonathan Stanton [email protected] |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| John Schultz [email protected] |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Contributors: |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Amy Babay [email protected] - accelerated ring protocol. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Ryan Caudy [email protected] - contribution to process groups.|\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Claudiu Danilov [email protected] - scalable, wide-area support. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Cristina Nita-Rotaru [email protected] - GC security. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Theo Schlossnagle [email protected] - Perl, autoconf, old skiplist |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Dan Schoenblum [email protected] - Java Interface Developer. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Special thanks to the following for discussions and ideas: |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Ken Birman, Danny Dolev, Jacob Green, Mike Goodrich, Ben Laurie, |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| David Shaw, Gene Tsudik, Robbert VanRenesse. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Partial funding provided by the Defense Advanced Research Project Agency |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| (DARPA) and the National Security Agency (NSA) 2000-2004. The Spread |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| toolkit is not necessarily endorsed by DARPA or the NSA. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| For a full list of contributors, see Readme.txt in the distribution. |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| WWW: www.spread.org www.spreadconcepts.com |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Contact: [email protected] |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| |\n"); Alarmp( SPLOG_PRINT, SYSTEM, "| Version %d.%02d.%02d Built %-17s |\n", (int)SP_MAJOR_VERSION, (int)SP_MINOR_VERSION, (int)SP_PATCH_VERSION, Spread_build_date ); Alarmp( SPLOG_PRINT, SYSTEM, "\\===========================================================================/\n"); #ifdef ARCH_PC_WIN95 ret = WSAStartup( MAKEWORD(2,0), &WSAData ); if( ret != 0 ) Alarm( EXIT, "sptmonitor: main: winsock initialization error %d\n", ret ); #endif /* ARCH_PC_WIN95 */ Usage( argc, argv ); Alarm_set_interactive(); read_configuration(); initialize_locks(); for( i=0; i < Conf_num_procs( &Cn ); i++ ) Partition[i] = 0; for( i=0; i < Conf_num_procs( &Cn ); i++ ) Status_vector[i] = 0; Pack_scat.elements[0].len = sizeof( packet_header ); Pack_scat.elements[0].buf = (char *)&Pack; Pack.proc_id = My.id; Pack.seq = My_port; Pack.memb_id.proc_id = 15051963; Report_scat.num_elements = 2; Report_scat.elements[0].buf = (char *)&Report_pack; Report_scat.elements[0].len = sizeof(packet_header); Report_scat.elements[1].buf = (char *)&GlobalStatus; Report_scat.elements[1].len = sizeof(status); SendChan = DL_init_channel( SEND_CHANNEL , My_port, 0, 0 ); Report_socket = DL_init_channel( RECV_CHANNEL, My_port, 0, 0 ); E_init(); /* both reentrent and non-reentrant code uses events */ #ifndef _REENTRANT E_attach_fd( 0, READ_FD, User_command, 0, NULL, LOW_PRIORITY ); E_attach_fd( Report_socket, READ_FD, Report_message, 0, NULL, HIGH_PRIORITY ); #endif /* _REENTRANT */ Print_menu(); #ifdef _REENTRANT #ifndef ARCH_PC_WIN95 ret = pthread_create( &Read_thread, NULL, Read_thread_routine, 0 ); ret = pthread_create( &Status_thread, NULL, Status_send_thread_routine, 0 ); ret = pthread_create( &Partition_thread, NULL, Partition_send_thread_routine, 0 ); #else /* ARCH_PC_WIN95 */ Read_thread = CreateThread( NULL, 0, Read_thread_routine, NULL, 0, &ret ); Status_thread = CreateThread( NULL, 0, Status_send_thread_routine, NULL, 0, &ret ); Partition_thread = CreateThread( NULL, 0, Partition_send_thread_routine, NULL, 0, &ret ); #endif /* ARCH_PC_WIN95 */ for(;;) { User_command(); } #else /*! _REENTRANT */ E_handle_events(); #endif /* _REENTRANT */ return 0; }
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); }