]> git.mxchange.org Git - hub.git/blob - application/hub/main/registry/socket/class_SocketRegistry.php
737faf8c24dace19101d1936209f308684ebedae
[hub.git] / application / hub / main / registry / socket / class_SocketRegistry.php
1 <?php
2 /**
3  * A Socket registry
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class SocketRegistry extends BaseRegistry implements Register, RegisterableSocket {
25         // Exception constants
26         const SOCKET_NOT_REGISTERED = 0xd200;
27
28         /**
29          * Instance of this class
30          */
31         private static $registryInstance = NULL;
32
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Creates a singleton instance of this registry class
45          *
46          * @return      $registryInstance       An instance of this class
47          */
48         public static final function createSocketRegistry () {
49                 // Is an instance there?
50                 if (is_null(self::$registryInstance)) {
51                         // Not yet, so create one
52                         self::$registryInstance = new SocketRegistry();
53                 } // END - if
54
55                 // Return the instance
56                 return self::$registryInstance;
57         }
58
59         /**
60          * "Getter" to get a string respresentation for a key for the sub-registry
61          * in this format: class:type:port
62          *
63          * @param       $infoInstance   An instance of a ShareableInfo class
64          * @return      $key                    A string representation of the socket for the registry
65          */
66         private function getSubRegistryKey (ShareableInfo $infoInstance) {
67                 // Debug message
68                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: infoInstance=' . $infoInstance->__toString() . ' - ENTERED!');
69
70                 // Default address is invalid
71                 $address = '*invalid*';
72                 $port    = 0;
73
74                 // Get address and port
75                 $address = $infoInstance->getAddress();
76                 $port    = $infoInstance->getPort();
77
78                 // Debug message
79                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: address=' . $address . ',port=' . $port);
80
81                 // Get connection type and port number and add both together
82                 $key = sprintf('%s:%s:%s:%s',
83                         $infoInstance->__toString(),
84                         $infoInstance->getProtocolName(),
85                         $address,
86                         $port
87                 );
88
89                 // Debug message
90                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' .  $key . ' - EXIT!');
91
92                 // Return resulting key
93                 return $key;
94         }
95
96         /**
97          * "Getter" to get a string respresentation of the listener
98          *
99          * @param       $infoInstance   An instance of a ShareableInfo class
100          * @return      $key                    A string representation of the listener for the registry
101          */
102         private function getRegistryKeyFromInfo (ShareableInfo $infoInstance) {
103                 // Debug message
104                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',infoInstance=' . $infoInstance->__toString() . ' - ENTERED!');
105
106                 // Get the key
107                 $key = $infoInstance->getProtocolName();
108
109                 // Debug message
110                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' .  $key . ' - EXIT!');
111
112                 // Return resulting key
113                 return $key;
114         }
115
116         /**
117          * Checks whether the shared connection info is registered
118          *
119          * @param       $infoInstance   An instance of a ShareableInfo class
120          * @return      $isRegistered   Whether the listener is registered
121          */
122         private function isInfoRegistered (ShareableInfo $infoInstance) {
123                 // Debug message
124                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ' - ENTERED!');
125
126                 // Get the key
127                 $key = $this->getRegistryKeyFromInfo($infoInstance);
128
129                 // Determine it
130                 $isRegistered = $this->instanceExists($key);
131
132                 // Debug message
133                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
134
135                 // Return result
136                 return $isRegistered;
137         }
138
139         /**
140          * Checks whether given socket resource is registered. If $socketResource is
141          * FALSE only the instance will be checked.
142          *
143          * @param       $infoInstance           An instance of a ShareableInfo class
144          * @param       $socketResource         A valid socket resource
145          * @return      $isRegistered           Whether the given socket resource is registered
146          */
147         public function isSocketRegistered (ShareableInfo $infoInstance, $socketResource) {
148                 // Debug message
149                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
150
151                 // Default is not registered
152                 $isRegistered = FALSE;
153
154                 // First, check for the instance, there can be only once
155                 if ($this->isInfoRegistered($infoInstance)) {
156                         // That one is found so "get" a registry key from it
157                         $key = $this->getRegistryKeyFromInfo($infoInstance);
158
159                         // Debug message
160                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
161
162                         // Get the registry
163                         $registryInstance = $this->getInstance($key);
164
165                         // "Get" a key for the socket
166                         $socketKey = $this->getSubRegistryKey($infoInstance);
167
168                         // Debug message
169                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
170
171                         // Is it there?
172                         if ($registryInstance->instanceExists($socketKey)) {
173                                 // Debug message
174                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey));
175
176                                 // Get the instance
177                                 $registeredInstance = $registryInstance->getInstance($socketKey);
178
179                                 // Is it SocketContainer and same socket?
180                                 $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource)));
181
182                                 // Debug message
183                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered));
184                         } // END - if
185                 } // END - if
186
187                 // Debug message
188                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
189
190                 // Return the result
191                 return $isRegistered;
192         }
193
194         /**
195          * Registeres given socket for listener or throws an exception if it is already registered
196          *
197          * @param       $infoInstance           An instance of a ShareableInfo class
198          * @param       $socketResource         A valid socket resource
199          * @param       $packageData            Optional raw package data
200          * @throws      SocketAlreadyRegisteredException        If the given socket is already registered
201          * @return      void
202          */
203         public function registerSocket (ShareableInfo $infoInstance, $socketResource, array $packageData = array()) {
204                 // Debug message
205                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
206
207                 // Is the socket already registered?
208                 if ($this->isSocketRegistered($infoInstance, $socketResource)) {
209                         // Throw the exception
210                         throw new SocketAlreadyRegisteredException(array($infoInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
211                 } // END - if
212
213                 // Does the instance exist?
214                 if (!$this->isInfoRegistered($infoInstance)) {
215                         // No, not found so we create a sub registry (not needed to configure!)
216                         $registryInstance = SubRegistry::createSubRegistry();
217
218                         // Now we can create the sub-registry for this info
219                         $this->addInstance($this->getRegistryKeyFromInfo($infoInstance), $registryInstance);
220                 } else {
221                         // Get the sub-registry back
222                         $registryInstance = $this->getInstance($this->getRegistryKeyFromInfo($infoInstance));
223                 }
224
225                 // Get a key for sub-registries
226                 $socketKey = $this->getSubRegistryKey($infoInstance);
227
228                 // Get a socket container
229                 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $infoInstance, $packageData));
230
231                 // We have a sub-registry, the socket key and the socket, now we need to put all together
232                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
233                 $registryInstance->addInstance($socketKey, $socketInstance);
234         }
235
236         /**
237          * Getter for given listener's socket resource
238          *
239          * @param       $listenerInstance       An instance of a Listenable class
240          * @return      $socketResource         A valid socket resource
241          * @throws      NoSocketRegisteredException             If the requested socket is not registered
242          */
243         public function getRegisteredSocketResource (Listenable $listenerInstance) {
244                 // Debug message
245                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
246
247                 // The socket must be registered before we can return it
248                 if (!$this->isInfoRegistered($listenerInstance)) {
249                         // Throw the exception
250                         throw new NoSocketRegisteredException ($listenerInstance, self::SOCKET_NOT_REGISTERED);
251                 } // END - if
252
253                 // Now get the key from the listener
254                 $key = $this->getRegistryKeyFromInfo($listenerInstance);
255
256                 // And get the registry
257                 $registryInstance = $this->getInstance($key);
258
259                 // Get a socket key
260                 $socketKey = $this->getSubRegistryKey($listenerInstance);
261
262                 // And the final socket resource
263                 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
264
265                 // Debug message
266                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!');
267
268                 // Return the resource
269                 return $socketResource;
270         }
271
272         /**
273          * "Getter" for protocol/connection instance from given package data
274          *
275          * @param       $packageData            Raw package data
276          * @return      $protocolInstance       An instance of a HandleableProtocol class
277          */
278         public function getHandlerInstanceFromPackageData (array $packageData) {
279                 // Init protocol instance
280                 $protocolInstance = NULL;
281                 //* DIE-DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE));
282
283                 // Get all keys and check them
284                 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
285                         // Debug message
286                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
287
288                         // This is always a SubRegistry instance
289                         foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
290                                 // Debug message
291                                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
292
293                                 // Is this a SocketContainer instance and is the address the same?
294                                 if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
295                                         // Found one, so get the protocol instance and abort any further search
296                                         $protocolInstance = $containerInstance->getProtocolInstance();
297
298                                         // Debug message
299                                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',protocolInstance[]=' . gettype($protocolInstance) . ' - FOUND!');
300                                         break;
301                                 } // END - if
302                         } // END - foreach
303                 } // END - foreach
304
305                 // Return the protocol instance
306                 return $protocolInstance;
307         }
308 }
309
310 // [EOF]
311 ?>