check LOCK versions
[IRC.git] / Robust / src / Runtime / DSTM / interface / dstmserver.c
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 */
3
4 #include "dstm.h"
5 #include "mlookup.h"
6 #include "llookup.h"
7 #include "threadnotify.h"
8 #ifdef COMPILER
9 #include "thread.h"
10 #endif
11
12 #define BACKLOG 10 //max pending connections
13 #define RECEIVE_BUFFER_SIZE 2048
14
15 extern int classsize[];
16 extern int numHostsInSystem;
17 extern pthread_mutex_t notifymutex;
18
19 objstr_t *mainobjstore;
20 pthread_mutex_t mainobjstore_mutex;
21 pthread_mutex_t lockObjHeader;
22 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
23
24 sockPoolHashTable_t *transPResponseSocketPool;
25
26 /* This function initializes the main objects store and creates the 
27  * global machine and location lookup table */
28
29 int dstmInit(void)
30 {
31         mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
32         /* Initialize attribute for mutex */
33         pthread_mutexattr_init(&mainobjstore_mutex_attr);
34         pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
35         pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
36         pthread_mutex_init(&lockObjHeader,NULL);
37         if (mhashCreate(HASH_SIZE, LOADFACTOR))
38                 return 1; //failure
39         
40         if (lhashCreate(HASH_SIZE, LOADFACTOR))
41                 return 1; //failure
42
43         if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
44                 return 1; //failure
45
46     //Initialize socket pool
47     if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, 2*numHostsInSystem+1)) == NULL) {
48         printf("Error in creating new socket pool at  %s line %d\n", __FILE__, __LINE__);
49         return 0;
50     }
51
52         return 0;
53 }
54
55 /* This function starts the thread to listen on a socket 
56  * for tranaction calls */
57 void *dstmListen()
58 {
59         int listenfd, acceptfd;
60         struct sockaddr_in my_addr;
61         struct sockaddr_in client_addr;
62         socklen_t addrlength = sizeof(struct sockaddr);
63         pthread_t thread_dstm_accept;
64         int i;
65         int setsockflag=1;
66
67         listenfd = socket(AF_INET, SOCK_STREAM, 0);
68         if (listenfd == -1)
69         {
70                 perror("socket");
71                 exit(1);
72         }
73
74         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
75           perror("socket");
76           exit(1);
77         }
78 #ifdef MAC
79         if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
80           perror("socket");
81           exit(1);
82         }
83 #endif
84
85         my_addr.sin_family = AF_INET;
86         my_addr.sin_port = htons(LISTEN_PORT);
87         my_addr.sin_addr.s_addr = INADDR_ANY;
88         memset(&(my_addr.sin_zero), '\0', 8);
89
90         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
91         {
92                 perror("bind");
93                 exit(1);
94         }
95         
96         if (listen(listenfd, BACKLOG) == -1)
97         {
98                 perror("listen");
99                 exit(1);
100         }
101
102         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
103         while(1)
104         {
105           int retval;
106           acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
107           do {
108             retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
109           } while(retval!=0);
110           pthread_detach(thread_dstm_accept);
111         }
112 }
113 /* This function accepts a new connection request, decodes the control message in the connection 
114  * and accordingly calls other functions to process new requests */
115 void *dstmAccept(void *acceptfd) {
116   int val, retval, size, sum, sockid;
117   unsigned int oid;
118   char *buffer;
119   char control,ctrl;
120   char *ptr;
121   void *srcObj;
122   objheader_t *h;
123   trans_commit_data_t transinfo;
124   unsigned short objType, *versionarry, version;
125   unsigned int *oidarry, numoid, mid, threadid;
126   
127   /* Receive control messages from other machines */
128   while(1) {
129     int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
130     if (ret==-1) {
131       printf("DEBUG -> RECV Error!.. retrying\n");
132       break;
133     }
134     switch(control) {
135     case READ_REQUEST:
136       /* Read oid requested and search if available */
137       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
138       if((srcObj = mhashSearch(oid)) == NULL) {
139         printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
140         break;
141       }
142       h = (objheader_t *) srcObj;
143       GETSIZE(size, h);
144       size += sizeof(objheader_t);
145       sockid = (int) acceptfd;
146       
147       if (h == NULL) {
148         ctrl = OBJECT_NOT_FOUND;
149         send_data(sockid, &ctrl, sizeof(char));
150       } else {
151         /* Type */
152         char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
153         *((int *)&msg[1])=size;
154         send_data(sockid, &msg, sizeof(msg));
155         send_data(sockid, h, size);
156       }
157       break;
158       
159     case READ_MULT_REQUEST:
160       break;
161       
162     case MOVE_REQUEST:
163       break;
164       
165     case MOVE_MULT_REQUEST:
166       break;
167       
168     case TRANS_REQUEST:
169       /* Read transaction request */
170       transinfo.objlocked = NULL;
171       transinfo.objnotfound = NULL;
172       transinfo.modptr = NULL;
173       transinfo.numlocked = 0;
174       transinfo.numnotfound = 0;
175       if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
176         printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
177         pthread_exit(NULL);
178       }
179       break;
180     case TRANS_PREFETCH:
181       if((val = prefetchReq((int)acceptfd)) != 0) {
182         printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
183         break;
184       }
185       break;
186     case TRANS_PREFETCH_RESPONSE:
187       if((val = getPrefetchResponse((int) acceptfd)) != 0) {
188         printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
189         break;
190       }
191       break;
192     case START_REMOTE_THREAD:
193       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
194       objType = getObjType(oid);
195       startDSMthread(oid, objType);
196       break;
197       
198     case THREAD_NOTIFY_REQUEST:
199       recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
200       size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
201       if((buffer = calloc(1,size)) == NULL) {
202         printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
203         pthread_exit(NULL);
204       }
205       
206       recv_data((int)acceptfd, buffer, size);
207       
208       oidarry = calloc(numoid, sizeof(unsigned int)); 
209       memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
210       size = sizeof(unsigned int) * numoid;
211       versionarry = calloc(numoid, sizeof(unsigned short));
212       memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
213       size += sizeof(unsigned short) * numoid;
214       mid = *((unsigned int *)(buffer+size));
215       size += sizeof(unsigned int);
216       threadid = *((unsigned int *)(buffer+size));
217       processReqNotify(numoid, oidarry, versionarry, mid, threadid);
218       free(buffer);
219       
220       break;
221
222     case THREAD_NOTIFY_RESPONSE:
223       size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
224       if((buffer = calloc(1,size)) == NULL) {
225         printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
226         pthread_exit(NULL);
227       }
228       
229       recv_data((int)acceptfd, buffer, size);
230       
231       oid = *((unsigned int *)buffer);
232       size = sizeof(unsigned int);
233       version = *((unsigned short *)(buffer+size));
234       size += sizeof(unsigned short);
235       threadid = *((unsigned int *)(buffer+size));
236       threadNotify(oid,version,threadid);
237       free(buffer);
238       break;
239
240     case CLOSE_CONNECTION:
241       goto closeconnection;
242
243     default:
244       printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
245     }
246   }
247
248  closeconnection:
249   /* Close connection */
250   if (close((int)acceptfd) == -1)
251     perror("close");
252   pthread_exit(NULL);
253 }
254   
255 /* This function reads the information available in a transaction request
256  * and makes a function call to process the request */
257 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
258         char *ptr;
259         void *modptr;
260         unsigned int *oidmod, oid;
261         fixed_data_t fixed;
262         objheader_t *headaddr;
263         int sum, i, size, n, val;
264
265         oidmod = NULL;
266
267         /* Read fixed_data_t data structure */ 
268         size = sizeof(fixed) - 1;
269         ptr = (char *)&fixed;;
270         fixed.control = TRANS_REQUEST;
271         recv_data((int)acceptfd, ptr+1, size);
272
273         /* Read list of mids */
274         int mcount = fixed.mcount;
275         size = mcount * sizeof(unsigned int);
276         unsigned int listmid[mcount];
277         ptr = (char *) listmid;
278         recv_data((int)acceptfd, ptr, size);
279         
280         /* Read oid and version tuples for those objects that are not modified in the transaction */
281         int numread = fixed.numread;
282         size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
283         char objread[size];
284         if(numread != 0) { //If pile contains more than one object to be read, 
285                           // keep reading all objects
286                 recv_data((int)acceptfd, objread, size);        
287         }
288         
289         /* Read modified objects */
290         if(fixed.nummod != 0) {
291                 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
292                         printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
293                         return 1;
294                 }
295                 size = fixed.sum_bytes;
296                 recv_data((int)acceptfd, modptr, size); 
297         }
298
299         /* Create an array of oids for modified objects */
300         oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
301         if (oidmod == NULL)
302         {
303                 printf("calloc error %s, %d\n", __FILE__, __LINE__);
304                 return 1;
305         }
306         ptr = (char *) modptr;
307         for(i = 0 ; i < fixed.nummod; i++) {
308           int tmpsize;
309           headaddr = (objheader_t *) ptr;
310           oid = OID(headaddr);
311           oidmod[i] = oid;
312           GETSIZE(tmpsize, headaddr);
313           ptr += sizeof(objheader_t) + tmpsize;
314         }
315         
316         /*Process the information read */
317         if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
318                 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
319                 /* Free resources */
320                 if(oidmod != NULL) {
321                         free(oidmod);
322                 }
323                 return 1;
324         }
325
326         /* Free resources */
327         if(oidmod != NULL) {
328                 free(oidmod);
329         }
330
331         return 0;
332 }
333
334 /* This function processes the Coordinator's transaction request using "handleTransReq" 
335  * function and sends a reply to the co-ordinator.
336  * Following this it also receives a new control message from the co-ordinator and processes this message*/
337 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
338                 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
339         char control, sendctrl, retval;
340         objheader_t *tmp_header;
341         void *header;
342         int  i = 0, val;
343
344         /* Send reply to the Coordinator */
345         if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
346                 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
347                 return 1;
348         }
349
350         recv_data((int)acceptfd, &control, sizeof(char));
351         
352         /* Process the new control message */
353         switch(control) {
354                 case TRANS_ABORT:
355                         if (fixed->nummod > 0)
356                                 free(modptr);
357                         /* Unlock objects that was locked due to this transaction */
358                         for(i = 0; i< transinfo->numlocked; i++) {
359                                 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
360                                         printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
361                                         return 1;
362                                 }
363                                 STATUS(((objheader_t *)header)) &= ~(LOCK);             
364                         }
365
366                         /* Send ack to Coordinator */
367                         sendctrl = TRANS_UNSUCESSFUL;
368                         send_data((int)acceptfd, &sendctrl, sizeof(char));
369                         break;
370
371                 case TRANS_COMMIT:
372                         /* Invoke the transCommit process() */
373                         if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
374                                 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
375                                 /* Free memory */
376                                 if (transinfo->objlocked != NULL) {
377                                         free(transinfo->objlocked);
378                                 }
379                                 if (transinfo->objnotfound != NULL) {
380                                         free(transinfo->objnotfound);
381                                 }
382                                 return 1;
383                         }
384                         break;
385
386                 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
387                         break;
388                 default:
389                         printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
390                         //TODO Use fixed.trans_id  TID since Client may have died
391                         break;
392         }
393
394         /* Free memory */
395         if (transinfo->objlocked != NULL) {
396                 free(transinfo->objlocked);
397         }
398         if (transinfo->objnotfound != NULL) {
399                 free(transinfo->objnotfound);
400         }
401
402         return 0;
403 }
404
405 /* This function increments counters while running a voting decision on all objects involved 
406  * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
407 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
408         int val, i = 0, j;
409         unsigned short version;
410         char control = 0, *ptr;
411         unsigned int oid;
412         unsigned int *oidnotfound, *oidlocked;
413         void *mobj;
414         objheader_t *headptr;
415
416         /* Counters and arrays to formulate decision on control message to be sent */
417         oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
418         oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
419         int objnotfound = 0, objlocked = 0;
420         int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
421
422         /* modptr points to the beginning of the object store 
423          * created at the Pariticipant. 
424          * Object store holds the modified objects involved in the transaction request */ 
425         ptr = (char *) modptr;
426         
427         /* Process each oid in the machine pile/ group per thread */
428         for (i = 0; i < fixed->numread + fixed->nummod; i++) {
429                 if (i < fixed->numread) {//Objs only read and not modified
430                         int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
431                         incr *= i;
432                         oid = *((unsigned int *)(objread + incr));
433                         incr += sizeof(unsigned int);
434                         version = *((unsigned short *)(objread + incr));
435                 } else {//Objs modified
436                   int tmpsize;
437                   headptr = (objheader_t *) ptr;
438                   oid = OID(headptr);
439           version = headptr->version;
440           GETSIZE(tmpsize, headptr);
441           ptr += sizeof(objheader_t) + tmpsize;
442         }
443
444         /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
445
446         if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
447           /* Save the oids not found and number of oids not found for later use */
448           oidnotfound[objnotfound] = oid;
449           objnotfound++;
450         } else { /* If Obj found in machine (i.e. has not moved) */
451           /* Check if Obj is locked by any previous transaction */
452           pthread_mutex_lock(&lockObjHeader);
453           if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {                 
454             pthread_mutex_unlock(&lockObjHeader);
455             if (version == ((objheader_t *)mobj)->version) {      /* If locked then match versions */
456               v_matchlock++;
457             } else {/* If versions don't match ...HARD ABORT */
458               v_nomatch++;
459               /* Send TRANS_DISAGREE to Coordinator */
460               control = TRANS_DISAGREE;
461               if (objlocked > 0) {
462                 for(j = 0; j < objlocked; j++) {
463                   if((headptr = mhashSearch(oidlocked[j])) == NULL) {
464                     printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
465                     return 0;
466                   }
467                   STATUS(headptr) &= ~(LOCK);
468                 }
469                 free(oidlocked);
470               }
471               send_data(acceptfd, &control, sizeof(char));
472               return control;
473             }
474           } else {/* If Obj is not locked then lock object */
475             STATUS(((objheader_t *)mobj)) |= LOCK;
476             pthread_mutex_unlock(&lockObjHeader);
477             /* Save all object oids that are locked on this machine during this transaction request call */
478             oidlocked[objlocked] = OID(((objheader_t *)mobj));
479             objlocked++;
480             if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
481               v_matchnolock++;
482             } else { /* If versions don't match ...HARD ABORT */
483               v_nomatch++;
484               control = TRANS_DISAGREE;
485               if (objlocked > 0) {
486                 for(j = 0; j < objlocked; j++) {
487                   if((headptr = mhashSearch(oidlocked[j])) == NULL) {
488                     printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
489                     return 0;
490                   }
491                   STATUS(headptr) &= ~(LOCK);
492                 }
493                 free(oidlocked);
494               }
495
496               /* Send TRANS_DISAGREE to Coordinator */
497               send_data(acceptfd, &control, sizeof(char));
498               return control;
499             }
500           }
501         }
502         }
503         
504         /* Decide what control message to send to Coordinator */
505         if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
506                                         modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
507                 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
508                 return 0;
509         }
510         
511         return control;
512
513 }
514 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
515  * to send to Coordinator based on the votes of oids involved in the transaction */
516 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock, 
517                 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr, 
518                 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
519         int val;
520         char control = 0;
521
522         /* Condition to send TRANS_AGREE */
523         if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
524                 control = TRANS_AGREE;
525                 /* Send control message */
526                 send_data(acceptfd, &control, sizeof(char));
527         }
528         /* Condition to send TRANS_SOFT_ABORT */
529         if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
530                 control = TRANS_SOFT_ABORT;
531
532                 /* Send control message */
533                 send_data(acceptfd, &control, sizeof(char));
534         
535                 /* Send number of oids not found and the missing oids if objects are missing in the machine */
536                 if(*(objnotfound) != 0) { 
537                         int msg[1];
538                         msg[0] = *(objnotfound);
539                         send_data(acceptfd, &msg, sizeof(int));
540                         int size = sizeof(unsigned int)* *(objnotfound);
541                         send_data(acceptfd, oidnotfound, size);
542                 }
543         }
544
545         /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
546          * if Participant receives a TRANS_COMMIT */
547         transinfo->objlocked = oidlocked;
548         transinfo->objnotfound = oidnotfound;
549         transinfo->modptr = modptr;
550         transinfo->numlocked = *(objlocked);
551         transinfo->numnotfound = *(objnotfound);
552
553         return control;
554 }
555
556 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer 
557  * addresses in lookup table and also changes version number
558  * Sends an ACK back to Coordinator */
559 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
560         objheader_t *header;
561         objheader_t *newheader;
562         int i = 0, offset = 0;
563         char control;
564         int tmpsize;
565
566         /* Process each modified object saved in the mainobject store */
567         for(i = 0; i < nummod; i++) {
568                 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
569                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
570                         return 1;
571                 }
572                 GETSIZE(tmpsize,header);
573                 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
574                 header->version += 1; 
575                 /* If threads are waiting on this object to be updated, notify them */
576         pthread_mutex_lock(&notifymutex);
577                 if(header->notifylist != NULL) {
578                         notifyAll(&header->notifylist, OID(header), header->version);
579                 }
580         pthread_mutex_unlock(&notifymutex);
581                 offset += sizeof(objheader_t) + tmpsize;
582         }
583
584         if (nummod > 0)
585                 free(modptr);
586
587         /* Unlock locked objects */
588         for(i = 0; i < numlocked; i++) {
589                 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
590                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
591                         return 1;
592                 }
593                 STATUS(header) &= ~(LOCK);
594         }
595         //TODO Update location lookup table
596
597         /* Send ack to coordinator */
598         control = TRANS_SUCESSFUL;
599         send_data((int)acceptfd, &control, sizeof(char));
600         return 0;
601 }
602
603 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
604  * Looks for the objects to be prefetched in the main object store.
605  * If objects are not found then record those and if objects are found
606  * then use offset values to prefetch references to other objects */
607
608 int prefetchReq(int acceptfd) {
609   int i, size, objsize, numoffset = 0;
610   int length;
611   char *recvbuffer, control;
612   unsigned int oid, mid=-1;
613   objheader_t *header;
614   oidmidpair_t oidmid;
615   int sd = -1;
616       
617   while(1) {
618     recv_data((int)acceptfd, &numoffset, sizeof(int));
619     if(numoffset == -1) 
620       break;
621     recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
622     oid = oidmid.oid;
623     if (mid != oidmid.mid) {
624       if (mid!=-1) {
625         freeSockWithLock(transPResponseSocketPool, mid, sd);
626       }
627       mid=oidmid.mid;
628       sd = getSockWithLock(transPResponseSocketPool, mid);
629     }
630     short offsetarry[numoffset];
631     recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
632     
633     /*Process each oid */
634     if ((header = mhashSearch(oid)) == NULL) {/* Obj not found */
635       /* Save the oids not found in buffer for later use */
636       size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
637       char sendbuffer[size];
638       *((int *) sendbuffer) = size;
639       *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
640       *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
641       control = TRANS_PREFETCH_RESPONSE;
642       sendPrefetchResponse(sd, &control, sendbuffer, &size);
643     } else { /* Object Found */
644       int incr = 0;
645       GETSIZE(objsize, header);
646       size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
647       char sendbuffer[size];
648       *((int *) (sendbuffer + incr)) = size;
649       incr += sizeof(int);
650       *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
651       incr += sizeof(char);
652       *((unsigned int *)(sendbuffer+incr)) = oid;
653       incr += sizeof(unsigned int);
654       memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
655       
656       control = TRANS_PREFETCH_RESPONSE;
657       sendPrefetchResponse(sd, &control, sendbuffer, &size);
658       
659       /* Calculate the oid corresponding to the offset value */
660       for(i = 0 ; i< numoffset ; i++) {
661         /* Check for arrays  */
662         if(TYPE(header) > NUMCLASSES) {
663           int elementsize = classsize[TYPE(header)];
664           struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
665           unsigned short length = ao->___length___;
666           /* Check if array out of bounds */
667           if(offsetarry[i]< 0 || offsetarry[i] >= length) {
668             break;
669           }
670           oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
671         } else {
672           oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
673         }
674         
675         /* Don't continue if we hit a NULL pointer */
676         if (oid==0)
677           break;
678
679         if((header = mhashSearch(oid)) == NULL) {
680           size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
681           char sendbuffer[size];
682           *((int *) sendbuffer) = size;
683           *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
684           *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
685           
686           control = TRANS_PREFETCH_RESPONSE;
687           sendPrefetchResponse(sd, &control, sendbuffer, &size);
688           break;
689         } else {/* Obj Found */
690           int incr = 0;
691           GETSIZE(objsize, header);
692           size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
693           char sendbuffer[size];
694           *((int *) (sendbuffer + incr)) = size;
695           incr += sizeof(int);
696           *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
697           incr += sizeof(char);
698           *((unsigned int *)(sendbuffer+incr)) = oid;
699           incr += sizeof(unsigned int);
700           memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
701           
702           control = TRANS_PREFETCH_RESPONSE;
703           sendPrefetchResponse(sd, &control, sendbuffer, &size);
704         }
705       }
706     }
707   }
708   //Release socket
709   if (mid!=-1)
710     freeSockWithLock(transPResponseSocketPool, mid, sd);
711     
712   return 0;
713 }
714
715 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
716         send_data(sd, control, sizeof(char));
717         /* Send the buffer with its size */
718         int length = *(size);
719         send_data(sd, sendbuffer, length);
720 }
721
722 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
723         objheader_t *header;
724         unsigned int oid;
725         unsigned short newversion;
726         char msg[1+  2 * sizeof(unsigned int) + sizeof(unsigned short)];
727         int sd;
728         struct sockaddr_in remoteAddr;
729         int bytesSent;
730         int size;
731         int i = 0;
732
733         while(i < numoid) {
734                 oid = *(oidarry + i);
735                 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
736                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
737                         return;
738                 } else {
739                         /* Check to see if versions are same */
740 checkversion:
741                         if ((STATUS(header) & LOCK) != LOCK) {          
742                 pthread_mutex_lock(&notifymutex);
743                                 STATUS(header) |= LOCK;
744                                 newversion = header->version;
745                                 if(newversion == *(versionarry + i)) {
746                                         //Add to the notify list 
747                                         if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
748                                                 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__); 
749                         pthread_mutex_unlock(&notifymutex);
750                                                 return;
751                                         }
752                                         STATUS(header) &= ~(LOCK);              
753                     pthread_mutex_unlock(&notifymutex);
754                                 } else {
755                                         STATUS(header) &= ~(LOCK);              
756                     pthread_mutex_unlock(&notifymutex);
757                                         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
758                                                 perror("processReqNotify():socket()");
759                                                 return;
760                                         }
761                                         bzero(&remoteAddr, sizeof(remoteAddr));
762                                         remoteAddr.sin_family = AF_INET;
763                                         remoteAddr.sin_port = htons(LISTEN_PORT);
764                                         remoteAddr.sin_addr.s_addr = htonl(mid);
765
766                                         if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
767                                                 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
768                                                                 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
769                                                 close(sd);
770                                                 return;
771                                         } else {
772                                                 //Send Update notification
773                                                 msg[0] = THREAD_NOTIFY_RESPONSE;
774                                                 *((unsigned int *)&msg[1]) = oid;
775                                                 size = sizeof(unsigned int);
776                                                 *((unsigned short *)(&msg[1]+size)) = newversion;
777                                                 size += sizeof(unsigned short);
778                                                 *((unsigned int *)(&msg[1]+size)) = threadid;
779                                                 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
780                                                 send_data(sd, msg, size);
781                                         }
782                                         close(sd);
783                                 }
784                         } else {
785                                 randomdelay();
786                                 goto checkversion;
787                         }
788                 }
789                 i++;
790         }
791         free(oidarry);
792         free(versionarry);
793 }