gecco.server.actionprocessor
Class ActionProcessor

java.lang.Object
  |
  +--gecco.server.actionprocessor.ActionProcessor
All Implemented Interfaces:
Runnable

public class ActionProcessor
extends Object
implements Runnable

The action processor handles the scheduling and execution of actions for the units in the ongoing game.

Author:
Henrik Bäärnhielm, Karl Hasselström, Henrik Hägerström

Field Summary
private  TreeSet actionQueue
          Description of the Field
private  Client comClient
          Description of the Field
private  UnitManager unitManager
          Description of the Field
private  VisibilityManager visibilityManager
          Description of the Field
 
Constructor Summary
ActionProcessor(UnitManager _unitManager, VisibilityManager _visibilityManager, Client _comClient)
          Constructor for the ActionProcessor object
 
Method Summary
 boolean abortAction(String fromRole, int actionHandle)
          Aborts an ongoing action for a unit.
private  void addUpdateToQueue(QueuedAction qA, long msToExecution)
          Adds an action to the action queue, with a given execution time.
private  void checkActionBlocklist(Unit u)
          Checks the ongoing actions of a given unit and notify the roles (clients) that commands the role about any updates.
private  void checkActionReturn(QueuedAction qA, Unit u, ActionReturn actRet)
          Check if a given action, executed by a given unit, is completed, aborted or still ongoing, and take the necessary steps in each case.
private  void checkChangedPosition(double oldx, double oldy, Unit u)
          Check if a given unit has changed from a known last position.
private  void checkChangedProperties(Unit u)
          Checks all properties of the given unit, and notify all roles (clients) that observes the unit about any updates.
private static void debug(String s)
          Outputs a debug message to the server log, as well as to the stdout of the server.
 void executeInstantaneousAction(Action action)
          Executes an instantaneous action.
 TreeSet getActionQueue()
          Returns the TreeSet object which is the queue that holds the current scheduled actions for the units in the game.
 long getCurrentTime()
          Simply returns the current system time.
 void handleAutomatonUnitEvent(int x, int y, UnitEvent unitEvent)
          This method handles an incoming UnitEvent coming from the automaton-layer, most probably due to a change of state in the automaton at position (x, y).
 void handleUnitToUnitEvent(Unit unit, UnitEvent unitEvent)
          This method handles an incoming UnitEvent which originates from another unit in the game.
 void initiateAction(String fromRole, Action action)
          Initiates an action for a unit.
private static void log(String s)
          Outputs a message to the server log.
 void run()
          Main processing method for the execution of actions.
private  void sendUnitMessageToAll(Unit unit, String unitMessage)
          Sends a message to all observers of a given unit.
private  void sendUnitMessageToAllButCommander(Unit unit, String unitMessage)
          Sends a message to all observers, except the commander, of a given unit.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

actionQueue

private TreeSet actionQueue
Description of the Field

unitManager

private UnitManager unitManager
Description of the Field

visibilityManager

private VisibilityManager visibilityManager
Description of the Field

comClient

private Client comClient
Description of the Field
Constructor Detail

ActionProcessor

public ActionProcessor(UnitManager _unitManager,
                       VisibilityManager _visibilityManager,
                       Client _comClient)
Constructor for the ActionProcessor object
Parameters:
_unitManager - An instance of the unit manager
_visibilityManager - An instance of the visibility manager
_comClient - An instance of the communication client interface
Method Detail

getActionQueue

public TreeSet getActionQueue()
Returns the TreeSet object which is the queue that holds the current scheduled actions for the units in the game. This method is primarily used when another thread wants to synchronize on the queue object, preventing any other thread to add a new action to the queue.
Returns:
The priority queue that is used for scheduling actions for units.

getCurrentTime

public long getCurrentTime()
Simply returns the current system time.
Returns:
The CurrentTime value

initiateAction

public void initiateAction(String fromRole,
                           Action action)
Initiates an action for a unit.
Parameters:
fromRole - The commanding role which initiated the action.
action - The Action class itself.

executeInstantaneousAction

public void executeInstantaneousAction(Action action)
Executes an instantaneous action. An instantaneous action is an action that can be executed during the execution of another (scheduled) action. A unit can for example be executing a movement action, and fire a missile at another unit at the same time. The instantaneous action will never be queued. There is only one single call made to the checkPoint() method in the action class.
Parameters:
action - The class representing the instantaneous action.

handleAutomatonUnitEvent

public void handleAutomatonUnitEvent(int x,
                                     int y,
                                     UnitEvent unitEvent)
This method handles an incoming UnitEvent coming from the automaton-layer, most probably due to a change of state in the automaton at position (x, y).
Parameters:
x - The x-coordinate of the automaton.
y - The y-coordinate of the automaton.
unitEvent - The UnitEvent class.

handleUnitToUnitEvent

public void handleUnitToUnitEvent(Unit unit,
                                  UnitEvent unitEvent)
This method handles an incoming UnitEvent which originates from another unit in the game.
Parameters:
unit - The affected unit.
unitEvent - The UnitEvent class.

abortAction

public boolean abortAction(String fromRole,
                           int actionHandle)
Aborts an ongoing action for a unit.
Parameters:
fromRole - The commanding role
actionHandle - The actionHandle
Returns:
true if the action was found and removed, false otherwise.

run

public void run()
Main processing method for the execution of actions.
Specified by:
run in interface Runnable

addUpdateToQueue

private void addUpdateToQueue(QueuedAction qA,
                              long msToExecution)
Adds an action to the action queue, with a given execution time.
Parameters:
qA - The action to be added.
msToExecution - The time until execution.

checkChangedPosition

private void checkChangedPosition(double oldx,
                                  double oldy,
                                  Unit u)
Check if a given unit has changed from a known last position. If the unit has moved, inform the unit manager and visibility manager of the update.
Parameters:
oldx - The last known x coordinate
oldy - The last known x coordinate
u - The unit.

checkActionBlocklist

private void checkActionBlocklist(Unit u)
Checks the ongoing actions of a given unit and notify the roles (clients) that commands the role about any updates.
Parameters:
u - The unit to check.

checkChangedProperties

private void checkChangedProperties(Unit u)
Checks all properties of the given unit, and notify all roles (clients) that observes the unit about any updates. Also notify roles with status as god's eye.
Parameters:
u - The unit to check.

checkActionReturn

private void checkActionReturn(QueuedAction qA,
                               Unit u,
                               ActionReturn actRet)
Check if a given action, executed by a given unit, is completed, aborted or still ongoing, and take the necessary steps in each case.
Parameters:
qA - The action to check.
u - The unit executing the action.
actRet - The return from the last checkpoint call.

sendUnitMessageToAllButCommander

private void sendUnitMessageToAllButCommander(Unit unit,
                                              String unitMessage)
Sends a message to all observers, except the commander, of a given unit. Also send to all roles with status as god's eye.
Parameters:
unit - The unit.
unitMessage - The message to send.

sendUnitMessageToAll

private void sendUnitMessageToAll(Unit unit,
                                  String unitMessage)
Sends a message to all observers of a given unit. Also send to all roles with status as god's eye.
Parameters:
unit - The unit.
unitMessage - The message to send.

log

private static void log(String s)
Outputs a message to the server log.
Parameters:
s - The message to output.

debug

private static void debug(String s)
Outputs a debug message to the server log, as well as to the stdout of the server.
Parameters:
s - The message to output.