setPartDescr("Hub-Command-Processor"); // Set unique ID $this->createUniqueID(); // Tidy up a little $this->removeSystemArray(); $this->removeNumberFormaters(); } /** * Creates an instance of a HubCommandProcessor class by a HubPeer class * * @param $peerInstance An instance of a HubPeer class * @return $command An instance of this class */ public final static function createHubCommandProcessor (HubPeer $peerInstance) { // Get a new instance of this class $command = new HubCommandProcessor(); // Set the hub instance $command->setPeerInstance($peerInstance); // Return the instance return $command; } /** * Setter for HubPeer instances * * @param $peerInstance An instance of a HubPeer class * @return void */ public final function setPeerInstance(HubPeer $peerInstance) { $this->peerInstance = $peerInstance; } /** * Getter for HubPeer instances * * @return $peerInstance An instance of a HubPeer class */ public final function getPeerInstance() { return $this->peerInstance; } /** * Handles simple commands which require an answer command from the peer without any parameters * * @param $sendCommand The command we shall send to the peer * @param $expectCommand The command we except from the peer * @return void */ public function simpleExecute ($sendCommand, $expectCommand) { // Remember the command $this->sentCommandsAwaitingAnswer[$expectCommand] = $sendCommand; // Debug message $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Sending command %s to peer %s", $this->__toString(), $sendCommand, $this->getPeerInstance()->getValidatedIP() )); // Execute the command $this->getPeerInstance()->sendMessage($sendCommand); } /** * Awaits a specified command from the peer and returns TRUE if received or FALSE if not. * * @param $exceptCommand The command we are expecting from the peer * @return $await Wether the awaited command has been received * @throws UnexpectedAwaitCommandException If the awaited command is not being awaited... */ public function ifAwaitsCommand ($expectCommand) { $await = false; // Do we wait for this command? if (!isset($this->sentCommandsAwaitingAnswer[$expectCommand])) { // We are not waiting for this command throw new UnexpectedAwaitCommandException ( array( 'this' => $this, 'await' => $expectCommand ), self::EXCEPTION_COMMAND_AWAIT_INVALID ); } // END - if // Then process it... :-) $readCommand = $this->getPeerInstance()->readFromSocket(); // Is the command not empty? if (!empty($readCommand)) { // Debug message $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Raw command %s received from peer %s", $this->__toString(), $readCommand, $this->getPeerInstance()->getValidatedIP() )); } // END - if if ($readCommand == $expectCommand) { // Debug message $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Awaited command %s received from peer %s", $this->__toString(), $expectCommand, $this->getPeerInstance()->getValidatedIP() )); // Expected command has been received! unset($this->sentCommandsAwaitingAnswer[$expectCommand]); $await = true; } // END - if // Return the result return $await; } /** * Awaits any command from the peer * * @return $command The sent command from the peer */ public function awaitAnyCommand () { $command = false; $readCommand = $this->getPeerInstance()->readFromSocket(); if (!empty($readCommand)) { // Remember this command // TODO Add some validation here! $this->masterCommands[] = $readCommand; // A command is in the queue $command = true; } // END - if // Return status return $command; } } // END - class // [EOF] ?>