changes and bug fixes
[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
18 objstr_t *mainobjstore;
19 pthread_mutex_t mainobjstore_mutex;
20 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
21
22 sockPoolHashTable_t *transPResponseSocketPool;
23
24 /* This function initializes the main objects store and creates the 
25  * global machine and location lookup table */
26
27 int dstmInit(void)
28 {
29         mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
30         /* Initialize attribute for mutex */
31         pthread_mutexattr_init(&mainobjstore_mutex_attr);
32         pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
33         pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
34         if (mhashCreate(HASH_SIZE, LOADFACTOR))
35                 return 1; //failure
36         
37         if (lhashCreate(HASH_SIZE, LOADFACTOR))
38                 return 1; //failure
39
40         if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
41                 return 1; //failure
42
43     //Initialize socket pool
44     if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, 2*numHostsInSystem+1)) == NULL) {
45         printf("Error in creating new socket pool at  %s line %d\n", __FILE__, __LINE__);
46         return 0;
47     }
48
49         return 0;
50 }
51
52 /* This function starts the thread to listen on a socket 
53  * for tranaction calls */
54 void *dstmListen()
55 {
56         int listenfd, acceptfd;
57         struct sockaddr_in my_addr;
58         struct sockaddr_in client_addr;
59         socklen_t addrlength = sizeof(struct sockaddr);
60         pthread_t thread_dstm_accept;
61         int i;
62         int setsockflag=1;
63
64         listenfd = socket(AF_INET, SOCK_STREAM, 0);
65         if (listenfd == -1)
66         {
67                 perror("socket");
68                 exit(1);
69         }
70
71         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
72           perror("socket");
73           exit(1);
74         }
75 #ifdef MAC
76         if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
77           perror("socket");
78           exit(1);
79         }
80 #endif
81
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);
86
87         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
88         {
89                 perror("bind");
90                 exit(1);
91         }
92         
93         if (listen(listenfd, BACKLOG) == -1)
94         {
95                 perror("listen");
96                 exit(1);
97         }
98
99         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
100         while(1)
101         {
102           int retval;
103           acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
104           do {
105             retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
106           } while(retval!=0);
107           pthread_detach(thread_dstm_accept);
108         }
109 }
110 /* This function accepts a new connection request, decodes the control message in the connection 
111  * and accordingly calls other functions to process new requests */
112 void *dstmAccept(void *acceptfd) {
113   int val, retval, size, sum, sockid;
114   unsigned int oid;
115   char *buffer;
116   char control,ctrl;
117   char *ptr;
118   void *srcObj;
119   objheader_t *h;
120   trans_commit_data_t transinfo;
121   unsigned short objType, *versionarry, version;
122   unsigned int *oidarry, numoid, mid, threadid;
123   
124   /*
125   transinfo.objlocked = NULL;
126   transinfo.objnotfound = NULL;
127   transinfo.modptr = NULL;
128   transinfo.numlocked = 0;
129   transinfo.numnotfound = 0;
130   */
131   
132   /* Receive control messages from other machines */
133   while(1) {
134     int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
135     if (ret==-1)
136       break;
137     switch(control) {
138     case READ_REQUEST:
139       /* Read oid requested and search if available */
140       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
141       if((srcObj = mhashSearch(oid)) == NULL) {
142         printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
143         break;
144       }
145       h = (objheader_t *) srcObj;
146       GETSIZE(size, h);
147       size += sizeof(objheader_t);
148       sockid = (int) acceptfd;
149       
150       if (h == NULL) {
151         ctrl = OBJECT_NOT_FOUND;
152         send_data(sockid, &ctrl, sizeof(char));
153       } else {
154         /* Type */
155         char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
156         *((int *)&msg[1])=size;
157         send_data(sockid, &msg, sizeof(msg));
158         send_data(sockid, h, size);
159       }
160       break;
161       
162     case READ_MULT_REQUEST:
163       break;
164       
165     case MOVE_REQUEST:
166       break;
167       
168     case MOVE_MULT_REQUEST:
169       break;
170       
171     case TRANS_REQUEST:
172       /* Read transaction request */
173       transinfo.objlocked = NULL;
174       transinfo.objnotfound = NULL;
175       transinfo.modptr = NULL;
176       transinfo.numlocked = 0;
177       transinfo.numnotfound = 0;
178       if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
179         printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
180         pthread_exit(NULL);
181       }
182       break;
183     case TRANS_PREFETCH:
184       if((val = prefetchReq((int)acceptfd)) != 0) {
185         printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
186         break;
187       }
188       break;
189     case TRANS_PREFETCH_RESPONSE:
190       if((val = getPrefetchResponse((int) acceptfd)) != 0) {
191         printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
192         break;
193       }
194       break;
195     case START_REMOTE_THREAD:
196       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
197       objType = getObjType(oid);
198       startDSMthread(oid, objType);
199       break;
200       
201     case THREAD_NOTIFY_REQUEST:
202       recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
203       size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
204       if((buffer = calloc(1,size)) == NULL) {
205         printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
206         pthread_exit(NULL);
207       }
208       
209       recv_data((int)acceptfd, buffer, size);
210       
211       oidarry = calloc(numoid, sizeof(unsigned int)); 
212       memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
213       size = sizeof(unsigned int) * numoid;
214       versionarry = calloc(numoid, sizeof(unsigned short));
215       memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
216       size += sizeof(unsigned short) * numoid;
217       mid = *((unsigned int *)(buffer+size));
218       size += sizeof(unsigned int);
219       threadid = *((unsigned int *)(buffer+size));
220       processReqNotify(numoid, oidarry, versionarry, mid, threadid);
221       free(buffer);
222       
223       break;
224
225     case THREAD_NOTIFY_RESPONSE:
226       size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
227       if((buffer = calloc(1,size)) == NULL) {
228         printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
229         pthread_exit(NULL);
230       }
231       
232       recv_data((int)acceptfd, buffer, size);
233       
234       oid = *((unsigned int *)buffer);
235       size = sizeof(unsigned int);
236       version = *((unsigned short *)(buffer+size));
237       size += sizeof(unsigned short);
238       threadid = *((unsigned int *)(buffer+size));
239       threadNotify(oid,version,threadid);
240       free(buffer);
241       break;
242
243     case CLOSE_CONNECTION:
244       goto closeconnection;
245
246     default:
247       printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
248     }
249   }
250
251  closeconnection:
252   /* Close connection */
253   if (close((int)acceptfd) == -1)
254     perror("close");
255   pthread_exit(NULL);
256 }
257   
258 /* This function reads the information available in a transaction request
259  * and makes a function call to process the request */
260 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
261         char *ptr;
262         void *modptr;
263         unsigned int *oidmod, oid;
264         fixed_data_t fixed;
265         objheader_t *headaddr;
266         int sum, i, size, n, val;
267
268         oidmod = NULL;
269
270         /* Read fixed_data_t data structure */ 
271         size = sizeof(fixed) - 1;
272         ptr = (char *)&fixed;;
273         fixed.control = TRANS_REQUEST;
274         recv_data((int)acceptfd, ptr+1, size);
275
276         /* Read list of mids */
277         int mcount = fixed.mcount;
278         size = mcount * sizeof(unsigned int);
279         unsigned int listmid[mcount];
280         ptr = (char *) listmid;
281         recv_data((int)acceptfd, ptr, size);
282         
283         /* Read oid and version tuples for those objects that are not modified in the transaction */
284         int numread = fixed.numread;
285         size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
286         char objread[size];
287         if(numread != 0) { //If pile contains more than one object to be read, 
288                           // keep reading all objects
289                 recv_data((int)acceptfd, objread, size);        
290         }
291         
292         /* Read modified objects */
293         if(fixed.nummod != 0) {
294                 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
295                         printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
296                         return 1;
297                 }
298                 size = fixed.sum_bytes;
299                 recv_data((int)acceptfd, modptr, size); 
300         }
301
302         /* Create an array of oids for modified objects */
303         oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
304         if (oidmod == NULL)
305         {
306                 printf("calloc error %s, %d\n", __FILE__, __LINE__);
307                 return 1;
308         }
309         ptr = (char *) modptr;
310         for(i = 0 ; i < fixed.nummod; i++) {
311           int tmpsize;
312           headaddr = (objheader_t *) ptr;
313           oid = OID(headaddr);
314           oidmod[i] = oid;
315           GETSIZE(tmpsize, headaddr);
316           ptr += sizeof(objheader_t) + tmpsize;
317         }
318         
319         /*Process the information read */
320         if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
321                 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
322                 /* Free resources */
323                 if(oidmod != NULL) {
324                         free(oidmod);
325                 }
326                 return 1;
327         }
328
329         /* Free resources */
330         if(oidmod != NULL) {
331                 free(oidmod);
332         }
333
334         return 0;
335 }
336
337 /* This function processes the Coordinator's transaction request using "handleTransReq" 
338  * function and sends a reply to the co-ordinator.
339  * Following this it also receives a new control message from the co-ordinator and processes this message*/
340 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
341                 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
342         char control, sendctrl, retval;
343         objheader_t *tmp_header;
344         void *header;
345         int  i = 0, val;
346
347         /* Send reply to the Coordinator */
348         if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
349                 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
350                 return 1;
351         }
352
353         recv_data((int)acceptfd, &control, sizeof(char));
354         
355         /* Process the new control message */
356         switch(control) {
357                 case TRANS_ABORT:
358                         if (fixed->nummod > 0)
359                                 free(modptr);
360                         /* Unlock objects that was locked due to this transaction */
361                         for(i = 0; i< transinfo->numlocked; i++) {
362                                 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
363                                         printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
364                                         return 1;
365                                 }
366                                 STATUS(((objheader_t *)header)) &= ~(LOCK);             
367                         }
368
369                         /* Send ack to Coordinator */
370                         sendctrl = TRANS_UNSUCESSFUL;
371                         send_data((int)acceptfd, &sendctrl, sizeof(char));
372                         break;
373
374                 case TRANS_COMMIT:
375                         /* Invoke the transCommit process() */
376                         if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
377                                 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
378                                 /* Free memory */
379                                 if (transinfo->objlocked != NULL) {
380                                         free(transinfo->objlocked);
381                                 }
382                                 if (transinfo->objnotfound != NULL) {
383                                         free(transinfo->objnotfound);
384                                 }
385                                 return 1;
386                         }
387                         break;
388
389                 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
390                         break;
391                 default:
392                         printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
393                         //TODO Use fixed.trans_id  TID since Client may have died
394                         break;
395         }
396
397         /* Free memory */
398         if (transinfo->objlocked != NULL) {
399                 free(transinfo->objlocked);
400         }
401         if (transinfo->objnotfound != NULL) {
402                 free(transinfo->objnotfound);
403         }
404
405         return 0;
406 }
407
408 /* This function increments counters while running a voting decision on all objects involved 
409  * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
410 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
411         int val, i = 0, j;
412         unsigned short version;
413         char control = 0, *ptr;
414         unsigned int oid;
415         unsigned int *oidnotfound, *oidlocked;
416         void *mobj;
417         objheader_t *headptr;
418
419         /* Counters and arrays to formulate decision on control message to be sent */
420         oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
421         oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
422         int objnotfound = 0, objlocked = 0;
423         int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
424
425         /* modptr points to the beginning of the object store 
426          * created at the Pariticipant. 
427          * Object store holds the modified objects involved in the transaction request */ 
428         ptr = (char *) modptr;
429         
430         /* Process each oid in the machine pile/ group per thread */
431         for (i = 0; i < fixed->numread + fixed->nummod; i++) {
432                 if (i < fixed->numread) {//Objs only read and not modified
433                         int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
434                         incr *= i;
435                         oid = *((unsigned int *)(objread + incr));
436                         incr += sizeof(unsigned int);
437                         version = *((unsigned short *)(objread + incr));
438                 } else {//Objs modified
439                   int tmpsize;
440                   headptr = (objheader_t *) ptr;
441                   oid = OID(headptr);
442                   version = headptr->version;
443                   GETSIZE(tmpsize, headptr);
444                   ptr += sizeof(objheader_t) + tmpsize;
445                 }
446                 
447                 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
448
449                 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
450                         /* Save the oids not found and number of oids not found for later use */
451                         oidnotfound[objnotfound] = oid;
452                         objnotfound++;
453                 } else { /* If Obj found in machine (i.e. has not moved) */
454                         /* Check if Obj is locked by any previous transaction */
455                         if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {           
456                                 if (version == ((objheader_t *)mobj)->version) {      /* If locked then match versions */
457                                         v_matchlock++;
458                                 } else {/* If versions don't match ...HARD ABORT */
459                                         v_nomatch++;
460                                         /* Send TRANS_DISAGREE to Coordinator */
461                                         control = TRANS_DISAGREE;
462                                         if (objlocked > 0) {
463                                           for(j = 0; j < objlocked; j++) {
464                                                         if((headptr = mhashSearch(oidlocked[j])) == NULL) {
465                                                                 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
466                                                                 return 0;
467                                                         }
468                                                         STATUS(headptr) &= ~(LOCK);
469                                                 }
470                                                 free(oidlocked);
471                                         }
472                                         send_data(acceptfd, &control, sizeof(char));
473                                         return control;
474                                 }
475                         } else {/* If Obj is not locked then lock object */
476                                 STATUS(((objheader_t *)mobj)) |= LOCK;
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                 pthread_mutex_lock(&mainobjstore_mutex);
574                 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
575                 header->version += 1; 
576                 /* If threads are waiting on this object to be updated, notify them */
577                 if(header->notifylist != NULL) {
578                         notifyAll(&header->notifylist, OID(header), header->version);
579                 }
580                 pthread_mutex_unlock(&mainobjstore_mutex);
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
732         int i = 0;
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                                 //FIXME make locking atomic
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                                                 return;
750                                         }
751                                         STATUS(header) &= ~(LOCK);              
752                                 } else {
753                                         STATUS(header) &= ~(LOCK);              
754                                         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
755                                                 perror("processReqNotify():socket()");
756                                                 return;
757                                         }
758                                         bzero(&remoteAddr, sizeof(remoteAddr));
759                                         remoteAddr.sin_family = AF_INET;
760                                         remoteAddr.sin_port = htons(LISTEN_PORT);
761                                         remoteAddr.sin_addr.s_addr = htonl(mid);
762
763                                         if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
764                                                 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
765                                                                 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
766                                                 close(sd);
767                                                 return;
768                                         } else {
769                                                 //Send Update notification
770                                                 msg[0] = THREAD_NOTIFY_RESPONSE;
771                                                 *((unsigned int *)&msg[1]) = oid;
772                                                 size = sizeof(unsigned int);
773                                                 *((unsigned short *)(&msg[1]+size)) = newversion;
774                                                 size += sizeof(unsigned short);
775                                                 *((unsigned int *)(&msg[1]+size)) = threadid;
776                                                 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
777                                                 send_data(sd, msg, size);
778                                         }
779                                         close(sd);
780                                 }
781                         } else {
782                                 randomdelay();
783                                 goto checkversion;
784                         }
785                 }
786                 i++;
787         }
788         free(oidarry);
789         free(versionarry);
790 }