org.powertac.server
Class CompetitionControlService

java.lang.Object
  extended by org.powertac.server.CompetitionControlService
All Implemented Interfaces:
org.powertac.common.interfaces.BrokerMessageListener, org.powertac.common.interfaces.CompetitionControl, org.springframework.context.ApplicationContextAware

@Service
public class CompetitionControlService
extends Object
implements org.springframework.context.ApplicationContextAware, org.powertac.common.interfaces.CompetitionControl, org.powertac.common.interfaces.BrokerMessageListener

This is the competition controller. It has three major roles in the server:

  1. At server startup, the preGame() method must be called to set up the environment and allow configuration of the next game, through a web (or REST) interface.
  2. Once the game is configured, the init() method must be called. There are two versions of this method; the init() version runs a "bootstrap" simulation that runs the customer models and the wholesale market for a limited period of time to collect an initial dataset from which brokers can bootstrap their internal models. During a bootstrap simulation, external brokers cannot log in; only the default broker is active. The init(filename) version loads bootstrap data from the named file, validates it, and then opens up the broker login process, most of which is delegated to the BrokerProxy.
  3. Once the simulation starts, the step() method is called every timeslotLength seconds. This runs through timeslotPhaseCount phases, calling the activate() methods on registered components. Phases start at 1; by default there are four phases.
  4. When the number of timeslots equals timeslotCount, the simulation is ended.

Author:
John Collins

Constructor Summary
CompetitionControlService()
           
 
Method Summary
 void init()
          Runs the initialization process, starts the simulation thread, waits for it to complete, then shuts down and prepares for the next simulation run.
 boolean isBootstrapMode()
          True just in case the sim is running in bootstrap mode
 boolean loginBroker(String username)
          Logs in a broker, just in case the broker is on the authorizedBrokerList.
 void pause()
          Signals that the clock is paused due to server overrun.
 void preGame()
          Pre-game server setup - creates the basic configuration elements to make them accessible to the web-based game-setup functions.
 boolean preGame(File bootFile)
          Sets up the simulator, with config overrides provided in a file containing a sequence of PluginConfig instances.
 void receiveMessage(org.powertac.common.msg.BrokerAuthentication msg)
          Authenticate Broker.
 void receiveMessage(Object msg)
           
 void receiveMessage(org.powertac.common.msg.PauseRelease msg)
          Releases a broker-initiated pause.
 void receiveMessage(org.powertac.common.msg.PauseRequest msg)
          Allows a broker to request a pause.
 void registerTimeslotPhase(org.powertac.common.interfaces.TimeslotPhaseProcessor thing, int phase)
          Allows instances of TimeslotPhaseProcessor to register themselves to be activated during one of the processing phases in each timeslot.
 void resume(long newStart)
          Signals that the clock is resumed.
 void runOnce()
          Runs a simulation that is already set up.
 void runOnce(File datasetFile)
          Loads a bootstrap dataset, starts a simulation
 void runOnce(Writer datasetWriter)
          Runs a bootstrap sim, saves the bootstrap dataset in a file
 void setAlwaysAuthorizedBrokers(List<String> brokerList)
          Sets the list of broker usernames that are always authorized, even in bootstrap mode.
 void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
           
 void setAuthorizedBrokerList(ArrayList<String> brokerList)
          Sets the list of brokers allowed and expected to log in before starting a simulation.
 void setBootstrapDiscardedTimeslots(int count)
          Sets the number of timeslots to discard at the beginning of a bootstrap run.
 void setBootstrapTimeslotMillis(long length)
          Allows Spring to set the boostrap timeslot length
 void stop()
          Signals the simulation thread to stop after processing is completed in the current timeslot.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompetitionControlService

public CompetitionControlService()
Method Detail

preGame

public void preGame()
Pre-game server setup - creates the basic configuration elements to make them accessible to the web-based game-setup functions. This method must be called when the server is started, and again at the completion of each simulation. The actual simulation is started with a call to init().

Specified by:
preGame in interface org.powertac.common.interfaces.CompetitionControl

preGame

public boolean preGame(File bootFile)
Sets up the simulator, with config overrides provided in a file containing a sequence of PluginConfig instances. Errors are logged if one or more PluginConfig instances cannot be used in the current server setup.


init

public void init()
Runs the initialization process, starts the simulation thread, waits for it to complete, then shuts down and prepares for the next simulation run. This is probably only useful for a web-interface controller.


setAlwaysAuthorizedBrokers

public void setAlwaysAuthorizedBrokers(List<String> brokerList)
Sets the list of broker usernames that are always authorized, even in bootstrap mode. Normally this is just "defaultBroker". This method is intended to be called by Spring initialization.


setAuthorizedBrokerList

public void setAuthorizedBrokerList(ArrayList<String> brokerList)
Sets the list of brokers allowed and expected to log in before starting a simulation. The simulation will not start until all brokers in the list are logged in, unless a timeout is configured.

Specified by:
setAuthorizedBrokerList in interface org.powertac.common.interfaces.CompetitionControl

setBootstrapDiscardedTimeslots

public void setBootstrapDiscardedTimeslots(int count)
Sets the number of timeslots to discard at the beginning of a bootstrap run. Length of the sim will be the bootstrap length plus this length. Default value is 24.


runOnce

public void runOnce()
Runs a simulation that is already set up. This is intended to be called from a method that knows whether we are running a bootstrap sim or a normal sim.

Specified by:
runOnce in interface org.powertac.common.interfaces.CompetitionControl

runOnce

public void runOnce(File datasetFile)
Loads a bootstrap dataset, starts a simulation


runOnce

public void runOnce(Writer datasetWriter)
Runs a bootstrap sim, saves the bootstrap dataset in a file


loginBroker

public boolean loginBroker(String username)
Logs in a broker, just in case the broker is on the authorizedBrokerList. Returns true if the broker is authorized, otherwise false.

Specified by:
loginBroker in interface org.powertac.common.interfaces.CompetitionControl

stop

public void stop()
Signals the simulation thread to stop after processing is completed in the current timeslot.


registerTimeslotPhase

public void registerTimeslotPhase(org.powertac.common.interfaces.TimeslotPhaseProcessor thing,
                                  int phase)
Allows instances of TimeslotPhaseProcessor to register themselves to be activated during one of the processing phases in each timeslot.

Specified by:
registerTimeslotPhase in interface org.powertac.common.interfaces.CompetitionControl

isBootstrapMode

public boolean isBootstrapMode()
True just in case the sim is running in bootstrap mode

Specified by:
isBootstrapMode in interface org.powertac.common.interfaces.CompetitionControl

pause

public void pause()
Signals that the clock is paused due to server overrun. The pause must be communicated to brokers.


resume

public void resume(long newStart)
Signals that the clock is resumed. Brokers must be informed of the new start time in order to sync their own clocks.


receiveMessage

public void receiveMessage(org.powertac.common.msg.PauseRequest msg)
Allows a broker to request a pause. It may or may not be allowed. If allowed, then the pause will take effect when the current simulation cycle has finished, or immediately if no simulation cycle is currently in progress.

Specified by:
receiveMessage in interface org.powertac.common.interfaces.CompetitionControl

receiveMessage

public void receiveMessage(org.powertac.common.msg.PauseRelease msg)
Releases a broker-initiated pause. After the clock is re-started, the resume() method will be called to communicate a new start time.

Specified by:
receiveMessage in interface org.powertac.common.interfaces.CompetitionControl

receiveMessage

public void receiveMessage(org.powertac.common.msg.BrokerAuthentication msg)
Authenticate Broker. TODO: add auth-token processing


setBootstrapTimeslotMillis

public void setBootstrapTimeslotMillis(long length)
Allows Spring to set the boostrap timeslot length


setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
                           throws org.springframework.beans.BeansException
Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException

receiveMessage

public void receiveMessage(Object msg)
Specified by:
receiveMessage in interface org.powertac.common.interfaces.BrokerMessageListener


Copyright © 2011 Power TAC. All Rights Reserved.