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