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"
13 #define BACKLOG 10 //max pending connections
14 #define RECEIVE_BUFFER_SIZE 2048
16 extern int classsize[];
17 extern int numHostsInSystem;
18 extern pthread_mutex_t notifymutex;
20 objstr_t *mainobjstore;
21 pthread_mutex_t mainobjstore_mutex;
22 pthread_mutex_t lockObjHeader;
23 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
25 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__);
188 if((val = prefetchReq((int)acceptfd)) != 0) {
189 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
194 case TRANS_PREFETCH_RESPONSE:
195 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
196 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
201 case START_REMOTE_THREAD:
202 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
203 objType = getObjType(oid);
204 startDSMthread(oid, objType);
207 case THREAD_NOTIFY_REQUEST:
208 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
209 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
210 if((buffer = calloc(1,size)) == NULL) {
211 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
215 recv_data((int)acceptfd, buffer, size);
217 oidarry = calloc(numoid, sizeof(unsigned int));
218 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
219 size = sizeof(unsigned int) * numoid;
220 versionarry = calloc(numoid, sizeof(unsigned short));
221 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
222 size += sizeof(unsigned short) * numoid;
223 mid = *((unsigned int *)(buffer+size));
224 size += sizeof(unsigned int);
225 threadid = *((unsigned int *)(buffer+size));
226 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
231 case THREAD_NOTIFY_RESPONSE:
232 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
233 if((buffer = calloc(1,size)) == NULL) {
234 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
238 recv_data((int)acceptfd, buffer, size);
240 oid = *((unsigned int *)buffer);
241 size = sizeof(unsigned int);
242 version = *((unsigned short *)(buffer+size));
243 size += sizeof(unsigned short);
244 threadid = *((unsigned int *)(buffer+size));
245 threadNotify(oid,version,threadid);
249 case CLOSE_CONNECTION:
250 goto closeconnection;
253 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
258 /* Close connection */
259 if (close((int)acceptfd) == -1)
264 /* This function reads the information available in a transaction request
265 * and makes a function call to process the request */
266 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
269 unsigned int *oidmod, oid;
271 objheader_t *headaddr;
272 int sum, i, size, n, val;
276 /* Read fixed_data_t data structure */
277 size = sizeof(fixed) - 1;
278 ptr = (char *)&fixed;;
279 fixed.control = TRANS_REQUEST;
280 recv_data((int)acceptfd, ptr+1, size);
282 /* Read list of mids */
283 int mcount = fixed.mcount;
284 size = mcount * sizeof(unsigned int);
285 unsigned int listmid[mcount];
286 ptr = (char *) listmid;
287 recv_data((int)acceptfd, ptr, size);
289 /* Read oid and version tuples for those objects that are not modified in the transaction */
290 int numread = fixed.numread;
291 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
293 if(numread != 0) { //If pile contains more than one object to be read,
294 // keep reading all objects
295 recv_data((int)acceptfd, objread, size);
298 /* Read modified objects */
299 if(fixed.nummod != 0) {
300 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
301 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
304 size = fixed.sum_bytes;
305 recv_data((int)acceptfd, modptr, size);
308 /* Create an array of oids for modified objects */
309 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
310 if (oidmod == NULL) {
311 printf("calloc error %s, %d\n", __FILE__, __LINE__);
314 ptr = (char *) modptr;
315 for(i = 0 ; i < fixed.nummod; i++) {
317 headaddr = (objheader_t *) ptr;
320 GETSIZE(tmpsize, headaddr);
321 ptr += sizeof(objheader_t) + tmpsize;
324 /*Process the information read */
325 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
326 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
342 /* This function processes the Coordinator's transaction request using "handleTransReq"
343 * function and sends a reply to the co-ordinator.
344 * Following this it also receives a new control message from the co-ordinator and processes this message*/
345 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
346 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
347 char control, sendctrl, retval;
348 objheader_t *tmp_header;
352 /* Send reply to the Coordinator */
353 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
354 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
357 recv_data((int)acceptfd, &control, sizeof(char));
358 /* Process the new control message */
361 if (fixed->nummod > 0)
363 /* Unlock objects that was locked due to this transaction */
364 int useWriteUnlock = 0;
365 for(i = 0; i< transinfo->numlocked; i++) {
366 if(transinfo->objlocked[i] == -1) {
370 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
371 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
375 write_unlock(STATUSPTR(header));
377 read_unlock(STATUSPTR(header));
383 /* Invoke the transCommit process() */
384 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
385 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
387 if (transinfo->objlocked != NULL) {
388 free(transinfo->objlocked);
390 if (transinfo->objnotfound != NULL) {
391 free(transinfo->objnotfound);
397 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
401 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
402 //TODO Use fixed.trans_id TID since Client may have died
406 if (transinfo->objlocked != NULL) {
407 free(transinfo->objlocked);
409 if (transinfo->objnotfound != NULL) {
410 free(transinfo->objnotfound);
415 /* This function increments counters while running a voting decision on all objects involved
416 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
417 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
419 unsigned short version;
420 char control = 0, *ptr;
422 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
423 objheader_t *headptr;
425 /* Counters and arrays to formulate decision on control message to be sent */
426 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
427 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
428 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
429 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
430 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
432 /* modptr points to the beginning of the object store
433 * created at the Pariticipant.
434 * Object store holds the modified objects involved in the transaction request */
435 ptr = (char *) modptr;
437 /* Process each oid in the machine pile/ group per thread */
438 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
439 if (i < fixed->numread) { //Objs only read and not modified
440 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
442 oid = *((unsigned int *)(objread + incr));
443 incr += sizeof(unsigned int);
444 version = *((unsigned short *)(objread + incr));
445 getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
446 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
447 } else { //Objs modified
448 if(i == fixed->numread) {
449 oidlocked[objlocked] = -1;
453 headptr = (objheader_t *) ptr;
455 version = headptr->version;
456 GETSIZE(tmpsize, headptr);
457 ptr += sizeof(objheader_t) + tmpsize;
458 getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
459 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
460 &numBytes, &control, oid, version);
464 /* send TRANS_DISAGREE and objs*/
467 char *objs = calloc(1, numBytes);
469 for(j = 0; j<objvernotmatch; j++) {
470 objheader_t *header = mhashSearch(oidvernotmatch[j]);
472 GETSIZE(size, header);
473 size += sizeof(objheader_t);
474 memcpy(objs+offset, header, size);
479 int useWriteUnlock = 0;
480 for(j = 0; j < objlocked; j++) {
481 if(oidlocked[j] == -1) {
485 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
486 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
490 write_unlock(STATUSPTR(headptr));
492 read_unlock(STATUSPTR(headptr));
497 send_data(acceptfd, &control, sizeof(char));
499 send_data(acceptfd, &numBytes, sizeof(int));
500 send_data(acceptfd, objs, numBytes);
501 transinfo->objvernotmatch = oidvernotmatch;
502 transinfo->numvernotmatch = objvernotmatch;
504 free(transinfo->objvernotmatch);
509 /* Decide what control message to send to Coordinator */
510 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
511 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
512 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
518 /* Update Commit info for objects that are read */
519 void getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
520 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
521 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
522 char *control, unsigned int oid, unsigned short version) {
524 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
526 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
527 /* Save the oids not found and number of oids not found for later use */
528 oidnotfound[*objnotfound] = oid;
530 } else { /* If Obj found in machine (i.e. has not moved) */
531 /* Check if Obj is locked by any previous transaction */
532 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
533 if (version == ((objheader_t *)mobj)->version) { /* match versions */
535 } else { /* If versions don't match ...HARD ABORT */
537 oidvernotmatch[*objvernotmatch] = oid;
541 size += sizeof(objheader_t);
543 /* Send TRANS_DISAGREE to Coordinator */
544 *control = TRANS_DISAGREE;
546 //Keep track of oid locked
547 oidlocked[*objlocked] = OID(((objheader_t *)mobj));
549 } else { //we are locked
550 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
552 } else { /* If versions don't match ...HARD ABORT */
554 oidvernotmatch[*objvernotmatch] = oid;
558 size += sizeof(objheader_t);
560 *control = TRANS_DISAGREE;
566 /* Update Commit info for objects that are read */
567 void getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
568 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
569 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
571 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
572 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
573 /* Save the oids not found and number of oids not found for later use */
574 oidnotfound[*objnotfound] = oid;
576 } else { /* If Obj found in machine (i.e. has not moved) */
577 /* Check if Obj is locked by any previous transaction */
578 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
579 if (version == ((objheader_t *)mobj)->version) { /* match versions */
581 } else { /* If versions don't match ...HARD ABORT */
583 oidvernotmatch[*objvernotmatch] = oid;
587 size += sizeof(objheader_t);
589 /* Send TRANS_DISAGREE to Coordinator */
590 *control = TRANS_DISAGREE;
592 //Keep track of oid locked
593 oidlocked[*objlocked] = OID(((objheader_t *)mobj));
595 } else { /* Some other transaction has aquired a write lock on this object */
596 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
598 } else { /* If versions don't match ...HARD ABORT */
600 oidvernotmatch[*objvernotmatch] = oid;
604 size += sizeof(objheader_t);
606 *control = TRANS_DISAGREE;
612 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
613 * to send to Coordinator based on the votes of oids involved in the transaction */
614 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
615 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
616 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
620 /* Condition to send TRANS_AGREE */
621 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
622 control = TRANS_AGREE;
623 /* Send control message */
624 send_data(acceptfd, &control, sizeof(char));
626 /* Condition to send TRANS_SOFT_ABORT */
627 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
628 control = TRANS_SOFT_ABORT;
630 /* Send control message */
631 send_data(acceptfd, &control, sizeof(char));
633 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
634 if(*(objnotfound) != 0) {
636 msg[0] = *(objnotfound);
637 send_data(acceptfd, &msg, sizeof(int));
638 int size = sizeof(unsigned int)* *(objnotfound);
639 send_data(acceptfd, oidnotfound, size);
643 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
644 * if Participant receives a TRANS_COMMIT */
645 transinfo->objlocked = oidlocked;
646 transinfo->objnotfound = oidnotfound;
647 transinfo->modptr = modptr;
648 transinfo->numlocked = *(objlocked);
649 transinfo->numnotfound = *(objnotfound);
654 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
655 * addresses in lookup table and also changes version number
656 * Sends an ACK back to Coordinator */
657 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
659 objheader_t *newheader;
660 int i = 0, offset = 0;
664 /* Process each modified object saved in the mainobject store */
665 for(i = 0; i < nummod; i++) {
666 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
667 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
670 GETSIZE(tmpsize,header);
671 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
672 header->version += 1;
673 /* If threads are waiting on this object to be updated, notify them */
674 if(header->notifylist != NULL) {
675 notifyAll(&header->notifylist, OID(header), header->version);
677 offset += sizeof(objheader_t) + tmpsize;
683 /* Unlock locked objects */
684 int useWriteUnlock = 0;
685 for(i = 0; i < numlocked; i++) {
686 if(oidlocked[i] == -1) {
690 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
691 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
696 write_unlock(STATUSPTR(header));
698 read_unlock(STATUSPTR(header));
701 //TODO Update location lookup table
705 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
706 * Looks for the objects to be prefetched in the main object store.
707 * If objects are not found then record those and if objects are found
708 * then use offset values to prefetch references to other objects */
710 int prefetchReq(int acceptfd) {
711 int i, size, objsize, numoffset = 0;
713 char *recvbuffer, control;
714 unsigned int oid, mid=-1;
720 recv_data((int)acceptfd, &numoffset, sizeof(int));
723 recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
725 if (mid != oidmid.mid) {
727 freeSockWithLock(transPResponseSocketPool, mid, sd);
730 sd = getSockWithLock(transPResponseSocketPool, mid);
732 short offsetarry[numoffset];
733 recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
735 /*Process each oid */
736 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
737 /* Save the oids not found in buffer for later use */
738 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
739 char sendbuffer[size];
740 *((int *) sendbuffer) = size;
741 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
742 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
743 control = TRANS_PREFETCH_RESPONSE;
744 sendPrefetchResponse(sd, &control, sendbuffer, &size);
745 } else { /* Object Found */
747 GETSIZE(objsize, header);
748 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
749 char sendbuffer[size];
750 *((int *)(sendbuffer + incr)) = size;
752 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
753 incr += sizeof(char);
754 *((unsigned int *)(sendbuffer+incr)) = oid;
755 incr += sizeof(unsigned int);
756 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
758 control = TRANS_PREFETCH_RESPONSE;
759 sendPrefetchResponse(sd, &control, sendbuffer, &size);
761 /* Calculate the oid corresponding to the offset value */
762 for(i = 0 ; i< numoffset ; i++) {
763 /* Check for arrays */
764 if(TYPE(header) > NUMCLASSES) {
765 int elementsize = classsize[TYPE(header)];
766 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
767 unsigned short length = ao->___length___;
768 /* Check if array out of bounds */
769 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
772 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
774 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
777 /* Don't continue if we hit a NULL pointer */
781 if((header = mhashSearch(oid)) == NULL) {
782 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
783 char sendbuffer[size];
784 *((int *) sendbuffer) = size;
785 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
786 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
788 control = TRANS_PREFETCH_RESPONSE;
789 sendPrefetchResponse(sd, &control, sendbuffer, &size);
791 } else { /* Obj Found */
793 GETSIZE(objsize, header);
794 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
795 char sendbuffer[size];
796 *((int *)(sendbuffer + incr)) = size;
798 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
799 incr += sizeof(char);
800 *((unsigned int *)(sendbuffer+incr)) = oid;
801 incr += sizeof(unsigned int);
802 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
804 control = TRANS_PREFETCH_RESPONSE;
805 sendPrefetchResponse(sd, &control, sendbuffer, &size);
812 freeSockWithLock(transPResponseSocketPool, mid, sd);
817 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
818 send_data(sd, control, sizeof(char));
819 /* Send the buffer with its size */
820 int length = *(size);
821 send_data(sd, sendbuffer, length);
824 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
827 unsigned short newversion;
828 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
830 struct sockaddr_in remoteAddr;
836 oid = *(oidarry + i);
837 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
838 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
841 /* Check to see if versions are same */
843 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
844 newversion = header->version;
845 if(newversion == *(versionarry + i)) {
846 //Add to the notify list
847 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
848 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
851 write_unlock(STATUSPTR(header));
853 write_unlock(STATUSPTR(header));
854 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
855 perror("processReqNotify():socket()");
858 bzero(&remoteAddr, sizeof(remoteAddr));
859 remoteAddr.sin_family = AF_INET;
860 remoteAddr.sin_port = htons(LISTEN_PORT);
861 remoteAddr.sin_addr.s_addr = htonl(mid);
863 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
864 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
865 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
869 //Send Update notification
870 msg[0] = THREAD_NOTIFY_RESPONSE;
871 *((unsigned int *)&msg[1]) = oid;
872 size = sizeof(unsigned int);
873 *((unsigned short *)(&msg[1]+size)) = newversion;
874 size += sizeof(unsigned short);
875 *((unsigned int *)(&msg[1]+size)) = threadid;
876 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
877 send_data(sd, msg, size);