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"
14 #define BACKLOG 10 //max pending connections
15 #define RECEIVE_BUFFER_SIZE 2048
17 extern int classsize[];
18 extern int numHostsInSystem;
19 extern pthread_mutex_t notifymutex;
21 objstr_t *mainobjstore;
22 pthread_mutex_t mainobjstore_mutex;
23 pthread_mutex_t lockObjHeader;
24 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
26 sockPoolHashTable_t *transPResponseSocketPool;
28 /* This function initializes the main objects store and creates the
29 * global machine and location lookup table */
32 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
33 /* Initialize attribute for mutex */
34 pthread_mutexattr_init(&mainobjstore_mutex_attr);
35 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
36 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
37 pthread_mutex_init(&lockObjHeader,NULL);
38 if (mhashCreate(HASH_SIZE, LOADFACTOR))
41 if (lhashCreate(HASH_SIZE, LOADFACTOR))
44 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
47 //Initialize socket pool
48 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, DEFAULTSOCKPOOLSIZE)) == NULL) {
49 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
57 int startlistening() {
59 struct sockaddr_in my_addr;
60 socklen_t addrlength = sizeof(struct sockaddr);
63 listenfd = socket(AF_INET, SOCK_STREAM, 0);
69 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
74 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
80 my_addr.sin_family = AF_INET;
81 my_addr.sin_port = htons(LISTEN_PORT);
82 my_addr.sin_addr.s_addr = INADDR_ANY;
83 memset(&(my_addr.sin_zero), '\0', 8);
85 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
90 if (listen(listenfd, BACKLOG) == -1) {
97 /* This function starts the thread to listen on a socket
98 * for tranaction calls */
99 void *dstmListen(void *lfd) {
100 int listenfd=(int)lfd;
102 struct sockaddr_in client_addr;
103 socklen_t addrlength = sizeof(struct sockaddr);
104 pthread_t thread_dstm_accept;
106 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
110 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
111 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
113 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
115 pthread_detach(thread_dstm_accept);
118 /* This function accepts a new connection request, decodes the control message in the connection
119 * and accordingly calls other functions to process new requests */
120 void *dstmAccept(void *acceptfd) {
121 int val, retval, size, sum, sockid;
128 trans_commit_data_t transinfo;
129 unsigned short objType, *versionarry, version;
130 unsigned int *oidarry, numoid, mid, threadid;
132 /* Receive control messages from other machines */
134 int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
138 printf("DEBUG -> RECV Error!.. retrying\n");
143 /* Read oid requested and search if available */
144 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
145 if((srcObj = mhashSearch(oid)) == NULL) {
146 printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
149 h = (objheader_t *) srcObj;
151 size += sizeof(objheader_t);
152 sockid = (int) acceptfd;
154 ctrl = OBJECT_NOT_FOUND;
155 send_data(sockid, &ctrl, sizeof(char));
158 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
159 *((int *)&msg[1])=size;
160 send_data(sockid, &msg, sizeof(msg));
161 send_data(sockid, h, size);
165 case READ_MULT_REQUEST:
171 case MOVE_MULT_REQUEST:
175 /* Read transaction request */
176 transinfo.objlocked = NULL;
177 transinfo.objnotfound = NULL;
178 transinfo.modptr = NULL;
179 transinfo.numlocked = 0;
180 transinfo.numnotfound = 0;
181 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
182 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
189 if((val = rangePrefetchReq((int)acceptfd)) != 0) {
190 printf("Error: In rangePrefetchReq() %s, %d\n", __FILE__, __LINE__);
194 if((val = prefetchReq((int)acceptfd)) != 0) {
195 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
201 case TRANS_PREFETCH_RESPONSE:
203 if((val = getRangePrefetchResponse((int)acceptfd)) != 0) {
204 printf("Error: In getRangePrefetchRespose() %s, %d\n", __FILE__, __LINE__);
208 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
209 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
215 case START_REMOTE_THREAD:
216 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
217 objType = getObjType(oid);
218 startDSMthread(oid, objType);
221 case THREAD_NOTIFY_REQUEST:
222 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
223 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
224 if((buffer = calloc(1,size)) == NULL) {
225 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
229 recv_data((int)acceptfd, buffer, size);
231 oidarry = calloc(numoid, sizeof(unsigned int));
232 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
233 size = sizeof(unsigned int) * numoid;
234 versionarry = calloc(numoid, sizeof(unsigned short));
235 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
236 size += sizeof(unsigned short) * numoid;
237 mid = *((unsigned int *)(buffer+size));
238 size += sizeof(unsigned int);
239 threadid = *((unsigned int *)(buffer+size));
240 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
245 case THREAD_NOTIFY_RESPONSE:
246 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
247 if((buffer = calloc(1,size)) == NULL) {
248 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
252 recv_data((int)acceptfd, buffer, size);
254 oid = *((unsigned int *)buffer);
255 size = sizeof(unsigned int);
256 version = *((unsigned short *)(buffer+size));
257 size += sizeof(unsigned short);
258 threadid = *((unsigned int *)(buffer+size));
259 threadNotify(oid,version,threadid);
263 case CLOSE_CONNECTION:
264 goto closeconnection;
267 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
272 /* Close connection */
273 if (close((int)acceptfd) == -1)
278 /* This function reads the information available in a transaction request
279 * and makes a function call to process the request */
280 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
283 unsigned int *oidmod, oid;
285 objheader_t *headaddr;
286 int sum, i, size, n, val;
290 /* Read fixed_data_t data structure */
291 size = sizeof(fixed) - 1;
292 ptr = (char *)&fixed;;
293 fixed.control = TRANS_REQUEST;
294 recv_data((int)acceptfd, ptr+1, size);
296 /* Read list of mids */
297 int mcount = fixed.mcount;
298 size = mcount * sizeof(unsigned int);
299 unsigned int listmid[mcount];
300 ptr = (char *) listmid;
301 recv_data((int)acceptfd, ptr, size);
303 /* Read oid and version tuples for those objects that are not modified in the transaction */
304 int numread = fixed.numread;
305 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
307 if(numread != 0) { //If pile contains more than one object to be read,
308 // keep reading all objects
309 recv_data((int)acceptfd, objread, size);
312 /* Read modified objects */
313 if(fixed.nummod != 0) {
314 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
315 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
318 size = fixed.sum_bytes;
319 recv_data((int)acceptfd, modptr, size);
322 /* Create an array of oids for modified objects */
323 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
324 if (oidmod == NULL) {
325 printf("calloc error %s, %d\n", __FILE__, __LINE__);
328 ptr = (char *) modptr;
329 for(i = 0 ; i < fixed.nummod; i++) {
331 headaddr = (objheader_t *) ptr;
334 GETSIZE(tmpsize, headaddr);
335 ptr += sizeof(objheader_t) + tmpsize;
338 /*Process the information read */
339 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
340 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
356 /* This function processes the Coordinator's transaction request using "handleTransReq"
357 * function and sends a reply to the co-ordinator.
358 * Following this it also receives a new control message from the co-ordinator and processes this message*/
359 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
360 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
362 char control, sendctrl, retval;
363 objheader_t *tmp_header;
367 /* Send reply to the Coordinator */
368 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
369 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
373 recv_data((int)acceptfd, &control, sizeof(char));
374 /* Process the new control message */
377 if (fixed->nummod > 0)
379 /* Unlock objects that was locked due to this transaction */
380 int useWriteUnlock = 0;
381 for(i = 0; i< transinfo->numlocked; i++) {
382 if(transinfo->objlocked[i] == -1) {
386 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
387 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
391 write_unlock(STATUSPTR(header));
393 read_unlock(STATUSPTR(header));
399 /* Invoke the transCommit process() */
400 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
401 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
403 if (transinfo->objlocked != NULL) {
404 free(transinfo->objlocked);
406 if (transinfo->objnotfound != NULL) {
407 free(transinfo->objnotfound);
413 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
417 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
418 //TODO Use fixed.trans_id TID since Client may have died
423 if (transinfo->objlocked != NULL) {
424 free(transinfo->objlocked);
426 if (transinfo->objnotfound != NULL) {
427 free(transinfo->objnotfound);
433 /* This function increments counters while running a voting decision on all objects involved
434 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
435 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
437 unsigned short version;
438 char control = 0, *ptr;
440 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
441 objheader_t *headptr;
443 /* Counters and arrays to formulate decision on control message to be sent */
444 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
445 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
446 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
447 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
448 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
450 /* modptr points to the beginning of the object store
451 * created at the Pariticipant.
452 * Object store holds the modified objects involved in the transaction request */
453 ptr = (char *) modptr;
455 /* Process each oid in the machine pile/ group per thread */
456 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
457 if (i < fixed->numread) { //Objs only read and not modified
458 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
460 oid = *((unsigned int *)(objread + incr));
461 incr += sizeof(unsigned int);
462 version = *((unsigned short *)(objread + incr));
463 getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
464 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
465 } else { //Objs modified
466 if(i == fixed->numread) {
467 oidlocked[objlocked] = -1;
471 headptr = (objheader_t *) ptr;
473 version = headptr->version;
474 GETSIZE(tmpsize, headptr);
475 ptr += sizeof(objheader_t) + tmpsize;
476 getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
477 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
478 &numBytes, &control, oid, version);
482 /* send TRANS_DISAGREE and objs*/
485 char *objs = calloc(1, numBytes);
487 for(j = 0; j<objvernotmatch; j++) {
488 objheader_t *header = mhashSearch(oidvernotmatch[j]);
490 GETSIZE(size, header);
491 size += sizeof(objheader_t);
492 memcpy(objs+offset, header, size);
497 int useWriteUnlock = 0;
498 for(j = 0; j < objlocked; j++) {
499 if(oidlocked[j] == -1) {
503 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
504 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
508 write_unlock(STATUSPTR(headptr));
510 read_unlock(STATUSPTR(headptr));
515 send_data(acceptfd, &control, sizeof(char));
517 send_data(acceptfd, &numBytes, sizeof(int));
518 send_data(acceptfd, objs, numBytes);
519 transinfo->objvernotmatch = oidvernotmatch;
520 transinfo->numvernotmatch = objvernotmatch;
522 free(transinfo->objvernotmatch);
527 /* Decide what control message to send to Coordinator */
528 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
529 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
530 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
536 /* Update Commit info for objects that are read */
537 void getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
538 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
539 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
540 char *control, unsigned int oid, unsigned short version) {
542 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
544 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
545 /* Save the oids not found and number of oids not found for later use */
546 oidnotfound[*objnotfound] = oid;
548 } else { /* If Obj found in machine (i.e. has not moved) */
549 /* Check if Obj is locked by any previous transaction */
550 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
551 if (version == ((objheader_t *)mobj)->version) { /* match versions */
553 } else { /* If versions don't match ...HARD ABORT */
555 oidvernotmatch[*objvernotmatch] = oid;
559 size += sizeof(objheader_t);
561 /* Send TRANS_DISAGREE to Coordinator */
562 *control = TRANS_DISAGREE;
563 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
565 //Keep track of oid locked
566 oidlocked[*objlocked] = OID(((objheader_t *)mobj));
568 } else { //we are locked
569 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
571 } else { /* If versions don't match ...HARD ABORT */
573 oidvernotmatch[*objvernotmatch] = oid;
577 size += sizeof(objheader_t);
579 *control = TRANS_DISAGREE;
580 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
586 /* Update Commit info for objects that are read */
587 void getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
588 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
589 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
591 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
592 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
593 /* Save the oids not found and number of oids not found for later use */
594 oidnotfound[*objnotfound] = oid;
596 } else { /* If Obj found in machine (i.e. has not moved) */
597 /* Check if Obj is locked by any previous transaction */
598 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
599 if (version == ((objheader_t *)mobj)->version) { /* match versions */
601 } else { /* If versions don't match ...HARD ABORT */
603 oidvernotmatch[*objvernotmatch] = oid;
607 size += sizeof(objheader_t);
609 /* Send TRANS_DISAGREE to Coordinator */
610 *control = TRANS_DISAGREE;
611 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
613 //Keep track of oid locked
614 oidlocked[*objlocked] = OID(((objheader_t *)mobj));
616 } else { /* Some other transaction has aquired a write lock on this object */
617 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
619 } else { /* If versions don't match ...HARD ABORT */
621 oidvernotmatch[*objvernotmatch] = oid;
625 size += sizeof(objheader_t);
627 *control = TRANS_DISAGREE;
628 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
634 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
635 * to send to Coordinator based on the votes of oids involved in the transaction */
636 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
637 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
638 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
642 /* Condition to send TRANS_AGREE */
643 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
644 control = TRANS_AGREE;
645 /* Send control message */
646 send_data(acceptfd, &control, sizeof(char));
648 /* Condition to send TRANS_SOFT_ABORT */
649 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
650 control = TRANS_SOFT_ABORT;
652 /* Send control message */
653 send_data(acceptfd, &control, sizeof(char));
655 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
656 if(*(objnotfound) != 0) {
658 msg[0] = *(objnotfound);
659 send_data(acceptfd, &msg, sizeof(int));
660 int size = sizeof(unsigned int)* *(objnotfound);
661 send_data(acceptfd, oidnotfound, size);
665 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
666 * if Participant receives a TRANS_COMMIT */
667 transinfo->objlocked = oidlocked;
668 transinfo->objnotfound = oidnotfound;
669 transinfo->modptr = modptr;
670 transinfo->numlocked = *(objlocked);
671 transinfo->numnotfound = *(objnotfound);
675 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
676 * addresses in lookup table and also changes version number
677 * Sends an ACK back to Coordinator */
678 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
680 objheader_t *newheader;
681 int i = 0, offset = 0;
685 /* Process each modified object saved in the mainobject store */
686 for(i = 0; i < nummod; i++) {
687 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
688 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
691 GETSIZE(tmpsize,header);
692 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
693 header->version += 1;
694 /* If threads are waiting on this object to be updated, notify them */
695 if(header->notifylist != NULL) {
696 notifyAll(&header->notifylist, OID(header), header->version);
698 offset += sizeof(objheader_t) + tmpsize;
704 /* Unlock locked objects */
705 int useWriteUnlock = 0;
706 for(i = 0; i < numlocked; i++) {
707 if(oidlocked[i] == -1) {
711 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
712 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
717 write_unlock(STATUSPTR(header));
719 read_unlock(STATUSPTR(header));
722 //TODO Update location lookup table
726 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
727 * Looks for the objects to be prefetched in the main object store.
728 * If objects are not found then record those and if objects are found
729 * then use offset values to prefetch references to other objects */
731 int prefetchReq(int acceptfd) {
732 int i, size, objsize, numoffset = 0;
734 char *recvbuffer, control;
735 unsigned int oid, mid=-1;
741 recv_data((int)acceptfd, &numoffset, sizeof(int));
744 recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
746 if (mid != oidmid.mid) {
748 freeSockWithLock(transPResponseSocketPool, mid, sd);
751 sd = getSockWithLock(transPResponseSocketPool, mid);
753 short offsetarry[numoffset];
754 recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
756 /*Process each oid */
757 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
758 /* Save the oids not found in buffer for later use */
759 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
760 char sendbuffer[size];
761 *((int *) sendbuffer) = size;
762 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
763 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
764 control = TRANS_PREFETCH_RESPONSE;
765 sendPrefetchResponse(sd, &control, sendbuffer, &size);
766 } else { /* Object Found */
768 GETSIZE(objsize, header);
769 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
770 char sendbuffer[size];
771 *((int *)(sendbuffer + incr)) = size;
773 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
774 incr += sizeof(char);
775 *((unsigned int *)(sendbuffer+incr)) = oid;
776 incr += sizeof(unsigned int);
777 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
779 control = TRANS_PREFETCH_RESPONSE;
780 sendPrefetchResponse(sd, &control, sendbuffer, &size);
782 /* Calculate the oid corresponding to the offset value */
783 for(i = 0 ; i< numoffset ; i++) {
784 /* Check for arrays */
785 if(TYPE(header) > NUMCLASSES) {
786 int elementsize = classsize[TYPE(header)];
787 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
788 unsigned short length = ao->___length___;
789 /* Check if array out of bounds */
790 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
793 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
795 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
798 /* Don't continue if we hit a NULL pointer */
802 if((header = mhashSearch(oid)) == NULL) {
803 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
804 char sendbuffer[size];
805 *((int *) sendbuffer) = size;
806 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
807 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
809 control = TRANS_PREFETCH_RESPONSE;
810 sendPrefetchResponse(sd, &control, sendbuffer, &size);
812 } else { /* Obj Found */
814 GETSIZE(objsize, header);
815 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
816 char sendbuffer[size];
817 *((int *)(sendbuffer + incr)) = size;
819 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
820 incr += sizeof(char);
821 *((unsigned int *)(sendbuffer+incr)) = oid;
822 incr += sizeof(unsigned int);
823 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
825 control = TRANS_PREFETCH_RESPONSE;
826 sendPrefetchResponse(sd, &control, sendbuffer, &size);
833 freeSockWithLock(transPResponseSocketPool, mid, sd);
838 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
839 send_data(sd, control, sizeof(char));
840 /* Send the buffer with its size */
841 int length = *(size);
842 send_data(sd, sendbuffer, length);
845 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
848 unsigned short newversion;
849 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
851 struct sockaddr_in remoteAddr;
857 oid = *(oidarry + i);
858 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
859 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
862 /* Check to see if versions are same */
864 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
865 newversion = header->version;
866 if(newversion == *(versionarry + i)) {
867 //Add to the notify list
868 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
869 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
872 write_unlock(STATUSPTR(header));
874 write_unlock(STATUSPTR(header));
875 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
876 perror("processReqNotify():socket()");
879 bzero(&remoteAddr, sizeof(remoteAddr));
880 remoteAddr.sin_family = AF_INET;
881 remoteAddr.sin_port = htons(LISTEN_PORT);
882 remoteAddr.sin_addr.s_addr = htonl(mid);
884 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
885 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
886 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
890 //Send Update notification
891 msg[0] = THREAD_NOTIFY_RESPONSE;
892 *((unsigned int *)&msg[1]) = oid;
893 size = sizeof(unsigned int);
894 *((unsigned short *)(&msg[1]+size)) = newversion;
895 size += sizeof(unsigned short);
896 *((unsigned int *)(&msg[1]+size)) = threadid;
897 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
898 send_data(sd, msg, size);