Fixed bugs
[IRC.git] / Robust / src / Runtime / DSTM / interface / testserver.c
1 #include <pthread.h>
2 #include "dstm.h"
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include "ip.h"
7
8 extern objstr_t *mainobjstore;
9 int classsize[]={sizeof(int),sizeof(char),sizeof(short), sizeof(void *)};
10
11 int test1(void);
12 int test2(void);
13 int test3(void);
14
15 unsigned int createObjects(transrecord_t *record, unsigned short type) {
16         objheader_t *header, *tmp;
17         struct sockaddr_in antelope;
18         unsigned int size, mid;
19         size = sizeof(objheader_t) + classsize[type] ;
20         //Inserts in chashtable
21         header = transCreateObj(record, type);
22         tmp = (objheader_t *) objstrAlloc(mainobjstore, size);
23         memcpy(tmp, header, size);
24         mhashInsert(tmp->oid, tmp);
25         mid = iptoMid("128.200.9.10");
26         lhashInsert(tmp->oid, mid);
27         //Lock oid 3 object
28 //      if(tmp->oid == 3)
29 //              tmp->status |= LOCK;
30         return 0;
31 }
32
33 void init_obj(objheader_t *h, unsigned int oid, unsigned short type, \
34                 unsigned short version,\
35                 unsigned short rcount, char status) {
36         h->oid = oid;
37         h->type = type;
38         h->version = version;
39         h->rcount = rcount;
40         h->status |= status;
41         return;
42 }
43
44
45 int main()
46 {
47         //test1();
48 //      test3();
49         test4();
50 }
51
52 int test1()
53 {
54         unsigned int val;
55         transrecord_t *myTrans;
56         pthread_t thread_Listen;
57
58         dstmInit();     
59         pthread_create(&thread_Listen, NULL, dstmListen, NULL);
60         // Start Transaction    
61         myTrans = transStart();
62
63         printf("Creating Transaction\n");
64         //Create Object1
65         if((val = createObjects(myTrans, 0)) != 0) {
66                 printf("Error transCreateObj1");
67         }
68         //Create Object2
69         if((val = createObjects(myTrans, 1)) != 0) {
70                 printf("Error transCreateObj2");
71         }
72         //Create Object3
73         if((val = createObjects(myTrans, 2)) != 0) {
74                 printf("Error transCreateObj3");
75         }
76         //Create Object4
77         if((val = createObjects(myTrans, 3)) != 0) {
78                 printf("Error transCreateObj4");
79         }
80         //Create Object5
81         if((val = createObjects(myTrans, 0)) != 0) {
82                 printf("Error transCreateObj5");
83         }
84         //Create Object6
85         if((val = createObjects(myTrans, 1)) != 0) {
86                 printf("Error transCreateObj6");
87         }
88         pthread_join(thread_Listen, NULL);
89         return 0;
90 }
91
92 int test2() {
93         
94         unsigned int val, mid;
95         transrecord_t *myTrans;
96         pthread_t thread_Listen;
97
98         dstmInit();     
99         mid = iptoMid("128.200.9.27"); //d-2.eecs.uci.edu
100         //Inserting into lhashtable
101         lhashInsert(20, mid);
102         lhashInsert(21, mid);
103         lhashInsert(22, mid);
104         lhashInsert(23, mid);
105         lhashInsert(30, mid);
106         lhashInsert(28, mid);
107         lhashInsert(29, mid);
108         pthread_create(&thread_Listen, NULL, dstmListen, NULL);
109         // Start Transaction    
110         myTrans = transStart();
111
112         printf("Creating Transaction\n");
113         //Create Object1
114         if((val = createObjects(myTrans, 0)) != 0) {
115                 printf("Error transCreateObj1");
116         }
117         //Create Object2
118         if((val = createObjects(myTrans, 1)) != 0) {
119                 printf("Error transCreateObj2");
120         }
121         //Create Object3
122         if((val = createObjects(myTrans, 2)) != 0) {
123                 printf("Error transCreateObj3");
124         }
125         //Create Object4
126         if((val = createObjects(myTrans, 3)) != 0) {
127                 printf("Error transCreateObj4");
128         }
129         //Create Object5
130         if((val = createObjects(myTrans, 0)) != 0) {
131                 printf("Error transCreateObj5");
132         }
133         //Create Object6
134         if((val = createObjects(myTrans, 1)) != 0) {
135                 printf("Error transCreateObj6");
136         }
137         pthread_join(thread_Listen, NULL);
138 }
139 //Commit transaction with all locally available objects
140 int test3() {
141         unsigned int val, mid;
142         transrecord_t *myTrans;
143         unsigned int size;
144         objheader_t *header;
145         pthread_t thread_Listen;
146         pthread_attr_t attr;
147         objheader_t *h1, *h2, *h3;//h1,h2,h3 from local
148
149         dstmInit();     
150         pthread_attr_init(&attr);
151         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
152
153         // Create and Insert Oid 1
154         size = sizeof(objheader_t) + classsize[0] ;
155         header = (objheader_t *) objstrAlloc(mainobjstore, size);
156         init_obj(header, 1, 0, 1, 0, NEW);
157         mhashInsert(header->oid, header);
158         mid = iptoMid("128.200.9.10");
159         lhashInsert(header->oid, mid);
160
161         // Create and Insert Oid 2
162         size = sizeof(objheader_t) + classsize[1] ;
163         header = (objheader_t *) objstrAlloc(mainobjstore, size);
164         init_obj(header, 2, 1, 1, 0, NEW);
165         mhashInsert(header->oid, header);
166         mid = iptoMid("128.200.9.10");
167         lhashInsert(header->oid, mid);
168
169
170         // Create and Insert Oid 3
171         size = sizeof(objheader_t) + classsize[2] ;
172         header = (objheader_t *) objstrAlloc(mainobjstore, size);
173         init_obj(header, 3, 2, 1, 0, NEW);
174         mhashInsert(header->oid, header);
175         mid = iptoMid("128.200.9.10");
176         lhashInsert(header->oid, mid);
177
178         // Create and Insert Oid 4
179         size = sizeof(objheader_t) + classsize[3] ;
180         header = (objheader_t *) objstrAlloc(mainobjstore, size);
181         init_obj(header, 4, 3, 1, 0, NEW);
182         mhashInsert(header->oid, header);
183         mid = iptoMid("128.200.9.10");
184         lhashInsert(header->oid, mid);
185
186         //Inserting into lhashtable
187         mid = iptoMid("128.200.9.29"); //d-3.eecs.uci.edu
188         lhashInsert(20, mid);
189         lhashInsert(21, mid);
190         lhashInsert(22, mid);
191
192         mid = iptoMid("128.200.9.30"); //d-4.eecs.uci.edu
193         //Inserting into lhashtable
194         lhashInsert(31, mid);
195         lhashInsert(32, mid);
196         lhashInsert(33, mid);
197         pthread_create(&thread_Listen, &attr, dstmListen, NULL);
198
199         //Check if machine d-4 is up and running
200         checkServer(mid, "128.200.9.30");
201         mid = iptoMid("128.200.9.29");
202         //Check if machine d-3 is up and running
203         checkServer(mid, "128.200.9.29");
204
205         // Start Transaction    
206         myTrans = transStart();
207
208         //read object 1(present in local machine)
209         if((h1 = transRead(myTrans, 1)) == NULL){
210                 printf("Object not found\n");
211         }
212         //read object 2present in local machine)
213         if((h2 = transRead(myTrans, 2)) == NULL) {
214                 printf("Object not found\n");
215         }
216         //read object 3(present in local machine)
217         if((h3 = transRead(myTrans, 3)) == NULL) {
218                 printf("Object not found\n");
219         }
220
221         // Commit transaction
222         transCommit(myTrans);
223
224         pthread_join(thread_Listen, NULL);
225
226         return 0;
227 }
228
229 //Commit transaction with few locally available objects and other objects from machine d-1
230 // and d-2
231 int test4() {
232
233         unsigned int val, mid;
234         transrecord_t *myTrans;
235         unsigned int size;
236         objheader_t *header;
237         pthread_t thread_Listen;
238         pthread_attr_t attr;
239         objheader_t *h1, *h2, *h3, *h4;//h1,h2 from local ; h3 from d-1 , h-4 from d-2
240
241         dstmInit();     
242         pthread_attr_init(&attr);
243         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
244
245         // Create and Insert Oid 1
246         size = sizeof(objheader_t) + classsize[0] ;
247         header = (objheader_t *) objstrAlloc(mainobjstore, size);
248         init_obj(header, 1, 0, 1, 0, NEW);
249         mhashInsert(header->oid, header);
250         mid = iptoMid("128.200.9.10");
251         lhashInsert(header->oid, mid);
252
253         // Create and Insert Oid 2
254         size = sizeof(objheader_t) + classsize[1] ;
255         header = (objheader_t *) objstrAlloc(mainobjstore, size);
256         init_obj(header, 2, 1, 1, 0, NEW);
257         mhashInsert(header->oid, header);
258         mid = iptoMid("128.200.9.10");
259         lhashInsert(header->oid, mid);
260
261
262         // Create and Insert Oid 3
263         size = sizeof(objheader_t) + classsize[2] ;
264         header = (objheader_t *) objstrAlloc(mainobjstore, size);
265         init_obj(header, 3, 2, 1, 0, NEW);
266         mhashInsert(header->oid, header);
267         mid = iptoMid("128.200.9.10");
268         lhashInsert(header->oid, mid);
269
270         // Create and Insert Oid 4
271         size = sizeof(objheader_t) + classsize[3] ;
272         header = (objheader_t *) objstrAlloc(mainobjstore, size);
273         init_obj(header, 4, 3, 1, 0, NEW);
274         mhashInsert(header->oid, header);
275         mid = iptoMid("128.200.9.10");
276         lhashInsert(header->oid, mid);
277
278         //Inserting into lhashtable
279         mid = iptoMid("128.200.9.29"); //d-3.eecs.uci.edu
280         lhashInsert(20, mid);
281         lhashInsert(21, mid);
282         lhashInsert(22, mid);
283
284         mid = iptoMid("128.200.9.30"); //d-4.eecs.uci.edu
285         //Inserting into lhashtable
286         lhashInsert(31, mid);
287         lhashInsert(32, mid);
288         lhashInsert(33, mid);
289
290         pthread_create(&thread_Listen, &attr, dstmListen, NULL);
291         //Check if machine d-4 is up and running
292         checkServer(mid, "128.200.9.30");
293         mid = iptoMid("128.200.9.29");
294         //Check if machine d-3 is up and running
295         checkServer(mid, "128.200.9.29");
296
297         // Start Transaction    
298         myTrans = transStart();
299
300         //read object 1(present in local machine)
301         if((h1 = transRead(myTrans, 2)) == NULL){
302                 printf("Object not found\n");
303         }
304
305         //read object 2present in local machine)
306         if((h2 = transRead(myTrans, 1)) == NULL) {
307                 printf("Object not found\n");
308         }
309         //read object 31(present in d-4 machine)
310         if((h3 = transRead(myTrans, 31)) == NULL) {
311                 printf("Object not found\n");
312         }
313         //read object 21(present in d-3 machine)
314         if((h4 = transRead(myTrans, 21)) == NULL) {
315                 printf("Object not found\n");
316         }
317         
318         // Commit transaction
319         transCommit(myTrans);
320
321         pthread_join(thread_Listen, NULL);
322
323         return 0;
324 }
325 int test5() {
326         
327         unsigned int val, mid;
328         transrecord_t *myTrans;
329         unsigned int size;
330         objheader_t *header;
331         pthread_t thread_Listen;
332         pthread_attr_t attr;
333         objheader_t *h1, *h2, *h3, *h4, *h5;
334
335         dstmInit();     
336         pthread_attr_init(&attr);
337         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
338
339         mid = iptoMid("128.200.9.27"); //d-2.eecs.uci.edu
340         //Inserting into lhashtable
341         lhashInsert(20, mid);
342         lhashInsert(21, mid);
343         lhashInsert(22, mid);
344
345         mid = iptoMid("128.200.9.26"); //d-1.eecs.uci.edu
346         //Inserting into lhashtable
347         lhashInsert(31, mid);
348         lhashInsert(32, mid);
349         lhashInsert(33, mid);
350         pthread_create(&thread_Listen, &attr, dstmListen, NULL);
351
352         printf("DEBUG -> mid = %d\n", mid);
353         checkServer(mid, "128.200.9.26");
354         mid = iptoMid("128.200.9.27");
355         printf("DEBUG -> mid = %d\n", mid);
356         checkServer(mid, "128.200.9.27");
357
358         // Start Transaction    
359         myTrans = transStart();
360
361         // Create and Insert Oid 1
362         size = sizeof(objheader_t) + classsize[0] ;
363         header = (objheader_t *) objstrAlloc(mainobjstore, size);
364         init_obj(header, 1, 0, 1, 0, NEW);
365         mhashInsert(header->oid, header);
366         mid = iptoMid("128.200.9.10");
367         lhashInsert(header->oid, mid);
368
369         // Create and Insert Oid 2
370         size = sizeof(objheader_t) + classsize[1] ;
371         header = (objheader_t *) objstrAlloc(mainobjstore, size);
372         init_obj(header, 2, 1, 1, 0, NEW);
373         mhashInsert(header->oid, header);
374         mid = iptoMid("128.200.9.10");
375         lhashInsert(header->oid, mid);
376
377
378         // Create and Insert Oid 3
379         size = sizeof(objheader_t) + classsize[2] ;
380         header = (objheader_t *) objstrAlloc(mainobjstore, size);
381         init_obj(header, 3, 2, 1, 0, NEW);
382         mhashInsert(header->oid, header);
383         mid = iptoMid("128.200.9.10");
384         lhashInsert(header->oid, mid);
385
386         // Create and Insert Oid 4
387         size = sizeof(objheader_t) + classsize[3] ;
388         header = (objheader_t *) objstrAlloc(mainobjstore, size);
389         init_obj(header, 4, 3, 1, 0, NEW);
390         mhashInsert(header->oid, header);
391         mid = iptoMid("128.200.9.10");
392         lhashInsert(header->oid, mid);
393
394         // Create and Insert Oid 5
395         size = sizeof(objheader_t) + classsize[0] ;
396         header = (objheader_t *) objstrAlloc(mainobjstore, size);
397         init_obj(header, 5, 0, 1, 0, NEW);
398         mhashInsert(header->oid, header);
399         mid = iptoMid("128.200.9.10");
400         lhashInsert(header->oid, mid);
401         
402         // Create and Insert Oid 6
403         size = sizeof(objheader_t) + classsize[1] ;
404         header = (objheader_t *) objstrAlloc(mainobjstore, size);
405         init_obj(header, 6, 1, 1, 0, NEW);
406         mhashInsert(header->oid, header);
407         mid = iptoMid("128.200.9.10");
408         lhashInsert(header->oid, mid);
409         
410         //read object 1(present in local machine)
411         if((h1 = transRead(myTrans, 1)) == NULL){
412                 printf("Object not found\n");
413         }
414         //read object 2present in local machine)
415         if((h2 = transRead(myTrans, 2)) == NULL) {
416                 printf("Object not found\n");
417         }
418         //read object 3(present in local machine)
419         if((h3 = transRead(myTrans, 3)) == NULL) {
420                 printf("Object not found\n");
421         }
422         //read object 31 (present in d-1. eecs)
423         if((h4 = transRead(myTrans, 31)) == NULL) {
424                 printf("Object not found\n");
425         }
426         //read object 20 (present in d-2. eecs)
427         if((h5 = transRead(myTrans, 20)) == NULL) {
428                 printf("Object not found\n");
429         }
430
431         transCommit(myTrans);
432
433         pthread_join(thread_Listen, NULL);
434
435         return 0;
436 }