tp.impl
Class MyRM

java.lang.Object
  extended by tp.impl.CSEP545ServiceObject
      extended by tp.impl.MyRM
All Implemented Interfaces:
Remote, CSEP545Service, RM

public class MyRM
extends CSEP545ServiceObject
implements RM

Your Resource Manager. Rudimentary implementation is provided for your convenience.

Command line arguments

The following arguments are further processed by this class in addition to common arguments processed by CSEP545ServiceObject.main(String[]).

See Also:
CSEP545ServiceObject

Nested Class Summary
 
Nested classes/interfaces inherited from interface tp.impl.CSEP545Service
CSEP545Service.Type
 
Field Summary
 
Fields inherited from class tp.impl.CSEP545ServiceObject
dir, registry, svcid
 
Constructor Summary
MyRM()
           
 
Method Summary
 void abort(Transaction xid)
          abort transaction
 boolean add(Transaction xid, RID i, int count, int price)
          add qty items described by resource.
 void commit(Transaction xid)
          commit transaction
 boolean delete(Transaction xid, RID rid)
          Drop resource from this RM.
 boolean delete(Transaction xid, RID i, int count)
          Remove exactly qty unreserved resource from this RM.
 String getName()
          get the name of this RM
protected  void init(String[] args)
          IMPLEMENT THIS METHOD process command line parameters and setup any parameterized values.
protected  void initStorage()
          IMPLEMENT THIS METHOD Initialize necessary data files.
 Customer[] listCustomers(Transaction context)
          list of customers who reserve resources managed by this RM
 String[] listResources(Transaction context, RID.Type type)
          list of resources
 int query(Transaction xid, RID rid)
          query: equivalent to queryCars, queryFlights, queryRooms
 int queryPrice(Transaction xid, RID i)
          query: equivalent to queryCarsPrice, queryFlightsPrice, queryRoomsPrice
 String queryReserved(Transaction context, Customer customer)
          Get the bill for the customer return a string representation of reservations
 int queryReservedPrice(Transaction context, Customer customer)
          Get the total amount of money the customer owes in this RM.
protected  void readyToServe()
          IMPLEMENT THIS METHOD Bootstrapping completed and the server is up and running.
protected  void recovery()
          IMPLEMENT THIS METHOD Run recovery algorithm and reconstruct the state of service before failure.
 void refresh()
          Refresh all remote references used by this service.
 boolean reserve(Transaction context, Customer c, RID i)
          reserve a resource i on behalf of customer c
 void selfDestruct(int diskWritesToWait)
          Exit (simulate a failure) after a specified number of disk writes.
 void shutdown()
          Gracefully shutdown target service.
protected  void startUp()
          IMPLEMENT THIS METHOD If you use extra threads in your service, start them in this method.
 void unreserve(Transaction context, Customer c)
          drop all reserved resources for customer customer
 
Methods inherited from class tp.impl.CSEP545ServiceObject
getDataRoot, getServiceID, kill, lookupRemote, lookupRemote, lookupRemote, lookupRemote, main
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MyRM

public MyRM()
Method Detail

commit

public void commit(Transaction xid)
            throws InvalidTransactionException,
                   TransactionAbortedException,
                   RemoteException
Description copied from interface: RM
commit transaction

Specified by:
commit in interface RM
Parameters:
xid - transaction identifier
Throws:
InvalidTransactionException
TransactionAbortedException
RemoteException

abort

public void abort(Transaction xid)
           throws RemoteException
Description copied from interface: RM
abort transaction

Specified by:
abort in interface RM
Parameters:
xid - transaction identifier
Throws:
RemoteException

add

public boolean add(Transaction xid,
                   RID i,
                   int count,
                   int price)
            throws TransactionAbortedException,
                   InvalidTransactionException,
                   RemoteException
Description copied from interface: RM
add qty items described by resource. If resource does not exist in the RM, create a new one.

Specified by:
add in interface RM
Parameters:
xid - transaction identifier
i - resource identifier
count - quantity (seats, cars, rooms)
price - price per unit
Returns:
true on success. false otherwise
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.addSeats(Transaction, String, int, int), WC.addCars(Transaction, String, int, int), WC.addRooms(Transaction, String, int, int)

delete

public boolean delete(Transaction xid,
                      RID rid)
               throws TransactionAbortedException,
                      InvalidTransactionException,
                      RemoteException
Description copied from interface: RM
Drop resource from this RM. All reservations on resource must be dropped as well.

Specified by:
delete in interface RM
Parameters:
xid - transaction identifier
rid - resource identifier
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.deleteFlight(Transaction, String)

delete

public boolean delete(Transaction xid,
                      RID i,
                      int count)
               throws TransactionAbortedException,
                      InvalidTransactionException,
                      RemoteException
Description copied from interface: RM
Remove exactly qty unreserved resource from this RM.

Specified by:
delete in interface RM
Parameters:
xid - transaction identifier
i - resource identifier
count - number of resource to remove. -1 removes all remaining
Returns:
true on successful deletion. If there is no such resource, return true. false otherwise. If there are not enough qty to remove, return false.
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.deleteSeats(Transaction, String, int), WC.deleteCars(Transaction, String, int), WC.deleteRooms(Transaction, String, int)

query

public int query(Transaction xid,
                 RID rid)
          throws TransactionAbortedException,
                 InvalidTransactionException,
                 RemoteException
Description copied from interface: RM
query: equivalent to queryCars, queryFlights, queryRooms

Specified by:
query in interface RM
Parameters:
xid - transaction identifier
rid - resource identifier
Returns:
available quantity
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.queryCar(Transaction, String), WC.queryRoom(Transaction, String), WC.queryFlight(Transaction, String)

queryPrice

public int queryPrice(Transaction xid,
                      RID i)
               throws TransactionAbortedException,
                      InvalidTransactionException,
                      RemoteException
Description copied from interface: RM
query: equivalent to queryCarsPrice, queryFlightsPrice, queryRoomsPrice

Specified by:
queryPrice in interface RM
Parameters:
xid - transaction identifier
i - resource identifier
Returns:
price per unit for requested item resource
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.queryCarPrice(Transaction, String), WC.queryRoomPrice(Transaction, String), WC.queryFlightPrice(Transaction, String)

queryReserved

public String queryReserved(Transaction context,
                            Customer customer)
                     throws TransactionAbortedException,
                            InvalidTransactionException,
                            RemoteException
Description copied from interface: RM
Get the bill for the customer return a string representation of reservations

Specified by:
queryReserved in interface RM
Parameters:
context - the transaction ID
customer - the customer ID
Returns:
a string representation of reservations
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.queryItinerary(Transaction, Customer)

queryReservedPrice

public int queryReservedPrice(Transaction context,
                              Customer customer)
                       throws TransactionAbortedException,
                              InvalidTransactionException,
                              RemoteException
Description copied from interface: RM
Get the total amount of money the customer owes in this RM.

Specified by:
queryReservedPrice in interface RM
Parameters:
context - the transaction ID
customer - the customer ID
Returns:
total price of all reservations
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.queryItineraryPrice(Transaction, Customer)

reserve

public boolean reserve(Transaction context,
                       Customer c,
                       RID i)
                throws TransactionAbortedException,
                       InvalidTransactionException,
                       RemoteException
Description copied from interface: RM
reserve a resource i on behalf of customer c

Specified by:
reserve in interface RM
Parameters:
context - the transaction ID
c - the customer ID
i - resource to reserve
Returns:
true on successful reservation. false otherwise.
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.reserveItinerary(Customer, String[], String, boolean, boolean)

unreserve

public void unreserve(Transaction context,
                      Customer c)
               throws TransactionAbortedException,
                      InvalidTransactionException,
                      RemoteException
Description copied from interface: RM
drop all reserved resources for customer customer

Specified by:
unreserve in interface RM
Parameters:
context - the transaction ID
c - the customer ID
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.cancelItinerary(Customer)

listResources

public String[] listResources(Transaction context,
                              RID.Type type)
                       throws TransactionAbortedException,
                              InvalidTransactionException,
                              RemoteException
Description copied from interface: RM
list of resources

Specified by:
listResources in interface RM
type - listing resource type
Returns:
array of comma separated strings of available resource information. The format of each string is following:

resource name ',' available quantity ',' price

Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.listCars(Transaction), WC.listFlights(Transaction), WC.listRooms(Transaction)

listCustomers

public Customer[] listCustomers(Transaction context)
                         throws TransactionAbortedException,
                                InvalidTransactionException,
                                RemoteException
Description copied from interface: RM
list of customers who reserve resources managed by this RM

Specified by:
listCustomers in interface RM
Parameters:
context - the transaction ID
Returns:
list of customers
Throws:
TransactionAbortedException
InvalidTransactionException
RemoteException
See Also:
WC.listCustomers(Transaction)

shutdown

public void shutdown()
              throws RemoteException
Description copied from interface: CSEP545Service
Gracefully shutdown target service. On restart, the service should not need to recover its state. Please make sure to call Registry.unbind(String)

Specified by:
shutdown in interface CSEP545Service
Overrides:
shutdown in class CSEP545ServiceObject
Throws:
RemoteException
See Also:
Registry.unbind(String)

selfDestruct

public void selfDestruct(int diskWritesToWait)
                  throws RemoteException
Description copied from interface: RM
Exit (simulate a failure) after a specified number of disk writes. Support for this method requires a wrapper around the system's write system call that decrements the counter set by this method. This counter should be set to zero by default, which makes the wrapper doing nothing. If the counter is non-zero, the wrapper should decrement it, see if it is zero, and if so call exit(). This method is not part of a transaction. It is intended to simulate an RM failure.

Specified by:
selfDestruct in interface RM
Parameters:
diskWritesToWait - number of disk writes to wait until terminate
Throws:
RemoteException

getName

public String getName()
               throws RemoteException
Description copied from interface: RM
get the name of this RM

Specified by:
getName in interface RM
Returns:
the name of this RM
Throws:
RemoteException

init

protected void init(String[] args)
             throws Exception
Description copied from class: CSEP545ServiceObject
IMPLEMENT THIS METHOD process command line parameters and setup any parameterized values. For example, setting self destruct counter should be done in this method.

Specified by:
init in class CSEP545ServiceObject
Parameters:
args - command line arguments except common arguments
Throws:
Exception
To do:
setup selfDestruct(int) here

initStorage

protected void initStorage()
                    throws Exception
Description copied from class: CSEP545ServiceObject
IMPLEMENT THIS METHOD Initialize necessary data files. Invoked in two cases. For example, you can create database files for RM and commit log file for TM

Specified by:
initStorage in class CSEP545ServiceObject
Throws:
Exception

recovery

protected void recovery()
                 throws Exception
Description copied from class: CSEP545ServiceObject
IMPLEMENT THIS METHOD Run recovery algorithm and reconstruct the state of service before failure. This method is invoked when the data directory already exists. Note that if the service has been shutdown cleanly, calling this method should not affect the state.

Specified by:
recovery in class CSEP545ServiceObject
Throws:
Exception

startUp

protected void startUp()
                throws Exception
Description copied from class: CSEP545ServiceObject
IMPLEMENT THIS METHOD If you use extra threads in your service, start them in this method. For example, you can launch periodic deadlock-detector thread here. Or, any kinds of asynchronous taks such as timeout-retry, failure detector, etc.

Specified by:
startUp in class CSEP545ServiceObject
Throws:
Exception

readyToServe

protected void readyToServe()
                     throws Exception
Description copied from class: CSEP545ServiceObject
IMPLEMENT THIS METHOD Bootstrapping completed and the server is up and running.

Specified by:
readyToServe in class CSEP545ServiceObject
Throws:
Exception

refresh

public void refresh()
             throws RemoteException,
                    NotBoundException
Description copied from interface: CSEP545Service
Refresh all remote references used by this service. For easier automated testing recovery from failure.

Specified by:
refresh in interface CSEP545Service
Throws:
RemoteException
NotBoundException