diff --git a/code/Client/Client/Client.java b/code/Client/Client/Client.java index 4490742..92f7acd 100644 --- a/code/Client/Client/Client.java +++ b/code/Client/Client/Client.java @@ -223,7 +223,7 @@ public void execute(Command cmd, Vector arguments) throws RemoteExceptio System.out.println("Deleting a customer from the database [xid=" + arguments.elementAt(1) + "]"); System.out.println("-Customer ID: " + arguments.elementAt(2)); - + int id = toInt(arguments.elementAt(1)); int customerID = toInt(arguments.elementAt(2)); @@ -239,7 +239,7 @@ public void execute(Command cmd, Vector arguments) throws RemoteExceptio System.out.println("Querying a flight [xid=" + arguments.elementAt(1) + "]"); System.out.println("-Flight Number: " + arguments.elementAt(2)); - + int id = toInt(arguments.elementAt(1)); int flightNum = toInt(arguments.elementAt(2)); @@ -252,7 +252,7 @@ public void execute(Command cmd, Vector arguments) throws RemoteExceptio System.out.println("Querying cars location [xid=" + arguments.elementAt(1) + "]"); System.out.println("-Car Location: " + arguments.elementAt(2)); - + int id = toInt(arguments.elementAt(1)); String location = arguments.elementAt(2); @@ -265,7 +265,7 @@ public void execute(Command cmd, Vector arguments) throws RemoteExceptio System.out.println("Querying rooms location [xid=" + arguments.elementAt(1) + "]"); System.out.println("-Room Location: " + arguments.elementAt(2)); - + int id = toInt(arguments.elementAt(1)); String location = arguments.elementAt(2); @@ -284,11 +284,11 @@ public void execute(Command cmd, Vector arguments) throws RemoteExceptio String bill = m_resourceManager.queryCustomerInfo(id, customerID); System.out.print(bill); - break; + break; } case QueryFlightPrice: { checkArgumentsCount(3, arguments.size()); - + System.out.println("Querying a flight price [xid=" + arguments.elementAt(1) + "]"); System.out.println("-Flight Number: " + arguments.elementAt(2)); @@ -367,7 +367,7 @@ public void execute(Command cmd, Vector arguments) throws RemoteExceptio System.out.println("Reserving a room at a location [xid=" + arguments.elementAt(1) + "]"); System.out.println("-Customer ID: " + arguments.elementAt(2)); System.out.println("-Room Location: " + arguments.elementAt(3)); - + int id = toInt(arguments.elementAt(1)); int customerID = toInt(arguments.elementAt(2)); String location = arguments.elementAt(3); @@ -396,12 +396,12 @@ public void execute(Command cmd, Vector arguments) throws RemoteExceptio int id = toInt(arguments.elementAt(1)); int customerID = toInt(arguments.elementAt(2)); - Vector flightNumbers = new Vector(); + Vector flightNumbers = new Vector(); for (int i = 0; i < arguments.size() - 6; ++i) { - flightNumbers.addElement(arguments.elementAt(3+i)); + flightNumbers.addElement(toInt(arguments.elementAt(3+i))); } - String location = arguments.elementAt(arguments.size()-2); + String location = arguments.elementAt(arguments.size()-3); boolean car = toBoolean(arguments.elementAt(arguments.size()-2)); boolean room = toBoolean(arguments.elementAt(arguments.size()-1)); diff --git a/code/Client/Client/RMIClient.java b/code/Client/Client/RMIClient.java index 677d3cc..a93a67e 100644 --- a/code/Client/Client/RMIClient.java +++ b/code/Client/Client/RMIClient.java @@ -13,8 +13,8 @@ public class RMIClient extends Client { private static String s_serverHost = "localhost"; - private static int s_serverPort = 1099; - private static String s_serverName = "Server"; + private static int s_serverPort = 2005; + private static String s_serverName = "MiddlewareServer"; //TODO: REPLACE 'ALEX' WITH YOUR GROUP NUMBER TO COMPILE private static String s_rmiPrefix = "group25_"; diff --git a/code/Server/Server/Common/AbstractRMHashMapManager.java b/code/Server/Server/Common/AbstractRMHashMapManager.java index 634cabe..58673f1 100644 --- a/code/Server/Server/Common/AbstractRMHashMapManager.java +++ b/code/Server/Server/Common/AbstractRMHashMapManager.java @@ -92,41 +92,41 @@ protected int queryPrice(int xid, String key) } // Reserve an item - protected boolean reserveItem(int xid, int customerID, String key, String location) - { - Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" ); - // Read customer object if it exists (and read lock it) - Customer customer = (Customer)readData(xid, Customer.getKey(customerID)); - if (customer == null) - { - Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); - return false; - } - - // Check if the item is available - ReservableItem item = (ReservableItem)readData(xid, key); - if (item == null) - { - Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist"); - return false; - } - else if (item.getCount() == 0) - { - Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items"); - return false; - } - else - { - customer.reserve(key, location, item.getPrice()); - writeData(xid, customer.getKey(), customer); - - // Decrease the number of available items in the storage - item.setCount(item.getCount() - 1); - item.setReserved(item.getReserved() + 1); - writeData(xid, item.getKey(), item); - - Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded"); - return true; - } - } + // protected boolean reserveItem(int xid, int customerID, String key, String location) + // { + // Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" ); + // // Read customer object if it exists (and read lock it) + // Customer customer = (Customer)readData(xid, Customer.getKey(customerID)); + // if (customer == null) + // { + // Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); + // return false; + // } + // + // // Check if the item is available + // ReservableItem item = (ReservableItem)readData(xid, key); + // if (item == null) + // { + // Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist"); + // return false; + // } + // else if (item.getCount() == 0) + // { + // Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items"); + // return false; + // } + // else + // { + // customer.reserve(key, location, item.getPrice()); + // writeData(xid, customer.getKey(), customer); + // + // // Decrease the number of available items in the storage + // item.setCount(item.getCount() - 1); + // item.setReserved(item.getReserved() + 1); + // writeData(xid, item.getKey(), item); + // + // Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded"); + // return true; + // } + // } } diff --git a/code/Server/Server/Common/CarResourceManager.java b/code/Server/Server/Common/CarResourceManager.java index 83384f3..23a04af 100644 --- a/code/Server/Server/Common/CarResourceManager.java +++ b/code/Server/Server/Common/CarResourceManager.java @@ -6,10 +6,17 @@ import java.rmi.RemoteException; import java.io.*; -public class CarResourceManager extends AbstractRMHashMapManager implements ICarResourceManager +public abstract class CarResourceManager extends AbstractRMHashMapManager implements ICarResourceManager { // Create a new car location or add cars to an existing location // NOTE: if price <= 0 and the location already exists, it maintains its current price + private String m_name = ""; + protected ICustomerResourceManager customerRM; + + public CarResourceManager(String p_name) { + m_name = p_name; + }; + public boolean addCars(int xid, String location, int count, int price) throws RemoteException { Trace.info("RM::addCars(" + xid + ", " + location + ", " + count + ", $" + price + ") called"); @@ -57,4 +64,63 @@ public int queryCarsPrice(int xid, String location) throws RemoteException { return queryPrice(xid, Car.getKey(location)); } + + public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException + { + Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" ); + // Read customer object if it exists (and read lock it) + Customer customer = null; + try { + customer = getCustomer(xid, customerID); + if (customer == null) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); + return false; + } + } catch (RemoteException e) { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist--remote exception"); + return false; + } + + // Check if the item is available + ReservableItem item = (ReservableItem)readData(xid, key); + if (item == null) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist"); + return false; + } + else if (item.getCount() == 0) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items"); + return false; + } + else + { + customer.reserve(key, location, item.getPrice()); + writeData(xid, customer.getKey(), customer); + + // Decrease the number of available items in the storage + item.setCount(item.getCount() - 1); + item.setReserved(item.getReserved() + 1); + writeData(xid, item.getKey(), item); + + Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded"); + return true; + } + } + + /** + TODO public Customer getCustomer(int xid, int cid) { + customerRM.getCustomer(xid, cid) + } + */ + public Customer getCustomer(int xid, int customerID) throws RemoteException + { + return customerRM.getCustomer(xid, customerID); + } + + public String getName() throws RemoteException + { + return m_name; + } } diff --git a/code/Server/Server/Common/CustomerResourceManager.java b/code/Server/Server/Common/CustomerResourceManager.java new file mode 100644 index 0000000..1f09063 --- /dev/null +++ b/code/Server/Server/Common/CustomerResourceManager.java @@ -0,0 +1,106 @@ +package Server.Common; + +import Server.Interface.*; + +import java.util.*; +import java.rmi.RemoteException; +import java.io.*; + +public class CustomerResourceManager extends AbstractRMHashMapManager implements ICustomerResourceManager +{ + private String m_name=""; + + public CustomerResourceManager(String p_name) { + m_name = p_name; + }; + + public int newCustomer(int xid) throws RemoteException { + Trace.info("RM::newCustomer(" + xid + ") called"); +// Generate a globally unique ID for the new customer + int cid = Integer.parseInt(String.valueOf(xid) + + String.valueOf(Calendar.getInstance().get(Calendar.MILLISECOND)) + + String.valueOf(Math.round(Math.random() * 100 + 1))); + Customer customer = new Customer(cid); + writeData(xid, customer.getKey(), customer); + Trace.info("RM::newCustomer(" + cid + ") returns ID=" + cid); + return cid; + } + + public boolean newCustomer(int xid, int customerID) throws RemoteException + { + Trace.info("RM::newCustomer(" + xid + ", " + customerID + ") called"); + Customer customer = (Customer)readData(xid, Customer.getKey(customerID)); + if (customer == null) + { + customer = new Customer(customerID); + writeData(xid, customer.getKey(), customer); + Trace.info("RM::newCustomer(" + xid + ", " + customerID + ") created a new customer"); + return true; + } + else + { + Trace.info("INFO: RM::newCustomer(" + xid + ", " + customerID + ") failed--customer already exists"); + return false; + } + } + + + public boolean deleteCustomer(int xid, int customerID) throws RemoteException + { + Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") called"); + Customer customer = (Customer)readData(xid, Customer.getKey(customerID)); + if (customer == null) + { + Trace.warn("RM::deleteCustomer(" + xid + ", " + customerID + ") failed--customer doesn't exist"); + return false; + } + else + { + // Increase the reserved numbers of all reservable items which the customer reserved. + RMHashMap reservations = customer.getReservations(); + for (String reservedKey : reservations.keySet()) + { + ReservedItem reserveditem = customer.getReservedItem(reservedKey); + Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") has reserved " + reserveditem.getKey() + " " + reserveditem.getCount() + " times"); + ReservableItem item = (ReservableItem)readData(xid, reserveditem.getKey()); + Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") has reserved " + reserveditem.getKey() + " which is reserved " + item.getReserved() + " times and is still available " + item.getCount() + " times"); + item.setReserved(item.getReserved() - reserveditem.getCount()); + item.setCount(item.getCount() + reserveditem.getCount()); + writeData(xid, item.getKey(), item); + } + + // Remove the customer from the storage + removeData(xid, customer.getKey()); + Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") succeeded"); + return true; + } + } + + public String queryCustomerInfo(int xid, int customerID) throws RemoteException + { + Trace.info("RM::queryCustomerInfo(" + xid + ", " + customerID + ") called"); + Customer customer = (Customer)readData(xid, Customer.getKey(customerID)); + if (customer == null) + { + Trace.warn("RM::queryCustomerInfo(" + xid + ", " + customerID + ") failed--customer doesn't exist"); + // NOTE: don't change this--WC counts on this value indicating a customer does not exist... + return ""; + } + else + { + Trace.info("RM::queryCustomerInfo(" + xid + ", " + customerID + ")"); + System.out.println(customer.getBill()); + return customer.getBill(); + } + } + + public String getName() throws RemoteException + { + return m_name; + } + + public Customer getCustomer(int xid, int customerID) throws RemoteException + { + return (Customer)readData(xid, Customer.getKey(customerID)); + } +} diff --git a/code/Server/Server/Common/FlightResourceManager.java b/code/Server/Server/Common/FlightResourceManager.java index 8ab527e..81f0fb0 100644 --- a/code/Server/Server/Common/FlightResourceManager.java +++ b/code/Server/Server/Common/FlightResourceManager.java @@ -6,10 +6,17 @@ import java.rmi.RemoteException; import java.io.*; -public class FlightResourceManager extends AbstractRMHashMapManager implements IFlightResourceManager +public abstract class FlightResourceManager extends AbstractRMHashMapManager implements IFlightResourceManager { // Create a new flight, or add seats to existing flight // NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price + private String m_name = ""; + protected ICustomerResourceManager customerRM; + + public FlightResourceManager(String p_name) { + m_name = p_name; + } + public boolean addFlight(int xid, int flightNum, int flightSeats, int flightPrice) throws RemoteException { Trace.info("RM::addFlight(" + xid + ", " + flightNum + ", " + flightSeats + ", $" + flightPrice + ") called"); @@ -58,4 +65,58 @@ public int queryFlightPrice(int xid, int flightNum) throws RemoteException { return queryPrice(xid, Flight.getKey(flightNum)); } + + public String getName() throws RemoteException + { + return m_name; + } + + public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException + { + Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" ); + // Read customer object if it exists (and read lock it) + Customer customer = null; + try { + customer = getCustomer(xid, customerID); + if (customer == null) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); + return false; + } + } catch (RemoteException e) { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist--remote exception"); + return false; + } + + // Check if the item is available + ReservableItem item = (ReservableItem)readData(xid, key); + if (item == null) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist"); + return false; + } + else if (item.getCount() == 0) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items"); + return false; + } + else + { + customer.reserve(key, location, item.getPrice()); + writeData(xid, customer.getKey(), customer); + + // Decrease the number of available items in the storage + item.setCount(item.getCount() - 1); + item.setReserved(item.getReserved() + 1); + writeData(xid, item.getKey(), item); + + Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded"); + return true; + } + } + + public Customer getCustomer(int xid, int customerID) throws RemoteException + { + return customerRM.getCustomer(xid, customerID); + } } diff --git a/code/Server/Server/Common/MiddlewareResourceManager.java b/code/Server/Server/Common/MiddlewareResourceManager.java index 66363e1..5d80634 100644 --- a/code/Server/Server/Common/MiddlewareResourceManager.java +++ b/code/Server/Server/Common/MiddlewareResourceManager.java @@ -8,27 +8,32 @@ public abstract class MiddlewareResourceManager implements IResourceManager { - private CarResourceManager carRM; - private FlightResourceManager flightRM; - private RoomResourceManager roomRM; - + protected ICarResourceManager carRM; + protected IFlightResourceManager flightRM; + protected IRoomResourceManager roomRM; + protected ICustomerResourceManager customerRM; + private String m_name = ""; + + public MiddlewareResourceManager(String p_name) { + m_name = p_name; + } // Create a new flight, or add seats to existing flight // NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price public boolean addFlight(int xid, int flightNum, int flightSeats, int flightPrice) throws RemoteException { - flightRM.addFlight(xid, flightNum, flightSeats, flightPrice); + return flightRM.addFlight(xid, flightNum, flightSeats, flightPrice); }; // Deletes flight public boolean deleteFlight(int xid, int flightNum) throws RemoteException { - flightRM.deleteFlight(xid, flightNum); + return flightRM.deleteFlight(xid, flightNum); } // Returns the number of empty seats in this flight public int queryFlight(int xid, int flightNum) throws RemoteException { - flightRM.queryFlight(xid, flightNum); + return flightRM.queryFlight(xid, flightNum); }; // Returns price of a seat in this flight @@ -58,7 +63,7 @@ public boolean addCars(int xid, String location, int count, int price) throws Re // Delete cars at a location public boolean deleteCars(int xid, String location) throws RemoteException { - return carRM.deleteItem(xid, location); + return carRM.deleteCars(xid, location); } // Returns the number of cars available at a location @@ -70,7 +75,7 @@ public int queryCars(int xid, String location) throws RemoteException // Returns price of cars at this location public int queryCarsPrice(int xid, String location) throws RemoteException { - return carRM.queryPrice(xid, location); + return carRM.queryCarsPrice(xid, location); } // Adds room reservation to this customer @@ -104,4 +109,65 @@ public boolean deleteRooms(int xid, String location) throws RemoteException return roomRM.deleteRooms(xid, location); } + public int newCustomer(int id) throws RemoteException + { + return customerRM.newCustomer(id); + } + + public boolean newCustomer(int id, int cid) throws RemoteException + { + return customerRM.newCustomer(id, cid); + } + + public boolean deleteCustomer(int id, int customerID) throws RemoteException + { + return customerRM.deleteCustomer(id, customerID); + } + + public String queryCustomerInfo(int id, int customerID) throws RemoteException + { + return customerRM.queryCustomerInfo(id, customerID); + } + /** + * Reserve a bundle for the trip. + * + * @return Success + */ + public boolean bundle(int id, int customerID, Vector flightNumbers, String location, boolean car, boolean room) throws RemoteException + { + for (int flightNumber : flightNumbers) { + // try and reserve all flights. at first failure, return false + boolean flightReserved = flightRM.reserveFlight(id, customerID, flightNumber); + if (!flightReserved) return false; + } + + // TODO magic number until later TODO + int xid = 1; + + if (car) { + // get a reservable car. if none exist, return false. else reserve it + int numCars = carRM.queryCars(xid, location); + if (!(numCars > 0)) return false; // no cars at this location + + boolean carReserved = carRM.reserveCar(xid, customerID, location); + if (!carReserved) return false; + } + + if (room) { + // get a reservable room. if none exist, return false. else reserve it + int numRooms = roomRM.queryRooms(xid, location); + if (!(numRooms > 0)) return false; // no rooms at this location + + boolean roomReserved = roomRM.reserveRoom(xid, customerID, location); + if (!roomReserved) return false; + } + + return true; + } + + public String getName() throws RemoteException + { + return m_name; + } + } diff --git a/code/Server/Server/Common/ResourceManager.java b/code/Server/Server/Common/ResourceManager.java index 958b9a5..41f5a29 100644 --- a/code/Server/Server/Common/ResourceManager.java +++ b/code/Server/Server/Common/ResourceManager.java @@ -81,40 +81,40 @@ protected int queryNum(int xid, String key) { Trace.info("RM::queryNum(" + xid + ", " + key + ") called"); ReservableItem curObj = (ReservableItem)readData(xid, key); - int value = 0; + int value = 0; if (curObj != null) { value = curObj.getCount(); } Trace.info("RM::queryNum(" + xid + ", " + key + ") returns count=" + value); return value; - } + } // Query the price of an item protected int queryPrice(int xid, String key) { Trace.info("RM::queryPrice(" + xid + ", " + key + ") called"); ReservableItem curObj = (ReservableItem)readData(xid, key); - int value = 0; + int value = 0; if (curObj != null) { value = curObj.getPrice(); } Trace.info("RM::queryPrice(" + xid + ", " + key + ") returns cost=$" + value); - return value; + return value; } // Reserve an item protected boolean reserveItem(int xid, int customerID, String key, String location) { - Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" ); + Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" ); // Read customer object if it exists (and read lock it) Customer customer = (Customer)readData(xid, Customer.getKey(customerID)); if (customer == null) { Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); return false; - } + } // Check if the item is available ReservableItem item = (ReservableItem)readData(xid, key); @@ -129,8 +129,8 @@ else if (item.getCount() == 0) return false; } else - { - customer.reserve(key, location, item.getPrice()); + { + customer.reserve(key, location, item.getPrice()); writeData(xid, customer.getKey(), customer); // Decrease the number of available items in the storage @@ -140,7 +140,7 @@ else if (item.getCount() == 0) Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded"); return true; - } + } } // Create a new flight, or add seats to existing flight @@ -335,11 +335,11 @@ public boolean deleteCustomer(int xid, int customerID) throws RemoteException return false; } else - { - // Increase the reserved numbers of all reservable items which the customer reserved. + { + // Increase the reserved numbers of all reservable items which the customer reserved. RMHashMap reservations = customer.getReservations(); for (String reservedKey : reservations.keySet()) - { + { ReservedItem reserveditem = customer.getReservedItem(reservedKey); Trace.info("RM::deleteCustomer(" + xid + ", " + customerID + ") has reserved " + reserveditem.getKey() + " " + reserveditem.getCount() + " times"); ReservableItem item = (ReservableItem)readData(xid, reserveditem.getKey()); @@ -374,8 +374,8 @@ public boolean reserveRoom(int xid, int customerID, String location) throws Remo return reserveItem(xid, customerID, Room.getKey(location), location); } - // Reserve bundle - public boolean bundle(int xid, int customerId, Vector flightNumbers, String location, boolean car, boolean room) throws RemoteException + // Reserve bundle + public boolean bundle(int xid, int customerId, Vector flightNumbers, String location, boolean car, boolean room) throws RemoteException { return false; } @@ -385,4 +385,3 @@ public String getName() throws RemoteException return m_name; } } - diff --git a/code/Server/Server/Common/RoomResourceManager.java b/code/Server/Server/Common/RoomResourceManager.java index 09885e5..31880d9 100644 --- a/code/Server/Server/Common/RoomResourceManager.java +++ b/code/Server/Server/Common/RoomResourceManager.java @@ -6,10 +6,16 @@ import java.rmi.RemoteException; import java.io.*; -public class RoomResourceManager extends AbstractRMHashMapManager implements IRoomResourceManager +public abstract class RoomResourceManager extends AbstractRMHashMapManager implements IRoomResourceManager { // Create a new room location or add rooms to an existing location // NOTE: if price <= 0 and the room location already exists, it maintains its current price + private String m_name = ""; + protected ICustomerResourceManager customerRM; + + public RoomResourceManager(String p_name) { + m_name = p_name; + } public boolean addRooms(int xid, String location, int count, int price) throws RemoteException { Trace.info("RM::addRooms(" + xid + ", " + location + ", " + count + ", $" + price + ") called"); @@ -57,4 +63,56 @@ public boolean reserveRoom(int xid, int customerID, String location) throws Remo return reserveItem(xid, customerID, Room.getKey(location), location); } + public String getName() throws RemoteException + { + return m_name; + } + public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException + { + Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" ); + // Read customer object if it exists (and read lock it) + Customer customer = null; + try { + customer = getCustomer(xid, customerID); + if (customer == null) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); + return false; + } + } catch (RemoteException e) { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist--remote exception"); + return false; + } + + // Check if the item is available + ReservableItem item = (ReservableItem)readData(xid, key); + if (item == null) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist"); + return false; + } + else if (item.getCount() == 0) + { + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items"); + return false; + } + else + { + customer.reserve(key, location, item.getPrice()); + writeData(xid, customer.getKey(), customer); + + // Decrease the number of available items in the storage + item.setCount(item.getCount() - 1); + item.setReserved(item.getReserved() + 1); + writeData(xid, item.getKey(), item); + + Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded"); + return true; + } + } + + public Customer getCustomer(int xid, int customerID) throws RemoteException + { + return customerRM.getCustomer(xid, customerID); + } } diff --git a/code/Server/Server/Interface/ICarResourceManager.java b/code/Server/Server/Interface/ICarResourceManager.java index 9991d6f..8dde023 100644 --- a/code/Server/Server/Interface/ICarResourceManager.java +++ b/code/Server/Server/Interface/ICarResourceManager.java @@ -5,7 +5,7 @@ import java.util.*; -public interface ICarResourceManager extends Remote +public interface ICarResourceManager extends Remote, ICustomerReservationManager { /** * Reserve a car at this location. @@ -62,4 +62,3 @@ public int queryCarsPrice(int id, String location) public String getName() throws RemoteException; } -} diff --git a/code/Server/Server/Interface/ICustomerReservationManager.java b/code/Server/Server/Interface/ICustomerReservationManager.java new file mode 100644 index 0000000..a66e16a --- /dev/null +++ b/code/Server/Server/Interface/ICustomerReservationManager.java @@ -0,0 +1,15 @@ +package Server.Interface; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +import java.util.*; + +import Server.Common.Customer; + +public interface ICustomerReservationManager +{ + public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException; + + public Customer getCustomer(int xid, int customerID) throws RemoteException; +} diff --git a/code/Server/Server/Interface/ICustomerResourceManager.java b/code/Server/Server/Interface/ICustomerResourceManager.java new file mode 100644 index 0000000..f6d0d24 --- /dev/null +++ b/code/Server/Server/Interface/ICustomerResourceManager.java @@ -0,0 +1,58 @@ +package Server.Interface; + +import Server.Common.Customer; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +import java.util.*; + +public interface ICustomerResourceManager extends Remote { + /** + * Add customer. + * + * @return Unique customer identifier + */ + public int newCustomer(int id) +throws RemoteException; + +/** +* Add customer with id. +* +* @return Success +*/ + public boolean newCustomer(int id, int cid) +throws RemoteException; + + /** + * Delete a customer and associated reservations. + * + * @return Success + */ + public boolean deleteCustomer(int id, int customerID) +throws RemoteException; + + /** + * Query the customer reservations. + * + * @return A formatted bill for the customer + */ + public String queryCustomerInfo(int id, int customerID) +throws RemoteException; + +/** + * Convenience for probing the resource manager. + * + * @return Name + */ +public String getName() + throws RemoteException; + +/** + * Get a customer + * + * @return the customer + */ +public Customer getCustomer(int xid, int customerID) + throws RemoteException; +} diff --git a/code/Server/Server/Interface/IFlightResourceManager.java b/code/Server/Server/Interface/IFlightResourceManager.java index 089b5fd..6345ca0 100644 --- a/code/Server/Server/Interface/IFlightResourceManager.java +++ b/code/Server/Server/Interface/IFlightResourceManager.java @@ -5,7 +5,7 @@ import java.util.*; -public interface IFlightResourceManager extends Remote +public interface IFlightResourceManager extends Remote, ICustomerReservationManager { /** * Add seats to a flight. diff --git a/code/Server/Server/Interface/IRemoteResourceManagerGetter.java b/code/Server/Server/Interface/IRemoteResourceManagerGetter.java new file mode 100644 index 0000000..9760241 --- /dev/null +++ b/code/Server/Server/Interface/IRemoteResourceManagerGetter.java @@ -0,0 +1,10 @@ +package Server.Interface; + +import java.rmi.Remote; + +/** + * RRM = RemoteResourceManager + */ +public interface IRemoteResourceManagerGetter { + public Remote getRemoteResourceManager(String hostname, int port, String name); +} diff --git a/code/Server/Server/Interface/IResourceManager.java b/code/Server/Server/Interface/IResourceManager.java index ab81c08..713ba2e 100644 --- a/code/Server/Server/Interface/IResourceManager.java +++ b/code/Server/Server/Interface/IResourceManager.java @@ -5,23 +5,23 @@ import java.util.*; -/** +/** * Simplified version from CSE 593 Univ. of Washington * * Distributed System in Java. - * - * failure reporting is done using two pieces, exceptions and boolean + * + * failure reporting is done using two pieces, exceptions and boolean * return values. Exceptions are used for systemy things. Return * values are used for operations that would affect the consistency - * - * If there is a boolean return value and you're not sure how it + * + * If there is a boolean return value and you're not sure how it * would be used in your implementation, ignore it. I used boolean - * return values in the interface generously to allow flexibility in + * return values in the interface generously to allow flexibility in * implementation. But don't forget to return true when the operation * has succeeded. */ -public interface IResourceManager extends Remote +public interface IResourceManager extends Remote { /** * Add seats to a flight. @@ -33,9 +33,9 @@ public interface IResourceManager extends Remote * * @return Success */ - public boolean addFlight(int id, int flightNum, int flightSeats, int flightPrice) - throws RemoteException; - + public boolean addFlight(int id, int flightNum, int flightSeats, int flightPrice) + throws RemoteException; + /** * Add car at a location. * @@ -44,9 +44,9 @@ public boolean addFlight(int id, int flightNum, int flightSeats, int flightPrice * * @return Success */ - public boolean addCars(int id, String location, int numCars, int price) - throws RemoteException; - + public boolean addCars(int id, String location, int numCars, int price) + throws RemoteException; + /** * Add room at a location. * @@ -55,17 +55,17 @@ public boolean addCars(int id, String location, int numCars, int price) * * @return Success */ - public boolean addRooms(int id, String location, int numRooms, int price) - throws RemoteException; - + public boolean addRooms(int id, String location, int numRooms, int price) + throws RemoteException; + /** * Add customer. * * @return Unique customer identifier */ - public int newCustomer(int id) - throws RemoteException; - + public int newCustomer(int id) + throws RemoteException; + /** * Add customer with id. * @@ -81,19 +81,19 @@ public boolean newCustomer(int id, int cid) * reservation on the flight, then the flight cannot be deleted * * @return Success - */ - public boolean deleteFlight(int id, int flightNum) - throws RemoteException; - + */ + public boolean deleteFlight(int id, int flightNum) + throws RemoteException; + /** * Delete all cars at a location. * * It may not succeed if there are reservations for this location * * @return Success - */ - public boolean deleteCars(int id, String location) - throws RemoteException; + */ + public boolean deleteCars(int id, String location) + throws RemoteException; /** * Delete all rooms at a location. @@ -102,104 +102,104 @@ public boolean deleteCars(int id, String location) * * @return Success */ - public boolean deleteRooms(int id, String location) - throws RemoteException; - + public boolean deleteRooms(int id, String location) + throws RemoteException; + /** * Delete a customer and associated reservations. * * @return Success */ - public boolean deleteCustomer(int id, int customerID) - throws RemoteException; + public boolean deleteCustomer(int id, int customerID) + throws RemoteException; /** * Query the status of a flight. * * @return Number of empty seats */ - public int queryFlight(int id, int flightNumber) - throws RemoteException; + public int queryFlight(int id, int flightNumber) + throws RemoteException; /** * Query the status of a car location. * * @return Number of available cars at this location */ - public int queryCars(int id, String location) - throws RemoteException; + public int queryCars(int id, String location) + throws RemoteException; /** * Query the status of a room location. * * @return Number of available rooms at this location */ - public int queryRooms(int id, String location) - throws RemoteException; + public int queryRooms(int id, String location) + throws RemoteException; /** * Query the customer reservations. * * @return A formatted bill for the customer */ - public String queryCustomerInfo(int id, int customerID) - throws RemoteException; - + public String queryCustomerInfo(int id, int customerID) + throws RemoteException; + /** * Query the status of a flight. * * @return Price of a seat in this flight */ - public int queryFlightPrice(int id, int flightNumber) - throws RemoteException; + public int queryFlightPrice(int id, int flightNumber) + throws RemoteException; /** * Query the status of a car location. * * @return Price of car */ - public int queryCarsPrice(int id, String location) - throws RemoteException; + public int queryCarsPrice(int id, String location) + throws RemoteException; /** * Query the status of a room location. * * @return Price of a room */ - public int queryRoomsPrice(int id, String location) - throws RemoteException; + public int queryRoomsPrice(int id, String location) + throws RemoteException; /** * Reserve a seat on this flight. * * @return Success */ - public boolean reserveFlight(int id, int customerID, int flightNumber) - throws RemoteException; + public boolean reserveFlight(int id, int customerID, int flightNumber) + throws RemoteException; /** * Reserve a car at this location. * * @return Success */ - public boolean reserveCar(int id, int customerID, String location) - throws RemoteException; + public boolean reserveCar(int id, int customerID, String location) + throws RemoteException; /** * Reserve a room at this location. * * @return Success */ - public boolean reserveRoom(int id, int customerID, String location) - throws RemoteException; + public boolean reserveRoom(int id, int customerID, String location) + throws RemoteException; /** * Reserve a bundle for the trip. * * @return Success */ - public boolean bundle(int id, int customerID, Vector flightNumbers, String location, boolean car, boolean room) - throws RemoteException; + public boolean bundle(int id, int customerID, Vector flightNumbers, String location, boolean car, boolean room) + throws RemoteException; /** * Convenience for probing the resource manager. diff --git a/code/Server/Server/Interface/IRoomResourceManager.java b/code/Server/Server/Interface/IRoomResourceManager.java index 82de81a..5e79cfb 100644 --- a/code/Server/Server/Interface/IRoomResourceManager.java +++ b/code/Server/Server/Interface/IRoomResourceManager.java @@ -5,7 +5,7 @@ import java.util.*; -public interface IRoomResourceManager extends Remote +public interface IRoomResourceManager extends Remote, ICustomerReservationManager { /** * Add room at a location. diff --git a/code/Server/Server/RMI/RMICarResourceManager.java b/code/Server/Server/RMI/RMICarResourceManager.java index d004618..a8f0eaf 100644 --- a/code/Server/Server/RMI/RMICarResourceManager.java +++ b/code/Server/Server/RMI/RMICarResourceManager.java @@ -11,6 +11,7 @@ import java.rmi.NotBoundException; import java.util.*; +import java.rmi.Remote; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; @@ -22,7 +23,7 @@ // RNHashMap takes a key and stores the object as the value // Read data simply pulls object values from this RNHashMap -public class RMICarResourceManager extends CarResourceManager +public class RMICarResourceManager extends CarResourceManager implements IRemoteResourceManagerGetter { private static String s_serverName = "CarServer"; //TODO: REPLACE 'ALEX' WITH YOUR GROUP NUMBER TO COMPILE @@ -40,6 +41,8 @@ public static void main(String args[]) // Create a new Server object RMICarResourceManager server = new RMICarResourceManager(s_serverName); + server.customerRM = (ICustomerResourceManager)server.getRemoteResourceManager("localhost", 2003, "CustomerServer"); + // Dynamically generate the stub (client proxy) ICarResourceManager resourceManager = (ICarResourceManager)UnicastRemoteObject.exportObject(server, 0); @@ -79,6 +82,33 @@ public void run() { System.setSecurityManager(new SecurityManager()); } } + public Remote getRemoteResourceManager(String hostname, int port, String name) { + Remote remoteResourceManager = null; + try { + boolean first = true; + while (true) { + try { + Registry registry = LocateRegistry.getRegistry(hostname, port); + remoteResourceManager = registry.lookup(s_rmiPrefix + name); + System.out.println("Connected to '" + name + "' server [" + hostname + ":" + port + "/" + s_rmiPrefix + name + "]"); + break; + } + catch (NotBoundException|RemoteException e) { + if (first) { + System.out.println("Waiting for '" + name + "' server [" + hostname + ":" + port + "/" + s_rmiPrefix + name + "]"); + first = false; + } + } + Thread.sleep(500); + } + } + catch (Exception e) { + System.err.println((char)27 + "[31;1mServer exception: " + (char)27 + "[0mUncaught exception"); + e.printStackTrace(); + System.exit(1); + } + return remoteResourceManager; // this line only reached if success + } public RMICarResourceManager(String name) { diff --git a/code/Server/Server/RMI/RMICustomerResourceManager.java b/code/Server/Server/RMI/RMICustomerResourceManager.java new file mode 100644 index 0000000..52bbafe --- /dev/null +++ b/code/Server/Server/RMI/RMICustomerResourceManager.java @@ -0,0 +1,87 @@ +// ------------------------------- +// adapted from Kevin T. Manley +// CSE 593 +// ------------------------------- + +package Server.RMI; + +import Server.Interface.*; +import Server.Common.*; + +import java.rmi.NotBoundException; +import java.util.*; + +import java.rmi.registry.Registry; +import java.rmi.registry.LocateRegistry; +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; + +// RMIResourceManager is a whole class containing a registry and references to objects +// These objects can be created through "creators", i.e. addFlight, addCars, addRooms +// After creation or updates, through creators/setters, the object is written to RNHashMap +// RNHashMap takes a key and stores the object as the value +// Read data simply pulls object values from this RNHashMap + +public class RMICustomerResourceManager extends CustomerResourceManager +{ + private static String s_serverName = "CustomerServer"; + //TODO: REPLACE 'ALEX' WITH YOUR GROUP NUMBER TO COMPILE + private static String s_rmiPrefix = "group25_"; + + public static void main(String args[]) + { + if (args.length > 0) + { + s_serverName = args[0]; + } + + // Create the RMI server entry + try { + // Create a new Server object + RMICustomerResourceManager server = new RMICustomerResourceManager(s_serverName); + + // Dynamically generate the stub (client proxy) + ICustomerResourceManager resourceManager = (ICustomerResourceManager)UnicastRemoteObject.exportObject(server, 0); + + // Bind the remote object's stub in the registry + Registry l_registry; + try { + l_registry = LocateRegistry.createRegistry(2003); + } catch (RemoteException e) { + l_registry = LocateRegistry.getRegistry(2003); + } + final Registry registry = l_registry; + registry.rebind(s_rmiPrefix + s_serverName, resourceManager); + + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + try { + registry.unbind(s_rmiPrefix + s_serverName); + System.out.println("'" + s_serverName + "' resource manager unbound"); + } + catch(Exception e) { + System.err.println((char)27 + "[31;1mServer exception: " + (char)27 + "[0mUncaught exception"); + e.printStackTrace(); + } + } + }); + System.out.println("'" + s_serverName + "' resource manager server ready and bound to '" + s_rmiPrefix + s_serverName + "'"); + } + catch (Exception e) { + System.err.println((char)27 + "[31;1mServer exception: " + (char)27 + "[0mUncaught exception"); + e.printStackTrace(); + System.exit(1); + } + + // Create and install a security manager + if (System.getSecurityManager() == null) + { + System.setSecurityManager(new SecurityManager()); + } + } + + public RMICustomerResourceManager(String name) + { + super(name); + } +} diff --git a/code/Server/Server/RMI/RMIFlightResourceManager.java b/code/Server/Server/RMI/RMIFlightResourceManager.java index 980147b..ba9d67e 100644 --- a/code/Server/Server/RMI/RMIFlightResourceManager.java +++ b/code/Server/Server/RMI/RMIFlightResourceManager.java @@ -11,6 +11,7 @@ import java.rmi.NotBoundException; import java.util.*; +import java.rmi.Remote; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; @@ -22,7 +23,7 @@ // RNHashMap takes a key and stores the object as the value // Read data simply pulls object values from this RNHashMap -public class RMIFlightResourceManager extends FlightResourceManager +public class RMIFlightResourceManager extends FlightResourceManager implements IRemoteResourceManagerGetter { private static String s_serverName = "FlightServer"; //TODO: REPLACE 'ALEX' WITH YOUR GROUP NUMBER TO COMPILE @@ -38,7 +39,9 @@ public static void main(String args[]) // Create the RMI server entry try { // Create a new Server object - RMIFlightResourceManager server = new RMIFliResourceManager(s_serverName); + RMIFlightResourceManager server = new RMIFlightResourceManager(s_serverName); + + server.customerRM = (ICustomerResourceManager)server.getRemoteResourceManager("localhost", 2003, "CustomerServer"); // Dynamically generate the stub (client proxy) IFlightResourceManager resourceManager = (IFlightResourceManager)UnicastRemoteObject.exportObject(server, 0); @@ -79,6 +82,33 @@ public void run() { System.setSecurityManager(new SecurityManager()); } } + public Remote getRemoteResourceManager(String hostname, int port, String name) { + Remote remoteResourceManager = null; + try { + boolean first = true; + while (true) { + try { + Registry registry = LocateRegistry.getRegistry(hostname, port); + remoteResourceManager = registry.lookup(s_rmiPrefix + name); + System.out.println("Connected to '" + name + "' server [" + hostname + ":" + port + "/" + s_rmiPrefix + name + "]"); + break; + } + catch (NotBoundException|RemoteException e) { + if (first) { + System.out.println("Waiting for '" + name + "' server [" + hostname + ":" + port + "/" + s_rmiPrefix + name + "]"); + first = false; + } + } + Thread.sleep(500); + } + } + catch (Exception e) { + System.err.println((char)27 + "[31;1mServer exception: " + (char)27 + "[0mUncaught exception"); + e.printStackTrace(); + System.exit(1); + } + return remoteResourceManager; // this line only reached if success + } public RMIFlightResourceManager(String name) { diff --git a/code/Server/Server/RMI/RMIMiddlewareResourceManager.java b/code/Server/Server/RMI/RMIMiddlewareResourceManager.java index 6f2b44a..0857a7b 100644 --- a/code/Server/Server/RMI/RMIMiddlewareResourceManager.java +++ b/code/Server/Server/RMI/RMIMiddlewareResourceManager.java @@ -9,6 +9,7 @@ import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; +import java.rmi.Remote; import java.rmi.server.UnicastRemoteObject; public class RMIMiddlewareResourceManager extends MiddlewareResourceManager @@ -24,10 +25,6 @@ public static void main(String args[]) s_serverName = args[0]; } - // get car, flight, room resourceManagers - // @TODO add parameters - connectAllServers("localhost", 2000, "FlightServer", "localhost", 2001, "CarServer", "localhost", 2002, "RoomServer"); - // Create the RMI server entry try { // Create a new Server object @@ -39,9 +36,9 @@ public static void main(String args[]) // Bind the remote object's stub in the registry Registry l_registry; try { - l_registry = LocateRegistry.createRegistry(1099); + l_registry = LocateRegistry.createRegistry(2005); } catch (RemoteException e) { - l_registry = LocateRegistry.getRegistry(1099); + l_registry = LocateRegistry.getRegistry(2005); } final Registry registry = l_registry; registry.rebind(s_rmiPrefix + s_serverName, resourceManager); @@ -59,6 +56,16 @@ public void run() { } }); System.out.println("'" + s_serverName + "' resource manager server ready and bound to '" + s_rmiPrefix + s_serverName + "'"); + + + // get car, flight, resourceManagers + // @TODO add parameters + // server.connectAllServers("localhost", 2000, "FlightServer", "localhost", 2001, "CarServer", "localhost", 2002, "Server"); + // server.connectCustomerServer("localhost", 2003, "CustomerServer"); + server.flightRM = (IFlightResourceManager)server.getRemoteResourceManager("localhost", 2000, "FlightServer"); + server.carRM = (ICarResourceManager)server.getRemoteResourceManager("localhost", 2001, "CarServer"); + server.roomRM = (IRoomResourceManager)server.getRemoteResourceManager("localhost", 2002, "RoomServer"); + server.customerRM = (ICustomerResourceManager)server.getRemoteResourceManager("localhost", 2003, "CustomerServer"); } catch (Exception e) { System.err.println((char)27 + "[31;1mServer exception: " + (char)27 + "[0mUncaught exception"); @@ -73,53 +80,25 @@ public void run() { } } - public RMIResourceManager(String name) + public RMIMiddlewareResourceManager(String name) { super(name); } - public void connectAllServers(String serverFlight, int portFlight, String nameFlight, - String serverCar, int portCar, String nameCar, - String serverRoom, int portRoom, String nameRoom) - { - // connect to flights server - try { - boolean first = true; - while (true) { - try { - Registry registry = LocateRegistry.getRegistry(serverFlight, portFlight); - flightRM = (IFlightResourceManager)registry.lookup(s_rmiPrefix + nameFlight); - System.out.println("Connected to '" + nameFlight + "' server [" + serverFlight + ":" + portFlight + "/" + s_rmiPrefix + nameFlight + "]"); - break; - } - catch (NotBoundException|RemoteException e) { - if (first) { - System.out.println("Waiting for '" + nameFlight + "' server [" + serverFlight + ":" + portFlight + "/" + s_rmiPrefix + nameFlight + "]"); - first = false; - } - } - Thread.sleep(500); - } - } - catch (Exception e) { - System.err.println((char)27 + "[31;1mServer exception: " + (char)27 + "[0mUncaught exception"); - e.printStackTrace(); - System.exit(1); - } - - // connect to cars server + public Remote getRemoteResourceManager(String server, int port, String name) { + Remote remoteResourceManager = null; try { boolean first = true; while (true) { try { - Registry registry = LocateRegistry.getRegistry(serverCar, portCar); - carRM = (ICarResourceManager)registry.lookup(s_rmiPrefix + nameCar); - System.out.println("Connected to '" + nameCar + "' server [" + serverCar + ":" + portCar + "/" + s_rmiPrefix + nameCar + "]"); + Registry registry = LocateRegistry.getRegistry(server, port); + remoteResourceManager = registry.lookup(s_rmiPrefix + name); + System.out.println("Connected to '" + name + "' server [" + server + ":" + port + "/" + s_rmiPrefix + name + "]"); break; } catch (NotBoundException|RemoteException e) { if (first) { - System.out.println("Waiting for '" + nameCar + "' server [" + serverCar + ":" + portCar + "/" + s_rmiPrefix + nameCar + "]"); + System.out.println("Waiting for '" + name + "' server [" + server + ":" + port + "/" + s_rmiPrefix + name + "]"); first = false; } } @@ -131,30 +110,6 @@ public void connectAllServers(String serverFlight, int portFlight, String nameFl e.printStackTrace(); System.exit(1); } - - // connect rooms server - try { - boolean first = true; - while (true) { - try { - Registry registry = LocateRegistry.getRegistry(serverRoom, portRoom); - roomRM = (IRoomResourceManager)registry.lookup(s_rmiPrefix + nameRoom); - System.out.println("Connected to '" + nameRoom + "' server [" + serverRoom + ":" + portRoom + "/" + s_rmiPrefix + nameRoom + "]"); - break; - } - catch (NotBoundException|RemoteException e) { - if (first) { - System.out.println("Waiting for '" + nameRoom + "' server [" + serverRoom + ":" + portRoom + "/" + s_rmiPrefix + nameRoom + "]"); - first = false; - } - } - Thread.sleep(500); - } - } - catch (Exception e) { - System.err.println((char)27 + "[31;1mServer exception: " + (char)27 + "[0mUncaught exception"); - e.printStackTrace(); - System.exit(1); - } - } + return remoteResourceManager; // this line only reached if success + } } diff --git a/code/Server/Server/RMI/RMIRoomResourceManager.java b/code/Server/Server/RMI/RMIRoomResourceManager.java index b9a6af5..f7822bf 100644 --- a/code/Server/Server/RMI/RMIRoomResourceManager.java +++ b/code/Server/Server/RMI/RMIRoomResourceManager.java @@ -11,6 +11,7 @@ import java.rmi.NotBoundException; import java.util.*; +import java.rmi.Remote; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; @@ -22,7 +23,7 @@ // RNHashMap takes a key and stores the object as the value // Read data simply pulls object values from this RNHashMap -public class RMIRoomResourceManager extends RoomResourceManager +public class RMIRoomResourceManager extends RoomResourceManager implements IRemoteResourceManagerGetter { private static String s_serverName = "RoomServer"; //TODO: REPLACE 'ALEX' WITH YOUR GROUP NUMBER TO COMPILE @@ -40,6 +41,8 @@ public static void main(String args[]) // Create a new Server object RMIRoomResourceManager server = new RMIRoomResourceManager(s_serverName); + server.customerRM = (ICustomerResourceManager)server.getRemoteResourceManager("localhost", 2003, "CustomerServer"); + // Dynamically generate the stub (client proxy) IRoomResourceManager resourceManager = (IRoomResourceManager)UnicastRemoteObject.exportObject(server, 0); @@ -80,7 +83,35 @@ public void run() { } } - public RMICarResourceManager(String name) + public Remote getRemoteResourceManager(String hostname, int port, String name) { + Remote remoteResourceManager = null; + try { + boolean first = true; + while (true) { + try { + Registry registry = LocateRegistry.getRegistry(hostname, port); + remoteResourceManager = registry.lookup(s_rmiPrefix + name); + System.out.println("Connected to '" + name + "' server [" + hostname + ":" + port + "/" + s_rmiPrefix + name + "]"); + break; + } + catch (NotBoundException|RemoteException e) { + if (first) { + System.out.println("Waiting for '" + name + "' server [" + hostname + ":" + port + "/" + s_rmiPrefix + name + "]"); + first = false; + } + } + Thread.sleep(500); + } + } + catch (Exception e) { + System.err.println((char)27 + "[31;1mServer exception: " + (char)27 + "[0mUncaught exception"); + e.printStackTrace(); + System.exit(1); + } + return remoteResourceManager; // this line only reached if success + } + + public RMIRoomResourceManager(String name) { super(name); } diff --git a/code/Server/kill_servers.sh b/code/Server/kill_servers.sh new file mode 100755 index 0000000..9fe4d00 --- /dev/null +++ b/code/Server/kill_servers.sh @@ -0,0 +1,5 @@ +# kill all RMIXXXResourceManagers +ps aux | egrep 'java .*RMI.*Manager' | grep -v 'grep' | awk '{print $2}' | xargs kill + +# kill all rmiregistries +ps aux | egrep 'rmiregistry' | egrep 'smasha2|ndevas' | grep -v 'grep' | awk '{print $2}' | xargs kill diff --git a/code/Server/run_middleware.sh b/code/Server/run_middleware.sh index befe15f..f24cf0f 100755 --- a/code/Server/run_middleware.sh +++ b/code/Server/run_middleware.sh @@ -1,8 +1,8 @@ -./run_rmi.sh > /dev/null +./run_rmi.sh 2005 > /dev/null echo "Edit file run_middleware.sh to include instructions for launching the middleware" echo ' $1 - hostname of Flights' echo ' $2 - hostname of Cars' echo ' $3 - hostname of Rooms' -# java -Djava.security.policy=java.policy -Djava.rmi.server.codebase=file:$(pwd)/ Server.RMI.RMIMiddleware $1 $2 $3 +java -Djava.security.policy=java.policy -Djava.rmi.server.codebase=file:$(pwd)/ Server.RMI.RMIMiddlewareResourceManager # $1 $2 $3 $4 diff --git a/code/Server/run_rmi.sh b/code/Server/run_rmi.sh index a0ebd26..29e1a36 100755 --- a/code/Server/run_rmi.sh +++ b/code/Server/run_rmi.sh @@ -1 +1 @@ -rmiregistry -J-Djava.rmi.server.useCodebaseOnly=false 1099 & +rmiregistry -J-Djava.rmi.server.useCodebaseOnly=false $1 & diff --git a/code/Server/run_server.sh b/code/Server/run_server.sh index e974ea7..55d054a 100755 --- a/code/Server/run_server.sh +++ b/code/Server/run_server.sh @@ -1,4 +1,7 @@ #Usage: ./run_server.sh [] -./run_rmi.sh > /dev/null 2>&1 -java -Djava.security.policy=java.policy -Djava.rmi.server.codebase=file:$(pwd)/ Server.RMI.RMIResourceManager $1 +# $1 registry port +# $2 resource manager name + +./run_rmi.sh $1 > /dev/null 2>&1 +java -Djava.security.policy=java.policy -Djava.rmi.server.codebase=file:$(pwd)/ Server.RMI.RMI$2ResourceManager diff --git a/code/Server/run_servers.sh b/code/Server/run_servers.sh index 0aeac7c..203049a 100755 --- a/code/Server/run_servers.sh +++ b/code/Server/run_servers.sh @@ -1,18 +1,60 @@ -#!/bin/bash +#!/bin/bash #TODO: SPECIFY THE HOSTNAMES OF 4 CS MACHINES (lab1-1, cs-2, etc...) -MACHINES=() +# MACHINES=() +# +# tmux new-session \; \ +# split-window -v \; \ +# split-window -h \; \ +# split-window -h \; \ +# select-pane -t 1 \; \ +# split-window -h \; \ +# select-pane -t 0 \; \ +# split-window -h \; \ +# select-pane -t 5 \; \ +# send-keys "ssh -t ${MACHINES[0]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; ./run_server.sh 2003 Customer\"" C-m \; \ +# select-pane -t 4 \; \ +# send-keys "ssh -t ${MACHINES[0]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; ./run_server.sh 2000 Flight\"" C-m \; \ +# select-pane -t 3 \; \ +# send-keys "ssh -t ${MACHINES[1]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; ./run_server.sh 2001 Car\"" C-m \; \ +# select-pane -t 2 \; \ +# send-keys "ssh -t ${MACHINES[2]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; ./run_server.sh 2002 Room\"" C-m \; \ +# select-pane -t 1 \; \ +# send-keys "ssh -t ${MACHINES[3]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; sleep .5s; ./run_middleware.sh ${MACHINES[0]} ${MACHINES[1]} ${MACHINES[2]}\"" C-m \; -tmux new-session \; \ - split-window -h \; \ - split-window -v \; \ - split-window -v \; \ - select-layout main-vertical \; \ - select-pane -t 2 \; \ - send-keys "ssh -t ${MACHINES[0]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; ./run_server.sh Flights\"" C-m \; \ - select-pane -t 3 \; \ - send-keys "ssh -t ${MACHINES[1]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; ./run_server.sh Cars\"" C-m \; \ - select-pane -t 4 \; \ - send-keys "ssh -t ${MACHINES[2]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; ./run_server.sh Rooms\"" C-m \; \ - select-pane -t 1 \; \ - send-keys "ssh -t ${MACHINES[3]} \"cd $(pwd) > /dev/null; echo -n 'Connected to '; hostname; sleep .5s; ./run_middleware.sh ${MACHINES[0]} ${MACHINES[1]} ${MACHINES[2]}\"" C-m \; +#------------------------------------------ +# Just the localhost version +#------------------------------------------ + +if [ -z "$TMUX" ] # if not in tmux session +then + tmux new-session \; \ + split-window -v \; \ + split-window -h \; \ + split-window -h \; \ + select-pane -t 1 \; \ + split-window -h \; \ + select-pane -t 0 \; \ + split-window -h \; \ + select-pane -t 5 \; \ + send-keys "./run_server.sh 2003 Customer" C-m \; \ + select-pane -t 4 \; \ + send-keys "./run_server.sh 2000 Flight" C-m \; \ + select-pane -t 3 \; \ + send-keys "./run_server.sh 2001 Car" C-m \; \ + select-pane -t 2 \; \ + send-keys "./run_server.sh 2002 Room" C-m \; \ + select-pane -t 1 \; \ + send-keys "./run_middleware.sh" C-m \; +else + tmux select-pane -t 5 \; \ + send-keys "./run_server.sh 2003 Customer" C-m \; \ + select-pane -t 4 \; \ + send-keys "./run_server.sh 2000 Flight" C-m \; \ + select-pane -t 3 \; \ + send-keys "./run_server.sh 2001 Car" C-m \; \ + select-pane -t 2 \; \ + send-keys "./run_server.sh 2002 Room" C-m \; \ + select-pane -t 1 \; \ + send-keys "./run_middleware.sh" C-m \; +fi