* @version 0.0 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class HubCommandProcessor extends BaseFrameworkSystem { /** * An instance of a HubPeer class */ private $peerInstance = null; /** * A list of sent commands but not replied (yet) */ private $sentCommandsAwaitingAnswer = array(); /** * Queued commands from the master hub */ private $masterCommands = array(); // Exceptions const EXCEPTION_COMMAND_AWAIT_INVALID = 0xc00; /** * The private constructor * * @return void */ private function __construct () { // Call parent constructor parent::constructor(__CLASS__); // Set description $this->setObjectDescription("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] ?>