1 /* Coordinator => Machine that initiates the transaction request call for commiting a transaction
2 * Participant => Machines that host the objects involved in a transaction commit */
15 #include "threadnotify.h"
21 #define LISTEN_PORT 2156
22 #define BACKLOG 10 //max pending connections
23 #define RECEIVE_BUFFER_SIZE 2048
24 #define PRE_BUF_SIZE 2048
26 extern int classsize[];
28 objstr_t *mainobjstore;
29 pthread_mutex_t mainobjstore_mutex;
30 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
32 /* This function initializes the main objects store and creates the
33 * global machine and location lookup table */
37 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
38 /* Initialize attribute for mutex */
39 pthread_mutexattr_init(&mainobjstore_mutex_attr);
40 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
41 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
42 if (mhashCreate(HASH_SIZE, LOADFACTOR))
45 if (lhashCreate(HASH_SIZE, LOADFACTOR))
48 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
54 /* This function starts the thread to listen on a socket
55 * for tranaction calls */
58 int listenfd, acceptfd;
59 struct sockaddr_in my_addr;
60 struct sockaddr_in client_addr;
61 socklen_t addrlength = sizeof(struct sockaddr);
62 pthread_t thread_dstm_accept;
66 listenfd = socket(AF_INET, SOCK_STREAM, 0);
73 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
78 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
84 my_addr.sin_family = AF_INET;
85 my_addr.sin_port = htons(LISTEN_PORT);
86 my_addr.sin_addr.s_addr = INADDR_ANY;
87 memset(&(my_addr.sin_zero), '\0', 8);
89 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
95 if (listen(listenfd, BACKLOG) == -1)
101 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
105 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
107 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
109 pthread_detach(thread_dstm_accept);
112 /* This function accepts a new connection request, decodes the control message in the connection
113 * and accordingly calls other functions to process new requests */
114 void *dstmAccept(void *acceptfd)
116 int val, retval, size;
118 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
122 trans_commit_data_t transinfo;
123 unsigned short objType, *versionarry, version;
124 unsigned int *oidarry, numoid, mid, threadid;
126 transinfo.objlocked = NULL;
127 transinfo.objnotfound = NULL;
128 transinfo.modptr = NULL;
129 transinfo.numlocked = 0;
130 transinfo.numnotfound = 0;
132 /* Receive control messages from other machines */
133 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
135 pthread_exit(NULL); // Testing connection
137 perror("Error in receiving control from coordinator\n");
143 printf("DEBUG -> Recv READ_REQUEST\n");
144 /* Read oid requested and search if available */
145 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
146 perror("Error receiving object from cooridnator\n");
149 if((srcObj = mhashSearch(oid)) == NULL) {
150 printf("Object 0x%x is not found in Main Object Store %s %d\n", oid, __FILE__, __LINE__);
153 h = (objheader_t *) srcObj;
155 size += sizeof(objheader_t);
158 ctrl = OBJECT_NOT_FOUND;
159 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
160 perror("Error sending control msg to coordinator\n");
165 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
166 *((int *)&msg[1])=size;
167 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
168 perror("Error sending size of object to coordinator\n");
171 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
172 perror("Error in sending object\n");
178 case READ_MULT_REQUEST:
179 printf("DEBUG-> READ_MULT_REQUEST\n");
183 printf("DEBUG -> MOVE_REQUEST\n");
186 case MOVE_MULT_REQUEST:
187 printf("DEBUG -> MOVE_MULT_REQUEST\n");
191 /* Read transaction request */
192 printf("DEBUG -> Recv TRANS_REQUEST\n");
193 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
194 printf("Error in readClientReq\n");
199 printf("DEBUG -> Recv TRANS_PREFETCH\n");
200 if((val = prefetchReq((int)acceptfd)) != 0) {
201 printf("Error in transPrefetch\n");
205 case START_REMOTE_THREAD:
206 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
208 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
209 else if (retval != sizeof(unsigned int))
210 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
214 objType = getObjType(oid);
215 startDSMthread(oid, objType);
219 case THREAD_NOTIFY_REQUEST:
220 size = sizeof(unsigned int);
221 retval = recv((int)acceptfd, &numoid, size, 0);
222 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
223 bzero(&buffer, RECEIVE_BUFFER_SIZE);
224 retval = recv((int)acceptfd, &buffer, size, 0);
225 oidarry = calloc(numoid, sizeof(unsigned int));
226 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
227 size = sizeof(unsigned int) * numoid;
228 versionarry = calloc(numoid, sizeof(unsigned short));
229 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
230 size += sizeof(unsigned short) * numoid;
231 mid = *((unsigned int *)(buffer+size));
232 size += sizeof(unsigned int);
233 threadid = *((unsigned int *)(buffer+size));
234 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
238 case THREAD_NOTIFY_RESPONSE:
239 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
240 bzero(&buffer, RECEIVE_BUFFER_SIZE);
241 retval = recv((int)acceptfd, &buffer, size, 0);
243 perror("dstmAccept(): error receiving THREAD_NOTIFY_RESPONSE msg");
244 else if( retval != 2*sizeof(unsigned int) + sizeof(unsigned short))
245 printf("dstmAccept(): incorrect smsg size %d for THREAD_NOTIFY_RESPONSE msg\n", retval);
247 oid = *((unsigned int *)buffer);
248 size = sizeof(unsigned int);
249 version = *((unsigned short *)(buffer+size));
250 size += sizeof(unsigned short);
251 threadid = *((unsigned int *)(buffer+size));
252 threadNotify(oid,version,threadid);
258 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
261 /* Close connection */
262 if (close((int)acceptfd) == -1)
268 /* This function reads the information available in a transaction request
269 * and makes a function call to process the request */
270 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
273 unsigned int *oidmod, oid;
275 objheader_t *headaddr;
276 int sum = 0, i, N, n, val;
280 /* Read fixed_data_t data structure */
281 N = sizeof(fixed) - 1;
282 ptr = (char *)&fixed;;
283 fixed.control = TRANS_REQUEST;
285 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
287 } while(sum < N && n != 0);
289 /* Read list of mids */
290 int mcount = fixed.mcount;
291 N = mcount * sizeof(unsigned int);
292 unsigned int listmid[mcount];
293 ptr = (char *) listmid;
296 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
298 } while(sum < N && n != 0);
300 /* Read oid and version tuples for those objects that are not modified in the transaction */
301 int numread = fixed.numread;
302 N = numread * (sizeof(unsigned int) + sizeof(short));
304 if(numread != 0) { //If pile contains more than one object to be read,
305 // keep reading all objects
308 n = recv((int)acceptfd, (void *) objread, N, 0);
310 } while(sum < N && n != 0);
313 /* Read modified objects */
314 if(fixed.nummod != 0) {
315 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
316 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
320 do { // Recv the objs that are modified by the Coordinator
321 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
323 } while (sum < fixed.sum_bytes && n != 0);
326 /* Create an array of oids for modified objects */
327 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
330 printf("calloc error %s, %d\n", __FILE__, __LINE__);
333 ptr = (char *) modptr;
334 for(i = 0 ; i < fixed.nummod; i++) {
336 headaddr = (objheader_t *) ptr;
339 GETSIZE(tmpsize, headaddr);
340 ptr += sizeof(objheader_t) + tmpsize;
343 /*Process the information read */
344 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
345 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
361 /* This function processes the Coordinator's transaction request using "handleTransReq"
362 * function and sends a reply to the co-ordinator.
363 * Following this it also receives a new control message from the co-ordinator and processes this message*/
364 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
365 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
366 char *ptr, control, sendctrl;
367 objheader_t *tmp_header;
369 int i = 0, val, retval;
371 /* Send reply to the Coordinator */
372 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
373 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
377 /* Read new control message from Coordiator */
378 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
379 perror("Error in receiving control message\n");
383 /* Process the new control message */
386 if (fixed->nummod > 0)
388 /* Unlock objects that was locked due to this transaction */
389 for(i = 0; i< transinfo->numlocked; i++) {
390 header = mhashSearch(transinfo->objlocked[i]);// find the header address
391 STATUS(((objheader_t *)header)) &= ~(LOCK);
394 /* Send ack to Coordinator */
395 sendctrl = TRANS_SUCESSFUL;
396 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
397 perror("Error sending ACK to coordinator\n");
398 if (transinfo->objlocked != NULL) {
399 free(transinfo->objlocked);
401 if (transinfo->objnotfound != NULL) {
402 free(transinfo->objnotfound);
411 /* Invoke the transCommit process() */
412 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
413 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
415 if (transinfo->objlocked != NULL) {
416 free(transinfo->objlocked);
418 if (transinfo->objnotfound != NULL) {
419 free(transinfo->objnotfound);
425 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
426 //TODO expect another transrequest from client
427 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
430 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
431 //TODO Use fixed.trans_id TID since Client may have died
436 if (transinfo->objlocked != NULL) {
437 free(transinfo->objlocked);
439 if (transinfo->objnotfound != NULL) {
440 free(transinfo->objnotfound);
446 /* This function increments counters while running a voting decision on all objects involved
447 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
448 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
450 unsigned short version;
451 char control = 0, *ptr;
453 unsigned int *oidnotfound, *oidlocked;
455 objheader_t *headptr;
457 /* Counters and arrays to formulate decision on control message to be sent */
458 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
459 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
460 int objnotfound = 0, objlocked = 0;
461 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
463 /* modptr points to the beginning of the object store
464 * created at the Pariticipant.
465 * Object store holds the modified objects involved in the transaction request */
466 ptr = (char *) modptr;
468 /* Process each oid in the machine pile/ group per thread */
469 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
470 if (i < fixed->numread) {//Objs only read and not modified
471 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
473 oid = *((unsigned int *)(objread + incr));
474 incr += sizeof(unsigned int);
475 version = *((unsigned short *)(objread + incr));
476 } else {//Objs modified
478 headptr = (objheader_t *) ptr;
480 version = headptr->version;
481 GETSIZE(tmpsize, headptr);
482 ptr += sizeof(objheader_t) + tmpsize;
485 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
487 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
488 /* Save the oids not found and number of oids not found for later use */
489 oidnotfound[objnotfound] = oid;
491 } else { /* If Obj found in machine (i.e. has not moved) */
492 /* Check if Obj is locked by any previous transaction */
493 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
494 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
496 } else {/* If versions don't match ...HARD ABORT */
498 /* Send TRANS_DISAGREE to Coordinator */
499 control = TRANS_DISAGREE;
500 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
501 perror("Error in sending control to the Coordinator\n");
506 } else {/* If Obj is not locked then lock object */
507 STATUS(((objheader_t *)mobj)) |= LOCK;
508 /* Save all object oids that are locked on this machine during this transaction request call */
509 oidlocked[objlocked] = OID(((objheader_t *)mobj));
511 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
513 } else { /* If versions don't match ...HARD ABORT */
515 control = TRANS_DISAGREE;
516 /* Send TRANS_DISAGREE to Coordinator */
517 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
518 perror("Error in sending control to the Coordinator\n");
522 STATUS(((objheader_t *)mobj)) &= ~(LOCK);
531 /* Decide what control message to send to Coordinator */
532 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
533 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
534 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
541 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
542 * to send to Coordinator based on the votes of oids involved in the transaction */
543 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
544 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
545 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
548 /* Condition to send TRANS_AGREE */
549 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
550 control = TRANS_AGREE;
551 /* Send control message */
552 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
553 perror("Error in sending control to Coordinator\n");
557 /* Condition to send TRANS_SOFT_ABORT */
558 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
559 control = TRANS_SOFT_ABORT;
560 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
561 *((int*)&msg[1])= *(objnotfound);
563 /* Send control message */
564 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
565 perror("Error in sending no of objects that are not found\n");
568 /* Send number of oids not found and the missing oids if objects are missing in the machine */
569 if(*(objnotfound) != 0) {
570 int size = sizeof(unsigned int)* *(objnotfound);
571 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
572 perror("Error in sending objects that are not found\n");
578 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
579 * if Participant receives a TRANS_COMMIT */
580 transinfo->objlocked = oidlocked;
581 transinfo->objnotfound = oidnotfound;
582 transinfo->modptr = modptr;
583 transinfo->numlocked = *(objlocked);
584 transinfo->numnotfound = *(objnotfound);
589 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
590 * addresses in lookup table and also changes version number
591 * Sends an ACK back to Coordinator */
592 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
594 objheader_t *newheader;
595 int i = 0, offset = 0;
599 /* Process each modified object saved in the mainobject store */
600 for(i = 0; i < nummod; i++) {
601 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
602 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
605 GETSIZE(tmpsize,header);
606 pthread_mutex_lock(&mainobjstore_mutex);
607 memcpy(header, (char *)modptr + offset, tmpsize + sizeof(objheader_t));
608 header->version += 1;
609 /* If threads are waiting on this object to be updated, notify them */
610 if(header->notifylist != NULL) {
611 notifyAll(&header->notifylist, OID(header), header->version);
613 pthread_mutex_unlock(&mainobjstore_mutex);
614 offset += sizeof(objheader_t) + tmpsize;
620 /* Unlock locked objects */
621 for(i = 0; i < numlocked; i++) {
622 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
623 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
626 STATUS(header) &= ~(LOCK);
628 //TODO Update location lookup table
630 /* Send ack to coordinator */
631 control = TRANS_SUCESSFUL;
632 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
633 perror("Error sending ACK to coordinator\n");
639 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
640 * Looks for the objects to be prefetched in the main object store.
641 * If objects are not found then record those and if objects are found
642 * then use offset values to prefetch references to other objects */
644 int prefetchReq(int acceptfd) {
645 int i, length, sum, n, numbytes, numoffset, N, size, count = 0;
646 int isArray = 0, bytesRecvd;
647 unsigned int oid, index = 0;
648 unsigned int objoid, myIpAddr;
649 char *ptr, control, buffer[PRE_BUF_SIZE];
651 objheader_t * header;
654 myIpAddr = getMyIpAddr("en1");
656 myIpAddr = getMyIpAddr("eth0");
659 /* Repeatedly recv the oid and offset pairs sent for prefetch */
660 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
664 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
665 // first 4 bytes are saved to send the
666 // size of the buffer (that is computed at the end of the loop)
669 bytesRecvd += recv((int)acceptfd, (char *)&oid +bytesRecvd,
670 sizeof(unsigned int) - bytesRecvd, 0);
671 } while (bytesRecvd < sizeof(unsigned int));
672 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
673 N = numoffset * sizeof(short);
674 short offset[numoffset];
675 ptr = (char *)&offset;
677 /* Recv the offset values per oid */
679 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
681 } while(sum < N && n != 0);
683 /* Process each oid */
684 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
685 /* Save the oids not found in buffer for later use */
686 *(buffer + index) = OBJECT_NOT_FOUND;
687 index += sizeof(char);
688 *((unsigned int *)(buffer+index)) = oid;
689 index += sizeof(unsigned int);
690 } else { /* If Obj found in machine (i.e. has not moved) */
691 /* send the oid, it's size, it's header and data */
692 header = (objheader_t *)mobj;
693 GETSIZE(size, header);
694 size += sizeof(objheader_t);
695 *(buffer + index) = OBJECT_FOUND;
696 index += sizeof(char);
697 *((unsigned int *)(buffer+index)) = oid;
698 index += sizeof(unsigned int);
699 *((int *)(buffer+index)) = size;
700 index += sizeof(int);
701 memcpy(buffer + index, header, size);
703 /* Calculate the oid corresponding to the offset value */
704 for(i = 0 ; i< numoffset ; i++) {
705 /* Check for arrays */
706 if(TYPE(header) > NUMCLASSES) {
710 int elementsize = classsize[TYPE(header)];
711 objoid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offset[i])));
713 objoid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset[i]));
715 if((header = mhashSearch(objoid)) == NULL) {
716 /* Obj not found, send oid */
717 *(buffer + index) = OBJECT_NOT_FOUND;
718 index += sizeof(char);
719 *((unsigned int *)(buffer+index)) = objoid;
720 index += sizeof(unsigned int);
722 } else {/* Obj Found */
723 /* send the oid, it's size, it's header and data */
724 GETSIZE(size, header);
725 size+=sizeof(objheader_t);
726 *(buffer+index) = OBJECT_FOUND;
727 index += sizeof(char);
728 *((unsigned int *)(buffer+index)) = objoid;
729 index += sizeof(unsigned int);
730 *((int *)(buffer+index)) = size;
731 index += sizeof(int);
732 memcpy(buffer+index, header, size);
739 /* Check for overflow in the buffer */
740 if (index >= PRE_BUF_SIZE) {
741 printf("Char buffer is overflowing\n");
744 /* Send Prefetch response control message only once*/
746 control = TRANS_PREFETCH_RESPONSE;
747 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
748 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
753 /* Add the buffer size into buffer as a parameter */
754 *((unsigned int *)buffer)=index;
755 /* Send the entire buffer with its size and oids found and not found */
756 if(send((int)acceptfd, &buffer, index, MSG_NOSIGNAL) < sizeof(index -1)) {
757 perror("Error sending oids found\n");
764 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
767 unsigned short newversion;
768 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
770 struct sockaddr_in remoteAddr;
772 int status, size, retry = 0;
776 oid = *(oidarry + i);
777 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
778 printf("processReqNotify(): Object is not found in mlookup %s, %d\n", __FILE__, __LINE__);
781 /* Check to see if versions are same */
783 if ((STATUS(header) & LOCK) != LOCK) {
784 STATUS(header) |= LOCK;
785 if(header->version == *(versionarry + i)) {
786 //Add to the notify list
787 insNode(header->notifylist, threadid, mid);
789 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
790 perror("processReqNotify():socket()");
793 bzero(&remoteAddr, sizeof(remoteAddr));
794 remoteAddr.sin_family = AF_INET;
795 remoteAddr.sin_port = htons(LISTEN_PORT);
796 remoteAddr.sin_addr.s_addr = htonl(mid);
798 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
799 printf("processReqNotify():error %d connecting to %s:%d\n", errno,
800 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
803 //Send Update notification
804 msg[0] = THREAD_NOTIFY_RESPONSE;
806 size = sizeof(unsigned int);
807 *((unsigned short *)(&msg[1]+size)) = newversion;
808 size += sizeof(unsigned short);
809 *((unsigned int *)(&msg[1]+size)) = threadid;
810 bytesSent = send(sd, msg, 1+ 2*sizeof(unsigned int) + sizeof(unsigned short), 0);
812 perror("processReqNotify():send()");
814 } else if (bytesSent != 1 + sizeof(unsigned short) + 2*sizeof(unsigned int)){
815 printf("processReqNotify(): error, sent %d bytes\n", bytesSent);
824 STATUS(header) &= ~(LOCK);
827 printf("processReqNotify() Object is still locked\n");