bug fixes and changes to the matrixmultiply benchmark
[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 "dstm.h"
5 #include "mlookup.h"
6 #include "llookup.h"
7 #include "threadnotify.h"
8 #ifdef COMPILER
9 #include "thread.h"
10 #endif
11
12 #define BACKLOG 10 //max pending connections
13 #define RECEIVE_BUFFER_SIZE 2048
14
15 extern int classsize[];
16 extern int numHostsInSystem;
17 extern pthread_mutex_t notifymutex;
18
19 objstr_t *mainobjstore;
20 pthread_mutex_t mainobjstore_mutex;
21 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
22
23 sockPoolHashTable_t *transPResponseSocketPool;
24
25 /* This function initializes the main objects store and creates the 
26  * global machine and location lookup table */
27
28 int dstmInit(void)
29 {
30         mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
31         /* Initialize attribute for mutex */
32         pthread_mutexattr_init(&mainobjstore_mutex_attr);
33         pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
34         pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
35         if (mhashCreate(HASH_SIZE, LOADFACTOR))
36                 return 1; //failure
37         
38         if (lhashCreate(HASH_SIZE, LOADFACTOR))
39                 return 1; //failure
40
41         if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
42                 return 1; //failure
43
44     //Initialize socket pool
45     if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, 2*numHostsInSystem+1)) == NULL) {
46         printf("Error in creating new socket pool at  %s line %d\n", __FILE__, __LINE__);
47         return 0;
48     }
49
50         return 0;
51 }
52
53 /* This function starts the thread to listen on a socket 
54  * for tranaction calls */
55 void *dstmListen()
56 {
57         int listenfd, acceptfd;
58         struct sockaddr_in my_addr;
59         struct sockaddr_in client_addr;
60         socklen_t addrlength = sizeof(struct sockaddr);
61         pthread_t thread_dstm_accept;
62         int i;
63         int setsockflag=1;
64
65         listenfd = socket(AF_INET, SOCK_STREAM, 0);
66         if (listenfd == -1)
67         {
68                 perror("socket");
69                 exit(1);
70         }
71
72         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
73           perror("socket");
74           exit(1);
75         }
76 #ifdef MAC
77         if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
78           perror("socket");
79           exit(1);
80         }
81 #endif
82
83         my_addr.sin_family = AF_INET;
84         my_addr.sin_port = htons(LISTEN_PORT);
85         my_addr.sin_addr.s_addr = INADDR_ANY;
86         memset(&(my_addr.sin_zero), '\0', 8);
87
88         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
89         {
90                 perror("bind");
91                 exit(1);
92         }
93         
94         if (listen(listenfd, BACKLOG) == -1)
95         {
96                 perror("listen");
97                 exit(1);
98         }
99
100         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
101         while(1)
102         {
103           int retval;
104           acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
105           do {
106             retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
107           } while(retval!=0);
108           pthread_detach(thread_dstm_accept);
109         }
110 }
111 /* This function accepts a new connection request, decodes the control message in the connection 
112  * and accordingly calls other functions to process new requests */
113 void *dstmAccept(void *acceptfd) {
114   int val, retval, size, sum, sockid;
115   unsigned int oid;
116   char *buffer;
117   char control,ctrl;
118   char *ptr;
119   void *srcObj;
120   objheader_t *h;
121   trans_commit_data_t transinfo;
122   unsigned short objType, *versionarry, version;
123   unsigned int *oidarry, numoid, mid, threadid;
124   
125   /* Receive control messages from other machines */
126   while(1) {
127     int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
128     if (ret==-1) {
129       printf("DEBUG -> RECV Error!.. retrying\n");
130       break;
131     }
132     switch(control) {
133     case READ_REQUEST:
134       /* Read oid requested and search if available */
135       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
136       if((srcObj = mhashSearch(oid)) == NULL) {
137         printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
138         break;
139       }
140       h = (objheader_t *) srcObj;
141       GETSIZE(size, h);
142       size += sizeof(objheader_t);
143       sockid = (int) acceptfd;
144       
145       if (h == NULL) {
146         ctrl = OBJECT_NOT_FOUND;
147         send_data(sockid, &ctrl, sizeof(char));
148       } else {
149         /* Type */
150         char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
151         *((int *)&msg[1])=size;
152         send_data(sockid, &msg, sizeof(msg));
153         send_data(sockid, h, size);
154       }
155       break;
156       
157     case READ_MULT_REQUEST:
158       break;
159       
160     case MOVE_REQUEST:
161       break;
162       
163     case MOVE_MULT_REQUEST:
164       break;
165       
166     case TRANS_REQUEST:
167       /* Read transaction request */
168       transinfo.objlocked = NULL;
169       transinfo.objnotfound = NULL;
170       transinfo.modptr = NULL;
171       transinfo.numlocked = 0;
172       transinfo.numnotfound = 0;
173       if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
174         printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
175         pthread_exit(NULL);
176       }
177       break;
178     case TRANS_PREFETCH:
179       if((val = prefetchReq((int)acceptfd)) != 0) {
180         printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
181         break;
182       }
183       break;
184     case TRANS_PREFETCH_RESPONSE:
185       if((val = getPrefetchResponse((int) acceptfd)) != 0) {
186         printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
187         break;
188       }
189       break;
190     case START_REMOTE_THREAD:
191       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
192       objType = getObjType(oid);
193       startDSMthread(oid, objType);
194       break;
195       
196     case THREAD_NOTIFY_REQUEST:
197       recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
198       size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
199       if((buffer = calloc(1,size)) == NULL) {
200         printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
201         pthread_exit(NULL);
202       }
203       
204       recv_data((int)acceptfd, buffer, size);
205       
206       oidarry = calloc(numoid, sizeof(unsigned int)); 
207       memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
208       size = sizeof(unsigned int) * numoid;
209       versionarry = calloc(numoid, sizeof(unsigned short));
210       memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
211       size += sizeof(unsigned short) * numoid;
212       mid = *((unsigned int *)(buffer+size));
213       size += sizeof(unsigned int);
214       threadid = *((unsigned int *)(buffer+size));
215       processReqNotify(numoid, oidarry, versionarry, mid, threadid);
216       free(buffer);
217       
218       break;
219
220     case THREAD_NOTIFY_RESPONSE:
221       size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
222       if((buffer = calloc(1,size)) == NULL) {
223         printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
224         pthread_exit(NULL);
225       }
226       
227       recv_data((int)acceptfd, buffer, size);
228       
229       oid = *((unsigned int *)buffer);
230       size = sizeof(unsigned int);
231       version = *((unsigned short *)(buffer+size));
232       size += sizeof(unsigned short);
233       threadid = *((unsigned int *)(buffer+size));
234       threadNotify(oid,version,threadid);
235       free(buffer);
236       break;
237
238     case CLOSE_CONNECTION:
239       goto closeconnection;
240
241     default:
242       printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
243     }
244   }
245
246  closeconnection:
247   /* Close connection */
248   if (close((int)acceptfd) == -1)
249     perror("close");
250   pthread_exit(NULL);
251 }
252   
253 /* This function reads the information available in a transaction request
254  * and makes a function call to process the request */
255 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
256         char *ptr;
257         void *modptr;
258         unsigned int *oidmod, oid;
259         fixed_data_t fixed;
260         objheader_t *headaddr;
261         int sum, i, size, n, val;
262
263         oidmod = NULL;
264
265         /* Read fixed_data_t data structure */ 
266         size = sizeof(fixed) - 1;
267         ptr = (char *)&fixed;;
268         fixed.control = TRANS_REQUEST;
269         recv_data((int)acceptfd, ptr+1, size);
270
271         /* Read list of mids */
272         int mcount = fixed.mcount;
273         size = mcount * sizeof(unsigned int);
274         unsigned int listmid[mcount];
275         ptr = (char *) listmid;
276         recv_data((int)acceptfd, ptr, size);
277         
278         /* Read oid and version tuples for those objects that are not modified in the transaction */
279         int numread = fixed.numread;
280         size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
281         char objread[size];
282         if(numread != 0) { //If pile contains more than one object to be read, 
283                           // keep reading all objects
284                 recv_data((int)acceptfd, objread, size);        
285         }
286         
287         /* Read modified objects */
288         if(fixed.nummod != 0) {
289                 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
290                         printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
291                         return 1;
292                 }
293                 size = fixed.sum_bytes;
294                 recv_data((int)acceptfd, modptr, size); 
295         }
296
297         /* Create an array of oids for modified objects */
298         oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
299         if (oidmod == NULL)
300         {
301                 printf("calloc error %s, %d\n", __FILE__, __LINE__);
302                 return 1;
303         }
304         ptr = (char *) modptr;
305         for(i = 0 ; i < fixed.nummod; i++) {
306           int tmpsize;
307           headaddr = (objheader_t *) ptr;
308           oid = OID(headaddr);
309           oidmod[i] = oid;
310           GETSIZE(tmpsize, headaddr);
311           ptr += sizeof(objheader_t) + tmpsize;
312         }
313         
314         /*Process the information read */
315         if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
316                 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
317                 /* Free resources */
318                 if(oidmod != NULL) {
319                         free(oidmod);
320                 }
321                 return 1;
322         }
323
324         /* Free resources */
325         if(oidmod != NULL) {
326                 free(oidmod);
327         }
328
329         return 0;
330 }
331
332 /* This function processes the Coordinator's transaction request using "handleTransReq" 
333  * function and sends a reply to the co-ordinator.
334  * Following this it also receives a new control message from the co-ordinator and processes this message*/
335 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
336                 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
337         char control, sendctrl, retval;
338         objheader_t *tmp_header;
339         void *header;
340         int  i = 0, val;
341
342         /* Send reply to the Coordinator */
343         if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
344                 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
345                 return 1;
346         }
347
348         recv_data((int)acceptfd, &control, sizeof(char));
349         
350         /* Process the new control message */
351         switch(control) {
352                 case TRANS_ABORT:
353                         if (fixed->nummod > 0)
354                                 free(modptr);
355                         /* Unlock objects that was locked due to this transaction */
356                         for(i = 0; i< transinfo->numlocked; i++) {
357                                 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
358                                         printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
359                                         return 1;
360                                 }
361                                 STATUS(((objheader_t *)header)) &= ~(LOCK);             
362                         }
363
364                         /* Send ack to Coordinator */
365                         sendctrl = TRANS_UNSUCESSFUL;
366                         send_data((int)acceptfd, &sendctrl, sizeof(char));
367                         break;
368
369                 case TRANS_COMMIT:
370                         /* Invoke the transCommit process() */
371                         if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
372                                 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
373                                 /* Free memory */
374                                 if (transinfo->objlocked != NULL) {
375                                         free(transinfo->objlocked);
376                                 }
377                                 if (transinfo->objnotfound != NULL) {
378                                         free(transinfo->objnotfound);
379                                 }
380                                 return 1;
381                         }
382                         break;
383
384                 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
385                         break;
386                 default:
387                         printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
388                         //TODO Use fixed.trans_id  TID since Client may have died
389                         break;
390         }
391
392         /* Free memory */
393         if (transinfo->objlocked != NULL) {
394                 free(transinfo->objlocked);
395         }
396         if (transinfo->objnotfound != NULL) {
397                 free(transinfo->objnotfound);
398         }
399
400         return 0;
401 }
402
403 /* This function increments counters while running a voting decision on all objects involved 
404  * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
405 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
406         int val, i = 0, j;
407         unsigned short version;
408         char control = 0, *ptr;
409         unsigned int oid;
410         unsigned int *oidnotfound, *oidlocked;
411         void *mobj;
412         objheader_t *headptr;
413
414         /* Counters and arrays to formulate decision on control message to be sent */
415         oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
416         oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
417         int objnotfound = 0, objlocked = 0;
418         int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
419
420         /* modptr points to the beginning of the object store 
421          * created at the Pariticipant. 
422          * Object store holds the modified objects involved in the transaction request */ 
423         ptr = (char *) modptr;
424         
425         /* Process each oid in the machine pile/ group per thread */
426         for (i = 0; i < fixed->numread + fixed->nummod; i++) {
427                 if (i < fixed->numread) {//Objs only read and not modified
428                         int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
429                         incr *= i;
430                         oid = *((unsigned int *)(objread + incr));
431                         incr += sizeof(unsigned int);
432                         version = *((unsigned short *)(objread + incr));
433                 } else {//Objs modified
434                   int tmpsize;
435                   headptr = (objheader_t *) ptr;
436                   oid = OID(headptr);
437                   version = headptr->version;
438                   GETSIZE(tmpsize, headptr);
439                   ptr += sizeof(objheader_t) + tmpsize;
440                 }
441                 
442                 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
443
444                 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
445                         /* Save the oids not found and number of oids not found for later use */
446                         oidnotfound[objnotfound] = oid;
447                         objnotfound++;
448                 } else { /* If Obj found in machine (i.e. has not moved) */
449                         /* Check if Obj is locked by any previous transaction */
450                         if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {           
451                                 if (version == ((objheader_t *)mobj)->version) {      /* If locked then match versions */
452                                         v_matchlock++;
453                                 } else {/* If versions don't match ...HARD ABORT */
454                                         v_nomatch++;
455                                         /* Send TRANS_DISAGREE to Coordinator */
456                                         control = TRANS_DISAGREE;
457                                         if (objlocked > 0) {
458                                           for(j = 0; j < objlocked; j++) {
459                                                         if((headptr = mhashSearch(oidlocked[j])) == NULL) {
460                                                                 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
461                                                                 return 0;
462                                                         }
463                                                         STATUS(headptr) &= ~(LOCK);
464                                                 }
465                                                 free(oidlocked);
466                                         }
467                                         send_data(acceptfd, &control, sizeof(char));
468                                         return control;
469                                 }
470                         } else {/* If Obj is not locked then lock object */
471                                 STATUS(((objheader_t *)mobj)) |= LOCK;
472                                 /* Save all object oids that are locked on this machine during this transaction request call */
473                                 oidlocked[objlocked] = OID(((objheader_t *)mobj));
474                                 objlocked++;
475                                 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
476                                         v_matchnolock++;
477                                 } else { /* If versions don't match ...HARD ABORT */
478                                         v_nomatch++;
479                                         control = TRANS_DISAGREE;
480                                         if (objlocked > 0) {
481                                                 for(j = 0; j < objlocked; j++) {
482                                                         if((headptr = mhashSearch(oidlocked[j])) == NULL) {
483                                                                 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
484                                                                 return 0;
485                                                         }
486                                                         STATUS(headptr) &= ~(LOCK);
487                                                 }
488                                                 free(oidlocked);
489                                         }
490
491                                         /* Send TRANS_DISAGREE to Coordinator */
492                                         send_data(acceptfd, &control, sizeof(char));
493                                         return control;
494                                 }
495                         }
496                 }
497         }
498         
499         /* Decide what control message to send to Coordinator */
500         if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
501                                         modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
502                 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
503                 return 0;
504         }
505         
506         return control;
507
508 }
509 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
510  * to send to Coordinator based on the votes of oids involved in the transaction */
511 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock, 
512                 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr, 
513                 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
514         int val;
515         char control = 0;
516
517         /* Condition to send TRANS_AGREE */
518         if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
519                 control = TRANS_AGREE;
520                 /* Send control message */
521                 send_data(acceptfd, &control, sizeof(char));
522         }
523         /* Condition to send TRANS_SOFT_ABORT */
524         if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
525                 control = TRANS_SOFT_ABORT;
526
527                 /* Send control message */
528                 send_data(acceptfd, &control, sizeof(char));
529         
530                 /* Send number of oids not found and the missing oids if objects are missing in the machine */
531                 if(*(objnotfound) != 0) { 
532                         int msg[1];
533                         msg[0] = *(objnotfound);
534                         send_data(acceptfd, &msg, sizeof(int));
535                         int size = sizeof(unsigned int)* *(objnotfound);
536                         send_data(acceptfd, oidnotfound, size);
537                 }
538         }
539
540         /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
541          * if Participant receives a TRANS_COMMIT */
542         transinfo->objlocked = oidlocked;
543         transinfo->objnotfound = oidnotfound;
544         transinfo->modptr = modptr;
545         transinfo->numlocked = *(objlocked);
546         transinfo->numnotfound = *(objnotfound);
547
548         return control;
549 }
550
551 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer 
552  * addresses in lookup table and also changes version number
553  * Sends an ACK back to Coordinator */
554 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
555         objheader_t *header;
556         objheader_t *newheader;
557         int i = 0, offset = 0;
558         char control;
559         int tmpsize;
560
561         /* Process each modified object saved in the mainobject store */
562         for(i = 0; i < nummod; i++) {
563                 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
564                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
565                         return 1;
566                 }
567                 GETSIZE(tmpsize,header);
568                 pthread_mutex_lock(&mainobjstore_mutex);
569                 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
570                 header->version += 1; 
571                 /* If threads are waiting on this object to be updated, notify them */
572         pthread_mutex_lock(&notifymutex);
573                 if(header->notifylist != NULL) {
574                         notifyAll(&header->notifylist, OID(header), header->version);
575                 }
576         pthread_mutex_unlock(&notifymutex);
577                 pthread_mutex_unlock(&mainobjstore_mutex);
578                 offset += sizeof(objheader_t) + tmpsize;
579         }
580
581         if (nummod > 0)
582                 free(modptr);
583
584         /* Unlock locked objects */
585         for(i = 0; i < numlocked; i++) {
586                 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
587                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
588                         return 1;
589                 }
590                 STATUS(header) &= ~(LOCK);
591         }
592         //TODO Update location lookup table
593
594         /* Send ack to coordinator */
595         control = TRANS_SUCESSFUL;
596         send_data((int)acceptfd, &control, sizeof(char));
597         return 0;
598 }
599
600 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
601  * Looks for the objects to be prefetched in the main object store.
602  * If objects are not found then record those and if objects are found
603  * then use offset values to prefetch references to other objects */
604
605 int prefetchReq(int acceptfd) {
606   int i, size, objsize, numoffset = 0;
607   int length;
608   char *recvbuffer, control;
609   unsigned int oid, mid=-1;
610   objheader_t *header;
611   oidmidpair_t oidmid;
612   int sd = -1;
613       
614   while(1) {
615     recv_data((int)acceptfd, &numoffset, sizeof(int));
616     if(numoffset == -1) 
617       break;
618     recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
619     oid = oidmid.oid;
620     if (mid != oidmid.mid) {
621       if (mid!=-1) {
622         freeSockWithLock(transPResponseSocketPool, mid, sd);
623       }
624       mid=oidmid.mid;
625       sd = getSockWithLock(transPResponseSocketPool, mid);
626     }
627     short offsetarry[numoffset];
628     recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
629     
630     /*Process each oid */
631     if ((header = mhashSearch(oid)) == NULL) {/* Obj not found */
632       /* Save the oids not found in buffer for later use */
633       size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
634       char sendbuffer[size];
635       *((int *) sendbuffer) = size;
636       *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
637       *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
638       control = TRANS_PREFETCH_RESPONSE;
639       sendPrefetchResponse(sd, &control, sendbuffer, &size);
640     } else { /* Object Found */
641       int incr = 0;
642       GETSIZE(objsize, header);
643       size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
644       char sendbuffer[size];
645       *((int *) (sendbuffer + incr)) = size;
646       incr += sizeof(int);
647       *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
648       incr += sizeof(char);
649       *((unsigned int *)(sendbuffer+incr)) = oid;
650       incr += sizeof(unsigned int);
651       memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
652       
653       control = TRANS_PREFETCH_RESPONSE;
654       sendPrefetchResponse(sd, &control, sendbuffer, &size);
655       
656       /* Calculate the oid corresponding to the offset value */
657       for(i = 0 ; i< numoffset ; i++) {
658         /* Check for arrays  */
659         if(TYPE(header) > NUMCLASSES) {
660           int elementsize = classsize[TYPE(header)];
661           struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
662           unsigned short length = ao->___length___;
663           /* Check if array out of bounds */
664           if(offsetarry[i]< 0 || offsetarry[i] >= length) {
665             break;
666           }
667           oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
668         } else {
669           oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
670         }
671         
672         /* Don't continue if we hit a NULL pointer */
673         if (oid==0)
674           break;
675
676         if((header = mhashSearch(oid)) == NULL) {
677           size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
678           char sendbuffer[size];
679           *((int *) sendbuffer) = size;
680           *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
681           *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
682           
683           control = TRANS_PREFETCH_RESPONSE;
684           sendPrefetchResponse(sd, &control, sendbuffer, &size);
685           break;
686         } else {/* Obj Found */
687           int incr = 0;
688           GETSIZE(objsize, header);
689           size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
690           char sendbuffer[size];
691           *((int *) (sendbuffer + incr)) = size;
692           incr += sizeof(int);
693           *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
694           incr += sizeof(char);
695           *((unsigned int *)(sendbuffer+incr)) = oid;
696           incr += sizeof(unsigned int);
697           memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
698           
699           control = TRANS_PREFETCH_RESPONSE;
700           sendPrefetchResponse(sd, &control, sendbuffer, &size);
701         }
702       }
703     }
704   }
705   //Release socket
706   if (mid!=-1)
707     freeSockWithLock(transPResponseSocketPool, mid, sd);
708     
709   return 0;
710 }
711
712 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
713         send_data(sd, control, sizeof(char));
714         /* Send the buffer with its size */
715         int length = *(size);
716         send_data(sd, sendbuffer, length);
717 }
718
719 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
720         objheader_t *header;
721         unsigned int oid;
722         unsigned short newversion;
723         char msg[1+  2 * sizeof(unsigned int) + sizeof(unsigned short)];
724         int sd;
725         struct sockaddr_in remoteAddr;
726         int bytesSent;
727         int size;
728         int i = 0;
729
730         while(i < numoid) {
731                 oid = *(oidarry + i);
732                 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
733                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
734                         return;
735                 } else {
736                         /* Check to see if versions are same */
737 checkversion:
738                         if ((STATUS(header) & LOCK) != LOCK) {          
739                 pthread_mutex_lock(&notifymutex);
740                                 STATUS(header) |= LOCK;
741                                 newversion = header->version;
742                                 if(newversion == *(versionarry + i)) {
743                                         //Add to the notify list 
744                                         if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
745                                                 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__); 
746                         pthread_mutex_unlock(&notifymutex);
747                                                 return;
748                                         }
749                                         STATUS(header) &= ~(LOCK);              
750                     pthread_mutex_unlock(&notifymutex);
751                                 } else {
752                                         STATUS(header) &= ~(LOCK);              
753                     pthread_mutex_unlock(&notifymutex);
754                                         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
755                                                 perror("processReqNotify():socket()");
756                                                 return;
757                                         }
758                                         bzero(&remoteAddr, sizeof(remoteAddr));
759                                         remoteAddr.sin_family = AF_INET;
760                                         remoteAddr.sin_port = htons(LISTEN_PORT);
761                                         remoteAddr.sin_addr.s_addr = htonl(mid);
762
763                                         if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
764                                                 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
765                                                                 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
766                                                 close(sd);
767                                                 return;
768                                         } else {
769                                                 //Send Update notification
770                                                 msg[0] = THREAD_NOTIFY_RESPONSE;
771                                                 *((unsigned int *)&msg[1]) = oid;
772                                                 size = sizeof(unsigned int);
773                                                 *((unsigned short *)(&msg[1]+size)) = newversion;
774                                                 size += sizeof(unsigned short);
775                                                 *((unsigned int *)(&msg[1]+size)) = threadid;
776                                                 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
777                                                 send_data(sd, msg, size);
778                                         }
779                                         close(sd);
780                                 }
781                         } else {
782                                 randomdelay();
783                                 goto checkversion;
784                         }
785                 }
786                 i++;
787         }
788         free(oidarry);
789         free(versionarry);
790 }