de.uni_paderborn.robots.robotArenaInterface.proxy
Class ProxyFactory

java.lang.Object
  |
  +--de.uni_paderborn.robots.robotArenaInterface.proxy.ProxyFactory

public class ProxyFactory
extends java.lang.Object

This (singleton) class provides a method for easy integrating a proxy between the arena and a robot.

Now just call:

RobotFunctions robotFunctions = null;
...
robotFunctions = ProxyFactory.getInstance().getProxy(new SomeRobot());

Instead of:

RobotFunctions robotFunctions = null;
...
robotFunctions = new SomeRobot();

See Also:
Proxy, RobotFunctions

Field Summary
private  java.sql.PreparedStatement actionCardLog
          Prepared SQL statement for logging the cards in the robot's action.
private  java.sql.PreparedStatement actionFieldLog
          Prepared SQL statement for logging the fields in the robot's action.
private  java.sql.PreparedStatement actionLog
          Prepared SQL statement for logging the robot's action.
private  java.lang.String arenaName
          The name of the arena.
private  java.sql.Connection connection
          The connection to the database.
private  java.sql.PreparedStatement exchangeLog
          Prepared SQL statement for logging the robot's exchange behaviour.
private  long gameUID
          A unique identifier for the game.
private  java.sql.PreparedStatement irregularityLog
          Prepared SQL statement for logging irregularities of the arena.
private  boolean logging
          Status bit for logging.
private  int nextRobotUID
          The unique identifier for the next registered robot.
private static ProxyFactory proxyFactory
          The only instance of this class.
private  ProxyLogWindow proxyLogWindow
          The window for standard and error output.
private  boolean proxyProductionReady
          Indicates whether the proxy factory is ready to produce proxies.
 
Constructor Summary
private ProxyFactory()
          Constructs this class.
 
Method Summary
static int correctSingleGame(java.sql.Connection connection, java.lang.String arenaName, long gameUID)
          Does final corrections on a single game.
(package private)  void doFinalCorrectionsOnGame()
          Does final corrections on the current game.
(package private)  boolean doneSomething(Proxy proxy)
          Notices that the robot has done at least one action (i.e., especially, that the game has already started).
protected  void finalize()
          Called by the garbage collector when garbage collection determines that there are no more references to this object.
static ProxyFactory getInstance()
          Returns the one and only instance of this (singleton) class.
 Proxy getProxy(RobotFunctions robot)
          Wraps a proxy around a robot.
private  void initGame()
          Does the required initialization for a new game.
 void println(java.lang.String line)
          Prints a line of text to the log window.
(package private)  boolean writeActionLog(int round, int robotUID, FieldData currentField, FieldData leftField, FieldData frontField, FieldData rightField, int energy, CardData[] hand, AbstractRobotAction returnedAction, long time)
          Inserts an action log entry into the database.
(package private)  boolean writeExchangeLog(int round, int firstRobotUID, int secondRobotUID, CardData firstProposal, CardData secondProposal, boolean firstOk, boolean secondOk)
          Inserts an exchange log entry into the database.
(package private)  boolean writeIrregularityLog(int round, java.lang.String description)
          Inserts an irregularity log entry into the database.
 
Methods inherited from class java.lang.Object
, clone, equals, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

proxyFactory

private static ProxyFactory proxyFactory
The only instance of this class.

proxyProductionReady

private boolean proxyProductionReady
Indicates whether the proxy factory is ready to produce proxies.

arenaName

private java.lang.String arenaName
The name of the arena.

gameUID

private long gameUID
A unique identifier for the game.

nextRobotUID

private int nextRobotUID
The unique identifier for the next registered robot.

logging

private boolean logging
Status bit for logging.

connection

private java.sql.Connection connection
The connection to the database.

actionLog

private java.sql.PreparedStatement actionLog
Prepared SQL statement for logging the robot's action.

actionFieldLog

private java.sql.PreparedStatement actionFieldLog
Prepared SQL statement for logging the fields in the robot's action.

actionCardLog

private java.sql.PreparedStatement actionCardLog
Prepared SQL statement for logging the cards in the robot's action.

exchangeLog

private java.sql.PreparedStatement exchangeLog
Prepared SQL statement for logging the robot's exchange behaviour.

irregularityLog

private java.sql.PreparedStatement irregularityLog
Prepared SQL statement for logging irregularities of the arena.

proxyLogWindow

private ProxyLogWindow proxyLogWindow
The window for standard and error output.
Constructor Detail

ProxyFactory

private ProxyFactory()
              throws java.lang.ClassNotFoundException,
                     java.io.IOException,
                     ProxyFactoryException,
                     java.sql.SQLException
Constructs this class. Since this is a singleton class, it can only be called from inside this class.
Throws:
java.lang.ClassNotFoundException - if the database driver could not be found
java.io.IOException - if an error occurred reading the config file
ProxyFactoryException - if an error occurred during initialization of this class
java.sql.SQLException - if an error occurred accessing the database
Method Detail

initGame

private void initGame()
               throws java.sql.SQLException
Does the required initialization for a new game.
Throws:
java.sql.SQLException - if an error occurred accessing the database

getInstance

public static ProxyFactory getInstance()
                                throws java.lang.ClassNotFoundException,
                                       java.io.IOException,
                                       ProxyFactoryException,
                                       java.sql.SQLException
Returns the one and only instance of this (singleton) class.
Returns:
the instance of this class
Throws:
java.lang.ClassNotFoundException - if the database driver could not be found
java.io.IOException - if an error occurred reading the config file
ProxyFactoryException - if an error occurred during initialization of this class
java.sql.SQLException - if an error occurred accessing the database

getProxy

public Proxy getProxy(RobotFunctions robot)
               throws java.lang.ClassNotFoundException,
                      java.sql.SQLException
Wraps a proxy around a robot.
Parameters:
robot - the robot to wrap a proxy around
Returns:
the proxy wrapped around the robot
Throws:
java.lang.ClassNotFoundException - if the database driver could not be found
java.sql.SQLException - if an error occurred during initialization of the proxy

doneSomething

boolean doneSomething(Proxy proxy)
Notices that the robot has done at least one action (i.e., especially, that the game has already started). Adds the robot to the list of the robots taking part in this game and closes the proxy production round for this game.
Parameters:
proxy - the proxy which has done something
Returns:
true if the proxy factory succeeded write the robot name log entry, false otherwise

println

public void println(java.lang.String line)
Prints a line of text to the log window.
Parameters:
line - the line of text to print

writeActionLog

boolean writeActionLog(int round,
                       int robotUID,
                       FieldData currentField,
                       FieldData leftField,
                       FieldData frontField,
                       FieldData rightField,
                       int energy,
                       CardData[] hand,
                       AbstractRobotAction returnedAction,
                       long time)
Inserts an action log entry into the database.
Parameters:
round - the round of the action
robotUID - the unique identifier of the robot which executed the action
currentField - the robot's current field
leftField - the robot's left field
frontField - the robot's front field
rightField - the robot's right field
energy - the robot's energy
hand - the robot's hand
returnedAction - the action the robot wanted to be executed
time - the time the robot needed for thinking (measurements in the arena may be delayed through the database access)
Returns:
true if the proxy factory succeeded write the action log entry, false otherwise

writeExchangeLog

boolean writeExchangeLog(int round,
                         int firstRobotUID,
                         int secondRobotUID,
                         CardData firstProposal,
                         CardData secondProposal,
                         boolean firstOk,
                         boolean secondOk)
Inserts an exchange log entry into the database.
Parameters:
round - the round of the exchange
firstRobotUID - the unique identifier of the first robot
secondRobotUID - the unique identifier of the second robot
firstProposal - the proposal of the first robot
secondProposal - the proposal of the second robot
firstOk - the ok value of the first robot
secondOk - the ok value of the second robot
Returns:
true if the proxy factory succeeded write the exchange log entry, false otherwise

writeIrregularityLog

boolean writeIrregularityLog(int round,
                             java.lang.String description)
Inserts an irregularity log entry into the database.
Parameters:
round - the round which the irregularity occurred in
description - a description of the irregularity
Returns:
true if the proxy factory succeeded write the irregularity log entry, false otherwise

finalize

protected void finalize()
                 throws java.lang.Throwable
Called by the garbage collector when garbage collection determines that there are no more references to this object. In this case final corrections are done on the last game.
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - the Exception raised by this method

doFinalCorrectionsOnGame

void doFinalCorrectionsOnGame()
Does final corrections on the current game. Increases the round number of all action log entries of the final round which are occidentially placed in the one before last round.

correctSingleGame

public static int correctSingleGame(java.sql.Connection connection,
                                    java.lang.String arenaName,
                                    long gameUID)
                             throws java.sql.SQLException,
                                    ProxyFactoryException
Does final corrections on a single game. Increases the round number of all action log entries of the final round which are occidentially placed in the one before last round.
Parameters:
connection - the connection to the database
arenaName - the name of the arena
gameUID - the unique identifier of the game
Returns:
the number of entries corrected
Throws:
java.sql.SQLException - if an error occurred accessing or manipulating the database
ProxyFactoryException - if there is an internal error in the data