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 */
31 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
32 /* Initialize attribute for mutex */
33 pthread_mutexattr_init(&mainobjstore_mutex_attr);
34 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
35 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
36 pthread_mutex_init(&lockObjHeader,NULL);
37 if (mhashCreate(HASH_SIZE, LOADFACTOR))
40 if (lhashCreate(HASH_SIZE, LOADFACTOR))
43 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
46 //Initialize socket pool
47 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, DEFAULTSOCKPOOLSIZE)) == NULL) {
48 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
56 int startlistening() {
58 struct sockaddr_in my_addr;
59 socklen_t addrlength = sizeof(struct sockaddr);
62 listenfd = socket(AF_INET, SOCK_STREAM, 0);
68 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
73 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
79 my_addr.sin_family = AF_INET;
80 my_addr.sin_port = htons(LISTEN_PORT);
81 my_addr.sin_addr.s_addr = INADDR_ANY;
82 memset(&(my_addr.sin_zero), '\0', 8);
84 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
89 if (listen(listenfd, BACKLOG) == -1) {
96 /* This function starts the thread to listen on a socket
97 * for tranaction calls */
98 void *dstmListen(void *lfd) {
99 int listenfd=(int)lfd;
101 struct sockaddr_in client_addr;
102 socklen_t addrlength = sizeof(struct sockaddr);
103 pthread_t thread_dstm_accept;
105 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
109 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
110 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
112 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
114 pthread_detach(thread_dstm_accept);
117 /* This function accepts a new connection request, decodes the control message in the connection
118 * and accordingly calls other functions to process new requests */
119 void *dstmAccept(void *acceptfd) {
120 int val, retval, size, sum, sockid;
127 trans_commit_data_t transinfo;
128 unsigned short objType, *versionarry, version;
129 unsigned int *oidarry, numoid, mid, threadid;
131 /* Receive control messages from other machines */
133 int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
137 printf("DEBUG -> RECV Error!.. retrying\n");
142 /* Read oid requested and search if available */
143 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
144 if((srcObj = mhashSearch(oid)) == NULL) {
145 printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
148 h = (objheader_t *) srcObj;
150 size += sizeof(objheader_t);
151 sockid = (int) acceptfd;
153 ctrl = OBJECT_NOT_FOUND;
154 send_data(sockid, &ctrl, sizeof(char));
157 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
158 *((int *)&msg[1])=size;
159 send_data(sockid, &msg, sizeof(msg));
160 send_data(sockid, h, size);
164 case READ_MULT_REQUEST:
170 case MOVE_MULT_REQUEST:
174 /* Read transaction request */
175 transinfo.objlocked = NULL;
176 transinfo.objnotfound = NULL;
177 transinfo.modptr = NULL;
178 transinfo.numlocked = 0;
179 transinfo.numnotfound = 0;
180 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
181 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__);
193 case TRANS_PREFETCH_RESPONSE:
194 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
195 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
200 case START_REMOTE_THREAD:
201 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
202 objType = getObjType(oid);
203 startDSMthread(oid, objType);
206 case THREAD_NOTIFY_REQUEST:
207 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
208 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
209 if((buffer = calloc(1,size)) == NULL) {
210 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
214 recv_data((int)acceptfd, buffer, size);
216 oidarry = calloc(numoid, sizeof(unsigned int));
217 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
218 size = sizeof(unsigned int) * numoid;
219 versionarry = calloc(numoid, sizeof(unsigned short));
220 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
221 size += sizeof(unsigned short) * numoid;
222 mid = *((unsigned int *)(buffer+size));
223 size += sizeof(unsigned int);
224 threadid = *((unsigned int *)(buffer+size));
225 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
230 case THREAD_NOTIFY_RESPONSE:
231 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
232 if((buffer = calloc(1,size)) == NULL) {
233 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
237 recv_data((int)acceptfd, buffer, size);
239 oid = *((unsigned int *)buffer);
240 size = sizeof(unsigned int);
241 version = *((unsigned short *)(buffer+size));
242 size += sizeof(unsigned short);
243 threadid = *((unsigned int *)(buffer+size));
244 threadNotify(oid,version,threadid);
248 case CLOSE_CONNECTION:
249 goto closeconnection;
252 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
257 /* Close connection */
258 if (close((int)acceptfd) == -1)
263 /* This function reads the information available in a transaction request
264 * and makes a function call to process the request */
265 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
268 unsigned int *oidmod, oid;
270 objheader_t *headaddr;
271 int sum, i, size, n, val;
275 /* Read fixed_data_t data structure */
276 size = sizeof(fixed) - 1;
277 ptr = (char *)&fixed;;
278 fixed.control = TRANS_REQUEST;
279 recv_data((int)acceptfd, ptr+1, size);
281 /* Read list of mids */
282 int mcount = fixed.mcount;
283 size = mcount * sizeof(unsigned int);
284 unsigned int listmid[mcount];
285 ptr = (char *) listmid;
286 recv_data((int)acceptfd, ptr, size);
288 /* Read oid and version tuples for those objects that are not modified in the transaction */
289 int numread = fixed.numread;
290 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
292 if(numread != 0) { //If pile contains more than one object to be read,
293 // keep reading all objects
294 recv_data((int)acceptfd, objread, size);
297 /* Read modified objects */
298 if(fixed.nummod != 0) {
299 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
300 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
303 size = fixed.sum_bytes;
304 recv_data((int)acceptfd, modptr, size);
307 /* Create an array of oids for modified objects */
308 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
309 if (oidmod == NULL) {
310 printf("calloc error %s, %d\n", __FILE__, __LINE__);
313 ptr = (char *) modptr;
314 for(i = 0 ; i < fixed.nummod; i++) {
316 headaddr = (objheader_t *) ptr;
319 GETSIZE(tmpsize, headaddr);
320 ptr += sizeof(objheader_t) + tmpsize;
323 /*Process the information read */
324 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
325 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
341 /* This function processes the Coordinator's transaction request using "handleTransReq"
342 * function and sends a reply to the co-ordinator.
343 * Following this it also receives a new control message from the co-ordinator and processes this message*/
344 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
345 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
346 char control, sendctrl, retval;
347 objheader_t *tmp_header;
351 /* Send reply to the Coordinator */
352 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
353 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
357 recv_data((int)acceptfd, &control, sizeof(char));
359 /* Process the new control message */
362 if (fixed->nummod > 0)
364 /* Unlock objects that was locked due to this transaction */
365 for(i = 0; i< transinfo->numlocked; i++) {
366 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
367 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
370 UnLock(STATUSPTR(header));
373 /* Send ack to Coordinator */
374 sendctrl = TRANS_UNSUCESSFUL;
375 send_data((int)acceptfd, &sendctrl, sizeof(char));
379 /* Invoke the transCommit process() */
380 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
381 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
383 if (transinfo->objlocked != NULL) {
384 free(transinfo->objlocked);
386 if (transinfo->objnotfound != NULL) {
387 free(transinfo->objnotfound);
393 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
397 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
398 //TODO Use fixed.trans_id TID since Client may have died
403 if (transinfo->objlocked != NULL) {
404 free(transinfo->objlocked);
406 if (transinfo->objnotfound != NULL) {
407 free(transinfo->objnotfound);
412 /* This function increments counters while running a voting decision on all objects involved
413 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
414 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
416 unsigned short version;
417 char control = 0, *ptr;
419 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
421 objheader_t *headptr;
423 /* Counters and arrays to formulate decision on control message to be sent */
424 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
425 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
426 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
427 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
428 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
430 /* modptr points to the beginning of the object store
431 * created at the Pariticipant.
432 * Object store holds the modified objects involved in the transaction request */
433 ptr = (char *) modptr;
435 /* Process each oid in the machine pile/ group per thread */
436 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
437 if (i < fixed->numread) { //Objs only read and not modified
438 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
440 oid = *((unsigned int *)(objread + incr));
441 incr += sizeof(unsigned int);
442 version = *((unsigned short *)(objread + incr));
443 } else { //Objs modified
445 headptr = (objheader_t *) ptr;
447 version = headptr->version;
448 GETSIZE(tmpsize, headptr);
449 ptr += sizeof(objheader_t) + tmpsize;
452 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
454 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
455 /* Save the oids not found and number of oids not found for later use */
456 oidnotfound[objnotfound] = oid;
458 } else { /* If Obj found in machine (i.e. has not moved) */
459 /* Check if Obj is locked by any previous transaction */
460 if (test_and_set(STATUSPTR(mobj))) {
462 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
464 } else { /* If versions don't match ...HARD ABORT */
466 oidvernotmatch[objvernotmatch] = oid;
470 size += sizeof(objheader_t);
472 /* Send TRANS_DISAGREE to Coordinator */
473 control = TRANS_DISAGREE;
475 } else { /* If Obj is not locked then lock object */
476 /* Save all object oids that are locked on this machine during this transaction request call */
477 oidlocked[objlocked] = OID(((objheader_t *)mobj));
479 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
481 } else { /* If versions don't match ...HARD ABORT */
483 oidvernotmatch[objvernotmatch] = oid;
487 size += sizeof(objheader_t);
489 control = TRANS_DISAGREE;
495 /* send TRANS_DISAGREE and objs*/
498 char *objs = calloc(1, numBytes);
500 for(j = 0; j<objvernotmatch; j++) {
501 objheader_t *header = mhashSearch(oidvernotmatch[j]);
503 GETSIZE(size, header);
504 size += sizeof(objheader_t);
505 memcpy(objs+offset, header, size);
510 for(j = 0; j < objlocked; j++) {
511 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
512 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
515 UnLock(STATUSPTR(headptr));
519 send_data(acceptfd, &control, sizeof(char));
521 send_data(acceptfd, &numBytes, sizeof(int));
522 send_data(acceptfd, objs, numBytes);
523 transinfo->objvernotmatch = oidvernotmatch;
524 transinfo->numvernotmatch = objvernotmatch;
526 free(transinfo->objvernotmatch);
531 /* Decide what control message to send to Coordinator */
532 if ((control = 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__);
540 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
541 * to send to Coordinator based on the votes of oids involved in the transaction */
542 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
543 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
544 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 send_data(acceptfd, &control, sizeof(char));
554 /* Condition to send TRANS_SOFT_ABORT */
555 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
556 control = TRANS_SOFT_ABORT;
558 /* Send control message */
559 send_data(acceptfd, &control, sizeof(char));
561 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
562 if(*(objnotfound) != 0) {
564 msg[0] = *(objnotfound);
565 send_data(acceptfd, &msg, sizeof(int));
566 int size = sizeof(unsigned int)* *(objnotfound);
567 send_data(acceptfd, oidnotfound, size);
571 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
572 * if Participant receives a TRANS_COMMIT */
573 transinfo->objlocked = oidlocked;
574 transinfo->objnotfound = oidnotfound;
575 transinfo->modptr = modptr;
576 transinfo->numlocked = *(objlocked);
577 transinfo->numnotfound = *(objnotfound);
582 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
583 * addresses in lookup table and also changes version number
584 * Sends an ACK back to Coordinator */
585 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
587 objheader_t *newheader;
588 int i = 0, offset = 0;
592 /* Process each modified object saved in the mainobject store */
593 for(i = 0; i < nummod; i++) {
594 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
595 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
598 GETSIZE(tmpsize,header);
599 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
600 header->version += 1;
601 /* If threads are waiting on this object to be updated, notify them */
602 if(header->notifylist != NULL) {
603 notifyAll(&header->notifylist, OID(header), header->version);
605 offset += sizeof(objheader_t) + tmpsize;
611 /* Unlock locked objects */
612 for(i = 0; i < numlocked; i++) {
613 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
614 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
617 UnLock(STATUSPTR(header));
619 //TODO Update location lookup table
621 /* Send ack to coordinator */
622 control = TRANS_SUCESSFUL;
623 send_data((int)acceptfd, &control, sizeof(char));
627 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
628 * Looks for the objects to be prefetched in the main object store.
629 * If objects are not found then record those and if objects are found
630 * then use offset values to prefetch references to other objects */
632 int prefetchReq(int acceptfd) {
633 int i, size, objsize, numoffset = 0;
635 char *recvbuffer, control;
636 unsigned int oid, mid=-1;
642 recv_data((int)acceptfd, &numoffset, sizeof(int));
645 recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
647 if (mid != oidmid.mid) {
649 freeSockWithLock(transPResponseSocketPool, mid, sd);
652 sd = getSockWithLock(transPResponseSocketPool, mid);
654 short offsetarry[numoffset];
655 recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
657 /*Process each oid */
658 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
659 /* Save the oids not found in buffer for later use */
660 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
661 char sendbuffer[size];
662 *((int *) sendbuffer) = size;
663 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
664 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
665 control = TRANS_PREFETCH_RESPONSE;
666 sendPrefetchResponse(sd, &control, sendbuffer, &size);
667 } else { /* Object Found */
669 GETSIZE(objsize, header);
670 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
671 char sendbuffer[size];
672 *((int *)(sendbuffer + incr)) = size;
674 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
675 incr += sizeof(char);
676 *((unsigned int *)(sendbuffer+incr)) = oid;
677 incr += sizeof(unsigned int);
678 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
680 control = TRANS_PREFETCH_RESPONSE;
681 sendPrefetchResponse(sd, &control, sendbuffer, &size);
683 /* Calculate the oid corresponding to the offset value */
684 for(i = 0 ; i< numoffset ; i++) {
685 /* Check for arrays */
686 if(TYPE(header) > NUMCLASSES) {
687 int elementsize = classsize[TYPE(header)];
688 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
689 unsigned short length = ao->___length___;
690 /* Check if array out of bounds */
691 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
694 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
696 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
699 /* Don't continue if we hit a NULL pointer */
703 if((header = mhashSearch(oid)) == NULL) {
704 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
705 char sendbuffer[size];
706 *((int *) sendbuffer) = size;
707 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
708 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
710 control = TRANS_PREFETCH_RESPONSE;
711 sendPrefetchResponse(sd, &control, sendbuffer, &size);
713 } else { /* Obj Found */
715 GETSIZE(objsize, header);
716 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
717 char sendbuffer[size];
718 *((int *)(sendbuffer + incr)) = size;
720 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
721 incr += sizeof(char);
722 *((unsigned int *)(sendbuffer+incr)) = oid;
723 incr += sizeof(unsigned int);
724 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
726 control = TRANS_PREFETCH_RESPONSE;
727 sendPrefetchResponse(sd, &control, sendbuffer, &size);
734 freeSockWithLock(transPResponseSocketPool, mid, sd);
739 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
740 send_data(sd, control, sizeof(char));
741 /* Send the buffer with its size */
742 int length = *(size);
743 send_data(sd, sendbuffer, length);
746 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
749 unsigned short newversion;
750 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
752 struct sockaddr_in remoteAddr;
758 oid = *(oidarry + i);
759 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
760 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
763 /* Check to see if versions are same */
765 if (test_and_set(STATUSPTR(header))==0) {
767 newversion = header->version;
768 if(newversion == *(versionarry + i)) {
769 //Add to the notify list
770 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
771 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
774 UnLock(STATUSPTR(header));
776 UnLock(STATUSPTR(header));
777 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
778 perror("processReqNotify():socket()");
781 bzero(&remoteAddr, sizeof(remoteAddr));
782 remoteAddr.sin_family = AF_INET;
783 remoteAddr.sin_port = htons(LISTEN_PORT);
784 remoteAddr.sin_addr.s_addr = htonl(mid);
786 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
787 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
788 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
792 //Send Update notification
793 msg[0] = THREAD_NOTIFY_RESPONSE;
794 *((unsigned int *)&msg[1]) = oid;
795 size = sizeof(unsigned int);
796 *((unsigned short *)(&msg[1]+size)) = newversion;
797 size += sizeof(unsigned short);
798 *((unsigned int *)(&msg[1]+size)) = threadid;
799 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
800 send_data(sd, msg, size);