adding a test case
[IRC.git] / Robust / src / Benchmarks / SingleTM / Vacation / Client.java
1 /* =============================================================================
2  *
3  * client.c
4  *
5  * =============================================================================
6  *
7  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
8  * Author: Chi Cao Minh
9  *
10  * =============================================================================
11  *
12  * For the license of bayes/sort.h and bayes/sort.c, please see the header
13  * of the files.
14  * 
15  * ------------------------------------------------------------------------
16  * 
17  * For the license of kmeans, please see kmeans/LICENSE.kmeans
18  * 
19  * ------------------------------------------------------------------------
20  * 
21  * For the license of ssca2, please see ssca2/COPYRIGHT
22  * 
23  * ------------------------------------------------------------------------
24  * 
25  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26  * header of the files.
27  * 
28  * ------------------------------------------------------------------------
29  * 
30  * For the license of lib/rbtree.h and lib/rbtree.c, please see
31  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
32  * 
33  * ------------------------------------------------------------------------
34  * 
35  * Unless otherwise noted, the following license applies to STAMP files:
36  * 
37  * Copyright (c) 2007, Stanford University
38  * All rights reserved.
39  * 
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions are
42  * met:
43  * 
44  *     * Redistributions of source code must retain the above copyright
45  *       notice, this list of conditions and the following disclaimer.
46  * 
47  *     * Redistributions in binary form must reproduce the above copyright
48  *       notice, this list of conditions and the following disclaimer in
49  *       the documentation and/or other materials provided with the
50  *       distribution.
51  * 
52  *     * Neither the name of Stanford University nor the names of its
53  *       contributors may be used to endorse or promote products derived
54  *       from this software without specific prior written permission.
55  * 
56  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66  * THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  * =============================================================================
69  */
70
71
72 public class Client extends Thread {
73   int id;
74   Manager managerPtr;
75   Random randomPtr;
76   int numOperation;
77   int numQueryPerTransaction;
78   int queryRange;
79   int percentUser;
80
81   public Client() {}
82
83 /* =============================================================================
84  * client_alloc
85  * -- Returns NULL on failure
86  * =============================================================================
87  */
88   public Client(int id,
89                 Manager managerPtr,
90                 int numOperation,
91                 int numQueryPerTransaction,
92                 int queryRange,
93                 int percentUser) {
94     this.randomPtr = new Random();
95     this.randomPtr.random_alloc();
96     this.id=id;
97     this.managerPtr = managerPtr;
98     randomPtr.random_seed(id);
99     this.numOperation = numOperation;
100     this.numQueryPerTransaction = numQueryPerTransaction;
101     this.queryRange = queryRange;
102     this.percentUser = percentUser;
103   }
104
105
106 /* =============================================================================
107  * selectAction
108  * =============================================================================
109  */
110   public int selectAction (int r, int percentUser) {
111     if (r < percentUser) {
112       return ACTION_MAKE_RESERVATION;
113     } else if ((r & 1)==1) {
114       return ACTION_DELETE_CUSTOMER;
115     } else {
116       return ACTION_UPDATE_TABLES;
117     }
118   }
119
120
121 /* =============================================================================
122  * client_run
123  * -- Execute list operations on the database
124  * =============================================================================
125  */
126   public void run() {
127     int myId = id;
128
129     Manager managerPtr = this.managerPtr;
130     Random randomPtr  = this.randomPtr;
131
132     int numOperation           = this.numOperation;
133     int numQueryPerTransaction = this.numQueryPerTransaction;
134     int queryRange             = this.queryRange;
135     int percentUser            = this.percentUser;
136
137     int types[]  = new int[numQueryPerTransaction];
138     int ids[]    = new int[numQueryPerTransaction];
139     int ops[]    = new int[numQueryPerTransaction];
140     int prices[] = new int[numQueryPerTransaction];
141
142     for (int i = 0; i < numOperation; i++) {
143       int r = randomPtr.posrandom_generate() % 100;
144       int action = selectAction(r, percentUser);
145
146       if(action==ACTION_MAKE_RESERVATION) {
147         int maxPrices[]=new int[NUM_RESERVATION_TYPE];
148         int maxIds[]=new int[NUM_RESERVATION_TYPE];
149         maxPrices[0]=-1;
150         maxPrices[1]=-1;
151         maxPrices[2]=-1;
152         maxIds[0]=-1;
153         maxIds[1]=-1;
154         maxIds[2]=-1;
155         int n;
156         int numQuery = randomPtr.posrandom_generate() % numQueryPerTransaction + 1;
157         int customerId = randomPtr.posrandom_generate() % queryRange + 1;
158         for (n = 0; n < numQuery; n++) {
159           types[n] = randomPtr.random_generate() % NUM_RESERVATION_TYPE;
160           ids[n] = (randomPtr.random_generate() % queryRange) + 1;
161         }
162         boolean isFound = false;
163         atomic {
164           for (n = 0; n < numQuery; n++) {
165             int t = types[n];
166             int id = ids[n];
167             int price = -1;
168             if (t==RESERVATION_CAR) {
169               if (managerPtr.manager_queryCar(id) >= 0) {
170                 price = managerPtr.manager_queryCarPrice(id);
171               }
172             } else if (t==RESERVATION_FLIGHT) {
173               if (managerPtr.manager_queryFlight(id) >= 0) {
174                 price = managerPtr.manager_queryFlightPrice(id);
175               }
176             } else if (t==RESERVATION_ROOM) {
177               if (managerPtr.manager_queryRoom(id) >= 0) {
178                 price = managerPtr.manager_queryRoomPrice(id);
179               }
180             }
181             if (price > maxPrices[t]) {
182               maxPrices[t] = price;
183               maxIds[t] = id;
184               isFound = true;
185             }
186           } /* for n */
187           if (isFound) {
188             managerPtr.manager_addCustomer(customerId);
189           }
190           if (maxIds[RESERVATION_CAR] > 0) {
191             managerPtr.manager_reserveCar(customerId, maxIds[RESERVATION_CAR]);
192           }
193           if (maxIds[RESERVATION_FLIGHT] > 0) {
194             managerPtr.manager_reserveFlight(customerId, maxIds[RESERVATION_FLIGHT]);
195           }
196           if (maxIds[RESERVATION_ROOM] > 0) {
197             managerPtr.manager_reserveRoom(customerId, maxIds[RESERVATION_ROOM]);
198           }
199         }//end atomic
200       } else if (action==ACTION_DELETE_CUSTOMER) {
201         int customerId = randomPtr.posrandom_generate() % queryRange + 1;
202         atomic {
203           int bill = managerPtr.manager_queryCustomerBill(customerId);
204           if (bill >= 0) {
205             managerPtr.manager_deleteCustomer(customerId);
206           }
207         }
208       } else if (action==ACTION_UPDATE_TABLES) {
209         int numUpdate = randomPtr.posrandom_generate() % numQueryPerTransaction + 1;
210         int n;
211         for (n = 0; n < numUpdate; n++) {
212           types[n] = randomPtr.posrandom_generate() % NUM_RESERVATION_TYPE;
213           ids[n] = (randomPtr.posrandom_generate() % queryRange) + 1;
214           ops[n] = randomPtr.posrandom_generate() % 2;
215           if (ops[n]==1) {
216             prices[n] = ((randomPtr.posrandom_generate() % 5) * 10) + 50;
217           }
218         }
219         atomic {
220           for (n = 0; n < numUpdate; n++) {
221             int t = types[n];
222             int id = ids[n];
223             int doAdd = ops[n];
224             if (doAdd==1) {
225               int newPrice = prices[n];
226               if (t==RESERVATION_CAR) {
227                 managerPtr.manager_addCar(id, 100, newPrice);
228               } else if (t==RESERVATION_FLIGHT) {
229                 managerPtr.manager_addFlight(id, 100, newPrice);
230               } else if (t==RESERVATION_ROOM) {
231                 managerPtr.manager_addRoom(id, 100, newPrice);
232               }
233             } else { /* do delete */
234               if (t==RESERVATION_CAR) {
235                 managerPtr.manager_deleteCar(id, 100);
236               } else if (t==RESERVATION_FLIGHT) {
237                 managerPtr.manager_deleteFlight(id);
238               } else if (t==RESERVATION_ROOM) {
239                 managerPtr.manager_deleteRoom(id, 100);
240               }
241             }
242           }
243         }
244       }
245     } /* for i */
246     Barrier.enterBarrier();
247   }
248 }
249
250 /* =============================================================================
251  *
252  * End of client.c
253  *
254  * =============================================================================
255  */
256
257
258
259