]> git.mxchange.org Git - hub.git/blob - application/hub/main/class_HubCommandProcessor.php
0aba7a4eb2b643607e34174de04a9cce5b5f7f3b
[hub.git] / application / hub / main / class_HubCommandProcessor.php
1 <?php
2 /**
3  * A class for handling commands to and from hubs including the master hub
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  *
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.
14  *
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.
19  *
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/>.
22  */
23 class HubCommandProcessor extends BaseFrameworkSystem {
24         /**
25          * An instance of a HubPeer class
26          */
27         private $peerInstance = null;
28
29         /**
30          * A list of sent commands but not replied (yet)
31          */
32         private $sentCommandsAwaitingAnswer = array();
33
34         /**
35          * Queued commands from the master hub
36          */
37         private $masterCommands = array();
38
39         // Exceptions
40         const EXCEPTION_COMMAND_AWAIT_INVALID   = 0xc00;
41
42         /**
43          * The private constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50
51                 // Tidy up a little
52                 $this->removeSystemArray();
53                 $this->removeNumberFormaters();
54         }
55
56         /**
57          * Creates an instance of a HubCommandProcessor class by a HubPeer class
58          *
59          * @param               $peerInstance           An instance of a HubPeer class
60          * @return      $command                        An instance of this class
61          */
62         public final static function createHubCommandProcessor (HubPeer $peerInstance) {
63                 // Get a new instance of this class
64                 $command = new HubCommandProcessor();
65
66                 // Set the hub instance
67                 $command->setPeerInstance($peerInstance);
68
69                 // Return the instance
70                 return $command;
71         }
72
73         /**
74          * Setter for HubPeer instances
75          *
76          * @param               $peerInstance           An instance of a HubPeer class
77          * @return      void
78          */
79         public final function setPeerInstance(HubPeer $peerInstance) {
80                 $this->peerInstance = $peerInstance;
81         }
82
83         /**
84          * Getter for HubPeer instances
85          *
86          * @return      $peerInstance           An instance of a HubPeer class
87          */
88         public final function getPeerInstance() {
89                 return $this->peerInstance;
90         }
91
92         /**
93          * Handles simple commands which require an answer command from the peer without any parameters
94          *
95          * @param               $sendCommand            The command we shall send to the peer
96          * @param               $expectCommand          The command we except from the peer
97          * @return      void
98          */
99         public function simpleExecute ($sendCommand, $expectCommand) {
100                 // Remember the command
101                 $this->sentCommandsAwaitingAnswer[$expectCommand] = $sendCommand;
102
103                 // Debug message
104                 $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Sending command %s to peer %s",
105                         $this->__toString(),
106                         $sendCommand,
107                         $this->getPeerInstance()->getValidatedIP()
108                 ));
109
110                 // Execute the command
111                 $this->getPeerInstance()->sendMessage($sendCommand);
112         }
113
114         /**
115          * Awaits a specified command from the peer and returns TRUE if received or FALSE if not.
116          *
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...
120          */
121         public function ifAwaitsCommand ($expectCommand) {
122                 $await = false;
123
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 (
128                                 array(
129                                         'this'  => $this,
130                                         'await' => $expectCommand
131                                 ), self::EXCEPTION_COMMAND_AWAIT_INVALID
132                         );
133                 } // END - if
134
135                 // Then process it... :-)
136                 $readCommand = $this->getPeerInstance()->readFromSocket();
137
138                 // Is the command not empty?
139                 if (!empty($readCommand)) {
140                         // Debug message
141                         $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Raw command %s received from peer %s",
142                                 $this->__toString(),
143                                 $readCommand,
144                                 $this->getPeerInstance()->getValidatedIP()
145                         ));
146                 } // END - if
147
148                 if ($readCommand == $expectCommand) {
149                         // Debug message
150                         $this->getPeerInstance()->getHubInstance()->getOutputInstance()->output(sprintf("[%s] Awaited command %s received from peer %s",
151                                 $this->__toString(),
152                                 $expectCommand,
153                                 $this->getPeerInstance()->getValidatedIP()
154                         ));
155
156                         // Expected command has been received!
157                         unset($this->sentCommandsAwaitingAnswer[$expectCommand]);
158                         $await = true;
159
160                 } // END - if
161
162                 // Return the result
163                 return $await;
164         }
165
166         /**
167          * Awaits any command from the peer
168          *
169          * @return      $command                The sent command from the peer
170          */
171         public function awaitAnyCommand () {
172                 $command = false;
173                 $readCommand = $this->getPeerInstance()->readFromSocket();
174                 if (!empty($readCommand)) {
175                         // Remember this command
176                         // TODO Add some validation here!
177                         $this->masterCommands[] = $readCommand;
178
179                         // A command is in the queue
180                         $command = true;
181                 } // END - if
182
183                 // Return status
184                 return $command;
185         }
186
187 } // END - class
188
189 // [EOF]
190 ?>