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