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"
15 #define BACKLOG 10 //max pending connections
16 #define RECEIVE_BUFFER_SIZE 2048
18 extern int classsize[];
19 extern int numHostsInSystem;
20 extern pthread_mutex_t notifymutex;
22 objstr_t *mainobjstore;
23 pthread_mutex_t mainobjstore_mutex;
24 pthread_mutex_t lockObjHeader;
25 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
27 sockPoolHashTable_t *transPResponseSocketPool;
29 /* This function initializes the main objects store and creates the
30 * global machine and location lookup table */
33 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
34 /* Initialize attribute for mutex */
35 pthread_mutexattr_init(&mainobjstore_mutex_attr);
36 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
37 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
38 pthread_mutex_init(&lockObjHeader,NULL);
39 if (mhashCreate(MHASH_SIZE, MLOADFACTOR))
42 if (lhashCreate(HASH_SIZE, LOADFACTOR))
45 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
48 //Initialize socket pool
49 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, DEFAULTSOCKPOOLSIZE)) == NULL) {
50 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
58 int startlistening() {
60 struct sockaddr_in my_addr;
61 socklen_t addrlength = sizeof(struct sockaddr);
64 listenfd = socket(AF_INET, SOCK_STREAM, 0);
70 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
75 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
81 my_addr.sin_family = AF_INET;
82 my_addr.sin_port = htons(LISTEN_PORT);
83 my_addr.sin_addr.s_addr = INADDR_ANY;
84 memset(&(my_addr.sin_zero), '\0', 8);
86 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
91 if (listen(listenfd, BACKLOG) == -1) {
98 /* This function starts the thread to listen on a socket
99 * for tranaction calls */
100 void *dstmListen(void *lfd) {
101 int listenfd=(int)lfd;
103 struct sockaddr_in client_addr;
104 socklen_t addrlength = sizeof(struct sockaddr);
105 pthread_t thread_dstm_accept;
107 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
111 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
112 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
114 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
116 pthread_detach(thread_dstm_accept);
119 /* This function accepts a new connection request, decodes the control message in the connection
120 * and accordingly calls other functions to process new requests */
121 void *dstmAccept(void *acceptfd) {
122 int val, retval, size, sum, sockid;
129 trans_commit_data_t transinfo;
130 unsigned short objType, *versionarry, version;
131 unsigned int *oidarry, numoid, mid, threadid;
133 /* Receive control messages from other machines */
135 int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
139 printf("DEBUG -> RECV Error!.. retrying\n");
144 /* Read oid requested and search if available */
145 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
146 while((srcObj = mhashSearch(oid)) == NULL) {
148 if((ret = sched_yield()) != 0) {
149 printf("%s(): error no %d in thread yield\n", __func__, errno);
152 h = (objheader_t *) srcObj;
154 size += sizeof(objheader_t);
155 sockid = (int) acceptfd;
157 ctrl = OBJECT_NOT_FOUND;
158 send_data(sockid, &ctrl, sizeof(char));
161 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
162 *((int *)&msg[1])=size;
163 send_data(sockid, &msg, sizeof(msg));
164 send_data(sockid, h, size);
168 case READ_MULT_REQUEST:
174 case MOVE_MULT_REQUEST:
178 /* Read transaction request */
179 transinfo.objlocked = NULL;
180 transinfo.objnotfound = NULL;
181 transinfo.modptr = NULL;
182 transinfo.numlocked = 0;
183 transinfo.numnotfound = 0;
184 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
185 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
192 if((val = rangePrefetchReq((int)acceptfd)) != 0) {
193 printf("Error: In rangePrefetchReq() %s, %d\n", __FILE__, __LINE__);
197 if((val = prefetchReq((int)acceptfd)) != 0) {
198 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
204 case TRANS_PREFETCH_RESPONSE:
206 if((val = getRangePrefetchResponse((int)acceptfd)) != 0) {
207 printf("Error: In getRangePrefetchRespose() %s, %d\n", __FILE__, __LINE__);
211 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
212 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
218 case START_REMOTE_THREAD:
219 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
220 objType = getObjType(oid);
221 startDSMthread(oid, objType);
224 case THREAD_NOTIFY_REQUEST:
225 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
226 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
227 if((buffer = calloc(1,size)) == NULL) {
228 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
232 recv_data((int)acceptfd, buffer, size);
234 oidarry = calloc(numoid, sizeof(unsigned int));
235 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
236 size = sizeof(unsigned int) * numoid;
237 versionarry = calloc(numoid, sizeof(unsigned short));
238 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
239 size += sizeof(unsigned short) * numoid;
240 mid = *((unsigned int *)(buffer+size));
241 size += sizeof(unsigned int);
242 threadid = *((unsigned int *)(buffer+size));
243 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
248 case THREAD_NOTIFY_RESPONSE:
249 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
250 if((buffer = calloc(1,size)) == NULL) {
251 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
255 recv_data((int)acceptfd, buffer, size);
257 oid = *((unsigned int *)buffer);
258 size = sizeof(unsigned int);
259 version = *((unsigned short *)(buffer+size));
260 size += sizeof(unsigned short);
261 threadid = *((unsigned int *)(buffer+size));
262 threadNotify(oid,version,threadid);
266 case CLOSE_CONNECTION:
267 goto closeconnection;
270 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
275 /* Close connection */
276 if (close((int)acceptfd) == -1)
281 /* This function reads the information available in a transaction request
282 * and makes a function call to process the request */
283 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
286 unsigned int *oidmod, oid;
288 objheader_t *headaddr;
289 int sum, i, size, n, val;
293 /* Read fixed_data_t data structure */
294 size = sizeof(fixed) - 1;
295 ptr = (char *)&fixed;;
296 fixed.control = TRANS_REQUEST;
297 recv_data((int)acceptfd, ptr+1, size);
299 /* Read list of mids */
300 int mcount = fixed.mcount;
301 size = mcount * sizeof(unsigned int);
302 unsigned int listmid[mcount];
303 ptr = (char *) listmid;
304 recv_data((int)acceptfd, ptr, size);
306 /* Read oid and version tuples for those objects that are not modified in the transaction */
307 int numread = fixed.numread;
308 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
310 if(numread != 0) { //If pile contains more than one object to be read,
311 // keep reading all objects
312 recv_data((int)acceptfd, objread, size);
315 /* Read modified objects */
316 if(fixed.nummod != 0) {
317 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
318 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
321 size = fixed.sum_bytes;
322 recv_data((int)acceptfd, modptr, size);
325 /* Create an array of oids for modified objects */
326 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
327 if (oidmod == NULL) {
328 printf("calloc error %s, %d\n", __FILE__, __LINE__);
331 ptr = (char *) modptr;
332 for(i = 0 ; i < fixed.nummod; i++) {
334 headaddr = (objheader_t *) ptr;
337 GETSIZE(tmpsize, headaddr);
338 ptr += sizeof(objheader_t) + tmpsize;
341 /*Process the information read */
342 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
343 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
359 /* This function processes the Coordinator's transaction request using "handleTransReq"
360 * function and sends a reply to the co-ordinator.
361 * Following this it also receives a new control message from the co-ordinator and processes this message*/
362 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
363 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
365 char control, sendctrl, retval;
366 objheader_t *tmp_header;
370 /* Send reply to the Coordinator */
371 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
372 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
376 recv_data((int)acceptfd, &control, sizeof(char));
377 /* Process the new control message */
380 if (fixed->nummod > 0)
382 /* Unlock objects that was locked due to this transaction */
383 int useWriteUnlock = 0;
384 for(i = 0; i< transinfo->numlocked; i++) {
385 if(transinfo->objlocked[i] == -1) {
389 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
390 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
394 write_unlock(STATUSPTR(header));
396 read_unlock(STATUSPTR(header));
402 /* Invoke the transCommit process() */
403 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
404 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
406 if (transinfo->objlocked != NULL) {
407 free(transinfo->objlocked);
409 if (transinfo->objnotfound != NULL) {
410 free(transinfo->objnotfound);
416 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
420 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
421 //TODO Use fixed.trans_id TID since Client may have died
426 if (transinfo->objlocked != NULL) {
427 free(transinfo->objlocked);
429 if (transinfo->objnotfound != NULL) {
430 free(transinfo->objnotfound);
436 /* This function increments counters while running a voting decision on all objects involved
437 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
438 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
440 unsigned short version;
441 char control = 0, *ptr;
443 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
444 objheader_t *headptr;
446 /* Counters and arrays to formulate decision on control message to be sent */
447 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
448 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
449 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
450 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
451 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
453 /* modptr points to the beginning of the object store
454 * created at the Pariticipant.
455 * Object store holds the modified objects involved in the transaction request */
456 ptr = (char *) modptr;
458 /* Process each oid in the machine pile/ group per thread */
459 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
460 if (i < fixed->numread) { //Objs only read and not modified
461 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
463 oid = *((unsigned int *)(objread + incr));
464 incr += sizeof(unsigned int);
465 version = *((unsigned short *)(objread + incr));
466 getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
467 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
468 } else { //Objs modified
469 if(i == fixed->numread) {
470 oidlocked[objlocked++] = -1;
473 headptr = (objheader_t *) ptr;
475 version = headptr->version;
476 GETSIZE(tmpsize, headptr);
477 ptr += sizeof(objheader_t) + tmpsize;
478 getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
479 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
480 &numBytes, &control, oid, version);
484 /* send TRANS_DISAGREE and objs*/
487 char *objs = calloc(1, numBytes);
489 for(j = 0; j<objvernotmatch; j++) {
490 objheader_t *header = mhashSearch(oidvernotmatch[j]);
492 GETSIZE(size, header);
493 size += sizeof(objheader_t);
494 memcpy(objs+offset, header, size);
499 int useWriteUnlock = 0;
500 for(j = 0; j < objlocked; j++) {
501 if(oidlocked[j] == -1) {
505 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
506 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
510 write_unlock(STATUSPTR(headptr));
512 read_unlock(STATUSPTR(headptr));
517 send_data(acceptfd, &control, sizeof(char));
519 send_data(acceptfd, &numBytes, sizeof(int));
520 send_data(acceptfd, objs, numBytes);
521 transinfo->objvernotmatch = oidvernotmatch;
522 transinfo->numvernotmatch = objvernotmatch;
524 free(transinfo->objvernotmatch);
529 /* Decide what control message to send to Coordinator */
530 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
531 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
532 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
538 /* Update Commit info for objects that are read */
539 void getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
540 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
541 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
542 char *control, unsigned int oid, unsigned short version) {
544 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
546 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
547 /* Save the oids not found and number of oids not found for later use */
548 oidnotfound[*objnotfound] = oid;
550 } else { /* If Obj found in machine (i.e. has not moved) */
551 /* Check if Obj is locked by any previous transaction */
552 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
553 if (version == ((objheader_t *)mobj)->version) { /* match versions */
555 } else { /* If versions don't match ...HARD ABORT */
557 oidvernotmatch[*objvernotmatch] = oid;
561 size += sizeof(objheader_t);
563 /* Send TRANS_DISAGREE to Coordinator */
564 *control = TRANS_DISAGREE;
565 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
567 //Keep track of oid locked
568 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
569 } else { //we are locked
570 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
572 } else { /* If versions don't match ...HARD ABORT */
574 oidvernotmatch[*objvernotmatch] = oid;
578 size += sizeof(objheader_t);
580 *control = TRANS_DISAGREE;
581 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
587 /* Update Commit info for objects that are read */
588 void getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
589 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
590 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
592 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
593 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
594 /* Save the oids not found and number of oids not found for later use */
595 oidnotfound[*objnotfound] = oid;
597 } else { /* If Obj found in machine (i.e. has not moved) */
598 /* Check if Obj is locked by any previous transaction */
599 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
600 if (version == ((objheader_t *)mobj)->version) { /* match versions */
602 } else { /* If versions don't match ...HARD ABORT */
604 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));
615 } else { /* Some other transaction has aquired a write lock on this object */
616 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
618 } else { /* If versions don't match ...HARD ABORT */
620 oidvernotmatch[*objvernotmatch] = oid;
624 size += sizeof(objheader_t);
626 *control = TRANS_DISAGREE;
627 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
633 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
634 * to send to Coordinator based on the votes of oids involved in the transaction */
635 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
636 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
637 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
641 /* Condition to send TRANS_AGREE */
642 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
643 control = TRANS_AGREE;
644 /* Send control message */
645 send_data(acceptfd, &control, sizeof(char));
647 /* Condition to send TRANS_SOFT_ABORT */
648 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
649 control = TRANS_SOFT_ABORT;
651 /* Send control message */
652 send_data(acceptfd, &control, sizeof(char));
654 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
655 if(*(objnotfound) != 0) {
657 msg[0] = *(objnotfound);
658 send_data(acceptfd, &msg, sizeof(int));
659 int size = sizeof(unsigned int)* *(objnotfound);
660 send_data(acceptfd, oidnotfound, size);
664 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
665 * if Participant receives a TRANS_COMMIT */
666 transinfo->objlocked = oidlocked;
667 transinfo->objnotfound = oidnotfound;
668 transinfo->modptr = modptr;
669 transinfo->numlocked = *(objlocked);
670 transinfo->numnotfound = *(objnotfound);
674 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
675 * addresses in lookup table and also changes version number
676 * Sends an ACK back to Coordinator */
677 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
679 objheader_t *newheader;
680 int i = 0, offset = 0;
684 /* Process each modified object saved in the mainobject store */
685 for(i = 0; i < nummod; i++) {
686 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
687 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
690 GETSIZE(tmpsize,header);
693 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
694 struct ___Object___ *src=(struct ___Object___*)((char*)modptr+sizeof(objheader_t)+offset);
696 dst->___cachedCode___=src->___cachedCode___;
697 dst->___cachedHash___=src->___cachedHash___;
698 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
700 header->version += 1;
701 /* If threads are waiting on this object to be updated, notify them */
702 if(header->notifylist != NULL) {
703 notifyAll(&header->notifylist, OID(header), header->version);
705 offset += sizeof(objheader_t) + tmpsize;
711 /* Unlock locked objects */
712 int useWriteUnlock = 0;
713 for(i = 0; i < numlocked; i++) {
714 if(oidlocked[i] == -1) {
718 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
719 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
724 write_unlock(STATUSPTR(header));
726 read_unlock(STATUSPTR(header));
729 //TODO Update location lookup table
733 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
734 * Looks for the objects to be prefetched in the main object store.
735 * If objects are not found then record those and if objects are found
736 * then use offset values to prefetch references to other objects */
738 int prefetchReq(int acceptfd) {
739 int i, size, objsize, numoffset = 0;
741 char *recvbuffer, control;
742 unsigned int oid, mid=-1;
748 recv_data((int)acceptfd, &numoffset, sizeof(int));
751 recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
753 if (mid != oidmid.mid) {
755 freeSockWithLock(transPResponseSocketPool, mid, sd);
758 sd = getSockWithLock(transPResponseSocketPool, mid);
760 short offsetarry[numoffset];
761 recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
763 /*Process each oid */
764 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
765 /* Save the oids not found in buffer for later use */
766 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
767 char sendbuffer[size];
768 *((int *) sendbuffer) = size;
769 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
770 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
771 control = TRANS_PREFETCH_RESPONSE;
772 sendPrefetchResponse(sd, &control, sendbuffer, &size);
773 } else { /* Object Found */
775 GETSIZE(objsize, header);
776 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
777 char sendbuffer[size];
778 *((int *)(sendbuffer + incr)) = size;
780 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
781 incr += sizeof(char);
782 *((unsigned int *)(sendbuffer+incr)) = oid;
783 incr += sizeof(unsigned int);
784 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
786 control = TRANS_PREFETCH_RESPONSE;
787 sendPrefetchResponse(sd, &control, sendbuffer, &size);
789 /* Calculate the oid corresponding to the offset value */
790 for(i = 0 ; i< numoffset ; i++) {
791 /* Check for arrays */
792 if(TYPE(header) > NUMCLASSES) {
793 int elementsize = classsize[TYPE(header)];
794 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
795 unsigned short length = ao->___length___;
796 /* Check if array out of bounds */
797 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
800 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
802 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
805 /* Don't continue if we hit a NULL pointer */
809 if((header = mhashSearch(oid)) == NULL) {
810 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
811 char sendbuffer[size];
812 *((int *) sendbuffer) = size;
813 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
814 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
816 control = TRANS_PREFETCH_RESPONSE;
817 sendPrefetchResponse(sd, &control, sendbuffer, &size);
819 } else { /* Obj Found */
821 GETSIZE(objsize, header);
822 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
823 char sendbuffer[size];
824 *((int *)(sendbuffer + incr)) = size;
826 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
827 incr += sizeof(char);
828 *((unsigned int *)(sendbuffer+incr)) = oid;
829 incr += sizeof(unsigned int);
830 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
832 control = TRANS_PREFETCH_RESPONSE;
833 sendPrefetchResponse(sd, &control, sendbuffer, &size);
840 freeSockWithLock(transPResponseSocketPool, mid, sd);
845 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
846 send_data(sd, control, sizeof(char));
847 /* Send the buffer with its size */
848 int length = *(size);
849 send_data(sd, sendbuffer, length);
852 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
855 unsigned short newversion;
856 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
858 struct sockaddr_in remoteAddr;
864 oid = *(oidarry + i);
865 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
866 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
869 /* Check to see if versions are same */
871 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
872 newversion = header->version;
873 if(newversion == *(versionarry + i)) {
874 //Add to the notify list
875 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
876 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
879 write_unlock(STATUSPTR(header));
881 write_unlock(STATUSPTR(header));
882 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
883 perror("processReqNotify():socket()");
886 bzero(&remoteAddr, sizeof(remoteAddr));
887 remoteAddr.sin_family = AF_INET;
888 remoteAddr.sin_port = htons(LISTEN_PORT);
889 remoteAddr.sin_addr.s_addr = htonl(mid);
891 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
892 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
893 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
897 //Send Update notification
898 msg[0] = THREAD_NOTIFY_RESPONSE;
899 *((unsigned int *)&msg[1]) = oid;
900 size = sizeof(unsigned int);
901 *((unsigned short *)(&msg[1]+size)) = newversion;
902 size += sizeof(unsigned short);
903 *((unsigned int *)(&msg[1]+size)) = threadid;
904 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
905 send_data(sd, msg, size);