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