Fixed bugs for memory leak . Trying to run two transactions commit
[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         //sleep(3);
48         test3();
49 }
50
51 int test1()
52 {
53         unsigned int val;
54         transrecord_t *myTrans;
55         pthread_t thread_Listen;
56
57         dstmInit();     
58         pthread_create(&thread_Listen, NULL, dstmListen, NULL);
59         // Start Transaction    
60         myTrans = transStart();
61
62         printf("Creating Transaction\n");
63         //Create Object1
64         if((val = createObjects(myTrans, 0)) != 0) {
65                 printf("Error transCreateObj1");
66         }
67         //Create Object2
68         if((val = createObjects(myTrans, 1)) != 0) {
69                 printf("Error transCreateObj2");
70         }
71         //Create Object3
72         if((val = createObjects(myTrans, 2)) != 0) {
73                 printf("Error transCreateObj3");
74         }
75         //Create Object4
76         if((val = createObjects(myTrans, 3)) != 0) {
77                 printf("Error transCreateObj4");
78         }
79         //Create Object5
80         if((val = createObjects(myTrans, 0)) != 0) {
81                 printf("Error transCreateObj5");
82         }
83         //Create Object6
84         if((val = createObjects(myTrans, 1)) != 0) {
85                 printf("Error transCreateObj6");
86         }
87         pthread_join(thread_Listen, NULL);
88         return 0;
89 }
90
91 int test2() {
92         
93         unsigned int val, mid;
94         transrecord_t *myTrans;
95         pthread_t thread_Listen;
96
97         dstmInit();     
98         mid = iptoMid("128.200.9.27"); //d-2.eecs.uci.edu
99         //Inserting into lhashtable
100         lhashInsert(20, mid);
101         lhashInsert(21, mid);
102         lhashInsert(22, mid);
103         lhashInsert(23, mid);
104         lhashInsert(30, mid);
105         lhashInsert(28, mid);
106         lhashInsert(29, mid);
107         pthread_create(&thread_Listen, NULL, dstmListen, NULL);
108         // Start Transaction    
109         myTrans = transStart();
110
111         printf("Creating Transaction\n");
112         //Create Object1
113         if((val = createObjects(myTrans, 0)) != 0) {
114                 printf("Error transCreateObj1");
115         }
116         //Create Object2
117         if((val = createObjects(myTrans, 1)) != 0) {
118                 printf("Error transCreateObj2");
119         }
120         //Create Object3
121         if((val = createObjects(myTrans, 2)) != 0) {
122                 printf("Error transCreateObj3");
123         }
124         //Create Object4
125         if((val = createObjects(myTrans, 3)) != 0) {
126                 printf("Error transCreateObj4");
127         }
128         //Create Object5
129         if((val = createObjects(myTrans, 0)) != 0) {
130                 printf("Error transCreateObj5");
131         }
132         //Create Object6
133         if((val = createObjects(myTrans, 1)) != 0) {
134                 printf("Error transCreateObj6");
135         }
136         pthread_join(thread_Listen, NULL);
137 }
138
139 int test3() {
140         
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, *h4, *h5;
148
149         dstmInit();     
150         pthread_attr_init(&attr);
151         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
152
153         mid = iptoMid("128.200.9.27"); //d-2.eecs.uci.edu
154         //Inserting into lhashtable
155         lhashInsert(20, mid);
156         lhashInsert(21, mid);
157         lhashInsert(22, mid);
158
159         mid = iptoMid("128.200.9.26"); //d-1.eecs.uci.edu
160         //Inserting into lhashtable
161         lhashInsert(31, mid);
162         lhashInsert(32, mid);
163         lhashInsert(33, mid);
164         pthread_create(&thread_Listen, &attr, dstmListen, NULL);
165 //      pthread_create(&thread_Listen, NULL, dstmListen, NULL);
166
167         printf("DEBUG -> mid = %d\n", mid);
168         checkServer(mid, "128.200.9.26");
169         mid = iptoMid("128.200.9.27");
170         printf("DEBUG -> mid = %d\n", mid);
171         checkServer(mid, "128.200.9.27");
172
173         // Start Transaction    
174         myTrans = transStart();
175 /*
176         //Create Object1
177         if((val = createObjects(myTrans, 0)) != 0) {
178                 printf("Error transCreateObj1");
179         }
180         //Create Object2
181         if((val = createObjects(myTrans, 1)) != 0) {
182                 printf("Error transCreateObj2");
183         }
184         //Create Object3
185         if((val = createObjects(myTrans, 2)) != 0) {
186                 printf("Error transCreateObj3");
187         }
188         //Create Object4
189         if((val = createObjects(myTrans, 3)) != 0) {
190                 printf("Error transCreateObj4");
191         }
192         //Create Object5
193         if((val = createObjects(myTrans, 0)) != 0) {
194                 printf("Error transCreateObj5");
195         }
196         //Create Object6
197         if((val = createObjects(myTrans, 1)) != 0) {
198                 printf("Error transCreateObj6");
199         }
200
201         */
202         // Create and Insert Oid 1
203         size = sizeof(objheader_t) + classsize[0] ;
204         header = (objheader_t *) objstrAlloc(mainobjstore, size);
205         init_obj(header, 1, 0, 1, 0, NEW);
206         mhashInsert(header->oid, header);
207         mid = iptoMid("128.200.9.10");
208         lhashInsert(header->oid, mid);
209
210         // Create and Insert Oid 2
211         size = sizeof(objheader_t) + classsize[1] ;
212         header = (objheader_t *) objstrAlloc(mainobjstore, size);
213         init_obj(header, 2, 1, 1, 0, NEW);
214         mhashInsert(header->oid, header);
215         mid = iptoMid("128.200.9.10");
216         lhashInsert(header->oid, mid);
217
218
219         // Create and Insert Oid 3
220         size = sizeof(objheader_t) + classsize[2] ;
221         header = (objheader_t *) objstrAlloc(mainobjstore, size);
222         init_obj(header, 3, 2, 1, 0, NEW);
223         mhashInsert(header->oid, header);
224         mid = iptoMid("128.200.9.10");
225         lhashInsert(header->oid, mid);
226
227         // Create and Insert Oid 4
228         size = sizeof(objheader_t) + classsize[3] ;
229         header = (objheader_t *) objstrAlloc(mainobjstore, size);
230         init_obj(header, 4, 3, 1, 0, NEW);
231         mhashInsert(header->oid, header);
232         mid = iptoMid("128.200.9.10");
233         lhashInsert(header->oid, mid);
234
235         // Create and Insert Oid 5
236         size = sizeof(objheader_t) + classsize[0] ;
237         header = (objheader_t *) objstrAlloc(mainobjstore, size);
238         init_obj(header, 5, 0, 1, 0, NEW);
239         mhashInsert(header->oid, header);
240         mid = iptoMid("128.200.9.10");
241         lhashInsert(header->oid, mid);
242         
243         // Create and Insert Oid 6
244         size = sizeof(objheader_t) + classsize[1] ;
245         header = (objheader_t *) objstrAlloc(mainobjstore, size);
246         init_obj(header, 6, 1, 1, 0, NEW);
247         mhashInsert(header->oid, header);
248         mid = iptoMid("128.200.9.10");
249         lhashInsert(header->oid, mid);
250         
251         //read object 1(present in local machine)
252         if((h1 = transRead(myTrans, 1)) == NULL){
253                 printf("Object not found\n");
254         }
255         //read object 2present in local machine)
256         if((h2 = transRead(myTrans, 2)) == NULL) {
257                 printf("Object not found\n");
258         }
259         //read object 3(present in local machine)
260         if((h3 = transRead(myTrans, 3)) == NULL) {
261                 printf("Object not found\n");
262         }
263         //read object 31 (present in d-1. eecs)
264         if((h4 = transRead(myTrans, 31)) == NULL) {
265                 printf("Object not found\n");
266         }
267         //read object 20 (present in d-2. eecs)
268         if((h5 = transRead(myTrans, 20)) == NULL) {
269                 printf("Object not found\n");
270         }
271
272         transCommit(myTrans);
273
274         pthread_join(thread_Listen, NULL);
275
276         return 0;
277 }