688d44a3bfa76a6f393e0b9799874398fb5f37bf
[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
10 extern objstr_t *mainobjstore;
11 //extern lhashtable_t llookup;          //Global Hash table
12 //extern mhashtable_t mlookup;          //Global Hash table
13
14 int classsize[]={sizeof(int),sizeof(char),sizeof(short), sizeof(void *)};       
15
16 int test1(void);
17 int test2(void);
18
19 unsigned int createObjects(transrecord_t *record) {
20         objheader_t *header, *tmp;
21         unsigned int size, mid;
22         int i = 0;
23         for(i = 20 ; i< 23; i++) {
24                 size = sizeof(objheader_t) + classsize[i-20] ;
25                 tmp = (objheader_t *)objstrAlloc(record->cache, size);
26                 tmp->oid = i;
27                 tmp->type = (i-20);
28                 tmp->version = 1;
29                 tmp->rcount = 0; //? not sure how to handle this yet
30                 tmp->status = 0;
31                 tmp->status |= NEW;
32                 chashInsert(record->lookupTable, tmp->oid, tmp);
33                 header = (objheader_t *) objstrAlloc(mainobjstore, size);
34                 memcpy(header, tmp, size);
35                 mhashInsert(header->oid, header);
36                 mid = iptoMid("128.200.9.27");//machine d-2
37                 printf("DEBUG -> createObjects mid is %x\n", mid);
38                 lhashInsert(header->oid, mid);
39         }
40         //      printf("Insert oid = %d at address %x\n",tmp->oid, tmp);
41         size = sizeof(objheader_t) + classsize[0] ;
42         header = (objheader_t *) objstrAlloc(mainobjstore, size);
43         header->oid = 30;
44         header->type = 0;
45         header->version = 1;
46         header->rcount = 0; //? not sure how to handle this yet
47         header->status = 0;
48         header->status |= NEW;
49         mhashInsert(header->oid, header);
50         mid = iptoMid("128.200.9.27");
51         lhashInsert(header->oid, mid);
52         size = sizeof(objheader_t) + classsize[1] ;
53         header = (objheader_t *) objstrAlloc(mainobjstore, size);
54         header->oid = 28;
55         header->type = 1;
56         header->version = 1;
57         header->rcount = 0; //? not sure how to handle this yet
58         header->status = 0;
59         header->status |= LOCK;
60         mhashInsert(header->oid, header);
61         mid = iptoMid("128.200.9.27");
62         lhashInsert(header->oid, mid);
63         size = sizeof(objheader_t) + classsize[2] ;
64         header = (objheader_t *) objstrAlloc(mainobjstore, size);
65         header->oid = 29;
66         header->type = 2;
67         header->version = 1;
68         header->rcount = 0; //? not sure how to handle this yet
69         header->status = 0;
70         header->status |= LOCK;
71         mhashInsert(header->oid, header);
72         mid = iptoMid("128.200.9.27");
73         lhashInsert(header->oid, mid);
74         return 0;
75 }
76
77 int main() 
78 {
79 //      test2();
80 //      test3();
81 //      test4();
82         test5();
83 }
84
85 int test1(void) {
86
87         transrecord_t *record;
88         objheader_t *h1,*h2,*h3,*h4,*h5, *h6;
89
90         dstmInit();
91         record = transStart();
92         printf("DEBUG -> Init done\n");
93         h1 = transRead(record, 1);
94         printf("oid = %d\tsize = %d\n", h1->oid,classsize[h1->type]);
95         h3 = transRead(record, 3);
96         printf("oid = %d\tsize = %d\n", h3->oid,classsize[h3->type]);
97         h4 = transRead(record, 4);
98         printf("oid = %d\tsize = %d\n", h4->oid,classsize[h4->type]);
99         h2 = transRead(record, 2);
100         printf("oid = %d\tsize = %d\n", h2->oid,classsize[h2->type]);
101         h4 = transRead(record, 4);
102         printf("oid = %d\tsize = %d\n", h4->oid,classsize[h4->type]);
103         h3 = transRead(record, 3);
104         printf("oid = %d\tsize = %d\n", h3->oid,classsize[h3->type]);
105         h5 = transRead(record, 5);
106         printf("oid = %d\tsize = %d\n", h5->oid,classsize[h5->type]);
107 //      getRemoteObj(&record, 0,1);
108 }
109
110 int test2(void) {
111
112         transrecord_t *record;
113         objheader_t *h1,*h2,*h3,*h4,*h5, *h6;
114
115         dstmInit();
116         record = transStart();
117
118         lhashInsert(1,1);
119         lhashInsert(2,1);
120         lhashInsert(3,1);
121         lhashInsert(4,1);
122         lhashInsert(5,1);
123         lhashInsert(6,1);
124         printf("DEBUG -> Init done\n");
125         h1 = transRead(record, 1);
126         lhashInsert(h1->oid, 1);
127         h2 = transRead(record, 2);
128         lhashInsert(h2->oid, 1);
129         h3 = transRead(record, 3);
130         lhashInsert(h3->oid, 1);
131         h4 = transRead(record, 4);
132         lhashInsert(h4->oid, 1);
133 //      h4->status |= DIRTY;
134         h5 = transRead(record, 5);
135         lhashInsert(h5->oid, 1);
136         h6 = transRead(record, 6);
137         lhashInsert(h6->oid, 1);
138 //      h6->status |= DIRTY;
139         
140         transCommit(record);
141
142         return 0;
143 }
144 //Test Read objects when objects are not found in  any participant 
145 int test3(void){
146         transrecord_t *record;
147         objheader_t *h1,*h2;
148
149         dstmInit();
150         record = transStart();
151         printf("DEBUG -> Init done\n");
152         //read object 11
153         if((h1 = transRead(record, 11)) == NULL){
154                 printf("Object not found\n");
155         }
156         //read object 12
157         if((h2 = transRead(record, 12)) == NULL) {
158                 printf("Object not found\n");
159         }
160         transCommit(record);
161
162         return 0;
163 }
164
165 //Test Read objects when some objects are found and other objects not found in  any participant 
166 int test4(void) {
167         transrecord_t *record;
168         objheader_t *h1,*h2, *h3, *h4;
169
170         dstmInit();
171         record = transStart();
172         printf("DEBUG -> Init done\n");
173         //read object 1
174         if((h1 = transRead(record, 1)) == NULL){
175                 printf("Object not found\n");
176         }
177         //read object 2
178         if((h2 = transRead(record, 2)) == NULL) {
179                 printf("Object not found\n");
180         }
181         //read object 11
182         if((h3 = transRead(record, 11)) == NULL) {
183                 printf("Object not found\n");
184         }
185         //read object 13
186         if((h4 = transRead(record, 13)) == NULL) {
187                 printf("Object not found\n");
188         }
189         if((h1 != NULL) && (h2 != NULL) && (h3 != NULL) && h4 !=NULL) { 
190                 transCommit(record);
191         }else {
192                 printf("Cannot complete this transaction\n");
193         }
194
195         return 0;
196 }
197
198 //Test for transaction objects when the objs are part of the Coordinator machine starting the 
199 //trans commit 
200 int test5(void) {
201         transrecord_t *record;
202         objheader_t *header;
203         unsigned int size, mid;
204         pthread_t thread_Listen;
205         pthread_attr_t attr;
206         objheader_t *h1,*h2, *h3, *h4;
207
208         dstmInit();
209         pthread_attr_init(&attr);
210         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
211         //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
212
213         record = transStart();
214         printf("DEBUG -> Init done\n");
215         mid = iptoMid("128.200.9.10");// Machine demsky.eecs.uci.edu    
216         lhashInsert(1,mid);
217         lhashInsert(2,mid);
218         lhashInsert(3,mid);
219         lhashInsert(4,mid);
220         lhashInsert(5,mid);
221         lhashInsert(6,mid);
222         pthread_create(&thread_Listen, &attr, dstmListen, NULL);
223         
224         //Create and Insert Oid 20
225         size = sizeof(objheader_t) + classsize[2] ;
226         header = (objheader_t *) objstrAlloc(mainobjstore, size);
227         header->oid = 20;
228         header->type = 2;
229         header->version = 1;
230         header->rcount = 0; //? not sure how to handle this yet
231         header->status = 0;
232         header->status |= NEW;
233         mhashInsert(header->oid, header);
234         mid = iptoMid("128.200.9.27");
235         lhashInsert(header->oid, mid);
236
237         //Create and Insert Oid 21
238         size = sizeof(objheader_t) + classsize[1] ;
239         header = (objheader_t *) objstrAlloc(mainobjstore, size);
240         header->oid = 21;
241         header->type = 1;
242         header->version = 1;
243         header->rcount = 0; //? not sure how to handle this yet
244         header->status = 0;
245         header->status |= NEW;
246         mhashInsert(header->oid, header);
247         mid = iptoMid("128.200.9.27");
248         lhashInsert(header->oid, mid);
249         //read object 1
250         if((h1 = transRead(record, 1)) == NULL){
251                 printf("Object not found\n");
252         }
253         //read object 5
254         if((h2 = transRead(record, 5)) == NULL) {
255                 printf("Object not found\n");
256         }
257         //read object 20(present in local machine)
258         if((h3 = transRead(record, 20)) == NULL) {
259                 printf("Object not found\n");
260         }
261         //read object 21(present in local machine)
262         if((h4 = transRead(record, 21)) == NULL) {
263                 printf("Object not found\n");
264         }
265         
266         transCommit(record);
267         pthread_join(thread_Listen, NULL);
268 }