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 */
4 #include <netinet/tcp.h>
8 #include "threadnotify.h"
13 #define BACKLOG 10 //max pending connections
14 #define RECEIVE_BUFFER_SIZE 2048
16 extern int classsize[];
17 extern int numHostsInSystem;
18 extern pthread_mutex_t notifymutex;
20 objstr_t *mainobjstore;
21 pthread_mutex_t mainobjstore_mutex;
22 pthread_mutex_t lockObjHeader;
23 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
25 sockPoolHashTable_t *transPResponseSocketPool;
27 /* This function initializes the main objects store and creates the
28 * global machine and location lookup table */
32 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
33 /* Initialize attribute for mutex */
34 pthread_mutexattr_init(&mainobjstore_mutex_attr);
35 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
36 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
37 pthread_mutex_init(&lockObjHeader,NULL);
38 if (mhashCreate(HASH_SIZE, LOADFACTOR))
41 if (lhashCreate(HASH_SIZE, LOADFACTOR))
44 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
47 //Initialize socket pool
48 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, 2*numHostsInSystem+1)) == NULL) {
49 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
57 int startlistening() {
59 struct sockaddr_in my_addr;
60 socklen_t addrlength = sizeof(struct sockaddr);
63 listenfd = socket(AF_INET, SOCK_STREAM, 0);
69 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
74 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
80 my_addr.sin_family = AF_INET;
81 my_addr.sin_port = htons(LISTEN_PORT);
82 my_addr.sin_addr.s_addr = INADDR_ANY;
83 memset(&(my_addr.sin_zero), '\0', 8);
85 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
90 if (listen(listenfd, BACKLOG) == -1) {
97 /* This function starts the thread to listen on a socket
98 * for tranaction calls */
99 void *dstmListen(void *lfd) {
100 int listenfd=(int)lfd;
102 struct sockaddr_in client_addr;
103 socklen_t addrlength = sizeof(struct sockaddr);
104 pthread_t thread_dstm_accept;
106 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
110 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
111 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
113 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
115 pthread_detach(thread_dstm_accept);
118 /* This function accepts a new connection request, decodes the control message in the connection
119 * and accordingly calls other functions to process new requests */
120 void *dstmAccept(void *acceptfd) {
121 int val, retval, size, sum, sockid;
128 trans_commit_data_t transinfo;
129 unsigned short objType, *versionarry, version;
130 unsigned int *oidarry, numoid, mid, threadid;
132 /* Receive control messages from other machines */
134 int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
138 printf("DEBUG -> RECV Error!.. retrying\n");
143 /* Read oid requested and search if available */
144 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
145 if((srcObj = mhashSearch(oid)) == NULL) {
146 printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
149 h = (objheader_t *) srcObj;
151 size += sizeof(objheader_t);
152 sockid = (int) acceptfd;
154 ctrl = OBJECT_NOT_FOUND;
155 send_data(sockid, &ctrl, sizeof(char));
158 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
159 *((int *)&msg[1])=size;
160 send_data(sockid, &msg, sizeof(msg));
161 send_data(sockid, h, size);
165 case READ_MULT_REQUEST:
171 case MOVE_MULT_REQUEST:
175 /* Read transaction request */
176 transinfo.objlocked = NULL;
177 transinfo.objnotfound = NULL;
178 transinfo.modptr = NULL;
179 transinfo.numlocked = 0;
180 transinfo.numnotfound = 0;
181 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
182 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
187 if((val = prefetchReq((int)acceptfd)) != 0) {
188 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
192 case TRANS_PREFETCH_RESPONSE:
193 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
194 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
198 case START_REMOTE_THREAD:
199 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
200 objType = getObjType(oid);
201 startDSMthread(oid, objType);
204 case THREAD_NOTIFY_REQUEST:
205 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
206 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
207 if((buffer = calloc(1,size)) == NULL) {
208 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
212 recv_data((int)acceptfd, buffer, size);
214 oidarry = calloc(numoid, sizeof(unsigned int));
215 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
216 size = sizeof(unsigned int) * numoid;
217 versionarry = calloc(numoid, sizeof(unsigned short));
218 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
219 size += sizeof(unsigned short) * numoid;
220 mid = *((unsigned int *)(buffer+size));
221 size += sizeof(unsigned int);
222 threadid = *((unsigned int *)(buffer+size));
223 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
228 case THREAD_NOTIFY_RESPONSE:
229 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
230 if((buffer = calloc(1,size)) == NULL) {
231 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
235 recv_data((int)acceptfd, buffer, size);
237 oid = *((unsigned int *)buffer);
238 size = sizeof(unsigned int);
239 version = *((unsigned short *)(buffer+size));
240 size += sizeof(unsigned short);
241 threadid = *((unsigned int *)(buffer+size));
242 threadNotify(oid,version,threadid);
246 case CLOSE_CONNECTION:
247 goto closeconnection;
250 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
255 /* Close connection */
256 if (close((int)acceptfd) == -1)
261 /* This function reads the information available in a transaction request
262 * and makes a function call to process the request */
263 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
266 unsigned int *oidmod, oid;
268 objheader_t *headaddr;
269 int sum, i, size, n, val;
273 /* Read fixed_data_t data structure */
274 size = sizeof(fixed) - 1;
275 ptr = (char *)&fixed;;
276 fixed.control = TRANS_REQUEST;
277 recv_data((int)acceptfd, ptr+1, size);
279 /* Read list of mids */
280 int mcount = fixed.mcount;
281 size = mcount * sizeof(unsigned int);
282 unsigned int listmid[mcount];
283 ptr = (char *) listmid;
284 recv_data((int)acceptfd, ptr, size);
286 /* Read oid and version tuples for those objects that are not modified in the transaction */
287 int numread = fixed.numread;
288 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
290 if(numread != 0) { //If pile contains more than one object to be read,
291 // keep reading all objects
292 recv_data((int)acceptfd, objread, size);
295 /* Read modified objects */
296 if(fixed.nummod != 0) {
297 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
298 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
301 size = fixed.sum_bytes;
302 recv_data((int)acceptfd, modptr, size);
305 /* Create an array of oids for modified objects */
306 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
309 printf("calloc error %s, %d\n", __FILE__, __LINE__);
312 ptr = (char *) modptr;
313 for(i = 0 ; i < fixed.nummod; i++) {
315 headaddr = (objheader_t *) ptr;
318 GETSIZE(tmpsize, headaddr);
319 ptr += sizeof(objheader_t) + tmpsize;
322 /*Process the information read */
323 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
324 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
340 /* This function processes the Coordinator's transaction request using "handleTransReq"
341 * function and sends a reply to the co-ordinator.
342 * Following this it also receives a new control message from the co-ordinator and processes this message*/
343 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
344 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
345 char control, sendctrl, retval;
346 objheader_t *tmp_header;
350 /* Send reply to the Coordinator */
351 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
352 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
356 recv_data((int)acceptfd, &control, sizeof(char));
358 /* Process the new control message */
361 if (fixed->nummod > 0)
363 /* Unlock objects that was locked due to this transaction */
364 for(i = 0; i< transinfo->numlocked; i++) {
365 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
366 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
369 UnLock(STATUSPTR(header));
372 /* Send ack to Coordinator */
373 sendctrl = TRANS_UNSUCESSFUL;
374 send_data((int)acceptfd, &sendctrl, sizeof(char));
378 /* Invoke the transCommit process() */
379 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
380 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
382 if (transinfo->objlocked != NULL) {
383 free(transinfo->objlocked);
385 if (transinfo->objnotfound != NULL) {
386 free(transinfo->objnotfound);
392 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
395 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
396 //TODO Use fixed.trans_id TID since Client may have died
401 if (transinfo->objlocked != NULL) {
402 free(transinfo->objlocked);
404 if (transinfo->objnotfound != NULL) {
405 free(transinfo->objnotfound);
410 /* This function increments counters while running a voting decision on all objects involved
411 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
412 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
414 unsigned short version;
415 char control = 0, *ptr;
417 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
419 objheader_t *headptr;
421 /* Counters and arrays to formulate decision on control message to be sent */
422 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
423 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
424 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
425 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
426 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
428 /* modptr points to the beginning of the object store
429 * created at the Pariticipant.
430 * Object store holds the modified objects involved in the transaction request */
431 ptr = (char *) modptr;
433 /* Process each oid in the machine pile/ group per thread */
434 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
435 if (i < fixed->numread) {//Objs only read and not modified
436 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
438 oid = *((unsigned int *)(objread + incr));
439 incr += sizeof(unsigned int);
440 version = *((unsigned short *)(objread + incr));
441 } else {//Objs modified
443 headptr = (objheader_t *) ptr;
445 version = headptr->version;
446 GETSIZE(tmpsize, headptr);
447 ptr += sizeof(objheader_t) + tmpsize;
450 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
452 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
453 /* Save the oids not found and number of oids not found for later use */
454 oidnotfound[objnotfound] = oid;
456 } else { /* If Obj found in machine (i.e. has not moved) */
457 /* Check if Obj is locked by any previous transaction */
458 if (test_and_set(STATUSPTR(mobj))) {
460 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
462 } else {/* If versions don't match ...HARD ABORT */
464 oidvernotmatch[objvernotmatch] = oid;
468 size += sizeof(objheader_t);
470 /* Send TRANS_DISAGREE to Coordinator */
471 control = TRANS_DISAGREE;
473 } else {/* If Obj is not locked then lock object */
474 /* Save all object oids that are locked on this machine during this transaction request call */
475 oidlocked[objlocked] = OID(((objheader_t *)mobj));
477 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
479 } else { /* If versions don't match ...HARD ABORT */
481 oidvernotmatch[objvernotmatch] = oid;
485 size += sizeof(objheader_t);
487 control = TRANS_DISAGREE;
493 /* send TRANS_DISAGREE and objs*/
495 char *objs = calloc(1, numBytes);
497 for(j = 0; j<objvernotmatch; j++) {
498 objheader_t *header = mhashSearch(oidvernotmatch[j]);
500 GETSIZE(size, header);
501 size += sizeof(objheader_t);
502 memcpy(objs+offset, header, size);
506 for(j = 0; j < objlocked; j++) {
507 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
508 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
511 UnLock(STATUSPTR(headptr));
515 send_data(acceptfd, &control, sizeof(char));
516 send_data(acceptfd, &numBytes, sizeof(int));
517 send_data(acceptfd, objs, numBytes);
518 transinfo->objvernotmatch = oidvernotmatch;
519 transinfo->numvernotmatch = objvernotmatch;
521 free(transinfo->objvernotmatch);
525 /* Decide what control message to send to Coordinator */
526 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
527 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
528 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
534 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
535 * to send to Coordinator based on the votes of oids involved in the transaction */
536 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
537 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
538 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
542 /* Condition to send TRANS_AGREE */
543 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
544 control = TRANS_AGREE;
545 /* Send control message */
546 send_data(acceptfd, &control, sizeof(char));
548 /* Condition to send TRANS_SOFT_ABORT */
549 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
550 control = TRANS_SOFT_ABORT;
552 /* Send control message */
553 send_data(acceptfd, &control, sizeof(char));
555 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
556 if(*(objnotfound) != 0) {
558 msg[0] = *(objnotfound);
559 send_data(acceptfd, &msg, sizeof(int));
560 int size = sizeof(unsigned int)* *(objnotfound);
561 send_data(acceptfd, oidnotfound, size);
565 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
566 * if Participant receives a TRANS_COMMIT */
567 transinfo->objlocked = oidlocked;
568 transinfo->objnotfound = oidnotfound;
569 transinfo->modptr = modptr;
570 transinfo->numlocked = *(objlocked);
571 transinfo->numnotfound = *(objnotfound);
576 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
577 * addresses in lookup table and also changes version number
578 * Sends an ACK back to Coordinator */
579 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
581 objheader_t *newheader;
582 int i = 0, offset = 0;
586 /* Process each modified object saved in the mainobject store */
587 for(i = 0; i < nummod; i++) {
588 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
589 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
592 GETSIZE(tmpsize,header);
593 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
594 header->version += 1;
595 /* If threads are waiting on this object to be updated, notify them */
596 if(header->notifylist != NULL) {
597 notifyAll(&header->notifylist, OID(header), header->version);
599 offset += sizeof(objheader_t) + tmpsize;
605 /* Unlock locked objects */
606 for(i = 0; i < numlocked; i++) {
607 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
608 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
611 UnLock(STATUSPTR(header));
613 //TODO Update location lookup table
615 /* Send ack to coordinator */
616 control = TRANS_SUCESSFUL;
617 send_data((int)acceptfd, &control, sizeof(char));
621 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
622 * Looks for the objects to be prefetched in the main object store.
623 * If objects are not found then record those and if objects are found
624 * then use offset values to prefetch references to other objects */
626 int prefetchReq(int acceptfd) {
627 int i, size, objsize, numoffset = 0;
629 char *recvbuffer, control;
630 unsigned int oid, mid=-1;
636 recv_data((int)acceptfd, &numoffset, sizeof(int));
639 recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
641 if (mid != oidmid.mid) {
643 freeSockWithLock(transPResponseSocketPool, mid, sd);
646 sd = getSockWithLock(transPResponseSocketPool, mid);
648 short offsetarry[numoffset];
649 recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
651 /*Process each oid */
652 if ((header = mhashSearch(oid)) == NULL) {/* Obj not found */
653 /* Save the oids not found in buffer for later use */
654 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
655 char sendbuffer[size];
656 *((int *) sendbuffer) = size;
657 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
658 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
659 control = TRANS_PREFETCH_RESPONSE;
660 sendPrefetchResponse(sd, &control, sendbuffer, &size);
661 } else { /* Object Found */
663 GETSIZE(objsize, header);
664 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
665 char sendbuffer[size];
666 *((int *) (sendbuffer + incr)) = size;
668 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
669 incr += sizeof(char);
670 *((unsigned int *)(sendbuffer+incr)) = oid;
671 incr += sizeof(unsigned int);
672 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
674 control = TRANS_PREFETCH_RESPONSE;
675 sendPrefetchResponse(sd, &control, sendbuffer, &size);
677 /* Calculate the oid corresponding to the offset value */
678 for(i = 0 ; i< numoffset ; i++) {
679 /* Check for arrays */
680 if(TYPE(header) > NUMCLASSES) {
681 int elementsize = classsize[TYPE(header)];
682 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
683 unsigned short length = ao->___length___;
684 /* Check if array out of bounds */
685 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
688 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
690 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
693 /* Don't continue if we hit a NULL pointer */
697 if((header = mhashSearch(oid)) == NULL) {
698 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
699 char sendbuffer[size];
700 *((int *) sendbuffer) = size;
701 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
702 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
704 control = TRANS_PREFETCH_RESPONSE;
705 sendPrefetchResponse(sd, &control, sendbuffer, &size);
707 } else {/* Obj Found */
709 GETSIZE(objsize, header);
710 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
711 char sendbuffer[size];
712 *((int *) (sendbuffer + incr)) = size;
714 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
715 incr += sizeof(char);
716 *((unsigned int *)(sendbuffer+incr)) = oid;
717 incr += sizeof(unsigned int);
718 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
720 control = TRANS_PREFETCH_RESPONSE;
721 sendPrefetchResponse(sd, &control, sendbuffer, &size);
728 freeSockWithLock(transPResponseSocketPool, mid, sd);
733 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
734 send_data(sd, control, sizeof(char));
735 /* Send the buffer with its size */
736 int length = *(size);
737 send_data(sd, sendbuffer, length);
740 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
743 unsigned short newversion;
744 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
746 struct sockaddr_in remoteAddr;
752 oid = *(oidarry + i);
753 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
754 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
757 /* Check to see if versions are same */
759 if (test_and_set(STATUSPTR(header))==0) {
761 newversion = header->version;
762 if(newversion == *(versionarry + i)) {
763 //Add to the notify list
764 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
765 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
768 UnLock(STATUSPTR(header));
770 UnLock(STATUSPTR(header));
771 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
772 perror("processReqNotify():socket()");
775 bzero(&remoteAddr, sizeof(remoteAddr));
776 remoteAddr.sin_family = AF_INET;
777 remoteAddr.sin_port = htons(LISTEN_PORT);
778 remoteAddr.sin_addr.s_addr = htonl(mid);
780 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
781 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
782 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
786 //Send Update notification
787 msg[0] = THREAD_NOTIFY_RESPONSE;
788 *((unsigned int *)&msg[1]) = oid;
789 size = sizeof(unsigned int);
790 *((unsigned short *)(&msg[1]+size)) = newversion;
791 size += sizeof(unsigned short);
792 *((unsigned int *)(&msg[1]+size)) = threadid;
793 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
794 send_data(sd, msg, size);