Skip to content

Commit 99dc2ff

Browse files
committed
Merge branch 'fix-bugs-to-run' into distribute-resource-managers
2 parents 2027ed8 + ea44526 commit 99dc2ff

26 files changed

+847
-226
lines changed

code/Client/Client/Client.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public void execute(Command cmd, Vector<String> arguments) throws RemoteExceptio
223223

224224
System.out.println("Deleting a customer from the database [xid=" + arguments.elementAt(1) + "]");
225225
System.out.println("-Customer ID: " + arguments.elementAt(2));
226-
226+
227227
int id = toInt(arguments.elementAt(1));
228228
int customerID = toInt(arguments.elementAt(2));
229229

@@ -239,7 +239,7 @@ public void execute(Command cmd, Vector<String> arguments) throws RemoteExceptio
239239

240240
System.out.println("Querying a flight [xid=" + arguments.elementAt(1) + "]");
241241
System.out.println("-Flight Number: " + arguments.elementAt(2));
242-
242+
243243
int id = toInt(arguments.elementAt(1));
244244
int flightNum = toInt(arguments.elementAt(2));
245245

@@ -252,7 +252,7 @@ public void execute(Command cmd, Vector<String> arguments) throws RemoteExceptio
252252

253253
System.out.println("Querying cars location [xid=" + arguments.elementAt(1) + "]");
254254
System.out.println("-Car Location: " + arguments.elementAt(2));
255-
255+
256256
int id = toInt(arguments.elementAt(1));
257257
String location = arguments.elementAt(2);
258258

@@ -265,7 +265,7 @@ public void execute(Command cmd, Vector<String> arguments) throws RemoteExceptio
265265

266266
System.out.println("Querying rooms location [xid=" + arguments.elementAt(1) + "]");
267267
System.out.println("-Room Location: " + arguments.elementAt(2));
268-
268+
269269
int id = toInt(arguments.elementAt(1));
270270
String location = arguments.elementAt(2);
271271

@@ -284,11 +284,11 @@ public void execute(Command cmd, Vector<String> arguments) throws RemoteExceptio
284284

285285
String bill = m_resourceManager.queryCustomerInfo(id, customerID);
286286
System.out.print(bill);
287-
break;
287+
break;
288288
}
289289
case QueryFlightPrice: {
290290
checkArgumentsCount(3, arguments.size());
291-
291+
292292
System.out.println("Querying a flight price [xid=" + arguments.elementAt(1) + "]");
293293
System.out.println("-Flight Number: " + arguments.elementAt(2));
294294

@@ -367,7 +367,7 @@ public void execute(Command cmd, Vector<String> arguments) throws RemoteExceptio
367367
System.out.println("Reserving a room at a location [xid=" + arguments.elementAt(1) + "]");
368368
System.out.println("-Customer ID: " + arguments.elementAt(2));
369369
System.out.println("-Room Location: " + arguments.elementAt(3));
370-
370+
371371
int id = toInt(arguments.elementAt(1));
372372
int customerID = toInt(arguments.elementAt(2));
373373
String location = arguments.elementAt(3);
@@ -396,12 +396,12 @@ public void execute(Command cmd, Vector<String> arguments) throws RemoteExceptio
396396

397397
int id = toInt(arguments.elementAt(1));
398398
int customerID = toInt(arguments.elementAt(2));
399-
Vector<String> flightNumbers = new Vector<String>();
399+
Vector<Integer> flightNumbers = new Vector<Integer>();
400400
for (int i = 0; i < arguments.size() - 6; ++i)
401401
{
402-
flightNumbers.addElement(arguments.elementAt(3+i));
402+
flightNumbers.addElement(toInt(arguments.elementAt(3+i)));
403403
}
404-
String location = arguments.elementAt(arguments.size()-2);
404+
String location = arguments.elementAt(arguments.size()-3);
405405
boolean car = toBoolean(arguments.elementAt(arguments.size()-2));
406406
boolean room = toBoolean(arguments.elementAt(arguments.size()-1));
407407

code/Client/Client/RMIClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
public class RMIClient extends Client
1414
{
1515
private static String s_serverHost = "localhost";
16-
private static int s_serverPort = 1099;
17-
private static String s_serverName = "Server";
16+
private static int s_serverPort = 2005;
17+
private static String s_serverName = "MiddlewareServer";
1818

1919
//TODO: REPLACE 'ALEX' WITH YOUR GROUP NUMBER TO COMPILE
2020
private static String s_rmiPrefix = "group25_";

code/Server/Server/Common/AbstractRMHashMapManager.java

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -92,41 +92,41 @@ protected int queryPrice(int xid, String key)
9292
}
9393

9494
// Reserve an item
95-
protected boolean reserveItem(int xid, int customerID, String key, String location)
96-
{
97-
Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" );
98-
// Read customer object if it exists (and read lock it)
99-
Customer customer = (Customer)readData(xid, Customer.getKey(customerID));
100-
if (customer == null)
101-
{
102-
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist");
103-
return false;
104-
}
105-
106-
// Check if the item is available
107-
ReservableItem item = (ReservableItem)readData(xid, key);
108-
if (item == null)
109-
{
110-
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist");
111-
return false;
112-
}
113-
else if (item.getCount() == 0)
114-
{
115-
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items");
116-
return false;
117-
}
118-
else
119-
{
120-
customer.reserve(key, location, item.getPrice());
121-
writeData(xid, customer.getKey(), customer);
122-
123-
// Decrease the number of available items in the storage
124-
item.setCount(item.getCount() - 1);
125-
item.setReserved(item.getReserved() + 1);
126-
writeData(xid, item.getKey(), item);
127-
128-
Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded");
129-
return true;
130-
}
131-
}
95+
// protected boolean reserveItem(int xid, int customerID, String key, String location)
96+
// {
97+
// Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" );
98+
// // Read customer object if it exists (and read lock it)
99+
// Customer customer = (Customer)readData(xid, Customer.getKey(customerID));
100+
// if (customer == null)
101+
// {
102+
// Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist");
103+
// return false;
104+
// }
105+
//
106+
// // Check if the item is available
107+
// ReservableItem item = (ReservableItem)readData(xid, key);
108+
// if (item == null)
109+
// {
110+
// Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist");
111+
// return false;
112+
// }
113+
// else if (item.getCount() == 0)
114+
// {
115+
// Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items");
116+
// return false;
117+
// }
118+
// else
119+
// {
120+
// customer.reserve(key, location, item.getPrice());
121+
// writeData(xid, customer.getKey(), customer);
122+
//
123+
// // Decrease the number of available items in the storage
124+
// item.setCount(item.getCount() - 1);
125+
// item.setReserved(item.getReserved() + 1);
126+
// writeData(xid, item.getKey(), item);
127+
//
128+
// Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded");
129+
// return true;
130+
// }
131+
// }
132132
}

code/Server/Server/Common/CarResourceManager.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
import java.rmi.RemoteException;
77
import java.io.*;
88

9-
public class CarResourceManager extends AbstractRMHashMapManager implements ICarResourceManager
9+
public abstract class CarResourceManager extends AbstractRMHashMapManager implements ICarResourceManager
1010
{
1111
// Create a new car location or add cars to an existing location
1212
// NOTE: if price <= 0 and the location already exists, it maintains its current price
13+
private String m_name = "";
14+
protected ICustomerResourceManager customerRM;
15+
16+
public CarResourceManager(String p_name) {
17+
m_name = p_name;
18+
};
19+
1320
public boolean addCars(int xid, String location, int count, int price) throws RemoteException
1421
{
1522
Trace.info("RM::addCars(" + xid + ", " + location + ", " + count + ", $" + price + ") called");
@@ -57,4 +64,63 @@ public int queryCarsPrice(int xid, String location) throws RemoteException
5764
{
5865
return queryPrice(xid, Car.getKey(location));
5966
}
67+
68+
public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException
69+
{
70+
Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" );
71+
// Read customer object if it exists (and read lock it)
72+
Customer customer = null;
73+
try {
74+
customer = getCustomer(xid, customerID);
75+
if (customer == null)
76+
{
77+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist");
78+
return false;
79+
}
80+
} catch (RemoteException e) {
81+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist--remote exception");
82+
return false;
83+
}
84+
85+
// Check if the item is available
86+
ReservableItem item = (ReservableItem)readData(xid, key);
87+
if (item == null)
88+
{
89+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist");
90+
return false;
91+
}
92+
else if (item.getCount() == 0)
93+
{
94+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items");
95+
return false;
96+
}
97+
else
98+
{
99+
customer.reserve(key, location, item.getPrice());
100+
writeData(xid, customer.getKey(), customer);
101+
102+
// Decrease the number of available items in the storage
103+
item.setCount(item.getCount() - 1);
104+
item.setReserved(item.getReserved() + 1);
105+
writeData(xid, item.getKey(), item);
106+
107+
Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded");
108+
return true;
109+
}
110+
}
111+
112+
/**
113+
TODO public Customer getCustomer(int xid, int cid) {
114+
customerRM.getCustomer(xid, cid)
115+
}
116+
*/
117+
public Customer getCustomer(int xid, int customerID) throws RemoteException
118+
{
119+
return customerRM.getCustomer(xid, customerID);
120+
}
121+
122+
public String getName() throws RemoteException
123+
{
124+
return m_name;
125+
}
60126
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package Server.Common;
2+
3+
import Server.Interface.*;
4+
5+
import java.util.*;
6+
import java.rmi.RemoteException;
7+
import java.io.*;
8+
9+
public class CustomerResourceManager extends AbstractRMHashMapManager implements ICustomerResourceManager
10+
{
11+
private String m_name="";
12+
13+
public CustomerResourceManager(String p_name) {
14+
m_name = p_name;
15+
};
16+
17+
public int newCustomer(int xid) throws RemoteException {
18+
Trace.info("RM::newCustomer(" + xid + ") called");
19+
// Generate a globally unique ID for the new customer
20+
int cid = Integer.parseInt(String.valueOf(xid) +
21+
String.valueOf(Calendar.getInstance().get(Calendar.MILLISECOND)) +
22+
String.valueOf(Math.round(Math.random() * 100 + 1)));
23+
Customer customer = new Customer(cid);
24+
writeData(xid, customer.getKey(), customer);
25+
Trace.info("RM::newCustomer(" + cid + ") returns ID=" + cid);
26+
return cid;
27+
}
28+
29+
public boolean newCustomer(int xid, int customerID) throws RemoteException
30+
{
31+
Trace.info("RM::newCustomer(" + xid + ", " + customerID + ") called");
32+
Customer customer = (Customer)readData(xid, Customer.getKey(customerID));
33+
if (customer == null)
34+
{
35+
customer = new Customer(customerID);
36+
writeData(xid, customer.getKey(), customer);
37+
Trace.info("RM::newCustomer(" + xid + ", " + customerID + ") created a new customer");
38+
return true;
39+
}
40+
else
41+
{
42+
Trace.info("INFO: RM::newCustomer(" + xid + ", " + customerID + ") failed--customer already exists");
43+
return false;
44+
}
45+
}
46+
47+
48+
public boolean deleteCustomer(int xid, int customerID) throws RemoteException
49+
{
50+
Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") called");
51+
Customer customer = (Customer)readData(xid, Customer.getKey(customerID));
52+
if (customer == null)
53+
{
54+
Trace.warn("RM::deleteCustomer(" + xid + ", " + customerID + ") failed--customer doesn't exist");
55+
return false;
56+
}
57+
else
58+
{
59+
// Increase the reserved numbers of all reservable items which the customer reserved.
60+
RMHashMap reservations = customer.getReservations();
61+
for (String reservedKey : reservations.keySet())
62+
{
63+
ReservedItem reserveditem = customer.getReservedItem(reservedKey);
64+
Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") has reserved " + reserveditem.getKey() + " " + reserveditem.getCount() + " times");
65+
ReservableItem item = (ReservableItem)readData(xid, reserveditem.getKey());
66+
Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") has reserved " + reserveditem.getKey() + " which is reserved " + item.getReserved() + " times and is still available " + item.getCount() + " times");
67+
item.setReserved(item.getReserved() - reserveditem.getCount());
68+
item.setCount(item.getCount() + reserveditem.getCount());
69+
writeData(xid, item.getKey(), item);
70+
}
71+
72+
// Remove the customer from the storage
73+
removeData(xid, customer.getKey());
74+
Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") succeeded");
75+
return true;
76+
}
77+
}
78+
79+
public String queryCustomerInfo(int xid, int customerID) throws RemoteException
80+
{
81+
Trace.info("RM::queryCustomerInfo(" + xid + ", " + customerID + ") called");
82+
Customer customer = (Customer)readData(xid, Customer.getKey(customerID));
83+
if (customer == null)
84+
{
85+
Trace.warn("RM::queryCustomerInfo(" + xid + ", " + customerID + ") failed--customer doesn't exist");
86+
// NOTE: don't change this--WC counts on this value indicating a customer does not exist...
87+
return "";
88+
}
89+
else
90+
{
91+
Trace.info("RM::queryCustomerInfo(" + xid + ", " + customerID + ")");
92+
System.out.println(customer.getBill());
93+
return customer.getBill();
94+
}
95+
}
96+
97+
public String getName() throws RemoteException
98+
{
99+
return m_name;
100+
}
101+
102+
public Customer getCustomer(int xid, int customerID) throws RemoteException
103+
{
104+
return (Customer)readData(xid, Customer.getKey(customerID));
105+
}
106+
}

0 commit comments

Comments
 (0)