transaction
Class ResourceManagerImpl

java.lang.Object
  extended by transaction.ResourceManagerImpl
All Implemented Interfaces:
java.rmi.Remote, IResourceManager

public class ResourceManagerImpl
extends java.lang.Object
implements IResourceManager


Field Summary
 
Fields inherited from interface transaction.IResourceManager
RMI_NAME_CAR_RM, RMI_NAME_FLIGHT_RM, RMI_NAME_HOTEL_RM
 
Constructor Summary
ResourceManagerImpl()
           
 
Method Summary
 void abort(int xid)
          Abort transaction.
 boolean addResource(int xid, java.lang.String rid, int num, int price)
          Add a new resource (e.g. hotel) to the database.
 int begin()
           
 boolean beginDistributed(int globalXID)
          Begin a new distributed transaction.
 boolean commit(int xid)
          Commit transaction.
 void dieAfterPointerSwitch()
          Closes ResourceManager by calling System.exit(-1) right after switching the pointer to the shadow copy file.
 void dieBeforePointerSwitch()
          Closes ResourceManager by calling System.exit(-1) right before switching the pointer to the shadow copy file.
 void dieNow()
          immediately closes the ResourceManager by calling System.exit(-1)
 boolean newCustomer(int xid, java.lang.String custName)
          Add a new customer to database.
 boolean prepare(int globalXID)
          This method is part of the 2PC protocol for atomic committment of distributed transactions.
 int queryCustomerBill(int xid, java.lang.String custName)
          Query the bill of a customer.
 int queryResourceAvailability(int xid, java.lang.String rid)
          Query the availability of a resource.
 int queryResourcePrice(int xid, java.lang.String rid)
          Query the price of a resource.
 boolean reserveResource(int xid, java.lang.String rid, java.lang.String custName)
          Reserve a resource for a given customer.
 boolean shutdown()
          Shutdown gracefully.
 void start(java.lang.String workingdir, java.lang.String rmiName)
           
 boolean updateResource(int xid, java.lang.String rid, int num, int price)
          Update the attributes (total capacity and price) of a resource.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceManagerImpl

public ResourceManagerImpl()
                    throws java.io.IOException
Throws:
java.io.IOException
Method Detail

start

public void start(java.lang.String workingdir,
                  java.lang.String rmiName)
           throws java.rmi.RemoteException
Specified by:
start in interface IResourceManager
Throws:
java.rmi.RemoteException

abort

public void abort(int xid)
           throws InvalidTransactionException
Description copied from interface: IResourceManager
Abort transaction.

Specified by:
abort in interface IResourceManager
Parameters:
xid - id of transaction to be aborted.
Throws:
InvalidTransactionException - if transaction id is invalid.

begin

public int begin()
          throws ShutdownException
Specified by:
begin in interface IResourceManager
Returns:
A unique local transaction ID > 0.
Throws:
ShutdownException - thrown if no new transactions can be accepted due to shutdown of the resource manager

beginDistributed

public boolean beginDistributed(int globalXID)
                         throws ShutdownException,
                                java.rmi.RemoteException,
                                InvalidTransactionException
Description copied from interface: IResourceManager
Begin a new distributed transaction. Part of starting a new distributed transaction is to call the transaction manager's enlist() method in order to register this RM as a participant of the given distributed transaction.

Specified by:
beginDistributed in interface IResourceManager
Parameters:
globalXID - the id of the distributed transaction which will be the parent of this local subtransaction. Use this id to enlist this RM at the transaction manager.
Returns:
true if successful, false otherwise
Throws:
ShutdownException - thrown if no new transactions can be accepted due to shutdown of the resource manager
InvalidTransactionException - if a distributed transaction with this globalXID has already been started at this RM
java.rmi.RemoteException

prepare

public boolean prepare(int globalXID)
                throws InvalidTransactionException,
                       java.rmi.RemoteException
Description copied from interface: IResourceManager
This method is part of the 2PC protocol for atomic committment of distributed transactions.

Specified by:
prepare in interface IResourceManager
Parameters:
globalXID - the transaction ID of the distributed transaction which needs to be prepared for commitment
Returns:
true if prepare was successful, false otherwise
Throws:
InvalidTransactionException
java.rmi.RemoteException

commit

public boolean commit(int xid)
               throws TransactionAbortedException,
                      InvalidTransactionException
Description copied from interface: IResourceManager
Commit transaction.

Specified by:
commit in interface IResourceManager
Parameters:
xid - id of transaction to be committed.
Returns:
true on success, false on failure.
Throws:
TransactionAbortedException - if transaction was aborted.
InvalidTransactionException - if transaction id is invalid.

shutdown

public boolean shutdown()
Description copied from interface: IResourceManager
Shutdown gracefully. Stop accepting new transactions, wait for running transactions to terminate, and clean up disk state. When this RM restarts, it should not attempt to recover its state if the client called shutdown to terminate it.

Specified by:
shutdown in interface IResourceManager
Returns:
true on success, false on failure.

newCustomer

public boolean newCustomer(int xid,
                           java.lang.String custName)
                    throws TransactionAbortedException,
                           InvalidTransactionException
Add a new customer to database. Should return success if customer already exists.

Specified by:
newCustomer in interface IResourceManager
Parameters:
xid - id of transaction.
custName - name of customer.
Returns:
true on success, false on failure.
Throws:
java.rmi.RemoteException - on communications failure.
TransactionAbortedException - if transaction was aborted.
InvalidTransactionException - if transaction id is invalid.

queryCustomerBill

public int queryCustomerBill(int xid,
                             java.lang.String custName)
                      throws TransactionAbortedException,
                             InvalidTransactionException
Description copied from interface: IResourceManager
Query the bill of a customer.

Specified by:
queryCustomerBill in interface IResourceManager
Parameters:
xid - the id of the transaction requesting this operation
custName - the unique customer name whose bill should be calculated
Returns:
the sum of costs accumulated due to reservations of this customer, -1 if the customer does not exist
Throws:
TransactionAbortedException - if transaction was aborted.
InvalidTransactionException - if transaction id is invalid.

addResource

public boolean addResource(int xid,
                           java.lang.String rid,
                           int num,
                           int price)
                    throws TransactionAbortedException,
                           InvalidTransactionException
Description copied from interface: IResourceManager
Add a new resource (e.g. hotel) to the database. A resource is identified by a unique name (e.g. HotelA).

Specified by:
addResource in interface IResourceManager
Parameters:
xid - the id of the transaction requesting this operation
rid - the unique identifier of the resource which is to be added (e.g. HotelA)
num - the total capacity of this resource
price - the price a customer needs to pay for reserving this resource
Returns:
true if the operation succeeded, false otherwise (e.g. resource already exists)
Throws:
TransactionAbortedException - if transaction was aborted.
InvalidTransactionException - if transaction id is invalid.

updateResource

public boolean updateResource(int xid,
                              java.lang.String rid,
                              int num,
                              int price)
                       throws TransactionAbortedException,
                              InvalidTransactionException
Description copied from interface: IResourceManager
Update the attributes (total capacity and price) of a resource.

Specified by:
updateResource in interface IResourceManager
Parameters:
xid - the id of the transaction requesting this operation
rid - the unique identifier of the resource which is to be added (e.g. HotelA)
num - a number indicating the change in total capacity, negative numbers decrease capacity, positive values increase capacity
price - the price a customer needs to pay for reserving this resource
Returns:
true if the operation succeeded, false otherwise (e.g. decrease > total capacity - reservations)
Throws:
TransactionAbortedException - if transaction was aborted.
InvalidTransactionException - if transaction id is invalid.

queryResourceAvailability

public int queryResourceAvailability(int xid,
                                     java.lang.String rid)
                              throws TransactionAbortedException,
                                     InvalidTransactionException
Description copied from interface: IResourceManager
Query the availability of a resource.

Specified by:
queryResourceAvailability in interface IResourceManager
Parameters:
xid - the id of the transaction requesting this operation
rid - the unique identifier of the resource
Returns:
the current availability (i.e. total capacity - reservations), -1 if the resource does not exist
Throws:
TransactionAbortedException - if transaction was aborted.
InvalidTransactionException - if transaction id is invalid.

queryResourcePrice

public int queryResourcePrice(int xid,
                              java.lang.String rid)
                       throws TransactionAbortedException,
                              InvalidTransactionException
Description copied from interface: IResourceManager
Query the price of a resource.

Specified by:
queryResourcePrice in interface IResourceManager
Parameters:
xid - the id of the transaction requesting this operation
rid - the unique identifier of the resource
Returns:
the price of the resource, -1 if the resource does not exist
Throws:
TransactionAbortedException - if transaction was aborted.
InvalidTransactionException - if transaction id is invalid.

reserveResource

public boolean reserveResource(int xid,
                               java.lang.String rid,
                               java.lang.String custName)
                        throws TransactionAbortedException,
                               InvalidTransactionException
Description copied from interface: IResourceManager
Reserve a resource for a given customer. The availability of the resource is decreased by one.

Specified by:
reserveResource in interface IResourceManager
Parameters:
xid - the id of the transaction requesting this operation
rid - the unique identifier of the resource
custName - the unique name of the customer
Returns:
true if the operation succeeded, false otherwise (e.g. customer or resource do not exist, or no availability)
Throws:
TransactionAbortedException - if transaction was aborted.
InvalidTransactionException - if transaction id is invalid.

dieNow

public void dieNow()
Description copied from interface: IResourceManager
immediately closes the ResourceManager by calling System.exit(-1)

Specified by:
dieNow in interface IResourceManager

dieBeforePointerSwitch

public void dieBeforePointerSwitch()
Description copied from interface: IResourceManager
Closes ResourceManager by calling System.exit(-1) right before switching the pointer to the shadow copy file.

Specified by:
dieBeforePointerSwitch in interface IResourceManager

dieAfterPointerSwitch

public void dieAfterPointerSwitch()
Description copied from interface: IResourceManager
Closes ResourceManager by calling System.exit(-1) right after switching the pointer to the shadow copy file.

Specified by:
dieAfterPointerSwitch in interface IResourceManager