3 * A class for handling commands to and from hubs including the master hub
5 class HubCommandProcessor extends BaseFrameworkSystem {
7 * An instance of a HubPeer class
9 private $peerInstance = null;
12 * A list of sent commands but not replied (yet)
14 private $sentCommandsAwaitingAnswer = array();
17 * Queued commands from the master hub
19 private $masterCommands = array();
22 const EXCEPTION_COMMAND_AWAIT_INVALID = 0xc00;
25 * The private constructor
29 private function __construct () {
30 // Call parent constructor
31 parent::constructor(__CLASS__);
34 $this->setPartDescr("Hub-Command-Processor");
37 $this->createUniqueID();
40 $this->removeSystemArray();
41 $this->removeNumberFormaters();
45 * Creates an instance of a HubCommandProcessor class by a HubPeer class
47 * @param $peerInstance An instance of a HubPeer class
48 * @return $command An instance of this class
50 public final static function createHubCommandProcessor (HubPeer $peerInstance) {
51 // Get a new instance of this class
52 $command = new HubCommandProcessor();
54 // Set the hub instance
55 $command->setPeerInstance($peerInstance);
57 // Return the instance
62 * Setter for HubPeer instances
64 * @param $peerInstance An instance of a HubPeer class
67 public final function setPeerInstance(HubPeer $peerInstance) {
68 $this->peerInstance = $peerInstance;
72 * Getter for HubPeer instances
74 * @return $peerInstance An instance of a HubPeer class
76 public final function getPeerInstance() {
77 return $this->peerInstance;
81 * Handles simple commands which require an answer command from the peer without any parameters
83 * @param $sendCommand The command we shall send to the peer
84 * @param $expectCommand The command we except from the peer
87 public function simpleExecute ($sendCommand, $expectCommand) {
88 // Remember the command
89 $this->sentCommandsAwaitingAnswer[$expectCommand] = $sendCommand;
92 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Sending command %s to peer %s",
95 $this->getPeerInstance()->getValidatedIP()
98 // Execute the command
99 $this->getPeerInstance()->sendMessage($sendCommand);
103 * Awaits a specified command from the peer and returns TRUE if received or FALSE if not.
105 * @param $exceptCommand The command we are expecting from the peer
106 * @return $await Wether the awaited command has been received
107 * @throws UnexpectedAwaitCommandException If the awaited command is not being awaited...
109 public function ifAwaitsCommand ($expectCommand) {
112 // Do we wait for this command?
113 if (!isset($this->sentCommandsAwaitingAnswer[$expectCommand])) {
114 // We are not waiting for this command
115 throw new UnexpectedAwaitCommandException (
118 'await' => $expectCommand
119 ), self::EXCEPTION_COMMAND_AWAIT_INVALID
123 // Then process it... :-)
124 $readCommand = $this->getPeerInstance()->readFromSocket();
126 // Is the command not empty?
127 if (!empty($readCommand)) {
129 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Raw command %s received from peer %s",
132 $this->getPeerInstance()->getValidatedIP()
136 if ($readCommand == $expectCommand) {
138 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Awaited command %s received from peer %s",
141 $this->getPeerInstance()->getValidatedIP()
144 // Expected command has been received!
145 unset($this->sentCommandsAwaitingAnswer[$expectCommand]);
155 * Awaits any command from the peer
157 * @return $command The sent command from the peer
159 public function awaitAnyCommand () {
161 $readCommand = $this->getPeerInstance()->readFromSocket();
162 if (!empty($readCommand)) {
163 // Remember this command
164 // TODO Add some validation here!
165 $this->masterCommands[] = $readCommand;
167 // A command is in the queue