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