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"
16 #define BACKLOG 10 //max pending connections
17 #define RECEIVE_BUFFER_SIZE 2048
19 extern int classsize[];
20 extern int numHostsInSystem;
21 extern pthread_mutex_t notifymutex;
23 objstr_t *mainobjstore;
24 pthread_mutex_t mainobjstore_mutex;
25 pthread_mutex_t lockObjHeader;
26 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
28 sockPoolHashTable_t *transPResponseSocketPool;
30 /* This function initializes the main objects store and creates the
31 * global machine and location lookup table */
34 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
35 /* Initialize attribute for mutex */
36 pthread_mutexattr_init(&mainobjstore_mutex_attr);
37 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
38 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
39 pthread_mutex_init(&lockObjHeader,NULL);
40 if (mhashCreate(MHASH_SIZE, MLOADFACTOR))
43 if (lhashCreate(HASH_SIZE, LOADFACTOR))
46 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
49 //Initialize socket pool
50 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, DEFAULTSOCKPOOLSIZE)) == NULL) {
51 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
59 int startlistening() {
61 struct sockaddr_in my_addr;
62 socklen_t addrlength = sizeof(struct sockaddr);
65 listenfd = socket(AF_INET, SOCK_STREAM, 0);
71 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
76 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
82 my_addr.sin_family = AF_INET;
83 my_addr.sin_port = htons(LISTEN_PORT);
84 my_addr.sin_addr.s_addr = INADDR_ANY;
85 memset(&(my_addr.sin_zero), '\0', 8);
87 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
92 if (listen(listenfd, BACKLOG) == -1) {
99 /* This function starts the thread to listen on a socket
100 * for tranaction calls */
101 void *dstmListen(void *lfd) {
102 int listenfd=(int)lfd;
104 struct sockaddr_in client_addr;
105 socklen_t addrlength = sizeof(struct sockaddr);
106 pthread_t thread_dstm_accept;
108 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
112 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
113 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
115 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
117 pthread_detach(thread_dstm_accept);
120 /* This function accepts a new connection request, decodes the control message in the connection
121 * and accordingly calls other functions to process new requests */
122 void *dstmAccept(void *acceptfd) {
123 int val, retval, size, sum, sockid;
130 trans_commit_data_t transinfo;
131 unsigned short objType, *versionarry, version;
132 unsigned int *oidarry, numoid, mid, threadid;
134 /* Receive control messages from other machines */
136 int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
140 printf("DEBUG -> RECV Error!.. retrying\n");
145 /* Read oid requested and search if available */
146 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
147 while((srcObj = mhashSearch(oid)) == NULL) {
149 if((ret = sched_yield()) != 0) {
150 printf("%s(): error no %d in thread yield\n", __func__, errno);
153 h = (objheader_t *) srcObj;
155 size += sizeof(objheader_t);
156 sockid = (int) acceptfd;
158 ctrl = OBJECT_NOT_FOUND;
159 send_data(sockid, &ctrl, sizeof(char));
162 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
163 *((int *)&msg[1])=size;
164 send_data(sockid, &msg, sizeof(msg));
165 send_data(sockid, h, size);
169 case READ_MULT_REQUEST:
175 case MOVE_MULT_REQUEST:
179 /* Read transaction request */
180 transinfo.objlocked = NULL;
181 transinfo.objnotfound = NULL;
182 transinfo.modptr = NULL;
183 transinfo.numlocked = 0;
184 transinfo.numnotfound = 0;
185 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
186 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
193 if((val = rangePrefetchReq((int)acceptfd)) != 0) {
194 printf("Error: In rangePrefetchReq() %s, %d\n", __FILE__, __LINE__);
198 if((val = prefetchReq((int)acceptfd)) != 0) {
199 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
205 case TRANS_PREFETCH_RESPONSE:
207 if((val = getRangePrefetchResponse((int)acceptfd)) != 0) {
208 printf("Error: In getRangePrefetchRespose() %s, %d\n", __FILE__, __LINE__);
212 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
213 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
219 case START_REMOTE_THREAD:
220 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
221 objType = getObjType(oid);
222 startDSMthread(oid, objType);
225 case THREAD_NOTIFY_REQUEST:
226 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
227 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
228 if((buffer = calloc(1,size)) == NULL) {
229 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
233 recv_data((int)acceptfd, buffer, size);
235 oidarry = calloc(numoid, sizeof(unsigned int));
236 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
237 size = sizeof(unsigned int) * numoid;
238 versionarry = calloc(numoid, sizeof(unsigned short));
239 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
240 size += sizeof(unsigned short) * numoid;
241 mid = *((unsigned int *)(buffer+size));
242 size += sizeof(unsigned int);
243 threadid = *((unsigned int *)(buffer+size));
244 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
249 case THREAD_NOTIFY_RESPONSE:
250 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
251 if((buffer = calloc(1,size)) == NULL) {
252 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
256 recv_data((int)acceptfd, buffer, size);
258 oid = *((unsigned int *)buffer);
259 size = sizeof(unsigned int);
260 version = *((unsigned short *)(buffer+size));
261 size += sizeof(unsigned short);
262 threadid = *((unsigned int *)(buffer+size));
263 threadNotify(oid,version,threadid);
267 case CLOSE_CONNECTION:
268 goto closeconnection;
271 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
276 /* Close connection */
277 if (close((int)acceptfd) == -1)
282 /* This function reads the information available in a transaction request
283 * and makes a function call to process the request */
284 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
287 unsigned int *oidmod, oid;
289 objheader_t *headaddr;
290 int sum, i, size, n, val;
294 /* Read fixed_data_t data structure */
295 size = sizeof(fixed) - 1;
296 ptr = (char *)&fixed;;
297 fixed.control = TRANS_REQUEST;
298 recv_data((int)acceptfd, ptr+1, size);
300 /* Read list of mids */
301 int mcount = fixed.mcount;
302 size = mcount * sizeof(unsigned int);
303 unsigned int listmid[mcount];
304 ptr = (char *) listmid;
305 recv_data((int)acceptfd, ptr, size);
307 /* Read oid and version tuples for those objects that are not modified in the transaction */
308 int numread = fixed.numread;
309 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
311 if(numread != 0) { //If pile contains more than one object to be read,
312 // keep reading all objects
313 recv_data((int)acceptfd, objread, size);
316 /* Read modified objects */
317 if(fixed.nummod != 0) {
318 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
319 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
322 size = fixed.sum_bytes;
323 recv_data((int)acceptfd, modptr, size);
326 /* Create an array of oids for modified objects */
327 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
328 if (oidmod == NULL) {
329 printf("calloc error %s, %d\n", __FILE__, __LINE__);
332 ptr = (char *) modptr;
333 for(i = 0 ; i < fixed.nummod; i++) {
335 headaddr = (objheader_t *) ptr;
338 GETSIZE(tmpsize, headaddr);
339 ptr += sizeof(objheader_t) + tmpsize;
342 /*Process the information read */
343 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
344 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
360 /* This function processes the Coordinator's transaction request using "handleTransReq"
361 * function and sends a reply to the co-ordinator.
362 * Following this it also receives a new control message from the co-ordinator and processes this message*/
363 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
364 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
366 char control, sendctrl, retval;
367 objheader_t *tmp_header;
371 /* Send reply to the Coordinator */
372 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
373 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
377 recv_data((int)acceptfd, &control, sizeof(char));
378 /* Process the new control message */
381 if (fixed->nummod > 0)
383 /* Unlock objects that was locked due to this transaction */
384 int useWriteUnlock = 0;
385 for(i = 0; i< transinfo->numlocked; i++) {
386 if(transinfo->objlocked[i] == -1) {
390 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
391 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
395 write_unlock(STATUSPTR(header));
397 read_unlock(STATUSPTR(header));
403 /* Invoke the transCommit process() */
404 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
405 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
407 if (transinfo->objlocked != NULL) {
408 free(transinfo->objlocked);
410 if (transinfo->objnotfound != NULL) {
411 free(transinfo->objnotfound);
417 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
421 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
422 //TODO Use fixed.trans_id TID since Client may have died
427 if (transinfo->objlocked != NULL) {
428 free(transinfo->objlocked);
430 if (transinfo->objnotfound != NULL) {
431 free(transinfo->objnotfound);
437 /* This function increments counters while running a voting decision on all objects involved
438 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
439 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
441 unsigned short version;
442 char control = 0, *ptr;
444 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
445 objheader_t *headptr;
447 /* Counters and arrays to formulate decision on control message to be sent */
448 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
449 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
450 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
451 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
452 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
454 /* modptr points to the beginning of the object store
455 * created at the Pariticipant.
456 * Object store holds the modified objects involved in the transaction request */
457 ptr = (char *) modptr;
459 /* Process each oid in the machine pile/ group per thread */
460 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
461 if (i < fixed->numread) { //Objs only read and not modified
462 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
464 oid = *((unsigned int *)(objread + incr));
465 incr += sizeof(unsigned int);
466 version = *((unsigned short *)(objread + incr));
467 getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
468 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
469 } else { //Objs modified
470 if(i == fixed->numread) {
471 oidlocked[objlocked++] = -1;
474 headptr = (objheader_t *) ptr;
476 version = headptr->version;
477 GETSIZE(tmpsize, headptr);
478 ptr += sizeof(objheader_t) + tmpsize;
479 getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
480 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
481 &numBytes, &control, oid, version);
485 /* send TRANS_DISAGREE and objs*/
488 char *objs = calloc(1, numBytes);
490 for(j = 0; j<objvernotmatch; j++) {
491 objheader_t *header = mhashSearch(oidvernotmatch[j]);
493 GETSIZE(size, header);
494 size += sizeof(objheader_t);
495 memcpy(objs+offset, header, size);
500 int useWriteUnlock = 0;
501 for(j = 0; j < objlocked; j++) {
502 if(oidlocked[j] == -1) {
506 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
507 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
511 write_unlock(STATUSPTR(headptr));
513 read_unlock(STATUSPTR(headptr));
518 send_data(acceptfd, &control, sizeof(char));
520 send_data(acceptfd, &numBytes, sizeof(int));
521 send_data(acceptfd, objs, numBytes);
522 transinfo->objvernotmatch = oidvernotmatch;
523 transinfo->numvernotmatch = objvernotmatch;
525 free(transinfo->objvernotmatch);
530 /* Decide what control message to send to Coordinator */
531 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
532 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
533 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
539 /* Update Commit info for objects that are read */
540 void getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
541 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
542 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
543 char *control, unsigned int oid, unsigned short version) {
545 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
547 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
548 /* Save the oids not found and number of oids not found for later use */
549 oidnotfound[*objnotfound] = oid;
551 } else { /* If Obj found in machine (i.e. has not moved) */
552 /* Check if Obj is locked by any previous transaction */
553 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
554 if (version == ((objheader_t *)mobj)->version) { /* match versions */
556 } else { /* If versions don't match ...HARD ABORT */
558 oidvernotmatch[*objvernotmatch] = oid;
562 size += sizeof(objheader_t);
564 /* Send TRANS_DISAGREE to Coordinator */
565 *control = TRANS_DISAGREE;
566 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
568 //Keep track of oid locked
569 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
570 } else { //we are locked
571 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
573 } else { /* If versions don't match ...HARD ABORT */
575 oidvernotmatch[*objvernotmatch] = oid;
579 size += sizeof(objheader_t);
581 *control = TRANS_DISAGREE;
582 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
588 /* Update Commit info for objects that are read */
589 void getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
590 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
591 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
593 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
594 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
595 /* Save the oids not found and number of oids not found for later use */
596 oidnotfound[*objnotfound] = oid;
598 } else { /* If Obj found in machine (i.e. has not moved) */
599 /* Check if Obj is locked by any previous transaction */
600 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
601 if (version == ((objheader_t *)mobj)->version) { /* match versions */
603 } else { /* If versions don't match ...HARD ABORT */
605 oidvernotmatch[(*objvernotmatch)++] = oid;
608 size += sizeof(objheader_t);
610 /* Send TRANS_DISAGREE to Coordinator */
611 *control = TRANS_DISAGREE;
612 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
614 //Keep track of oid locked
615 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);
694 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
695 struct ___Object___ *src=(struct ___Object___*)((char*)modptr+sizeof(objheader_t)+offset);
697 dst->___cachedCode___=src->___cachedCode___;
698 dst->___cachedHash___=src->___cachedHash___;
699 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
701 header->version += 1;
702 /* If threads are waiting on this object to be updated, notify them */
703 if(header->notifylist != NULL) {
704 notifyAll(&header->notifylist, OID(header), header->version);
706 offset += sizeof(objheader_t) + tmpsize;
712 /* Unlock locked objects */
713 int useWriteUnlock = 0;
714 for(i = 0; i < numlocked; i++) {
715 if(oidlocked[i] == -1) {
719 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
720 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
725 write_unlock(STATUSPTR(header));
727 read_unlock(STATUSPTR(header));
730 //TODO Update location lookup table
734 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
735 * Looks for the objects to be prefetched in the main object store.
736 * If objects are not found then record those and if objects are found
737 * then use offset values to prefetch references to other objects */
739 int prefetchReq(int acceptfd) {
740 int i, size, objsize, numoffset = 0;
742 char *recvbuffer, control;
743 unsigned int oid, mid=-1;
749 recv_data((int)acceptfd, &numoffset, sizeof(int));
752 recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
754 if (mid != oidmid.mid) {
756 freeSockWithLock(transPResponseSocketPool, mid, sd);
759 sd = getSockWithLock(transPResponseSocketPool, mid);
761 short offsetarry[numoffset];
762 recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
764 /*Process each oid */
765 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
766 /* Save the oids not found in buffer for later use */
767 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
768 char sendbuffer[size];
769 *((int *) sendbuffer) = size;
770 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
771 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
772 control = TRANS_PREFETCH_RESPONSE;
773 sendPrefetchResponse(sd, &control, sendbuffer, &size);
774 } else { /* Object Found */
776 GETSIZE(objsize, header);
777 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
778 char sendbuffer[size];
779 *((int *)(sendbuffer + incr)) = size;
781 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
782 incr += sizeof(char);
783 *((unsigned int *)(sendbuffer+incr)) = oid;
784 incr += sizeof(unsigned int);
785 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
787 control = TRANS_PREFETCH_RESPONSE;
788 sendPrefetchResponse(sd, &control, sendbuffer, &size);
790 /* Calculate the oid corresponding to the offset value */
791 for(i = 0 ; i< numoffset ; i++) {
792 /* Check for arrays */
793 if(TYPE(header) >= NUMCLASSES) {
794 int elementsize = classsize[TYPE(header)];
795 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
796 unsigned short length = ao->___length___;
797 /* Check if array out of bounds */
798 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
801 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
803 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
806 /* Don't continue if we hit a NULL pointer */
810 if((header = mhashSearch(oid)) == NULL) {
811 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
812 char sendbuffer[size];
813 *((int *) sendbuffer) = size;
814 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
815 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
817 control = TRANS_PREFETCH_RESPONSE;
818 sendPrefetchResponse(sd, &control, sendbuffer, &size);
820 } else { /* Obj Found */
822 GETSIZE(objsize, header);
823 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
824 char sendbuffer[size];
825 *((int *)(sendbuffer + incr)) = size;
827 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
828 incr += sizeof(char);
829 *((unsigned int *)(sendbuffer+incr)) = oid;
830 incr += sizeof(unsigned int);
831 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
833 control = TRANS_PREFETCH_RESPONSE;
834 sendPrefetchResponse(sd, &control, sendbuffer, &size);
841 freeSockWithLock(transPResponseSocketPool, mid, sd);
846 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
847 send_data(sd, control, sizeof(char));
848 /* Send the buffer with its size */
849 int length = *(size);
850 send_data(sd, sendbuffer, length);
853 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
856 unsigned short newversion;
857 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
859 struct sockaddr_in remoteAddr;
865 oid = *(oidarry + i);
866 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
867 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
870 /* Check to see if versions are same */
872 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
873 newversion = header->version;
874 if(newversion == *(versionarry + i)) {
875 //Add to the notify list
876 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
877 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
880 write_unlock(STATUSPTR(header));
882 write_unlock(STATUSPTR(header));
883 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
884 perror("processReqNotify():socket()");
887 bzero(&remoteAddr, sizeof(remoteAddr));
888 remoteAddr.sin_family = AF_INET;
889 remoteAddr.sin_port = htons(LISTEN_PORT);
890 remoteAddr.sin_addr.s_addr = htonl(mid);
892 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
893 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
894 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
898 //Send Update notification
899 msg[0] = THREAD_NOTIFY_RESPONSE;
900 *((unsigned int *)&msg[1]) = oid;
901 size = sizeof(unsigned int);
902 *((unsigned short *)(&msg[1]+size)) = newversion;
903 size += sizeof(unsigned short);
904 *((unsigned int *)(&msg[1]+size)) = threadid;
905 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
906 send_data(sd, msg, size);