ParkNPark.middletier
Class ClientManagerImpl

java.lang.Object
  extended by org.omg.PortableServer.Servant
      extended by ParkNPark.interfaces.ClientManagerPOA
          extended by ParkNPark.middletier.ClientManagerImpl
All Implemented Interfaces:
InvokeHandler, ClientManagerOperations

public class ClientManagerImpl
extends ClientManagerPOA

Server object that will manage communication between the client and the database.


Field Summary
(package private)  PreparedStatement checkDuplicate
          Statements used for duplicate message detection
private  int clientID
          Client's ID used for fault-tolerance
private  Connection conn
          Connection to the database
private  int level
          Level that client is in
private  int lot
          Lot that client is in
private static int MAX_ATTEMPTS
          Maximum number of attempts when trying to execute commends on database
private static boolean PRINT_LEVELS_STATUS
           
private static boolean PRINT_LOTS_STATUS
          Flags used to print out the status of lots and levels
private  int seq
          Client's current operation sequence number used for fault-tolerance
(package private)  PreparedStatement updateSequence
           
 
Constructor Summary
ClientManagerImpl()
          Create a client manager for a new client that will hold information specific to the client.
ClientManagerImpl(int clientID)
          Create a client manager for an existing client that will hold information specific to the client.
 
Method Summary
private  boolean alreadyCompleted(int seq)
          Checks if the current client request has already been completed.
 void closeClientManager()
          Closes the client manager and frees server resources associated with it, including the client manager's activation in the server's CORBA portable object adapter
 int[] enterLot(int seq, int lot)
          Moves the client's car into the lot with the given lot number and returns an array of level numbers that have available spaces
 void exitLot(int seq)
          Removes the client's car from the lot that it is currently in
 int getClientID()
          Returns the client's ID, which can be used in the client manager factory's getExistingClientManager() method
 int getCurrentLevel()
          Returns the car's current level number
 int[] getLots()
          Returns an array of valid lot numbers in the system, sorted by the lot number in ascending order
 int getMaxLevel()
          Returns the top level number of the car's current parking lot
 int getMinLevel()
          Returns the bottom level number of the car's current parking lot
 int[] getOtherLotAvailability(int lot)
          Returns an array of other lots that have availability, sorted by lot distance such that closer lots are listed first
 int getSeqNum()
          Returns the client's sequence number, which can be used if the client loses track of it
 int moveDownLevel(int seq)
          Moves the car from its present level to the level beneath it
 int moveUpLevel(int seq)
          Moves the car from its present level to the level above it
private  void printLevelsStatus(String message, int seq)
          Print the status of all the levels in the current lot
private  void printLotsStatus(String message, int seq)
          Print the status of all the lots
private  void setupDatabaseConn()
          Set up the database connection
private  void updateClientData(int seq, int lot, int level)
          Updates the client table, which is used for duplicate detection.
 
Methods inherited from class ParkNPark.interfaces.ClientManagerPOA
_all_interfaces, _invoke, _this, _this
 
Methods inherited from class org.omg.PortableServer.Servant
_default_POA, _get_delegate, _get_interface_def, _is_a, _non_existent, _object_id, _orb, _poa, _set_delegate, _this_object, _this_object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_ATTEMPTS

private static final int MAX_ATTEMPTS
Maximum number of attempts when trying to execute commends on database

See Also:
Constant Field Values

PRINT_LOTS_STATUS

private static final boolean PRINT_LOTS_STATUS
Flags used to print out the status of lots and levels

See Also:
Constant Field Values

PRINT_LEVELS_STATUS

private static final boolean PRINT_LEVELS_STATUS
See Also:
Constant Field Values

clientID

private int clientID
Client's ID used for fault-tolerance


seq

private int seq
Client's current operation sequence number used for fault-tolerance


lot

private int lot
Lot that client is in


level

private int level
Level that client is in


conn

private Connection conn
Connection to the database


checkDuplicate

PreparedStatement checkDuplicate
Statements used for duplicate message detection


updateSequence

PreparedStatement updateSequence
Constructor Detail

ClientManagerImpl

public ClientManagerImpl(int clientID)
                  throws ServiceUnavailableException,
                         InvalidClientException
Create a client manager for an existing client that will hold information specific to the client.

Parameters:
clientID -
seq -
Throws:
ServiceUnavailableException - Thrown when a database exception prevented the client manager from being created successfully
InvalidClientException

ClientManagerImpl

public ClientManagerImpl()
                  throws ServiceUnavailableException
Create a client manager for a new client that will hold information specific to the client.

Throws:
ServiceUnavailableException - Thrown when a database exception prevented the client manager from being created successfully
Method Detail

setupDatabaseConn

private void setupDatabaseConn()
                        throws ServiceUnavailableException
Set up the database connection

Throws:
ServiceUnavailableException - Thrown if a problem came up while connecting to the database server or setting it up after connecting

closeClientManager

public void closeClientManager()
                        throws ServiceUnavailableException
Closes the client manager and frees server resources associated with it, including the client manager's activation in the server's CORBA portable object adapter

Throws:
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the system from closing the client manager. The client manager remains open if this exception gets thrown

enterLot

public int[] enterLot(int seq,
                      int lot)
               throws AlreadyInLotException,
                      LotNotFoundException,
                      LotFullException,
                      ServiceUnavailableException
Moves the client's car into the lot with the given lot number and returns an array of level numbers that have available spaces

Parameters:
seq - The latest sequence number of the client
lot - The lot number to enter
Returns:
An array of level numbers that have available spaces
Throws:
AlreadyInLotException - Thrown when the client's car is already in a lot
LotNotFoundException - Thrown if the given lot number is not known to the system
LotFullException - Thrown if the given lot is full
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the car from entering the lot

exitLot

public void exitLot(int seq)
             throws NotInLotException,
                    NotOnExitLevelException,
                    ServiceUnavailableException
Removes the client's car from the lot that it is currently in

Parameters:
seq - The latest sequence number of the client
Throws:
NotInLotException - Thrown if the car is not in a lot
NotOnExitLevelException - Thrown if the car is in a lot but is not on a permitted exit level
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the car from exiting the lot

getClientID

public int getClientID()
Returns the client's ID, which can be used in the client manager factory's getExistingClientManager() method

Returns:
The client's ID

getSeqNum

public int getSeqNum()
Returns the client's sequence number, which can be used if the client loses track of it

Returns:

getCurrentLevel

public int getCurrentLevel()
                    throws NotInLotException,
                           ServiceUnavailableException
Returns the car's current level number

Returns:
The car's current level number
Throws:
NotInLotException - Thrown if the car is not in a lot
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the system from returning the car's current level

getLots

public int[] getLots()
              throws ServiceUnavailableException
Returns an array of valid lot numbers in the system, sorted by the lot number in ascending order

Returns:
An array of valid lot numbers in the system
Throws:
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the system from discovering the its defined lots

getMaxLevel

public int getMaxLevel()
                throws NotInLotException,
                       ServiceUnavailableException
Returns the top level number of the car's current parking lot

Returns:
The top level number of the car's current parking lot
Throws:
NotInLotException - Thrown if the car is not in a lot
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the system from returning the current lot's highest level

getMinLevel

public int getMinLevel()
                throws NotInLotException,
                       ServiceUnavailableException
Returns the bottom level number of the car's current parking lot

Returns:
The bottom level number of the car's current parking lot
Throws:
NotInLotException - Thrown if the car is not in a lot
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the system from returning the current lot's lowest level

getOtherLotAvailability

public int[] getOtherLotAvailability(int lot)
                              throws LotNotFoundException,
                                     ServiceUnavailableException
Returns an array of other lots that have availability, sorted by lot distance such that closer lots are listed first

Parameters:
lot - The lot to get lot distances from
Returns:
An array of other lots that have availability
Throws:
LotNotFoundException - Thrown if the given lot number is not known to the system
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the system from discovering the availability of other lots

moveDownLevel

public int moveDownLevel(int seq)
                  throws NotInLotException,
                         AtBottomLevelException,
                         ServiceUnavailableException
Moves the car from its present level to the level beneath it

Parameters:
seq - The latest sequence number of the client
Returns:
The level number that the client's car is now on
Throws:
NotInLotException - Thrown if the car is not in a lot
AtTopLevelException - Thrown if the car is already on the lowest level
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the car from moving to the lower level
AtBottomLevelException

moveUpLevel

public int moveUpLevel(int seq)
                throws NotInLotException,
                       AtTopLevelException,
                       ServiceUnavailableException
Moves the car from its present level to the level above it

Parameters:
seq - The latest sequence number of the client
Returns:
The level number that the client's car is now on
Throws:
NotInLotException - Thrown if the car is not in a lot
AtTopLevelException - Thrown if the car is already on the highest level
ServiceUnavailableException - Thrown if the database cannot be contacted or if some other reason prevents the car from moving to the next highest level

alreadyCompleted

private boolean alreadyCompleted(int seq)
                          throws ServiceUnavailableException
Checks if the current client request has already been completed.

Parameters:
seq -
Returns:
Throws:
ServiceUnavailableException

updateClientData

private void updateClientData(int seq,
                              int lot,
                              int level)
                       throws ServiceUnavailableException
Updates the client table, which is used for duplicate detection.

Parameters:
seq -
lot -
level -
Throws:
ServiceUnavailableException

printLotsStatus

private void printLotsStatus(String message,
                             int seq)
Print the status of all the lots

Parameters:
message - The message to display with the status
seq - The client request sequence number

printLevelsStatus

private void printLevelsStatus(String message,
                               int seq)
Print the status of all the levels in the current lot

Parameters:
message - The message to display with the status
seq - The client request sequence number