1 /* =============================================================================
5 * =============================================================================
7 * Copyright (C) Stanford University, 2006. All Rights Reserved.
10 * =============================================================================
12 * For the license of bayes/sort.h and bayes/sort.c, please see the header
15 * ------------------------------------------------------------------------
17 * For the license of kmeans, please see kmeans/LICENSE.kmeans
19 * ------------------------------------------------------------------------
21 * For the license of ssca2, please see ssca2/COPYRIGHT
23 * ------------------------------------------------------------------------
25 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26 * header of the files.
28 * ------------------------------------------------------------------------
30 * For the license of lib/rbtree.h and lib/rbtree.c, please see
31 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
33 * ------------------------------------------------------------------------
35 * Unless otherwise noted, the following license applies to STAMP files:
37 * Copyright (c) 2007, Stanford University
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions are
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
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
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.
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.
68 * =============================================================================
72 public class Client extends Thread {
77 int numQueryPerTransaction;
83 /* =============================================================================
85 * -- Returns NULL on failure
86 * =============================================================================
91 int numQueryPerTransaction,
94 this.randomPtr = new Random();
95 this.randomPtr.random_alloc();
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;
106 /* =============================================================================
108 * =============================================================================
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;
116 return ACTION_UPDATE_TABLES;
121 /* =============================================================================
123 * -- Execute list operations on the database
124 * =============================================================================
129 Manager managerPtr = this.managerPtr;
130 Random randomPtr = this.randomPtr;
132 int numOperation = this.numOperation;
133 int numQueryPerTransaction = this.numQueryPerTransaction;
134 int queryRange = this.queryRange;
135 int percentUser = this.percentUser;
137 int types[] = new int[numQueryPerTransaction];
138 int ids[] = new int[numQueryPerTransaction];
139 int ops[] = new int[numQueryPerTransaction];
140 int prices[] = new int[numQueryPerTransaction];
142 for (int i = 0; i < numOperation; i++) {
143 int r = randomPtr.posrandom_generate() % 100;
144 int action = selectAction(r, percentUser);
146 if(action==ACTION_MAKE_RESERVATION) {
147 int maxPrices[]=new int[NUM_RESERVATION_TYPE];
148 int maxIds[]=new int[NUM_RESERVATION_TYPE];
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;
162 boolean isFound = false;
164 for (n = 0; n < numQuery; n++) {
168 if (t==RESERVATION_CAR) {
169 if (managerPtr.manager_queryCar(id) >= 0) {
170 price = managerPtr.manager_queryCarPrice(id);
172 } else if (t==RESERVATION_FLIGHT) {
173 if (managerPtr.manager_queryFlight(id) >= 0) {
174 price = managerPtr.manager_queryFlightPrice(id);
176 } else if (t==RESERVATION_ROOM) {
177 if (managerPtr.manager_queryRoom(id) >= 0) {
178 price = managerPtr.manager_queryRoomPrice(id);
181 if (price > maxPrices[t]) {
182 maxPrices[t] = price;
188 managerPtr.manager_addCustomer(customerId);
190 if (maxIds[RESERVATION_CAR] > 0) {
191 managerPtr.manager_reserveCar(customerId, maxIds[RESERVATION_CAR]);
193 if (maxIds[RESERVATION_FLIGHT] > 0) {
194 managerPtr.manager_reserveFlight(customerId, maxIds[RESERVATION_FLIGHT]);
196 if (maxIds[RESERVATION_ROOM] > 0) {
197 managerPtr.manager_reserveRoom(customerId, maxIds[RESERVATION_ROOM]);
200 } else if (action==ACTION_DELETE_CUSTOMER) {
201 int customerId = randomPtr.posrandom_generate() % queryRange + 1;
203 int bill = managerPtr.manager_queryCustomerBill(customerId);
205 managerPtr.manager_deleteCustomer(customerId);
208 } else if (action==ACTION_UPDATE_TABLES) {
209 int numUpdate = randomPtr.posrandom_generate() % numQueryPerTransaction + 1;
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;
216 prices[n] = ((randomPtr.posrandom_generate() % 5) * 10) + 50;
220 for (n = 0; n < numUpdate; n++) {
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);
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);
246 Barrier.enterBarrier();
250 /* =============================================================================
254 * =============================================================================