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