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 PyObject * new_membership_msg(int type, PyObject *group, int num_members, char (*members)[MAX_GROUP_NAME], char *buffer, int size) { MembershipMsg *self; group_id grp_id; membership_info memb_info; int32 num_extra_members = 0; int i; int ret; assert(group != NULL); self = PyObject_New(MembershipMsg, &MembershipMsg_Type); if (self == NULL) return NULL; self->reason = type & CAUSED_BY_MASK; /* from sp.h defines */ self->msg_subtype = type & (TRANSITION_MESS | REG_MEMB_MESS); Py_INCREF(group); self->group = group; self->members = NULL; self->extra = NULL; self->group_id = NULL; self->changed_member = NULL; self->members = PyTuple_New(num_members); if (self->members == NULL) { Py_DECREF(self); return NULL; } for (i = 0; i < num_members; ++i) { PyObject *s = PyString_FromString(members[i]); if (!s) { Py_DECREF(self); return NULL; } PyTuple_SET_ITEM(self->members, i, s); } if ((ret = SP_get_memb_info(buffer, type, &memb_info)) < 0) { PyErr_Format(SpreadError, "error %d on SP_get_memb_info", ret); Py_DECREF(self); return NULL; } memcpy(&grp_id, &memb_info.gid, sizeof(group_id)); self->group_id = new_group_id(grp_id); if (self->group_id == NULL) { Py_DECREF(self); return NULL; } /* The extra attribute is a tuple initialized for 0 or more items. * If the member event is a single member event such as a join, leave or disconnect, * the changed member attribute is set from memb_info.changed_member * and a single value from vs_set, which is stored inside the message, is added to extra tuple. * If the member event is a merge or partition, the list of member names * from vs_set is stored in extra. */ if (Is_reg_memb_mess(type) && (Is_caused_join_mess(type) || Is_caused_disconnect_mess(type) || Is_caused_leave_mess(type))) { self->changed_member = PyString_FromString(memb_info.changed_member); if (!self->changed_member) { Py_DECREF(self); return NULL; } } else { self->changed_member = Py_BuildValue(""); } num_extra_members = memb_info.my_vs_set.num_members; self->extra = PyTuple_New(num_extra_members); if (self->extra == NULL) { Py_DECREF(self); return NULL; } if (num_extra_members > 0) { char (*member_names)[MAX_GROUP_NAME] = (char (*)[MAX_GROUP_NAME]) malloc(num_extra_members * MAX_GROUP_NAME); if (member_names == NULL) { Py_DECREF(self); PyErr_NoMemory(); return NULL; } if ((ret = SP_get_vs_set_members(buffer, &memb_info.my_vs_set, member_names, num_extra_members)) < 0) { PyErr_Format(SpreadError, "error %d on SP_get_vs_set_members", ret); Py_DECREF(self); free(member_names); return NULL; } for (i = 0; i < num_extra_members; i++) { PyObject *s; s = PyString_FromString(member_names[i]); if (!s) { Py_DECREF(self); free(member_names); return NULL; } PyTuple_SET_ITEM(self->extra, i, s); } free(member_names); } return (PyObject *)self; }
int main(int argc, const char *argv[]) { char from[MAX_GROUP_NAME]; char message[MAX_MESSLEN]; membership_info memb_info; int num_groups; char target_groups[100][MAX_GROUP_NAME]; int16 mess_type; int endian_mismatch; int service_type; int loop = 1; int rc; int ret; int i; sqlite3 *mydb; char *dbName; char *zErrMsg; connectionStatus status = UNKNOWN; char buffer[MAX_GROUP_NAME]; char sqlBuffer[1024]; char *member; char *host; char *tok; char ch; extern char *optarg; char scratch[255]; global.connected=0; global.Group=(char *)NULL; global.configFileName=(char *)NULL; global.locked = 0; global.rawClient =1; global.debug = 0; #ifdef FICL global.appDir=(char *)NULL; #endif char *group=(char *)NULL; setSymbol("BUILD",__DATE__,LOCK,LOCAL); setSymbol("CLIENT","raw",UNLOCK,GLOBAL); setSymbol("SPREAD_SERVER","4803",UNLOCK,GLOBAL); setSymbol("GROUP","global",UNLOCK,GLOBAL); setSymbol("USER","dbCache",LOCK,GLOBAL); setSymbol("DEBUG","false",UNLOCK,GLOBAL); setSymbol("MODE","local",UNLOCK,GLOBAL); setSymbol("START_FILE","./dbCache.rc",UNLOCK,GLOBAL); while((ch = getopt(argc, ( char **)argv,"dhc:")) != -1) { switch(ch) { case 'c': setSymbol("START_FILE", optarg,LOCK,GLOBAL); break; case 'd': global.debug=1; break; case 'h': usage(); exit(0); break; } } loadFile(getSymbol("START_FILE")); if( global.debug ) { setBoolean("DEBUG",1); } global.debug = getBoolean("DEBUG"); if(global.debug) { dumpGlobals(); dumpSymbols(); } dbName = getSymbol("DATABASE"); connectToSpread(); while(loop) { status = UNKNOWN; ret = SP_receive(global.Mbox, &service_type, from, 100, &num_groups, target_groups, &mess_type, &endian_mismatch, sizeof(message), message); if (Is_membership_mess (service_type)) { ret = SP_get_memb_info(message, service_type, &memb_info); if (Is_caused_join_mess(service_type)) { status=JOINED; } if (Is_caused_leave_mess (service_type)) { status = LEFT; } if (Is_caused_disconnect_mess (service_type)) { status = DISCONNECTED; } if (Is_caused_network_mess (service_type)) { status = NETWORK; } rc = sqlite3_open(dbName, &mydb); for (i = 0; i < num_groups; i++) { if( global.debug) { printf("\t%s\n",(char *) &target_groups[i][0]); } strcpy(buffer, &target_groups[i][1]); member=strtok(buffer,"#"); host=strtok(NULL,"#"); if( global.debug) { printf("\t\tMember:%s\n", member); printf("\t\tHost :%s\n", host); } statusToText( status, scratch ); if (JOINED == status ) { sprintf( sqlBuffer,"insert or replace into status ( member, host, grp, state ) values ( '%s','%s','%s','%s');", member, host,from,scratch); } if( global.debug) { printf ("%s\n",sqlBuffer); } rc = sqlite3_exec(mydb, sqlBuffer, 0, 0, &zErrMsg); } strcpy(buffer, &memb_info.changed_member[1]); member=strtok(buffer,"#"); host=strtok(NULL,"#"); statusToText( status, scratch ); sprintf( sqlBuffer, "update status set state='%s' where member = '%s' and host ='%s';",scratch, member,host); if( global.debug) { printf("CHANGE: %s %s\n",member,host); printf("CHANGE: %s\n", scratch); printf ("%s\n",sqlBuffer); } rc = sqlite3_exec(mydb, sqlBuffer, 0, 0, &zErrMsg); sqlite3_close(mydb); } } printf("============ After\n"); dumpGlobals(); printf("Sender %s\n",from); printf("Message >%s<\n",message); toSpread(from,"Message recieved\n"); spreadJoin("TEST"); sleep(2); spreadLeave("TEST"); sleep(2); spreadDisconnect(); }
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, 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); }