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
25 extern int classsize[];
27 objstr_t *mainobjstore;
28 pthread_mutex_t mainobjstore_mutex;
29 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
30 /**********************************************************
31 * Global variables to map socketid and remote mid
33 **************************************************/
34 midSocketInfo_t sockArray[NUM_MACHINES];
35 int sockCount; //number of connections with all remote machines(one socket per mc)
36 int sockIdFound; //track if socket file descriptor is already established
37 pthread_mutex_t sockLock = PTHREAD_MUTEX_INITIALIZER; //lock to prevent global sock variables to be inconsistent
39 /* This function initializes the main objects store and creates the
40 * global machine and location lookup table */
44 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
45 /* Initialize attribute for mutex */
46 pthread_mutexattr_init(&mainobjstore_mutex_attr);
47 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
48 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
49 if (mhashCreate(HASH_SIZE, LOADFACTOR))
52 if (lhashCreate(HASH_SIZE, LOADFACTOR))
55 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
58 //Initialize mid to socketid mapping array
61 for(t = 0; t < NUM_MACHINES; t++) {
63 sockArray[t].sockid = 0;
69 /* This function starts the thread to listen on a socket
70 * for tranaction calls */
73 int listenfd, acceptfd;
74 struct sockaddr_in my_addr;
75 struct sockaddr_in client_addr;
76 socklen_t addrlength = sizeof(struct sockaddr);
77 pthread_t thread_dstm_accept;
81 listenfd = socket(AF_INET, SOCK_STREAM, 0);
88 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
93 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
99 my_addr.sin_family = AF_INET;
100 my_addr.sin_port = htons(LISTEN_PORT);
101 my_addr.sin_addr.s_addr = INADDR_ANY;
102 memset(&(my_addr.sin_zero), '\0', 8);
104 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
110 if (listen(listenfd, BACKLOG) == -1)
116 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
120 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
122 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
124 pthread_detach(thread_dstm_accept);
127 /* This function accepts a new connection request, decodes the control message in the connection
128 * and accordingly calls other functions to process new requests */
129 void *dstmAccept(void *acceptfd) {
130 int val, retval, size, sum, sockid;
137 trans_commit_data_t transinfo;
138 unsigned short objType, *versionarry, version;
139 unsigned int *oidarry, numoid, mid, threadid;
141 transinfo.objlocked = NULL;
142 transinfo.objnotfound = NULL;
143 transinfo.modptr = NULL;
144 transinfo.numlocked = 0;
145 transinfo.numnotfound = 0;
147 /* Receive control messages from other machines */
149 int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
154 /* Read oid requested and search if available */
155 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
156 if((srcObj = mhashSearch(oid)) == NULL) {
157 printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
160 h = (objheader_t *) srcObj;
162 size += sizeof(objheader_t);
163 sockid = (int) acceptfd;
166 ctrl = OBJECT_NOT_FOUND;
167 send_data(sockid, &ctrl, sizeof(char));
170 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
171 *((int *)&msg[1])=size;
172 send_data(sockid, &msg, sizeof(msg));
173 send_data(sockid, h, size);
177 case READ_MULT_REQUEST:
183 case MOVE_MULT_REQUEST:
187 /* Read transaction request */
188 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
189 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
194 if((val = prefetchReq((int)acceptfd)) != 0) {
195 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
199 case TRANS_PREFETCH_RESPONSE:
200 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
201 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
205 case START_REMOTE_THREAD:
206 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
207 objType = getObjType(oid);
208 startDSMthread(oid, objType);
211 case THREAD_NOTIFY_REQUEST:
212 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
213 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
214 if((buffer = calloc(1,size)) == NULL) {
215 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
219 recv_data((int)acceptfd, buffer, size);
221 oidarry = calloc(numoid, sizeof(unsigned int));
222 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
223 size = sizeof(unsigned int) * numoid;
224 versionarry = calloc(numoid, sizeof(unsigned short));
225 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
226 size += sizeof(unsigned short) * numoid;
227 mid = *((unsigned int *)(buffer+size));
228 size += sizeof(unsigned int);
229 threadid = *((unsigned int *)(buffer+size));
230 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
235 case THREAD_NOTIFY_RESPONSE:
236 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
237 if((buffer = calloc(1,size)) == NULL) {
238 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
242 recv_data((int)acceptfd, buffer, size);
244 oid = *((unsigned int *)buffer);
245 size = sizeof(unsigned int);
246 version = *((unsigned short *)(buffer+size));
247 size += sizeof(unsigned short);
248 threadid = *((unsigned int *)(buffer+size));
249 threadNotify(oid,version,threadid);
253 case CLOSE_CONNECTION:
254 goto closeconnection;
257 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
262 /* Close connection */
263 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, i, size, n, val;
280 /* Read fixed_data_t data structure */
281 size = sizeof(fixed) - 1;
282 ptr = (char *)&fixed;;
283 fixed.control = TRANS_REQUEST;
284 recv_data((int)acceptfd, ptr+1, size);
286 /* Read list of mids */
287 int mcount = fixed.mcount;
288 size = mcount * sizeof(unsigned int);
289 unsigned int listmid[mcount];
290 ptr = (char *) listmid;
291 recv_data((int)acceptfd, ptr, size);
293 /* Read oid and version tuples for those objects that are not modified in the transaction */
294 int numread = fixed.numread;
295 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
297 if(numread != 0) { //If pile contains more than one object to be read,
298 // keep reading all objects
299 recv_data((int)acceptfd, objread, size);
302 /* Read modified objects */
303 if(fixed.nummod != 0) {
304 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
305 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
308 size = fixed.sum_bytes;
309 recv_data((int)acceptfd, modptr, size);
312 /* Create an array of oids for modified objects */
313 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
316 printf("calloc error %s, %d\n", __FILE__, __LINE__);
319 ptr = (char *) modptr;
320 for(i = 0 ; i < fixed.nummod; i++) {
322 headaddr = (objheader_t *) ptr;
325 GETSIZE(tmpsize, headaddr);
326 ptr += sizeof(objheader_t) + tmpsize;
329 /*Process the information read */
330 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
331 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
347 /* This function processes the Coordinator's transaction request using "handleTransReq"
348 * function and sends a reply to the co-ordinator.
349 * Following this it also receives a new control message from the co-ordinator and processes this message*/
350 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
351 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
352 char control, sendctrl, retval;
353 objheader_t *tmp_header;
357 /* Send reply to the Coordinator */
358 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
359 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
363 recv_data((int)acceptfd, &control, sizeof(char));
365 /* Process the new control message */
368 if (fixed->nummod > 0)
370 /* Unlock objects that was locked due to this transaction */
371 for(i = 0; i< transinfo->numlocked; i++) {
372 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
373 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
376 STATUS(((objheader_t *)header)) &= ~(LOCK);
379 /* Send ack to Coordinator */
380 sendctrl = TRANS_UNSUCESSFUL;
381 send_data((int)acceptfd, &sendctrl, sizeof(char));
385 /* Invoke the transCommit process() */
386 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
387 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
389 if (transinfo->objlocked != NULL) {
390 free(transinfo->objlocked);
392 if (transinfo->objnotfound != NULL) {
393 free(transinfo->objnotfound);
399 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
402 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
403 //TODO Use fixed.trans_id TID since Client may have died
408 if (transinfo->objlocked != NULL) {
409 free(transinfo->objlocked);
411 if (transinfo->objnotfound != NULL) {
412 free(transinfo->objnotfound);
418 /* This function increments counters while running a voting decision on all objects involved
419 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
420 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
422 unsigned short version;
423 char control = 0, *ptr;
425 unsigned int *oidnotfound, *oidlocked;
427 objheader_t *headptr;
429 /* Counters and arrays to formulate decision on control message to be sent */
430 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
431 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
432 int objnotfound = 0, objlocked = 0;
433 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
435 /* modptr points to the beginning of the object store
436 * created at the Pariticipant.
437 * Object store holds the modified objects involved in the transaction request */
438 ptr = (char *) modptr;
440 /* Process each oid in the machine pile/ group per thread */
441 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
442 if (i < fixed->numread) {//Objs only read and not modified
443 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
445 oid = *((unsigned int *)(objread + incr));
446 incr += sizeof(unsigned int);
447 version = *((unsigned short *)(objread + incr));
448 } else {//Objs modified
450 headptr = (objheader_t *) ptr;
452 version = headptr->version;
453 GETSIZE(tmpsize, headptr);
454 ptr += sizeof(objheader_t) + tmpsize;
457 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
459 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
460 /* Save the oids not found and number of oids not found for later use */
461 oidnotfound[objnotfound] = oid;
463 } else { /* If Obj found in machine (i.e. has not moved) */
464 /* Check if Obj is locked by any previous transaction */
465 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
466 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
468 } else {/* If versions don't match ...HARD ABORT */
470 /* Send TRANS_DISAGREE to Coordinator */
471 control = TRANS_DISAGREE;
473 for(j = 0; j < objlocked; j++) {
474 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
475 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
478 STATUS(headptr) &= ~(LOCK);
482 send_data(acceptfd, &control, sizeof(char));
485 } else {/* If Obj is not locked then lock object */
486 STATUS(((objheader_t *)mobj)) |= LOCK;
487 /* Save all object oids that are locked on this machine during this transaction request call */
488 oidlocked[objlocked] = OID(((objheader_t *)mobj));
490 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
492 } else { /* If versions don't match ...HARD ABORT */
494 control = TRANS_DISAGREE;
496 for(j = 0; j < objlocked; j++) {
497 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
498 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
501 STATUS(headptr) &= ~(LOCK);
506 /* Send TRANS_DISAGREE to Coordinator */
507 send_data(acceptfd, &control, sizeof(char));
514 /* Decide what control message to send to Coordinator */
515 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
516 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
517 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
524 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
525 * to send to Coordinator based on the votes of oids involved in the transaction */
526 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
527 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
528 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
532 /* Condition to send TRANS_AGREE */
533 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
534 control = TRANS_AGREE;
535 /* Send control message */
536 send_data(acceptfd, &control, sizeof(char));
538 /* Condition to send TRANS_SOFT_ABORT */
539 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
540 control = TRANS_SOFT_ABORT;
542 /* Send control message */
543 send_data(acceptfd, &control, sizeof(char));
545 /* Send number of oids not found and the missing oids if objects are missing in the machine */
546 if(*(objnotfound) != 0) {
548 msg[0] = *(objnotfound);
549 send_data(acceptfd, &msg, sizeof(int));
550 int size = sizeof(unsigned int)* *(objnotfound);
551 send_data(acceptfd, oidnotfound, size);
555 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
556 * if Participant receives a TRANS_COMMIT */
557 transinfo->objlocked = oidlocked;
558 transinfo->objnotfound = oidnotfound;
559 transinfo->modptr = modptr;
560 transinfo->numlocked = *(objlocked);
561 transinfo->numnotfound = *(objnotfound);
566 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
567 * addresses in lookup table and also changes version number
568 * Sends an ACK back to Coordinator */
569 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
571 objheader_t *newheader;
572 int i = 0, offset = 0;
576 /* Process each modified object saved in the mainobject store */
577 for(i = 0; i < nummod; i++) {
578 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
579 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
582 GETSIZE(tmpsize,header);
583 pthread_mutex_lock(&mainobjstore_mutex);
584 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
585 header->version += 1;
586 /* If threads are waiting on this object to be updated, notify them */
587 if(header->notifylist != NULL) {
588 notifyAll(&header->notifylist, OID(header), header->version);
590 pthread_mutex_unlock(&mainobjstore_mutex);
591 offset += sizeof(objheader_t) + tmpsize;
597 /* Unlock locked objects */
598 for(i = 0; i < numlocked; i++) {
599 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
600 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
603 STATUS(header) &= ~(LOCK);
605 //TODO Update location lookup table
607 /* Send ack to coordinator */
608 control = TRANS_SUCESSFUL;
609 send_data((int)acceptfd, &control, sizeof(char));
613 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
614 * Looks for the objects to be prefetched in the main object store.
615 * If objects are not found then record those and if objects are found
616 * then use offset values to prefetch references to other objects */
618 int prefetchReq(int acceptfd) {
619 int i, size, objsize, numbytes = 0, isArray = 0, numoffset = 0;
621 char *recvbuffer, *sendbuffer, control;
622 unsigned int oid, mid;
625 struct sockaddr_in remoteAddr;
628 recv_data((int)acceptfd, &length, sizeof(int));
630 size = length - sizeof(int);
631 if((recvbuffer = calloc(1, size)) == NULL) {
632 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
635 recv_data((int)acceptfd, recvbuffer, size);
636 oid = *((unsigned int *) recvbuffer);
637 mid = *((unsigned int *) (recvbuffer + sizeof(unsigned int)));
638 size = size - (2 * sizeof(unsigned int));
639 numoffset = size / sizeof(short);
640 if((offsetarry = calloc(numoffset, sizeof(short))) == NULL) {
641 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
645 memcpy(offsetarry, recvbuffer + (2 * sizeof(unsigned int)), size);
647 pthread_mutex_lock(&sockLock);
649 pthread_mutex_unlock(&sockLock);
650 /* If socket is already established then send data reusing socket */
651 for(i = 0; i < NUM_MACHINES; i++) {
652 if(sockArray[i].mid == mid) {
653 sd = sockArray[i].sockid;
654 pthread_mutex_lock(&sockLock);
656 pthread_mutex_unlock(&sockLock);
661 if(sockIdFound == 0) {
662 if(sockCount < NUM_MACHINES) {
663 /* Create socket to send information */
664 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
665 perror("prefetchReq():socket()");
668 bzero(&remoteAddr, sizeof(remoteAddr));
669 remoteAddr.sin_family = AF_INET;
670 remoteAddr.sin_port = htons(LISTEN_PORT);
671 remoteAddr.sin_addr.s_addr = htonl(mid);
673 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
675 printf("Error: prefetchReq():error %d connecting to %s:%d\n", errno,
676 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
680 sockArray[sockCount].mid = mid;
681 sockArray[sockCount].sockid = sd;
682 pthread_mutex_lock(&sockLock);
684 pthread_mutex_unlock(&sockLock);
686 //TODO Fix for connecting to more than 2 machines && close socket
687 printf("%s(): Error: Currently works for only 2 machines\n", __func__);
692 /*Process each oid */
693 if ((header = mhashSearch(oid)) == NULL) {/* Obj not found */
694 /* Save the oids not found in buffer for later use */
695 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
696 if((sendbuffer = calloc(1, size)) == NULL) {
697 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
702 *((int *) sendbuffer) = size;
703 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
704 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
706 control = TRANS_PREFETCH_RESPONSE;
707 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
709 printf("Error: %s() in sending prefetch response at %s, %d\n",
710 __func__, __FILE__, __LINE__);
714 } else { /* Object Found */
716 GETSIZE(objsize, header);
717 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
718 if((sendbuffer = calloc(1, size)) == NULL) {
719 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
724 *((int *) (sendbuffer + incr)) = size;
726 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
727 incr += sizeof(char);
728 *((unsigned int *)(sendbuffer+incr)) = oid;
729 incr += sizeof(unsigned int);
730 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
732 control = TRANS_PREFETCH_RESPONSE;
733 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
735 printf("Error: %s() in sending prefetch response at %s, %d\n",
736 __func__, __FILE__, __LINE__);
741 /* Calculate the oid corresponding to the offset value */
742 for(i = 0 ; i< numoffset ; i++) {
743 /* Check for arrays */
744 if(TYPE(header) > NUMCLASSES) {
748 int elementsize = classsize[TYPE(header)];
749 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
750 unsigned short length = ao->___length___;
751 /* Check if array out of bounds */
752 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
755 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
757 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
760 if((header = mhashSearch(oid)) == NULL) {
761 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
762 if((sendbuffer = calloc(1, size)) == NULL) {
763 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
768 *((int *) sendbuffer) = size;
769 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
770 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
772 control = TRANS_PREFETCH_RESPONSE;
773 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
775 printf("Error: %s() in sending prefetch response at %s, %d\n",
781 } else {/* Obj Found */
783 GETSIZE(objsize, header);
784 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
785 if((sendbuffer = calloc(1, size)) == NULL) {
786 printf("Calloc error at %s,%d\n", __func__, __FILE__, __LINE__);
791 *((int *) (sendbuffer + incr)) = size;
793 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
794 incr += sizeof(char);
795 *((unsigned int *)(sendbuffer+incr)) = oid;
796 incr += sizeof(unsigned int);
797 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
799 control = TRANS_PREFETCH_RESPONSE;
800 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
802 printf("Error: %s() in sending prefetch response at %s, %d\n",
803 __func__, __FILE__, __LINE__);
813 } while (length != -1);
817 int sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
820 send_data(sd, control, sizeof(char));
821 /* Send the buffer with its size */
822 int length = *(size);
823 send_data(sd, sendbuffer, length);
828 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
831 unsigned short newversion;
832 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
834 struct sockaddr_in remoteAddr;
840 oid = *(oidarry + i);
841 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
842 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
845 /* Check to see if versions are same */
847 if ((STATUS(header) & LOCK) != LOCK) {
848 //FIXME make locking atomic
849 STATUS(header) |= LOCK;
850 newversion = header->version;
851 if(newversion == *(versionarry + i)) {
852 //Add to the notify list
853 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
854 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
857 STATUS(header) &= ~(LOCK);
859 STATUS(header) &= ~(LOCK);
860 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
861 perror("processReqNotify():socket()");
864 bzero(&remoteAddr, sizeof(remoteAddr));
865 remoteAddr.sin_family = AF_INET;
866 remoteAddr.sin_port = htons(LISTEN_PORT);
867 remoteAddr.sin_addr.s_addr = htonl(mid);
869 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
870 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
871 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
875 //Send Update notification
876 msg[0] = THREAD_NOTIFY_RESPONSE;
877 *((unsigned int *)&msg[1]) = oid;
878 size = sizeof(unsigned int);
879 *((unsigned short *)(&msg[1]+size)) = newversion;
880 size += sizeof(unsigned short);
881 *((unsigned int *)(&msg[1]+size)) = threadid;
882 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
883 send_data(sd, msg, size);