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 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
52 $this->removeSystemArray();
53 $this->removeNumberFormaters();
57 * Creates an instance of a HubCommandProcessor class by a HubPeer class
59 * @param $peerInstance An instance of a HubPeer class
60 * @return $command An instance of this class
62 public final static function createHubCommandProcessor (HubPeer $peerInstance) {
63 // Get a new instance of this class
64 $command = new HubCommandProcessor();
66 // Set the hub instance
67 $command->setPeerInstance($peerInstance);
69 // Return the instance
74 * Setter for HubPeer instances
76 * @param $peerInstance An instance of a HubPeer class
79 public final function setPeerInstance(HubPeer $peerInstance) {
80 $this->peerInstance = $peerInstance;
84 * Getter for HubPeer instances
86 * @return $peerInstance An instance of a HubPeer class
88 public final function getPeerInstance() {
89 return $this->peerInstance;
93 * Handles simple commands which require an answer command from the peer without any parameters
95 * @param $sendCommand The command we shall send to the peer
96 * @param $expectCommand The command we except from the peer
99 public function simpleExecute ($sendCommand, $expectCommand) {
100 // Remember the command
101 $this->sentCommandsAwaitingAnswer[$expectCommand] = $sendCommand;
104 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Sending command %s to peer %s",
107 $this->getPeerInstance()->getValidatedIP()
110 // Execute the command
111 $this->getPeerInstance()->sendMessage($sendCommand);
115 * Awaits a specified command from the peer and returns TRUE if received or FALSE if not.
117 * @param $exceptCommand The command we are expecting from the peer
118 * @return $await Wether the awaited command has been received
119 * @throws UnexpectedAwaitCommandException If the awaited command is not being awaited...
121 public function ifAwaitsCommand ($expectCommand) {
124 // Do we wait for this command?
125 if (!isset($this->sentCommandsAwaitingAnswer[$expectCommand])) {
126 // We are not waiting for this command
127 throw new UnexpectedAwaitCommandException (
130 'await' => $expectCommand
131 ), self::EXCEPTION_COMMAND_AWAIT_INVALID
135 // Then process it... :-)
136 $readCommand = $this->getPeerInstance()->readFromSocket();
138 // Is the command not empty?
139 if (!empty($readCommand)) {
141 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Raw command %s received from peer %s",
144 $this->getPeerInstance()->getValidatedIP()
148 if ($readCommand == $expectCommand) {
150 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Awaited command %s received from peer %s",
153 $this->getPeerInstance()->getValidatedIP()
156 // Expected command has been received!
157 unset($this->sentCommandsAwaitingAnswer[$expectCommand]);
167 * Awaits any command from the peer
169 * @return $command The sent command from the peer
171 public function awaitAnyCommand () {
173 $readCommand = $this->getPeerInstance()->readFromSocket();
174 if (!empty($readCommand)) {
175 // Remember this command
176 // TODO Add some validation here!
177 $this->masterCommands[] = $readCommand;
179 // A command is in the queue