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