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