3 * A class for handling commands to and from hubs including the master hub
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 class HubCommandProcessor extends BaseFrameworkSystem {
25 * An instance of a HubPeer class
27 private $peerInstance = null;
30 * A list of sent commands but not replied (yet)
32 private $sentCommandsAwaitingAnswer = array();
35 * Queued commands from the master hub
37 private $masterCommands = array();
40 const EXCEPTION_COMMAND_AWAIT_INVALID = 0xc00;
43 * The private constructor
47 private function __construct () {
48 // Call parent constructor
49 parent::constructor(__CLASS__);
52 $this->setObjectDescription("Hub-Command-Processor");
55 $this->createUniqueID();
58 $this->removeSystemArray();
59 $this->removeNumberFormaters();
63 * Creates an instance of a HubCommandProcessor class by a HubPeer class
65 * @param $peerInstance An instance of a HubPeer class
66 * @return $command An instance of this class
68 public final static function createHubCommandProcessor (HubPeer $peerInstance) {
69 // Get a new instance of this class
70 $command = new HubCommandProcessor();
72 // Set the hub instance
73 $command->setPeerInstance($peerInstance);
75 // Return the instance
80 * Setter for HubPeer instances
82 * @param $peerInstance An instance of a HubPeer class
85 public final function setPeerInstance(HubPeer $peerInstance) {
86 $this->peerInstance = $peerInstance;
90 * Getter for HubPeer instances
92 * @return $peerInstance An instance of a HubPeer class
94 public final function getPeerInstance() {
95 return $this->peerInstance;
99 * Handles simple commands which require an answer command from the peer without any parameters
101 * @param $sendCommand The command we shall send to the peer
102 * @param $expectCommand The command we except from the peer
105 public function simpleExecute ($sendCommand, $expectCommand) {
106 // Remember the command
107 $this->sentCommandsAwaitingAnswer[$expectCommand] = $sendCommand;
110 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Sending command %s to peer %s",
113 $this->getPeerInstance()->getValidatedIP()
116 // Execute the command
117 $this->getPeerInstance()->sendMessage($sendCommand);
121 * Awaits a specified command from the peer and returns TRUE if received or FALSE if not.
123 * @param $exceptCommand The command we are expecting from the peer
124 * @return $await Wether the awaited command has been received
125 * @throws UnexpectedAwaitCommandException If the awaited command is not being awaited...
127 public function ifAwaitsCommand ($expectCommand) {
130 // Do we wait for this command?
131 if (!isset($this->sentCommandsAwaitingAnswer[$expectCommand])) {
132 // We are not waiting for this command
133 throw new UnexpectedAwaitCommandException (
136 'await' => $expectCommand
137 ), self::EXCEPTION_COMMAND_AWAIT_INVALID
141 // Then process it... :-)
142 $readCommand = $this->getPeerInstance()->readFromSocket();
144 // Is the command not empty?
145 if (!empty($readCommand)) {
147 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Raw command %s received from peer %s",
150 $this->getPeerInstance()->getValidatedIP()
154 if ($readCommand == $expectCommand) {
156 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Awaited command %s received from peer %s",
159 $this->getPeerInstance()->getValidatedIP()
162 // Expected command has been received!
163 unset($this->sentCommandsAwaitingAnswer[$expectCommand]);
173 * Awaits any command from the peer
175 * @return $command The sent command from the peer
177 public function awaitAnyCommand () {
179 $readCommand = $this->getPeerInstance()->readFromSocket();
180 if (!empty($readCommand)) {
181 // Remember this command
182 // TODO Add some validation here!
183 $this->masterCommands[] = $readCommand;
185 // A command is in the queue