Fixed bugs for memory leak . Trying to run two transactions commit
[IRC.git] / Robust / src / Runtime / DSTM / interface / testclient.c
1 #include<stdio.h>
2 #include<pthread.h>
3 #include "dstm.h"
4 #include "llookup.h"
5 #include "ip.h"
6 //#include <sys/socket.h>
7 //#include <netinet/in.h>
8 //#include <arpa/inet.h>
9 #define LISTEN_PORT 2156
10
11 extern objstr_t *mainobjstore;
12 //extern lhashtable_t llookup;          //Global Hash table
13 //extern mhashtable_t mlookup;          //Global Hash table
14
15 int classsize[]={sizeof(int),sizeof(char),sizeof(short), sizeof(void *)};       
16
17 int test1(void);
18 int test2(void);
19
20 unsigned int createObjects(transrecord_t *record) {
21         objheader_t *header, *tmp;
22         unsigned int size, mid;
23         int i = 0;
24         for(i = 20 ; i< 23; i++) {
25                 size = sizeof(objheader_t) + classsize[i-20] ;
26                 tmp = (objheader_t *)objstrAlloc(record->cache, size);
27                 tmp->oid = i;
28                 tmp->type = (i-20);
29                 tmp->version = 1;
30                 tmp->rcount = 0; //? not sure how to handle this yet
31                 tmp->status = 0;
32                 tmp->status |= NEW;
33                 chashInsert(record->lookupTable, tmp->oid, tmp);
34                 header = (objheader_t *) objstrAlloc(mainobjstore, size);
35                 memcpy(header, tmp, size);
36                 mhashInsert(header->oid, header);
37                 mid = iptoMid("128.200.9.27");//machine d-2
38                 printf("DEBUG -> createObjects mid is %x\n", mid);
39                 lhashInsert(header->oid, mid);
40         }
41         //      printf("Insert oid = %d at address %x\n",tmp->oid, tmp);
42         size = sizeof(objheader_t) + classsize[0] ;
43         header = (objheader_t *) objstrAlloc(mainobjstore, size);
44         header->oid = 30;
45         header->type = 0;
46         header->version = 1;
47         header->rcount = 0; //? not sure how to handle this yet
48         header->status = 0;
49         header->status |= NEW;
50         mhashInsert(header->oid, header);
51         mid = iptoMid("128.200.9.27");
52         lhashInsert(header->oid, mid);
53         size = sizeof(objheader_t) + classsize[1] ;
54         header = (objheader_t *) objstrAlloc(mainobjstore, size);
55         header->oid = 28;
56         header->type = 1;
57         header->version = 1;
58         header->rcount = 0; //? not sure how to handle this yet
59         header->status = 0;
60         header->status |= LOCK;
61         mhashInsert(header->oid, header);
62         mid = iptoMid("128.200.9.27");
63         lhashInsert(header->oid, mid);
64         size = sizeof(objheader_t) + classsize[2] ;
65         header = (objheader_t *) objstrAlloc(mainobjstore, size);
66         header->oid = 29;
67         header->type = 2;
68         header->version = 1;
69         header->rcount = 0; //? not sure how to handle this yet
70         header->status = 0;
71         header->status |= LOCK;
72         mhashInsert(header->oid, header);
73         mid = iptoMid("128.200.9.27");
74         lhashInsert(header->oid, mid);
75         return 0;
76 }
77
78 int main() 
79 {
80 //      test2();
81 //      test3();
82 //      test4();
83         //test5();
84         test6();
85
86 }
87
88 int test1(void) {
89
90         transrecord_t *record;
91         objheader_t *h1,*h2,*h3,*h4,*h5, *h6;
92
93         dstmInit();
94         record = transStart();
95         printf("DEBUG -> Init done\n");
96         h1 = transRead(record, 1);
97         printf("oid = %d\tsize = %d\n", h1->oid,classsize[h1->type]);
98         h3 = transRead(record, 3);
99         printf("oid = %d\tsize = %d\n", h3->oid,classsize[h3->type]);
100         h4 = transRead(record, 4);
101         printf("oid = %d\tsize = %d\n", h4->oid,classsize[h4->type]);
102         h2 = transRead(record, 2);
103         printf("oid = %d\tsize = %d\n", h2->oid,classsize[h2->type]);
104         h4 = transRead(record, 4);
105         printf("oid = %d\tsize = %d\n", h4->oid,classsize[h4->type]);
106         h3 = transRead(record, 3);
107         printf("oid = %d\tsize = %d\n", h3->oid,classsize[h3->type]);
108         h5 = transRead(record, 5);
109         printf("oid = %d\tsize = %d\n", h5->oid,classsize[h5->type]);
110 //      getRemoteObj(&record, 0,1);
111 }
112
113 int test2(void) {
114
115         transrecord_t *record;
116         objheader_t *h1,*h2,*h3,*h4,*h5, *h6;
117
118         dstmInit();
119         record = transStart();
120
121         lhashInsert(1,1);
122         lhashInsert(2,1);
123         lhashInsert(3,1);
124         lhashInsert(4,1);
125         lhashInsert(5,1);
126         lhashInsert(6,1);
127         printf("DEBUG -> Init done\n");
128         h1 = transRead(record, 1);
129         lhashInsert(h1->oid, 1);
130         h2 = transRead(record, 2);
131         lhashInsert(h2->oid, 1);
132         h3 = transRead(record, 3);
133         lhashInsert(h3->oid, 1);
134         h4 = transRead(record, 4);
135         lhashInsert(h4->oid, 1);
136 //      h4->status |= DIRTY;
137         h5 = transRead(record, 5);
138         lhashInsert(h5->oid, 1);
139         h6 = transRead(record, 6);
140         lhashInsert(h6->oid, 1);
141 //      h6->status |= DIRTY;
142         
143         transCommit(record);
144
145         return 0;
146 }
147 //Test Read objects when objects are not found in  any participant 
148 int test3(void){
149         transrecord_t *record;
150         objheader_t *h1,*h2;
151
152         dstmInit();
153         record = transStart();
154         printf("DEBUG -> Init done\n");
155         //read object 11
156         if((h1 = transRead(record, 11)) == NULL){
157                 printf("Object not found\n");
158         }
159         //read object 12
160         if((h2 = transRead(record, 12)) == NULL) {
161                 printf("Object not found\n");
162         }
163         transCommit(record);
164
165         return 0;
166 }
167
168 //Test Read objects when some objects are found and other objects not found in  any participant 
169 int test4(void) {
170         transrecord_t *record;
171         objheader_t *h1,*h2, *h3, *h4;
172
173         dstmInit();
174         record = transStart();
175         printf("DEBUG -> Init done\n");
176         //read object 1
177         if((h1 = transRead(record, 1)) == NULL){
178                 printf("Object not found\n");
179         }
180         //read object 2
181         if((h2 = transRead(record, 2)) == NULL) {
182                 printf("Object not found\n");
183         }
184         //read object 11
185         if((h3 = transRead(record, 11)) == NULL) {
186                 printf("Object not found\n");
187         }
188         //read object 13
189         if((h4 = transRead(record, 13)) == NULL) {
190                 printf("Object not found\n");
191         }
192         if((h1 != NULL) && (h2 != NULL) && (h3 != NULL) && h4 !=NULL) { 
193                 transCommit(record);
194         }else {
195                 printf("Cannot complete this transaction\n");
196         }
197
198         return 0;
199 }
200
201 //Test for transaction objects when the objs are part of the Coordinator machine starting the 
202 //trans commit 
203 int test5(void) {
204         transrecord_t *record;
205         objheader_t *header;
206         unsigned int size, mid;
207         pthread_t thread_Listen;
208         pthread_attr_t attr;
209         objheader_t *h1,*h2, *h3, *h4;
210
211         dstmInit();
212         pthread_attr_init(&attr);
213         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
214         //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
215
216         record = transStart();
217         printf("DEBUG -> Init done\n");
218         mid = iptoMid("128.200.9.10");// Machine demsky.eecs.uci.edu    
219         lhashInsert(1,mid);
220         lhashInsert(2,mid);
221         lhashInsert(3,mid);
222         lhashInsert(4,mid);
223         lhashInsert(5,mid);
224         lhashInsert(6,mid);
225         pthread_create(&thread_Listen, &attr, dstmListen, NULL);
226         
227         //Create and Insert Oid 20
228         size = sizeof(objheader_t) + classsize[2] ;
229         header = (objheader_t *) objstrAlloc(mainobjstore, size);
230         header->oid = 20;
231         header->type = 2;
232         header->version = 1;
233         header->rcount = 0; //? not sure how to handle this yet
234         header->status = 0;
235         header->status |= NEW;
236         mhashInsert(header->oid, header);
237         mid = iptoMid("128.200.9.27");
238         lhashInsert(header->oid, mid);
239
240         //Create and Insert Oid 21
241         size = sizeof(objheader_t) + classsize[1] ;
242         header = (objheader_t *) objstrAlloc(mainobjstore, size);
243         header->oid = 21;
244         header->type = 1;
245         header->version = 1;
246         header->rcount = 0; //? not sure how to handle this yet
247         header->status = 0;
248         header->status |= NEW;
249         mhashInsert(header->oid, header);
250         mid = iptoMid("128.200.9.27");
251         lhashInsert(header->oid, mid);
252         //read object 1
253         if((h1 = transRead(record, 1)) == NULL){
254                 printf("Object not found\n");
255         }
256         //read object 5
257         if((h2 = transRead(record, 5)) == NULL) {
258                 printf("Object not found\n");
259         }
260         //read object 20(present in local machine)
261         if((h3 = transRead(record, 20)) == NULL) {
262                 printf("Object not found\n");
263         }
264         //read object 21(present in local machine)
265         if((h4 = transRead(record, 21)) == NULL) {
266                 printf("Object not found\n");
267         }
268         
269         transCommit(record);
270         pthread_join(thread_Listen, NULL);
271 }
272
273 int test6(void) {
274         transrecord_t *record;
275         objheader_t *header;
276         unsigned int size, mid;
277         pthread_t thread_Listen;
278         pthread_attr_t attr;
279         objheader_t *h1,*h2, *h3, *h4, *h5, *h6;
280         int tmpsd;
281
282         dstmInit();
283         pthread_attr_init(&attr);
284         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
285         //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
286
287         record = transStart();
288         //printf("DEBUG -> Init done\n");
289         mid = iptoMid("128.200.9.10");// Machine demsky.eecs.uci.edu    
290         lhashInsert(1,mid);
291         lhashInsert(2,mid);
292         lhashInsert(3,mid);
293         lhashInsert(4,mid);
294         lhashInsert(5,mid);
295         lhashInsert(6,mid);
296         
297         mid = iptoMid("128.200.9.26");// Machine demsky.eecs.uci.edu    
298         lhashInsert(31,mid);
299         lhashInsert(32,mid);
300         lhashInsert(33,mid);
301         
302         pthread_create(&thread_Listen, &attr, dstmListen, NULL);
303         
304         checkServer(mid, "128.200.9.26");
305         mid = iptoMid("128.200.9.10");
306         checkServer(mid, "128.200.9.10");
307
308         //Create and Insert Oid 20
309         size = sizeof(objheader_t) + classsize[2] ;
310         header = (objheader_t *) objstrAlloc(mainobjstore, size);
311         header->oid = 20;
312         header->type = 2;
313         header->version = 1;
314         header->rcount = 0; //? not sure how to handle this yet
315         header->status = 0;
316         header->status |= NEW;
317         mhashInsert(header->oid, header);
318         mid = iptoMid("128.200.9.27");
319         lhashInsert(header->oid, mid);
320
321         //Create and Insert Oid 21
322         size = sizeof(objheader_t) + classsize[1] ;
323         header = (objheader_t *) objstrAlloc(mainobjstore, size);
324         header->oid = 21;
325         header->type = 1;
326         header->version = 1;
327         header->rcount = 0; //? not sure how to handle this yet
328         header->status = 0;
329         header->status |= NEW;
330         mhashInsert(header->oid, header);
331         mid = iptoMid("128.200.9.27");
332         lhashInsert(header->oid, mid);
333         sleep(3);       
334         //read object 1  //from demsky
335         if((h1 = transRead(record, 1)) == NULL){
336                 printf("Object not found\n");
337         }
338         //read object 2
339         if((h2 = transRead(record, 2)) == NULL) {
340                 printf("Object not found\n");
341         }
342         //read object 31 //Found in d-1
343         if((h2 = transRead(record, 31)) == NULL) {
344                 printf("Object not found\n");
345         }
346         //read object 32 //Found in d-1
347         if((h2 = transRead(record, 32)) == NULL) {
348                 printf("Object not found\n");
349         }
350         //read object 20(present in local machine)
351         if((h3 = transRead(record, 20)) == NULL) {
352                 printf("Object not found\n");
353         }
354         //read object 21(present in local machine)
355         if((h4 = transRead(record, 21)) == NULL) {
356                 printf("Object not found\n");
357         }
358         transCommit(record);
359         pthread_join(thread_Listen, NULL);
360         return 0;
361 }