changed the prefetch request send and prefetch response receive design
[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
25 extern int classsize[];
26
27 objstr_t *mainobjstore;
28 pthread_mutex_t mainobjstore_mutex;
29 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
30 pthread_mutex_t threadnotify_mutex = PTHREAD_MUTEX_INITIALIZER;
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, sum;
117         unsigned int oid;
118         char *buffer;
119         char control,ctrl;
120         char *ptr;
121         void *srcObj;
122         objheader_t *h;
123         trans_commit_data_t transinfo;
124         unsigned short objType, *versionarry, version;
125         unsigned int *oidarry, numoid, mid, threadid;
126         
127         transinfo.objlocked = NULL;
128         transinfo.objnotfound = NULL;
129         transinfo.modptr = NULL;
130         transinfo.numlocked = 0;
131         transinfo.numnotfound = 0;
132
133         /* Receive control messages from other machines */
134         if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
135                 printf("%s() Error: Receiving control = %d at %s, %d\n", __func__, control, __FILE__, __LINE__);
136                 pthread_exit(NULL);
137         }
138         
139         switch(control) {
140                 case READ_REQUEST:
141                         /* Read oid requested and search if available */
142                         if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
143                                 perror("Error: receiving 0x0 object from cooridnator\n");
144                                 pthread_exit(NULL);
145                         }
146                         if((srcObj = mhashSearch(oid)) == NULL) {
147                                 printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
148                                 pthread_exit(NULL);
149                         }
150                         h = (objheader_t *) srcObj;
151                         GETSIZE(size, h);
152                         size += sizeof(objheader_t);
153
154                         if (h == NULL) {
155                                 ctrl = OBJECT_NOT_FOUND;
156                                 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
157                                         perror("Error sending control msg to coordinator\n");
158                                         pthread_exit(NULL);
159                                 }
160                         } else {
161                                 /* Type */
162                                 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
163                                 *((int *)&msg[1])=size;
164                                 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
165                                         perror("Error sending size of object to coordinator\n");
166                                         pthread_exit(NULL);
167                                 }
168                                 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
169                                         perror("Error in sending object\n");
170                                         pthread_exit(NULL);
171                                 }
172                         }
173                         break;
174                 
175                 case READ_MULT_REQUEST:
176                         break;
177         
178                 case MOVE_REQUEST:
179                         break;
180
181                 case MOVE_MULT_REQUEST:
182                         break;
183
184                 case TRANS_REQUEST:
185                         /* Read transaction request */
186                         if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
187                                 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
188                                 pthread_exit(NULL);
189                         }
190                         break;
191                 case TRANS_PREFETCH:
192                         if((val = prefetchReq((int)acceptfd)) != 0) {
193                                 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
194                                 pthread_exit(NULL);
195                         }
196                         break;
197                 case TRANS_PREFETCH_RESPONSE:
198                         if((val = getPrefetchResponse((int) acceptfd)) != 0) {
199                                 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
200                                 pthread_exit(NULL);
201                         }
202                         break;
203                 case START_REMOTE_THREAD:
204                         retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
205                         if (retval <= 0)
206                                 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
207                         else if (retval != sizeof(unsigned int))
208                                 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD %s, %d\n",
209                                         retval, __FILE__, __LINE__);
210                         else
211                         {
212                                 objType = getObjType(oid);
213                                 startDSMthread(oid, objType);
214                         }
215                         break;
216
217                 case THREAD_NOTIFY_REQUEST:
218                         retval = recv((int)acceptfd, &numoid, sizeof(unsigned int), 0);
219                         size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
220                         if((buffer = calloc(1,size)) == NULL) {
221                                 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
222                                 pthread_exit(NULL);
223                         }
224                         sum = 0;
225                         do {
226                                 sum += recv((int)acceptfd, buffer+sum, size-sum, 0);
227                         } while(sum < size);
228
229                         oidarry = calloc(numoid, sizeof(unsigned int)); 
230                         memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
231                         size = sizeof(unsigned int) * numoid;
232                         versionarry = calloc(numoid, sizeof(unsigned short));
233                         memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
234                         size += sizeof(unsigned short) * numoid;
235                         mid = *((unsigned int *)(buffer+size));
236                         size += sizeof(unsigned int);
237                         threadid = *((unsigned int *)(buffer+size));
238                         processReqNotify(numoid, oidarry, versionarry, mid, threadid);
239                         free(buffer);
240
241                         break;
242
243                 case THREAD_NOTIFY_RESPONSE:
244                         size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
245                         if((buffer = calloc(1,size)) == NULL) {
246                                 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
247                                 pthread_exit(NULL);
248                         }
249
250                         sum = 0;
251                         do {
252                                 sum += recv((int)acceptfd, buffer+sum, size-sum, 0);
253                         } while(sum < size);
254
255                         oid = *((unsigned int *)buffer);
256                         size = sizeof(unsigned int);
257                         version = *((unsigned short *)(buffer+size));
258                         size += sizeof(unsigned short);
259                         threadid = *((unsigned int *)(buffer+size));
260                         threadNotify(oid,version,threadid);
261                         free(buffer);
262
263                         break;
264                 default:
265                         printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
266         }
267
268         /* Close connection */
269         if (close((int)acceptfd) == -1)
270                 perror("close");
271         
272         pthread_exit(NULL);
273 }
274
275 /* This function reads the information available in a transaction request
276  * and makes a function call to process the request */
277 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
278         char *ptr;
279         void *modptr;
280         unsigned int *oidmod, oid;
281         fixed_data_t fixed;
282         objheader_t *headaddr;
283         int sum = 0, i, N, n, val;
284
285         oidmod = NULL;
286
287         /* Read fixed_data_t data structure */ 
288         N = sizeof(fixed) - 1;
289         ptr = (char *)&fixed;;
290         fixed.control = TRANS_REQUEST;
291         do {
292                 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
293                 sum += n;
294         } while(sum < N && n != 0); 
295
296         /* Read list of mids */
297         int mcount = fixed.mcount;
298         N = mcount * sizeof(unsigned int);
299         unsigned int listmid[mcount];
300         ptr = (char *) listmid;
301         sum = 0;
302         do {
303                 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
304                 sum += n;
305         } while(sum < N && n != 0);
306
307         /* Read oid and version tuples for those objects that are not modified in the transaction */
308         int numread = fixed.numread;
309         N = numread * (sizeof(unsigned int) + sizeof(short));
310         char objread[N];
311         if(numread != 0) { //If pile contains more than one object to be read, 
312                           // keep reading all objects
313                 sum = 0;
314                 do {
315                         n = recv((int)acceptfd, (void *) objread+sum, N-sum, 0);
316                         sum += n;
317                 } while(sum < N && n != 0);
318         }
319         
320         /* Read modified objects */
321         if(fixed.nummod != 0) {
322                 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
323                         printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
324                         return 1;
325                 }
326                 sum = 0;
327                 do { // Recv the objs that are modified by the Coordinator
328                         n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
329                         sum += n;
330                 } while (sum < fixed.sum_bytes && n != 0);
331         }
332
333         /* Create an array of oids for modified objects */
334         oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
335         if (oidmod == NULL)
336         {
337                 printf("calloc error %s, %d\n", __FILE__, __LINE__);
338                 return 1;
339         }
340         ptr = (char *) modptr;
341         for(i = 0 ; i < fixed.nummod; i++) {
342           int tmpsize;
343           headaddr = (objheader_t *) ptr;
344           oid = OID(headaddr);
345           oidmod[i] = oid;
346           GETSIZE(tmpsize, headaddr);
347           ptr += sizeof(objheader_t) + tmpsize;
348         }
349         
350         /*Process the information read */
351         if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
352                 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
353                 /* Free resources */
354                 if(oidmod != NULL) {
355                         free(oidmod);
356                 }
357                 return 1;
358         }
359
360         /* Free resources */
361         if(oidmod != NULL) {
362                 free(oidmod);
363         }
364
365         return 0;
366 }
367
368 /* This function processes the Coordinator's transaction request using "handleTransReq" 
369  * function and sends a reply to the co-ordinator.
370  * Following this it also receives a new control message from the co-ordinator and processes this message*/
371 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
372                 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
373         char control, sendctrl, retval;
374         objheader_t *tmp_header;
375         void *header;
376         int  i = 0, val;
377
378         /* Send reply to the Coordinator */
379         if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
380                 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
381                 return 1;
382         }
383
384         do {
385                 retval = recv((int)acceptfd, &control, sizeof(char), 0);
386         } while(retval < sizeof(char));
387
388         /* Process the new control message */
389         switch(control) {
390                 case TRANS_ABORT:
391                         if (fixed->nummod > 0)
392                                 free(modptr);
393                         /* Unlock objects that was locked due to this transaction */
394                         for(i = 0; i< transinfo->numlocked; i++) {
395                                 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
396                                         printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
397                                         return 1;
398                                 }
399                                 STATUS(((objheader_t *)header)) &= ~(LOCK);             
400                         }
401
402                         /* Send ack to Coordinator */
403                         sendctrl = TRANS_UNSUCESSFUL;
404                         if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
405                                 perror("Error: In sending ACK to coordinator\n");
406                                 if (transinfo->objlocked != NULL) {
407                                         free(transinfo->objlocked);
408                                 }
409                                 if (transinfo->objnotfound != NULL) {
410                                         free(transinfo->objnotfound);
411                                 }
412
413                                 return 1;
414                         }
415                         break;
416
417                 case TRANS_COMMIT:
418                         /* Invoke the transCommit process() */
419                         if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
420                                 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
421                                 /* Free memory */
422                                 if (transinfo->objlocked != NULL) {
423                                         free(transinfo->objlocked);
424                                 }
425                                 if (transinfo->objnotfound != NULL) {
426                                         free(transinfo->objnotfound);
427                                 }
428                                 return 1;
429                         }
430                         break;
431
432                 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
433                         break;
434                 default:
435                         printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
436                         //TODO Use fixed.trans_id  TID since Client may have died
437                         break;
438         }
439
440         /* Free memory */
441         if (transinfo->objlocked != NULL) {
442                 free(transinfo->objlocked);
443         }
444         if (transinfo->objnotfound != NULL) {
445                 free(transinfo->objnotfound);
446         }
447
448         return 0;
449 }
450
451 /* This function increments counters while running a voting decision on all objects involved 
452  * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
453 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
454         int val, i = 0, j;
455         unsigned short version;
456         char control = 0, *ptr;
457         unsigned int oid;
458         unsigned int *oidnotfound, *oidlocked;
459         void *mobj;
460         objheader_t *headptr;
461
462         /* Counters and arrays to formulate decision on control message to be sent */
463         oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
464         oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
465         int objnotfound = 0, objlocked = 0;
466         int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
467
468         /* modptr points to the beginning of the object store 
469          * created at the Pariticipant. 
470          * Object store holds the modified objects involved in the transaction request */ 
471         ptr = (char *) modptr;
472         
473         /* Process each oid in the machine pile/ group per thread */
474         for (i = 0; i < fixed->numread + fixed->nummod; i++) {
475                 if (i < fixed->numread) {//Objs only read and not modified
476                         int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
477                         incr *= i;
478                         oid = *((unsigned int *)(objread + incr));
479                         incr += sizeof(unsigned int);
480                         version = *((unsigned short *)(objread + incr));
481                 } else {//Objs modified
482                   int tmpsize;
483                   headptr = (objheader_t *) ptr;
484                   oid = OID(headptr);
485                   version = headptr->version;
486                   GETSIZE(tmpsize, headptr);
487                   ptr += sizeof(objheader_t) + tmpsize;
488                 }
489                 
490                 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
491
492                 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
493                         /* Save the oids not found and number of oids not found for later use */
494                         oidnotfound[objnotfound] = oid;
495                         objnotfound++;
496                 } else { /* If Obj found in machine (i.e. has not moved) */
497                         /* Check if Obj is locked by any previous transaction */
498                         if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {           
499                                 if (version == ((objheader_t *)mobj)->version) {      /* If locked then match versions */
500                                         v_matchlock++;
501                                 } else {/* If versions don't match ...HARD ABORT */
502                                         v_nomatch++;
503                                         /* Send TRANS_DISAGREE to Coordinator */
504                                         control = TRANS_DISAGREE;
505                                         if (objlocked > 0) {
506                                           for(j = 0; j < objlocked; j++) {
507                                                         if((headptr = mhashSearch(oidlocked[j])) == NULL) {
508                                                                 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
509                                                                 return 0;
510                                                         }
511                                                         STATUS(headptr) &= ~(LOCK);
512                                                 }
513                                                 free(oidlocked);
514                                         }
515                                         if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
516                                                 perror("Error in sending control to the Coordinator\n");
517                                                 return 0;
518                                         }
519                                         return control;
520                                 }
521                         } else {/* If Obj is not locked then lock object */
522                                 STATUS(((objheader_t *)mobj)) |= LOCK;
523                                 /* Save all object oids that are locked on this machine during this transaction request call */
524                                 oidlocked[objlocked] = OID(((objheader_t *)mobj));
525                                 objlocked++;
526                                 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
527                                         v_matchnolock++;
528                                 } else { /* If versions don't match ...HARD ABORT */
529                                         v_nomatch++;
530                                         control = TRANS_DISAGREE;
531                                         if (objlocked > 0) {
532                                                 for(j = 0; j < objlocked; j++) {
533                                                         if((headptr = mhashSearch(oidlocked[j])) == NULL) {
534                                                                 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
535                                                                 return 0;
536                                                         }
537                                                         STATUS(headptr) &= ~(LOCK);
538                                                 }
539                                                 free(oidlocked);
540                                         }
541
542                                         /* Send TRANS_DISAGREE to Coordinator */
543                                         if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
544                                                 perror("Error in sending control to the Coordinator\n");
545                                                 return 0;
546                                         }
547                                         
548                                         return control;
549                                 }
550                         }
551                 }
552         }
553         
554         /* Decide what control message to send to Coordinator */
555         if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
556                                         modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
557                 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
558                 return 0;
559         }
560         
561         return control;
562
563 }
564 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
565  * to send to Coordinator based on the votes of oids involved in the transaction */
566 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock, 
567                 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr, 
568                 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
569         int val;
570         char control = 0;
571
572         /* Condition to send TRANS_AGREE */
573         if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
574                 control = TRANS_AGREE;
575                 /* Send control message */
576                 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
577                         perror("Error in sending control to Coordinator\n");
578                         return 0;
579                 }
580         }
581         /* Condition to send TRANS_SOFT_ABORT */
582         if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
583                 control = TRANS_SOFT_ABORT;
584
585                 /* Send control message */
586                 if((val = send(acceptfd, &control, sizeof(char),MSG_NOSIGNAL)) < sizeof(char)) {
587                         perror("Error in sending TRANS_SOFT_ABORT control\n");
588                         return 0;
589                 }
590
591                 /* Send number of oids not found and the missing oids if objects are missing in the machine */
592                 if(*(objnotfound) != 0) { 
593                         int msg[1];
594                         msg[0] = *(objnotfound);
595                         if((val = send(acceptfd, msg, sizeof(int) ,MSG_NOSIGNAL)) < sizeof(int)) {
596                                 perror("Error in sending objects that are not found\n");
597                                 return 0;
598                         }
599                         int size = sizeof(unsigned int)* *(objnotfound);
600                         if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
601                                 perror("Error in sending objects that are not found\n");
602                                 return 0;
603                         }
604                 }
605         }
606
607         /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
608          * if Participant receives a TRANS_COMMIT */
609         transinfo->objlocked = oidlocked;
610         transinfo->objnotfound = oidnotfound;
611         transinfo->modptr = modptr;
612         transinfo->numlocked = *(objlocked);
613         transinfo->numnotfound = *(objnotfound);
614         
615         return control;
616 }
617
618 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer 
619  * addresses in lookup table and also changes version number
620  * Sends an ACK back to Coordinator */
621 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
622         objheader_t *header;
623         objheader_t *newheader;
624         int i = 0, offset = 0;
625         char control;
626         int tmpsize;
627
628         /* Process each modified object saved in the mainobject store */
629         for(i = 0; i < nummod; i++) {
630                 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
631                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
632                         return 1;
633                 }
634                 GETSIZE(tmpsize,header);
635                 pthread_mutex_lock(&mainobjstore_mutex);
636                 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
637                 header->version += 1; 
638                 /* If threads are waiting on this object to be updated, notify them */
639                 if(header->notifylist != NULL) {
640                         notifyAll(&header->notifylist, OID(header), header->version);
641                 }
642                 pthread_mutex_unlock(&mainobjstore_mutex);
643                 offset += sizeof(objheader_t) + tmpsize;
644         }
645
646         if (nummod > 0)
647                 free(modptr);
648
649         /* Unlock locked objects */
650         for(i = 0; i < numlocked; i++) {
651                 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
652                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
653                         return 1;
654                 }
655                 STATUS(header) &= ~(LOCK);
656         }
657         //TODO Update location lookup table
658
659         /* Send ack to coordinator */
660         control = TRANS_SUCESSFUL;
661         if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
662                 perror("Error sending ACK to coordinator\n");
663                 return 1;
664         }
665         
666         return 0;
667 }
668
669 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
670  * Looks for the objects to be prefetched in the main object store.
671  * If objects are not found then record those and if objects are found
672  * then use offset values to prefetch references to other objects */
673
674 int prefetchReq(int acceptfd) {
675         int i, size, objsize, numbytes = 0, isArray = 0, numoffset = 0;
676         int length, sd;
677         char *recvbuffer, *sendbuffer, control;
678         unsigned int oid, mid;
679         unsigned short *offsetarry;
680         objheader_t *header;
681         struct sockaddr_in remoteAddr;
682
683         while((numbytes = recv((int)acceptfd, &length, sizeof(int), 0)) != 0) {
684                 if(length == -1) { //-1 is special character to represent end of sending oids and offsets
685                         break;
686                 } else {
687                         numbytes = 0;
688                         size = length - sizeof(int);
689                         if((recvbuffer = calloc(1, size)) == NULL) {
690                                 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
691                                 return -1;
692                         }
693                         while(numbytes < size) {
694                                 numbytes += recv((int)acceptfd, recvbuffer+numbytes, size-numbytes, 0);
695                         }
696
697                         oid = *((unsigned int *) recvbuffer);
698                         mid = *((unsigned int *) (recvbuffer + sizeof(unsigned int)));
699                         size = size - (2 * sizeof(unsigned int));
700                         numoffset = size / sizeof(short);
701                         if((offsetarry = calloc(numoffset, sizeof(unsigned short))) == NULL) {
702                                 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
703                                 free(recvbuffer);
704                                 return -1;
705                         }
706                         memcpy(offsetarry, recvbuffer + (2 * sizeof(unsigned int)), size);
707                         free(recvbuffer);
708
709                         /* Create socket to send information */
710                         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
711                                 perror("prefetchReq():socket()");
712                                 return;
713                         }
714                         bzero(&remoteAddr, sizeof(remoteAddr));
715                         remoteAddr.sin_family = AF_INET;
716                         remoteAddr.sin_port = htons(LISTEN_PORT);
717                         remoteAddr.sin_addr.s_addr = htonl(mid);
718
719                         if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
720                                 printf("Error: prefetchReq():error %d connecting to %s:%d\n", errno,
721                                                 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
722                                 close(sd);
723                                 return -1;
724                         }
725
726                         /*Process each oid */
727                         if ((header = mhashSearch(oid)) == NULL) {/* Obj not found */
728                                 /* Save the oids not found in buffer for later use */
729                                 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
730                                 if((sendbuffer = calloc(1, size)) == NULL) {
731                                         printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
732                                         free(offsetarry);
733                                         close(sd);
734                                         return -1;
735                                 }
736                                 *((int *) sendbuffer) = size;
737                                 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
738                                 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
739
740                                 control = TRANS_PREFETCH_RESPONSE;
741                                 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
742                                         free(offsetarry);
743                                         printf("Error: %s() in sending prefetch response at %s, %d\n",
744                                                         __func__, __FILE__, __LINE__);
745                                         close(sd);
746                                         return -1;
747                                 }
748                         } else { /* Object Found */
749                                 int incr = 0;
750                                 GETSIZE(objsize, header);
751                                 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
752                                 if((sendbuffer = calloc(1, size)) == NULL) {
753                                         printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
754                                         free(offsetarry);
755                                         close(sd);
756                                         return -1;
757                                 }
758                                 *((int *) (sendbuffer + incr)) = size;
759                                 incr += sizeof(int);
760                                 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
761                                 incr += sizeof(char);
762                                 *((unsigned int *)(sendbuffer+incr)) = oid;
763                                 incr += sizeof(unsigned int);
764                                 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
765
766                                 control = TRANS_PREFETCH_RESPONSE;
767                                 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
768                                         free(offsetarry);
769                                         printf("Error: %s() in sending prefetch response at %s, %d\n",
770                                                         __func__, __FILE__, __LINE__);
771                                         close(sd);
772                                         return -1;
773                                 }
774                                 /* Calculate the oid corresponding to the offset value */
775                                 for(i = 0 ; i< numoffset ; i++) {
776                                         /* Check for arrays  */
777                                         if(TYPE(header) > NUMCLASSES) {
778                                                 isArray = 1;
779                                         }
780                                         if(isArray == 1) {
781                                                 int elementsize = classsize[TYPE(header)];
782                                                 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
783                                         } else {
784                                                 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
785                                         }
786
787                                         if((header = mhashSearch(oid)) == NULL) {
788                                                 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
789                                                 if((sendbuffer = calloc(1, size)) == NULL) {
790                                                         printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
791                                                         free(offsetarry);
792                                                         close(sd);
793                                                         return -1;
794                                                 }
795                                                 *((int *) sendbuffer) = size;
796                                                 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
797                                                 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
798
799                                                 control = TRANS_PREFETCH_RESPONSE;
800                                                 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
801                                                         free(offsetarry);
802                                                         printf("Error: %s() in sending prefetch response at %s, %d\n",
803                                                                         __FILE__, __LINE__);
804                                                         close(sd);
805                                                         return -1;
806                                                 }
807                                                 break;
808                                         } else {/* Obj Found */
809                                                 int incr = 0;
810                                                 GETSIZE(objsize, header);
811                                                 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
812                                                 if((sendbuffer = calloc(1, size)) == NULL) {
813                                                         printf("Calloc error at %s,%d\n", __func__, __FILE__, __LINE__);
814                                                         free(offsetarry);
815                                                         close(sd);
816                                                         return -1;
817                                                 }
818                                                 *((int *) (sendbuffer + incr)) = size;
819                                                 incr += sizeof(int);
820                                                 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
821                                                 incr += sizeof(char);
822                                                 *((unsigned int *)(sendbuffer+incr)) = oid;
823                                                 incr += sizeof(unsigned int);
824                                                 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
825
826                                                 control = TRANS_PREFETCH_RESPONSE;
827                                                 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
828                                                         free(offsetarry);
829                                                         printf("Error: %s() in sending prefetch response at %s, %d\n",
830                                                                         __func__, __FILE__, __LINE__);
831                                                         close(sd);
832                                                         return -1;
833                                                 }
834                                         }
835                                         isArray = 0;
836                                 }
837                                 free(offsetarry);
838                         }
839                 }
840         }
841         close(sd);
842         return 0;
843 }
844
845 int sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
846         int numbytes = 0;
847
848         if((numbytes = send(sd, control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
849                 printf("%s() Error: in sending PREFETCH RESPONSE to Coordinator at %s, %d\n", __func__, __FILE__, __LINE__);
850                 free(sendbuffer);
851                 return -1;
852         }
853
854         /* Send the buffer with its size */
855         if((numbytes = send(sd, sendbuffer, *(size), MSG_NOSIGNAL)) < *(size)) {
856                 printf("%s() Error: in sending oid found at %s, %d size sent = %d, actual size = %d\n",
857                                 __func__, __FILE__, __LINE__, numbytes, *(size));
858                 free(sendbuffer);
859                 return -1;
860         }
861
862         free(sendbuffer);
863         return 0;
864 }
865
866 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
867         objheader_t *header;
868         unsigned int oid;
869         unsigned short newversion;
870         char msg[1+  2 * sizeof(unsigned int) + sizeof(unsigned short)];
871         int sd;
872         struct sockaddr_in remoteAddr;
873         int bytesSent;
874         int size;
875
876         int i = 0;
877         while(i < numoid) {
878                 oid = *(oidarry + i);
879                 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
880                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
881                         return;
882                 } else {
883                         /* Check to see if versions are same */
884 checkversion:
885                         if ((STATUS(header) & LOCK) != LOCK) {          
886                                 //FIXME make locking atomic
887                                 STATUS(header) |= LOCK;
888                                 newversion = header->version;
889                                 if(newversion == *(versionarry + i)) {
890                                         //Add to the notify list 
891                                         if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
892                                                 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__); 
893                                                 return;
894                                         }
895                                         STATUS(header) &= ~(LOCK);              
896                                 } else {
897                                         STATUS(header) &= ~(LOCK);              
898                                         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
899                                                 perror("processReqNotify():socket()");
900                                                 return;
901                                         }
902                                         bzero(&remoteAddr, sizeof(remoteAddr));
903                                         remoteAddr.sin_family = AF_INET;
904                                         remoteAddr.sin_port = htons(LISTEN_PORT);
905                                         remoteAddr.sin_addr.s_addr = htonl(mid);
906
907                                         if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
908                                                 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
909                                                                 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
910                                                 close(sd);
911                                                 return;
912                                         } else {
913                                                 //Send Update notification
914                                                 msg[0] = THREAD_NOTIFY_RESPONSE;
915                                                 *((unsigned int *)&msg[1]) = oid;
916                                                 size = sizeof(unsigned int);
917                                                 *((unsigned short *)(&msg[1]+size)) = newversion;
918                                                 size += sizeof(unsigned short);
919                                                 *((unsigned int *)(&msg[1]+size)) = threadid;
920                                                 bytesSent = send(sd, msg, 1+ 2*sizeof(unsigned int) + sizeof(unsigned short), 0);
921                                                 if (bytesSent < 0){
922                                                         perror("processReqNotify():send()");
923                                                         close(sd);
924                                                         return;
925                                                 } else if (bytesSent != 1 + sizeof(unsigned short) + 2*sizeof(unsigned int)){
926                                                         printf("Error: processReqNotify(): error, sent %d bytes %s, %d\n", 
927                                                                         bytesSent, __FILE__, __LINE__);
928                                                         close(sd);
929                                                         return;
930                                                 } else {
931                                                         close(sd);
932                                                         return;
933                                                 }
934
935                                         }
936                                         close(sd);
937                                 }
938                         } else {
939                                 randomdelay();
940                                 goto checkversion;
941                         }
942                 }
943                 i++;
944         }
945         free(oidarry);
946         free(versionarry);
947 }
948