Skip to content

Commit ea44526

Browse files
committed
finish first half of milestone1: distribute resource managers
1 parent c21e9fd commit ea44526

18 files changed

+313
-118
lines changed

code/Client/java.policy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
grant codebase "file:/home/2016/ndevas/comp512/Project1/DistributedSystemsProject/code/Client/" {
1+
grant codebase "file:/home/2015/smasha2/Documents/DistributedSystemsProject/code/Client/" {
22
permission java.security.AllPermission;
33
};

code/Server/Server/Common/CarResourceManager.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import java.rmi.RemoteException;
77
import java.io.*;
88

9-
public class CarResourceManager extends AbstractRMHashMapManager implements ICarResourceManager, ICustomerReservationManager
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
1313
private String m_name = "";
14-
// private carRM = new CarResourceManager();
14+
protected ICustomerResourceManager customerRM;
1515

1616
public CarResourceManager(String p_name) {
1717
m_name = p_name;
@@ -65,15 +65,22 @@ public int queryCarsPrice(int xid, String location) throws RemoteException
6565
return queryPrice(xid, Car.getKey(location));
6666
}
6767

68-
public boolean reserveItem(int xid, int customerID, String key, String location) {
68+
public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException
69+
{
6970
Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" );
7071
// Read customer object if it exists (and read lock it)
71-
Customer customer = getCustomer(xid, Customer.getKey(customerID));
72-
if (customer == null)
73-
{
74-
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist");
75-
return false;
76-
}
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+
}
7784

7885
// Check if the item is available
7986
ReservableItem item = (ReservableItem)readData(xid, key);
@@ -107,6 +114,11 @@ TODO public Customer getCustomer(int xid, int cid) {
107114
customerRM.getCustomer(xid, cid)
108115
}
109116
*/
117+
public Customer getCustomer(int xid, int customerID) throws RemoteException
118+
{
119+
return customerRM.getCustomer(xid, customerID);
120+
}
121+
110122
public String getName() throws RemoteException
111123
{
112124
return m_name;

code/Server/Server/Common/CustomerResourceManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public boolean newCustomer(int xid, int customerID) throws RemoteException
4343
return false;
4444
}
4545
}
46-
// TODO public Customer getCUstomer(int xid, int customerID) {} TODO
46+
4747

4848
public boolean deleteCustomer(int xid, int customerID) throws RemoteException
4949
{
@@ -98,4 +98,9 @@ public String getName() throws RemoteException
9898
{
9999
return m_name;
100100
}
101+
102+
public Customer getCustomer(int xid, int customerID) throws RemoteException
103+
{
104+
return (Customer)readData(xid, Customer.getKey(customerID));
105+
}
101106
}

code/Server/Server/Common/FlightResourceManager.java

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

9-
public class FlightResourceManager extends AbstractRMHashMapManager implements IFlightResourceManager
9+
public abstract class FlightResourceManager extends AbstractRMHashMapManager implements IFlightResourceManager
1010
{
1111
// Create a new flight, or add seats to existing flight
1212
// NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price
1313
private String m_name = "";
14+
protected ICustomerResourceManager customerRM;
15+
1416
public FlightResourceManager(String p_name) {
1517
m_name = p_name;
1618
}
@@ -68,4 +70,53 @@ public String getName() throws RemoteException
6870
{
6971
return m_name;
7072
}
73+
74+
public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException
75+
{
76+
Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" );
77+
// Read customer object if it exists (and read lock it)
78+
Customer customer = null;
79+
try {
80+
customer = getCustomer(xid, customerID);
81+
if (customer == null)
82+
{
83+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist");
84+
return false;
85+
}
86+
} catch (RemoteException e) {
87+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist--remote exception");
88+
return false;
89+
}
90+
91+
// Check if the item is available
92+
ReservableItem item = (ReservableItem)readData(xid, key);
93+
if (item == null)
94+
{
95+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist");
96+
return false;
97+
}
98+
else if (item.getCount() == 0)
99+
{
100+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items");
101+
return false;
102+
}
103+
else
104+
{
105+
customer.reserve(key, location, item.getPrice());
106+
writeData(xid, customer.getKey(), customer);
107+
108+
// Decrease the number of available items in the storage
109+
item.setCount(item.getCount() - 1);
110+
item.setReserved(item.getReserved() + 1);
111+
writeData(xid, item.getKey(), item);
112+
113+
Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded");
114+
return true;
115+
}
116+
}
117+
118+
public Customer getCustomer(int xid, int customerID) throws RemoteException
119+
{
120+
return customerRM.getCustomer(xid, customerID);
121+
}
71122
}

code/Server/Server/Common/RoomResourceManager.java

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

9-
public class RoomResourceManager extends AbstractRMHashMapManager implements IRoomResourceManager
9+
public abstract class RoomResourceManager extends AbstractRMHashMapManager implements IRoomResourceManager
1010
{
1111
// Create a new room location or add rooms to an existing location
1212
// NOTE: if price <= 0 and the room location already exists, it maintains its current price
1313
private String m_name = "";
14+
protected ICustomerResourceManager customerRM;
15+
1416
public RoomResourceManager(String p_name) {
1517
m_name = p_name;
1618
}
@@ -65,4 +67,52 @@ public String getName() throws RemoteException
6567
{
6668
return m_name;
6769
}
70+
public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException
71+
{
72+
Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" );
73+
// Read customer object if it exists (and read lock it)
74+
Customer customer = null;
75+
try {
76+
customer = getCustomer(xid, customerID);
77+
if (customer == null)
78+
{
79+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist");
80+
return false;
81+
}
82+
} catch (RemoteException e) {
83+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist--remote exception");
84+
return false;
85+
}
86+
87+
// Check if the item is available
88+
ReservableItem item = (ReservableItem)readData(xid, key);
89+
if (item == null)
90+
{
91+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist");
92+
return false;
93+
}
94+
else if (item.getCount() == 0)
95+
{
96+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items");
97+
return false;
98+
}
99+
else
100+
{
101+
customer.reserve(key, location, item.getPrice());
102+
writeData(xid, customer.getKey(), customer);
103+
104+
// Decrease the number of available items in the storage
105+
item.setCount(item.getCount() - 1);
106+
item.setReserved(item.getReserved() + 1);
107+
writeData(xid, item.getKey(), item);
108+
109+
Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded");
110+
return true;
111+
}
112+
}
113+
114+
public Customer getCustomer(int xid, int customerID) throws RemoteException
115+
{
116+
return customerRM.getCustomer(xid, customerID);
117+
}
68118
}

code/Server/Server/Interface/ICarResourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import java.util.*;
77

8-
public interface ICarResourceManager extends Remote
8+
public interface ICarResourceManager extends Remote, ICustomerReservationManager
99
{
1010
/**
1111
* Reserve a car at this location.

code/Server/Server/Interface/ICustomerReservationManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public interface ICustomerReservationManager
1111
{
12-
public boolean reserveItem(int xid, int customerID, String key, String location);
12+
public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException;
1313

14-
public Customer getCustomer(int xid, int customerID);
14+
public Customer getCustomer(int xid, int customerID) throws RemoteException;
1515
}

code/Server/Server/Interface/ICustomerResourceManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package Server.Interface;
22

3+
import Server.Common.Customer;
4+
35
import java.rmi.Remote;
46
import java.rmi.RemoteException;
57

@@ -45,4 +47,12 @@ public String queryCustomerInfo(int id, int customerID)
4547
*/
4648
public String getName()
4749
throws RemoteException;
50+
51+
/**
52+
* Get a customer
53+
*
54+
* @return the customer
55+
*/
56+
public Customer getCustomer(int xid, int customerID)
57+
throws RemoteException;
4858
}

code/Server/Server/Interface/IFlightResourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import java.util.*;
77

8-
public interface IFlightResourceManager extends Remote
8+
public interface IFlightResourceManager extends Remote, ICustomerReservationManager
99
{
1010
/**
1111
* Add seats to a flight.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package Server.Interface;
2+
3+
import java.rmi.Remote;
4+
5+
/**
6+
* RRM = RemoteResourceManager
7+
*/
8+
public interface IRemoteResourceManagerGetter {
9+
public Remote getRemoteResourceManager(String hostname, int port, String name);
10+
}

0 commit comments

Comments
 (0)