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)
131 int val, retval, size, sum, sockid;
138 trans_commit_data_t transinfo;
139 unsigned short objType, *versionarry, version;
140 unsigned int *oidarry, numoid, mid, threadid;
142 transinfo.objlocked = NULL;
143 transinfo.objnotfound = NULL;
144 transinfo.modptr = NULL;
145 transinfo.numlocked = 0;
146 transinfo.numnotfound = 0;
148 /* Receive control messages from other machines */
149 recv_data((int)acceptfd, &control, sizeof(char));
153 /* Read oid requested and search if available */
154 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
155 if((srcObj = mhashSearch(oid)) == NULL) {
156 printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
159 h = (objheader_t *) srcObj;
161 size += sizeof(objheader_t);
162 sockid = (int) acceptfd;
165 ctrl = OBJECT_NOT_FOUND;
166 send_data(sockid, &ctrl, sizeof(char));
169 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
170 *((int *)&msg[1])=size;
171 send_data(sockid, &msg, sizeof(msg));
172 send_data(sockid, h, size);
176 case READ_MULT_REQUEST:
182 case MOVE_MULT_REQUEST:
186 /* Read transaction request */
187 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
188 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__);
198 recv_data((int)acceptfd, &control, sizeof(char));
199 } while (control == TRANS_PREFETCH);
201 case TRANS_PREFETCH_RESPONSE:
203 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
204 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
207 //} while (control == TRANS_PREFETCH_RESPONSE);
209 case START_REMOTE_THREAD:
210 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
211 objType = getObjType(oid);
212 startDSMthread(oid, objType);
215 case THREAD_NOTIFY_REQUEST:
216 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
217 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
218 if((buffer = calloc(1,size)) == NULL) {
219 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
223 recv_data((int)acceptfd, buffer, size);
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);
239 case THREAD_NOTIFY_RESPONSE:
240 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
241 if((buffer = calloc(1,size)) == NULL) {
242 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
246 recv_data((int)acceptfd, buffer, size);
248 oid = *((unsigned int *)buffer);
249 size = sizeof(unsigned int);
250 version = *((unsigned short *)(buffer+size));
251 size += sizeof(unsigned short);
252 threadid = *((unsigned int *)(buffer+size));
253 threadNotify(oid,version,threadid);
258 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
261 /* Close connection */
262 if (close((int)acceptfd) == -1)
267 /* This function reads the information available in a transaction request
268 * and makes a function call to process the request */
269 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
272 unsigned int *oidmod, oid;
274 objheader_t *headaddr;
275 int sum, i, size, n, val;
279 /* Read fixed_data_t data structure */
280 size = sizeof(fixed) - 1;
281 ptr = (char *)&fixed;;
282 fixed.control = TRANS_REQUEST;
283 recv_data((int)acceptfd, ptr+1, size);
285 /* Read list of mids */
286 int mcount = fixed.mcount;
287 size = mcount * sizeof(unsigned int);
288 unsigned int listmid[mcount];
289 ptr = (char *) listmid;
290 recv_data((int)acceptfd, ptr, size);
292 /* Read oid and version tuples for those objects that are not modified in the transaction */
293 int numread = fixed.numread;
294 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
296 if(numread != 0) { //If pile contains more than one object to be read,
297 // keep reading all objects
298 recv_data((int)acceptfd, objread, size);
301 /* Read modified objects */
302 if(fixed.nummod != 0) {
303 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
304 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
307 size = fixed.sum_bytes;
308 recv_data((int)acceptfd, modptr, size);
311 /* Create an array of oids for modified objects */
312 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
315 printf("calloc error %s, %d\n", __FILE__, __LINE__);
318 ptr = (char *) modptr;
319 for(i = 0 ; i < fixed.nummod; i++) {
321 headaddr = (objheader_t *) ptr;
324 GETSIZE(tmpsize, headaddr);
325 ptr += sizeof(objheader_t) + tmpsize;
328 /*Process the information read */
329 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
330 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
346 /* This function processes the Coordinator's transaction request using "handleTransReq"
347 * function and sends a reply to the co-ordinator.
348 * Following this it also receives a new control message from the co-ordinator and processes this message*/
349 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
350 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
351 char control, sendctrl, retval;
352 objheader_t *tmp_header;
356 /* Send reply to the Coordinator */
357 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
358 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
362 recv_data((int)acceptfd, &control, sizeof(char));
364 /* Process the new control message */
367 if (fixed->nummod > 0)
369 /* Unlock objects that was locked due to this transaction */
370 for(i = 0; i< transinfo->numlocked; i++) {
371 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
372 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
375 STATUS(((objheader_t *)header)) &= ~(LOCK);
378 /* Send ack to Coordinator */
379 sendctrl = TRANS_UNSUCESSFUL;
380 send_data((int)acceptfd, &sendctrl, sizeof(char));
384 /* Invoke the transCommit process() */
385 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
386 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
388 if (transinfo->objlocked != NULL) {
389 free(transinfo->objlocked);
391 if (transinfo->objnotfound != NULL) {
392 free(transinfo->objnotfound);
398 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
401 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
402 //TODO Use fixed.trans_id TID since Client may have died
407 if (transinfo->objlocked != NULL) {
408 free(transinfo->objlocked);
410 if (transinfo->objnotfound != NULL) {
411 free(transinfo->objnotfound);
417 /* This function increments counters while running a voting decision on all objects involved
418 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
419 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
421 unsigned short version;
422 char control = 0, *ptr;
424 unsigned int *oidnotfound, *oidlocked;
426 objheader_t *headptr;
428 /* Counters and arrays to formulate decision on control message to be sent */
429 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
430 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
431 int objnotfound = 0, objlocked = 0;
432 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
434 /* modptr points to the beginning of the object store
435 * created at the Pariticipant.
436 * Object store holds the modified objects involved in the transaction request */
437 ptr = (char *) modptr;
439 /* Process each oid in the machine pile/ group per thread */
440 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
441 if (i < fixed->numread) {//Objs only read and not modified
442 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
444 oid = *((unsigned int *)(objread + incr));
445 incr += sizeof(unsigned int);
446 version = *((unsigned short *)(objread + incr));
447 } else {//Objs modified
449 headptr = (objheader_t *) ptr;
451 version = headptr->version;
452 GETSIZE(tmpsize, headptr);
453 ptr += sizeof(objheader_t) + tmpsize;
456 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
458 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
459 /* Save the oids not found and number of oids not found for later use */
460 oidnotfound[objnotfound] = oid;
462 } else { /* If Obj found in machine (i.e. has not moved) */
463 /* Check if Obj is locked by any previous transaction */
464 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
465 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
467 } else {/* If versions don't match ...HARD ABORT */
469 /* Send TRANS_DISAGREE to Coordinator */
470 control = TRANS_DISAGREE;
472 for(j = 0; j < objlocked; j++) {
473 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
474 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
477 STATUS(headptr) &= ~(LOCK);
481 send_data(acceptfd, &control, sizeof(char));
484 } else {/* If Obj is not locked then lock object */
485 STATUS(((objheader_t *)mobj)) |= LOCK;
486 /* Save all object oids that are locked on this machine during this transaction request call */
487 oidlocked[objlocked] = OID(((objheader_t *)mobj));
489 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
491 } else { /* If versions don't match ...HARD ABORT */
493 control = TRANS_DISAGREE;
495 for(j = 0; j < objlocked; j++) {
496 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
497 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
500 STATUS(headptr) &= ~(LOCK);
505 /* Send TRANS_DISAGREE to Coordinator */
506 send_data(acceptfd, &control, sizeof(char));
513 /* Decide what control message to send to Coordinator */
514 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
515 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
516 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
523 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
524 * to send to Coordinator based on the votes of oids involved in the transaction */
525 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
526 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
527 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
531 /* Condition to send TRANS_AGREE */
532 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
533 control = TRANS_AGREE;
534 /* Send control message */
535 send_data(acceptfd, &control, sizeof(char));
537 /* Condition to send TRANS_SOFT_ABORT */
538 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
539 control = TRANS_SOFT_ABORT;
541 /* Send control message */
542 send_data(acceptfd, &control, sizeof(char));
544 /* Send number of oids not found and the missing oids if objects are missing in the machine */
545 if(*(objnotfound) != 0) {
547 msg[0] = *(objnotfound);
548 send_data(acceptfd, &msg, sizeof(int));
549 int size = sizeof(unsigned int)* *(objnotfound);
550 send_data(acceptfd, oidnotfound, size);
554 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
555 * if Participant receives a TRANS_COMMIT */
556 transinfo->objlocked = oidlocked;
557 transinfo->objnotfound = oidnotfound;
558 transinfo->modptr = modptr;
559 transinfo->numlocked = *(objlocked);
560 transinfo->numnotfound = *(objnotfound);
565 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
566 * addresses in lookup table and also changes version number
567 * Sends an ACK back to Coordinator */
568 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
570 objheader_t *newheader;
571 int i = 0, offset = 0;
575 /* Process each modified object saved in the mainobject store */
576 for(i = 0; i < nummod; i++) {
577 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
578 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
581 GETSIZE(tmpsize,header);
582 pthread_mutex_lock(&mainobjstore_mutex);
583 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
584 header->version += 1;
585 /* If threads are waiting on this object to be updated, notify them */
586 if(header->notifylist != NULL) {
587 notifyAll(&header->notifylist, OID(header), header->version);
589 pthread_mutex_unlock(&mainobjstore_mutex);
590 offset += sizeof(objheader_t) + tmpsize;
596 /* Unlock locked objects */
597 for(i = 0; i < numlocked; i++) {
598 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
599 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
602 STATUS(header) &= ~(LOCK);
604 //TODO Update location lookup table
606 /* Send ack to coordinator */
607 control = TRANS_SUCESSFUL;
608 send_data((int)acceptfd, &control, sizeof(char));
612 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
613 * Looks for the objects to be prefetched in the main object store.
614 * If objects are not found then record those and if objects are found
615 * then use offset values to prefetch references to other objects */
617 int prefetchReq(int acceptfd) {
618 int i, size, objsize, numbytes = 0, isArray = 0, numoffset = 0;
620 char *recvbuffer, *sendbuffer, control;
621 unsigned int oid, mid;
624 struct sockaddr_in remoteAddr;
627 recv_data((int)acceptfd, &length, sizeof(int));
629 size = length - sizeof(int);
630 if((recvbuffer = calloc(1, size)) == NULL) {
631 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
634 recv_data((int)acceptfd, recvbuffer, size);
635 oid = *((unsigned int *) recvbuffer);
636 mid = *((unsigned int *) (recvbuffer + sizeof(unsigned int)));
637 size = size - (2 * sizeof(unsigned int));
638 numoffset = size / sizeof(short);
639 if((offsetarry = calloc(numoffset, sizeof(short))) == NULL) {
640 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
644 memcpy(offsetarry, recvbuffer + (2 * sizeof(unsigned int)), size);
647 pthread_mutex_lock(&sockLock);
649 pthread_mutex_unlock(&sockLock);
650 for(i = 0; i < NUM_MACHINES; i++) {
651 if(sockArray[i].mid == mid) {
652 sd = sockArray[i].sockid;
653 pthread_mutex_lock(&sockLock);
655 pthread_mutex_unlock(&sockLock);
660 if(sockIdFound == 0) {
661 if(sockCount < NUM_MACHINES) {
664 /* Create socket to send information */
665 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
666 perror("prefetchReq():socket()");
669 bzero(&remoteAddr, sizeof(remoteAddr));
670 remoteAddr.sin_family = AF_INET;
671 remoteAddr.sin_port = htons(LISTEN_PORT);
672 remoteAddr.sin_addr.s_addr = htonl(mid);
674 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);
682 sockArray[sockCount].mid = mid;
683 sockArray[sockCount].sockid = sd;
684 pthread_mutex_lock(&sockLock);
686 pthread_mutex_unlock(&sockLock);
688 //TODO Fix for connecting to more than 2 machines && close socket
689 printf("%s(): Error: Currently works for only 2 machines\n", __func__);
695 /*Process each oid */
696 if ((header = mhashSearch(oid)) == NULL) {/* Obj not found */
697 /* Save the oids not found in buffer for later use */
698 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
699 if((sendbuffer = calloc(1, size)) == NULL) {
700 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
705 *((int *) sendbuffer) = size;
706 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
707 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
709 control = TRANS_PREFETCH_RESPONSE;
710 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
712 printf("Error: %s() in sending prefetch response at %s, %d\n",
713 __func__, __FILE__, __LINE__);
717 } else { /* Object Found */
719 GETSIZE(objsize, header);
720 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
721 if((sendbuffer = calloc(1, size)) == NULL) {
722 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
727 *((int *) (sendbuffer + incr)) = size;
729 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
730 incr += sizeof(char);
731 *((unsigned int *)(sendbuffer+incr)) = oid;
732 incr += sizeof(unsigned int);
733 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
735 control = TRANS_PREFETCH_RESPONSE;
736 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
738 printf("Error: %s() in sending prefetch response at %s, %d\n",
739 __func__, __FILE__, __LINE__);
744 /* Calculate the oid corresponding to the offset value */
745 for(i = 0 ; i< numoffset ; i++) {
746 /* Check for arrays */
747 if(TYPE(header) > NUMCLASSES) {
751 int elementsize = classsize[TYPE(header)];
752 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
753 unsigned short length = ao->___length___;
754 /* Check if array out of bounds */
755 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
758 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
760 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
763 if((header = mhashSearch(oid)) == NULL) {
764 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
765 if((sendbuffer = calloc(1, size)) == NULL) {
766 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
771 *((int *) sendbuffer) = size;
772 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
773 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
775 control = TRANS_PREFETCH_RESPONSE;
776 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
778 printf("Error: %s() in sending prefetch response at %s, %d\n",
784 } else {/* Obj Found */
786 GETSIZE(objsize, header);
787 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
788 if((sendbuffer = calloc(1, size)) == NULL) {
789 printf("Calloc error at %s,%d\n", __func__, __FILE__, __LINE__);
794 *((int *) (sendbuffer + incr)) = size;
796 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
797 incr += sizeof(char);
798 *((unsigned int *)(sendbuffer+incr)) = oid;
799 incr += sizeof(unsigned int);
800 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
802 control = TRANS_PREFETCH_RESPONSE;
803 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
805 printf("Error: %s() in sending prefetch response at %s, %d\n",
806 __func__, __FILE__, __LINE__);
817 } while (length != -1);
821 int sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
824 send_data(sd, control, sizeof(char));
825 /* Send the buffer with its size */
826 int length = *(size);
827 send_data(sd, sendbuffer, length);
832 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
835 unsigned short newversion;
836 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
838 struct sockaddr_in remoteAddr;
844 oid = *(oidarry + i);
845 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
846 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
849 /* Check to see if versions are same */
851 if ((STATUS(header) & LOCK) != LOCK) {
852 //FIXME make locking atomic
853 STATUS(header) |= LOCK;
854 newversion = header->version;
855 if(newversion == *(versionarry + i)) {
856 //Add to the notify list
857 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
858 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
861 STATUS(header) &= ~(LOCK);
863 STATUS(header) &= ~(LOCK);
864 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
865 perror("processReqNotify():socket()");
868 bzero(&remoteAddr, sizeof(remoteAddr));
869 remoteAddr.sin_family = AF_INET;
870 remoteAddr.sin_port = htons(LISTEN_PORT);
871 remoteAddr.sin_addr.s_addr = htonl(mid);
873 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
874 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
875 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
879 //Send Update notification
880 msg[0] = THREAD_NOTIFY_RESPONSE;
881 *((unsigned int *)&msg[1]) = oid;
882 size = sizeof(unsigned int);
883 *((unsigned short *)(&msg[1]+size)) = newversion;
884 size += sizeof(unsigned short);
885 *((unsigned int *)(&msg[1]+size)) = threadid;
886 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
887 send_data(sd, msg, size);