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