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