/* Introduce self to group. */ int introduceselftogroup(member *node, address *joinaddr){ messagehdr *msg; #ifdef DEBUGLOG static char s[1024]; #endif if(memcmp(&node->addr, joinaddr, 4*sizeof(char)) == 0){ /* I am the group booter (first process to join the group). Boot up the group. */ #ifdef DEBUGLOG LOG(&node->addr, "Starting up group..."); #endif node->ingroup = 1; node->nodenumber = 0; // adding the membership data of node0 node->memtable[node->nodenumber].lastupdatetime=getcurrtime(); memcpy(&node->memtable[node->nodenumber].maddr,&node->addr,sizeof(address)); node->memtable[node->nodenumber].tocleanup=0; node->memtable[node->nodenumber].heartbeatCounter++; node->memcount=1; logNodeAdd(&node->addr,&node->addr); printf("log node added "); } else{ size_t msgsize = sizeof(messagehdr) + sizeof(address); msg=malloc(msgsize); /* create JOINREQ message: format of data is {struct address myaddr} */ msg->msgtype=JOINREQ; memcpy((char *)(msg+1), &node->addr, sizeof(address)); #ifdef DEBUGLOG sprintf(s, "Trying to join..."); LOG(&node->addr, s); #endif /* send JOINREQ message to introducer member. */ MPp2psend(&node->addr, joinaddr, (char *)msg, msgsize); // to increment unique node number count and assign to the newly formed node. MEMCOUNT++; node->nodenumber=MEMCOUNT; free(msg); } return 1; }
/* Introduce self to group. */ int introduceselftogroup(member *node, address *joinaddr){ messagehdr *msg; #ifdef DEBUGLOG static char s[1024]; #endif if(memcmp(&node->addr, joinaddr, 4*sizeof(char)) == 0){ /* I am the group booter (first process to join the group). Boot up the group. */ #ifdef DEBUGLOG LOG(&node->addr, "Starting up group..."); #endif node->ingroup = 1; UNID=0; node->nodenumber=UNID; } else{ node->nodenumber=UNID; size_t msgsize = sizeof(messagehdr) + sizeof(address)+ sizeof(int); //size_t msgsize = sizeof(messagehdr) + sizeof(address); msg=malloc(msgsize); printf("SIZE OF MSG %d:",msgsize); /* create JOINREQ message: format of data is {struct address myaddr} */ msg->msgtype=JOINREQ; memcpy((char *)(msg+1), &node->addr, sizeof(address)); // try to have node id also sent // fails here memcpy((char *)(msg+7),&node->nodenumber, sizeof(int)); #ifdef DEBUGLOG sprintf(s, "Trying to join...MSG"); LOG(&node->addr, s); #endif /* send JOINREQ message to introducer member. */ MPp2psend(&node->addr, joinaddr, (char *)msg, msgsize); free(msg); sprintf(s, "Trying to join... COMPLETED"); // give the node an id LOG(&node->addr, s); } // every node get a unique number : ID UNID++; return 1; }
/* Received a JOINREQ (joinrequest) message. */ void Process_joinreq(void *env, char *data, int size) { // This function is only called by the initiator for messages sent from different other nodes in the network. // copy message into the membership list and send joinrep message type to the node from where it received the message // env is the node on which it is called // data only contains the address of the sender member *node = (member *) env; address *destaddr= (address *)data; // typecast to address to directly get the dest address NODECOUNT++; // update the membership table node->memtable[node->memcount].lastupdatetime=getcurrtime(); node->memtable[node->memcount].heartbeatCounter++; memcpy(&node->memtable[node->memcount].maddr,destaddr,sizeof(address)); node->memcount++; logNodeAdd(&node->addr,destaddr); //create the message containing membership table and send to the sender's node messagehdr *msg; size_t msgsize = sizeof(messagehdr) + ((NODECOUNT+1) * sizeof(membershiplist)); msg=malloc(msgsize); msg->msgtype=JOINREP; messagehdr *src=msg; src=src+1; //src=msg+1; // do a for loop to copy each membership array struct into message int i; for(i=0;i<node->memcount;i++){ //printf("\n COPYING for %d",node->memtable[i].maddr.addr[0] ); memcpy((membershiplist *)(src)+i, &node->memtable[i], sizeof(membershiplist)); } MPp2psend(&node->addr, destaddr, (char *)msg, msgsize); return; }
/* Received a JOINREQ (joinrequest) message. // handled by the lead node and send jon rep */ void Process_joinreq(void *env, char *data, int size) { // code to get the timestamp recorded of jjjj struct timeval tp; gettimeofday(&tp,NULL); long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; /* <your code goes in here> */ messagehdr *msg; member *node = (member *) env; // QUESTION :first 6 bytes : sizeof(addr) has the address : fetch that alone how address *destaddr = (char *)data; // env holds the node on which it is called. node->mplist[UNID].heartbeatcounter=1; // copy address to memnodeip iside mplist memcpy(&(node->mplist[UNID].memnodeip),destaddr,sizeof(address)); node->mplist[UNID].lastupdatetime=ms; printf("\nSIZE OF MEMLIST %d",sizeof(node->mplist)); // once u get join req from a specific node // look at your membership list // update the current timestamp and increment the heartbeat counter for the node from which you received the message //for i in number of records of mplist for that source node // // node->mplist.heartbeatcounter++; // node->mplist. //creating response with ip:heartbeatcounter:timestamp[ local to the node ] size_t msgsize = sizeof(messagehdr) + sizeof(address)+sizeof(struct memberShipList); //size_t msgsize = sizeof(messagehdr) + sizeof(address); msg=malloc(msgsize); msg->msgtype=JOINREP; //memcpy((char *)(msg+1), &node->addr, sizeof(address)); //form and send response to join req printf("SENDING from within PROCESS_JOINREQ\n"); MPp2psend(&node->addr, destaddr, (char *)msg, msgsize); printf("DONE and value of node is %d",node->nodenumber); return; }
/* Received a GOSSIP message. */ void nodeloopops(member *node){ printf("\n ** INSIDE NODELOOPOPS %d",node->addr.addr[0]); if ( getcurrtime() % T_GOSSIP == 0 && node->memcount !=0 ){ // sending a gossip message by selecting a random node which is not its own int n=-1; int randomNode=-1; // to select random node to send the gossip message while(1){ randomNode = rand() % EN_GPSZ ; if ( randomNode == node->nodenumber) continue; else if ( randomNode > node->memcount) continue; else if (memcmp(&node->addr,&node->memtable[randomNode].maddr,sizeof(address))==0) continue; else if (node->memtable[randomNode].tocleanup!=0) continue; else break; } // when you found the node to send , loop through the memberlisttable and update your lastupdatetime. int i=0; for(i=0;i<node->memcount;i++){ printf("\n check time for %d",node->memtable[i].maddr.addr[0]); // check if the mem table contains the node's address n = memcmp ( &node->memtable[i].maddr, &node->addr,sizeof(address)); if ( n == 0 ){ // update current time for ur node printf("\nupdating the getcurrent time"); // check ur heartbeat and update //if ( node->memtable[i].heartbeatCounter < node-) node->memtable[i].lastupdatetime=getcurrtime(); node->memtable[i].heartbeatCounter++; }else{ if ( (getcurrtime() - node->memtable[i].heartbeatCounter) > T_CLEANUP /*getcurrtime() - node->memtable[i].lastupdatetime > T_CLEANUP */){ if ( ( getcurrtime() - node->memtable[i].heartbeatCounter ) < T_FAIL /* getcurrtime() - node->memtable[i].lastupdatetime < T_FAIL */ ) { printf("\nTime to set del bit for node %d",node->memtable[i].maddr.addr[0]); node->memtable[i].tocleanup=1; } else { if ( node->memtable[i].tocleanup != 2 ){ printf("\nTime to mark the node deleted %d",node->memtable[i].maddr.addr[0]); node->memtable[i].tocleanup=2; logNodeRemove(&node->addr,&node->memtable[i].maddr); } } } } } //create the message containing membership table and send to the sender's node messagehdr *msg; size_t msgsize = sizeof(messagehdr) + sizeof(address) +(node->memcount) * sizeof(membershiplist); msg=malloc(msgsize); msg->msgtype=GOSSIPMSG; membershiplist *src; struct address *temp; temp=(struct address*) (msg+1); memcpy(temp, &node->addr,sizeof(address)); src=(membershiplist *)(temp + 1); for(i=0;i<node->memcount;i++){ memcpy(src, &node->memtable[i],sizeof(membershiplist)); src=src + 1; } MPp2psend(&node->addr, &node->memtable[randomNode].maddr, (char *)msg, msgsize); // constructing the message to send to random node with message type GOSSIP printf("\n Number of records in %d mem table is %d ",node->addr.addr[0],--i); } printf("\n *** NODELOOP DONE *** "); return; }