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