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));
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);
175 recv_data((int)acceptfd, &control, sizeof(char));
176 } while(control == READ_REQUEST);
179 case READ_MULT_REQUEST:
185 case MOVE_MULT_REQUEST:
189 /* Read transaction request */
190 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
191 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
197 if((val = prefetchReq((int)acceptfd)) != 0) {
198 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
201 recv_data((int)acceptfd, &control, sizeof(char));
202 } while (control == TRANS_PREFETCH);
204 case TRANS_PREFETCH_RESPONSE:
206 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
207 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
210 recv_data((int)acceptfd, &control, sizeof(char));
211 } while (control == TRANS_PREFETCH_RESPONSE);
213 case START_REMOTE_THREAD:
214 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
215 objType = getObjType(oid);
216 startDSMthread(oid, objType);
219 case THREAD_NOTIFY_REQUEST:
220 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
221 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
222 if((buffer = calloc(1,size)) == NULL) {
223 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
227 recv_data((int)acceptfd, buffer, size);
229 oidarry = calloc(numoid, sizeof(unsigned int));
230 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
231 size = sizeof(unsigned int) * numoid;
232 versionarry = calloc(numoid, sizeof(unsigned short));
233 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
234 size += sizeof(unsigned short) * numoid;
235 mid = *((unsigned int *)(buffer+size));
236 size += sizeof(unsigned int);
237 threadid = *((unsigned int *)(buffer+size));
238 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
243 case THREAD_NOTIFY_RESPONSE:
244 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
245 if((buffer = calloc(1,size)) == NULL) {
246 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
250 recv_data((int)acceptfd, buffer, size);
252 oid = *((unsigned int *)buffer);
253 size = sizeof(unsigned int);
254 version = *((unsigned short *)(buffer+size));
255 size += sizeof(unsigned short);
256 threadid = *((unsigned int *)(buffer+size));
257 threadNotify(oid,version,threadid);
262 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
265 /* Close connection */
266 if (close((int)acceptfd) == -1)
271 /* This function reads the information available in a transaction request
272 * and makes a function call to process the request */
273 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
276 unsigned int *oidmod, oid;
278 objheader_t *headaddr;
279 int sum, i, size, n, val;
283 /* Read fixed_data_t data structure */
284 size = sizeof(fixed) - 1;
285 ptr = (char *)&fixed;;
286 fixed.control = TRANS_REQUEST;
287 recv_data((int)acceptfd, ptr+1, size);
289 /* Read list of mids */
290 int mcount = fixed.mcount;
291 size = mcount * sizeof(unsigned int);
292 unsigned int listmid[mcount];
293 ptr = (char *) listmid;
294 recv_data((int)acceptfd, ptr, size);
296 /* Read oid and version tuples for those objects that are not modified in the transaction */
297 int numread = fixed.numread;
298 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
300 if(numread != 0) { //If pile contains more than one object to be read,
301 // keep reading all objects
302 recv_data((int)acceptfd, objread, size);
305 /* Read modified objects */
306 if(fixed.nummod != 0) {
307 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
308 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
311 size = fixed.sum_bytes;
312 recv_data((int)acceptfd, modptr, size);
315 /* Create an array of oids for modified objects */
316 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
319 printf("calloc error %s, %d\n", __FILE__, __LINE__);
322 ptr = (char *) modptr;
323 for(i = 0 ; i < fixed.nummod; i++) {
325 headaddr = (objheader_t *) ptr;
328 GETSIZE(tmpsize, headaddr);
329 ptr += sizeof(objheader_t) + tmpsize;
332 /*Process the information read */
333 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
334 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
350 /* This function processes the Coordinator's transaction request using "handleTransReq"
351 * function and sends a reply to the co-ordinator.
352 * Following this it also receives a new control message from the co-ordinator and processes this message*/
353 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
354 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
355 char control, sendctrl, retval;
356 objheader_t *tmp_header;
360 /* Send reply to the Coordinator */
361 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
362 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
366 recv_data((int)acceptfd, &control, sizeof(char));
368 /* Process the new control message */
371 if (fixed->nummod > 0)
373 /* Unlock objects that was locked due to this transaction */
374 for(i = 0; i< transinfo->numlocked; i++) {
375 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
376 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
379 STATUS(((objheader_t *)header)) &= ~(LOCK);
382 /* Send ack to Coordinator */
383 sendctrl = TRANS_UNSUCESSFUL;
384 send_data((int)acceptfd, &sendctrl, sizeof(char));
388 /* Invoke the transCommit process() */
389 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
390 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
392 if (transinfo->objlocked != NULL) {
393 free(transinfo->objlocked);
395 if (transinfo->objnotfound != NULL) {
396 free(transinfo->objnotfound);
402 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
405 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
406 //TODO Use fixed.trans_id TID since Client may have died
411 if (transinfo->objlocked != NULL) {
412 free(transinfo->objlocked);
414 if (transinfo->objnotfound != NULL) {
415 free(transinfo->objnotfound);
421 /* This function increments counters while running a voting decision on all objects involved
422 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
423 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
425 unsigned short version;
426 char control = 0, *ptr;
428 unsigned int *oidnotfound, *oidlocked;
430 objheader_t *headptr;
432 /* Counters and arrays to formulate decision on control message to be sent */
433 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
434 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
435 int objnotfound = 0, objlocked = 0;
436 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
438 /* modptr points to the beginning of the object store
439 * created at the Pariticipant.
440 * Object store holds the modified objects involved in the transaction request */
441 ptr = (char *) modptr;
443 /* Process each oid in the machine pile/ group per thread */
444 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
445 if (i < fixed->numread) {//Objs only read and not modified
446 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
448 oid = *((unsigned int *)(objread + incr));
449 incr += sizeof(unsigned int);
450 version = *((unsigned short *)(objread + incr));
451 } else {//Objs modified
453 headptr = (objheader_t *) ptr;
455 version = headptr->version;
456 GETSIZE(tmpsize, headptr);
457 ptr += sizeof(objheader_t) + tmpsize;
460 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
462 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
463 /* Save the oids not found and number of oids not found for later use */
464 oidnotfound[objnotfound] = oid;
466 } else { /* If Obj found in machine (i.e. has not moved) */
467 /* Check if Obj is locked by any previous transaction */
468 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
469 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
471 } else {/* If versions don't match ...HARD ABORT */
473 /* Send TRANS_DISAGREE to Coordinator */
474 control = TRANS_DISAGREE;
476 for(j = 0; j < objlocked; j++) {
477 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
478 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
481 STATUS(headptr) &= ~(LOCK);
485 send_data(acceptfd, &control, sizeof(char));
488 } else {/* If Obj is not locked then lock object */
489 STATUS(((objheader_t *)mobj)) |= LOCK;
490 /* Save all object oids that are locked on this machine during this transaction request call */
491 oidlocked[objlocked] = OID(((objheader_t *)mobj));
493 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
495 } else { /* If versions don't match ...HARD ABORT */
497 control = TRANS_DISAGREE;
499 for(j = 0; j < objlocked; j++) {
500 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
501 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
504 STATUS(headptr) &= ~(LOCK);
509 /* Send TRANS_DISAGREE to Coordinator */
510 send_data(acceptfd, &control, sizeof(char));
517 /* Decide what control message to send to Coordinator */
518 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
519 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
520 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
527 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
528 * to send to Coordinator based on the votes of oids involved in the transaction */
529 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
530 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
531 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
535 /* Condition to send TRANS_AGREE */
536 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
537 control = TRANS_AGREE;
538 /* Send control message */
539 send_data(acceptfd, &control, sizeof(char));
541 /* Condition to send TRANS_SOFT_ABORT */
542 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
543 control = TRANS_SOFT_ABORT;
545 /* Send control message */
546 send_data(acceptfd, &control, sizeof(char));
548 /* Send number of oids not found and the missing oids if objects are missing in the machine */
549 if(*(objnotfound) != 0) {
551 msg[0] = *(objnotfound);
552 send_data(acceptfd, &msg, sizeof(int));
553 int size = sizeof(unsigned int)* *(objnotfound);
554 send_data(acceptfd, oidnotfound, size);
558 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
559 * if Participant receives a TRANS_COMMIT */
560 transinfo->objlocked = oidlocked;
561 transinfo->objnotfound = oidnotfound;
562 transinfo->modptr = modptr;
563 transinfo->numlocked = *(objlocked);
564 transinfo->numnotfound = *(objnotfound);
569 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
570 * addresses in lookup table and also changes version number
571 * Sends an ACK back to Coordinator */
572 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
574 objheader_t *newheader;
575 int i = 0, offset = 0;
579 /* Process each modified object saved in the mainobject store */
580 for(i = 0; i < nummod; i++) {
581 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
582 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
585 GETSIZE(tmpsize,header);
586 pthread_mutex_lock(&mainobjstore_mutex);
587 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
588 header->version += 1;
589 /* If threads are waiting on this object to be updated, notify them */
590 if(header->notifylist != NULL) {
591 notifyAll(&header->notifylist, OID(header), header->version);
593 pthread_mutex_unlock(&mainobjstore_mutex);
594 offset += sizeof(objheader_t) + tmpsize;
600 /* Unlock locked objects */
601 for(i = 0; i < numlocked; i++) {
602 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
603 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
606 STATUS(header) &= ~(LOCK);
608 //TODO Update location lookup table
610 /* Send ack to coordinator */
611 control = TRANS_SUCESSFUL;
612 send_data((int)acceptfd, &control, sizeof(char));
616 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
617 * Looks for the objects to be prefetched in the main object store.
618 * If objects are not found then record those and if objects are found
619 * then use offset values to prefetch references to other objects */
621 int prefetchReq(int acceptfd) {
622 int i, size, objsize, numbytes = 0, isArray = 0, numoffset = 0;
624 char *recvbuffer, *sendbuffer, control;
625 unsigned int oid, mid;
628 struct sockaddr_in remoteAddr;
631 recv_data((int)acceptfd, &length, sizeof(int));
633 size = length - sizeof(int);
634 if((recvbuffer = calloc(1, size)) == NULL) {
635 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
638 recv_data((int)acceptfd, recvbuffer, size);
639 oid = *((unsigned int *) recvbuffer);
640 mid = *((unsigned int *) (recvbuffer + sizeof(unsigned int)));
641 size = size - (2 * sizeof(unsigned int));
642 numoffset = size / sizeof(short);
643 if((offsetarry = calloc(numoffset, sizeof(short))) == NULL) {
644 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
648 memcpy(offsetarry, recvbuffer + (2 * sizeof(unsigned int)), size);
650 pthread_mutex_lock(&sockLock);
652 pthread_mutex_unlock(&sockLock);
653 /* If socket is already established then send data reusing socket */
654 for(i = 0; i < NUM_MACHINES; i++) {
655 if(sockArray[i].mid == mid) {
656 sd = sockArray[i].sockid;
657 pthread_mutex_lock(&sockLock);
659 pthread_mutex_unlock(&sockLock);
664 if(sockIdFound == 0) {
665 if(sockCount < NUM_MACHINES) {
666 /* Create socket to send information */
667 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
668 perror("prefetchReq():socket()");
671 bzero(&remoteAddr, sizeof(remoteAddr));
672 remoteAddr.sin_family = AF_INET;
673 remoteAddr.sin_port = htons(LISTEN_PORT);
674 remoteAddr.sin_addr.s_addr = htonl(mid);
676 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
678 printf("Error: prefetchReq():error %d connecting to %s:%d\n", errno,
679 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
683 sockArray[sockCount].mid = mid;
684 sockArray[sockCount].sockid = sd;
685 pthread_mutex_lock(&sockLock);
687 pthread_mutex_unlock(&sockLock);
689 //TODO Fix for connecting to more than 2 machines && close socket
690 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__);
816 } while (length != -1);
820 int sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
823 send_data(sd, control, sizeof(char));
824 /* Send the buffer with its size */
825 int length = *(size);
826 send_data(sd, sendbuffer, length);
831 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
834 unsigned short newversion;
835 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
837 struct sockaddr_in remoteAddr;
843 oid = *(oidarry + i);
844 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
845 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
848 /* Check to see if versions are same */
850 if ((STATUS(header) & LOCK) != LOCK) {
851 //FIXME make locking atomic
852 STATUS(header) |= LOCK;
853 newversion = header->version;
854 if(newversion == *(versionarry + i)) {
855 //Add to the notify list
856 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
857 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
860 STATUS(header) &= ~(LOCK);
862 STATUS(header) &= ~(LOCK);
863 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
864 perror("processReqNotify():socket()");
867 bzero(&remoteAddr, sizeof(remoteAddr));
868 remoteAddr.sin_family = AF_INET;
869 remoteAddr.sin_port = htons(LISTEN_PORT);
870 remoteAddr.sin_addr.s_addr = htonl(mid);
872 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
873 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
874 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
878 //Send Update notification
879 msg[0] = THREAD_NOTIFY_RESPONSE;
880 *((unsigned int *)&msg[1]) = oid;
881 size = sizeof(unsigned int);
882 *((unsigned short *)(&msg[1]+size)) = newversion;
883 size += sizeof(unsigned short);
884 *((unsigned int *)(&msg[1]+size)) = threadid;
885 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
886 send_data(sd, msg, size);