Skip to content

Commit

Permalink
Merge branch 'fix-bugs-to-run' into distribute-resource-managers
Browse files Browse the repository at this point in the history
  • Loading branch information
stumash committed Sep 30, 2018
2 parents 2027ed8 + ea44526 commit 99dc2ff
Show file tree
Hide file tree
Showing 26 changed files with 847 additions and 226 deletions.
20 changes: 10 additions & 10 deletions code/Client/Client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void execute(Command cmd, Vector<String> 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));

Expand All @@ -239,7 +239,7 @@ public void execute(Command cmd, Vector<String> 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));

Expand All @@ -252,7 +252,7 @@ public void execute(Command cmd, Vector<String> 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);

Expand All @@ -265,7 +265,7 @@ public void execute(Command cmd, Vector<String> 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);

Expand All @@ -284,11 +284,11 @@ public void execute(Command cmd, Vector<String> 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));

Expand Down Expand Up @@ -367,7 +367,7 @@ public void execute(Command cmd, Vector<String> 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);
Expand Down Expand Up @@ -396,12 +396,12 @@ public void execute(Command cmd, Vector<String> arguments) throws RemoteExceptio

int id = toInt(arguments.elementAt(1));
int customerID = toInt(arguments.elementAt(2));
Vector<String> flightNumbers = new Vector<String>();
Vector<Integer> flightNumbers = new Vector<Integer>();
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));

Expand Down
4 changes: 2 additions & 2 deletions code/Client/Client/RMIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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_";
Expand Down
74 changes: 37 additions & 37 deletions code/Server/Server/Common/AbstractRMHashMapManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
// }
// }
}
68 changes: 67 additions & 1 deletion code/Server/Server/Common/CarResourceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
}
106 changes: 106 additions & 0 deletions code/Server/Server/Common/CustomerResourceManager.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading

0 comments on commit 99dc2ff

Please sign in to comment.