Updated 'core'.
[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 - 2015 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() . ' - CALLED!');
69
70                 // Get address and port
71                 $address = $infoInstance->getAddress();
72                 $port    = $infoInstance->getPort();
73
74                 // Debug message
75                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: address=' . $address . ',port=' . $port);
76
77                 // Get connection type and port number and add both together
78                 $key = sprintf('%s:%s:%s:%s',
79                         $infoInstance->__toString(),
80                         $infoInstance->getProtocolName(),
81                         $address,
82                         $port
83                 );
84
85                 // Debug message
86                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' .  $key . ' - EXIT!');
87
88                 // Return resulting key
89                 return $key;
90         }
91
92         /**
93          * "Getter" to get a string respresentation of the listener
94          *
95          * @param       $infoInstance   An instance of a ShareableInfo class
96          * @return      $key                    A string representation of the listener for the registry
97          */
98         private function getRegistryKeyFromInfo (ShareableInfo $infoInstance) {
99                 // Debug message
100                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',infoInstance=' . $infoInstance->__toString() . ' - CALLED!');
101
102                 // Get the key
103                 $key = $infoInstance->getProtocolName();
104
105                 // Debug message
106                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' .  $key . ' - EXIT!');
107
108                 // Return resulting key
109                 return $key;
110         }
111
112         /**
113          * Checks whether the shared connection info is registered
114          *
115          * @param       $infoInstance   An instance of a ShareableInfo class
116          * @return      $isRegistered   Whether the listener is registered
117          */
118         private function isInfoRegistered (ShareableInfo $infoInstance) {
119                 // Debug message
120                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ' - CALLED!');
121
122                 // Get the key
123                 $key = $this->getRegistryKeyFromInfo($infoInstance);
124
125                 // Determine it
126                 $isRegistered = $this->instanceExists($key);
127
128                 // Debug message
129                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
130
131                 // Return result
132                 return $isRegistered;
133         }
134
135         /**
136          * Checks whether given socket resource is registered. If $socketResource is
137          * FALSE only the instance will be checked.
138          *
139          * @param       $infoInstance           An instance of a ShareableInfo class
140          * @param       $socketResource         A valid socket resource
141          * @return      $isRegistered           Whether the given socket resource is registered
142          */
143         public function isSocketRegistered (ShareableInfo $infoInstance, $socketResource) {
144                 // Debug message
145                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - CALLED!');
146
147                 // Default is not registered
148                 $isRegistered = FALSE;
149
150                 // First, check for the instance, there can be only once
151                 if ($this->isInfoRegistered($infoInstance)) {
152                         // That one is found so "get" a registry key from it
153                         $key = $this->getRegistryKeyFromInfo($infoInstance);
154
155                         // Debug message
156                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
157
158                         // Get the registry
159                         $registryInstance = $this->getInstance($key);
160
161                         // "Get" a key for the socket
162                         $socketKey = $this->getSubRegistryKey($infoInstance);
163
164                         // Debug message
165                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
166
167                         // Is it there?
168                         if ($registryInstance->instanceExists($socketKey)) {
169                                 // Debug message
170                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey));
171
172                                 // Get the instance
173                                 $registeredInstance = $registryInstance->getInstance($socketKey);
174
175                                 // Is it SocketContainer and same socket?
176                                 $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource)));
177
178                                 // Debug message
179                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered));
180                         } // END - if
181                 } // END - if
182
183                 // Debug message
184                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
185
186                 // Return the result
187                 return $isRegistered;
188         }
189
190         /**
191          * Registeres given socket for listener or throws an exception if it is already registered
192          *
193          * @param       $infoInstance           An instance of a ShareableInfo class
194          * @param       $socketResource         A valid socket resource
195          * @param       $packageData            Optional raw package data
196          * @throws      SocketAlreadyRegisteredException        If the given socket is already registered
197          * @return      void
198          */
199         public function registerSocket (ShareableInfo $infoInstance, $socketResource, array $packageData = array()) {
200                 // Debug message
201                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - CALLED!');
202
203                 // Is the socket already registered?
204                 if ($this->isSocketRegistered($infoInstance, $socketResource)) {
205                         // Throw the exception
206                         throw new SocketAlreadyRegisteredException(array($infoInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
207                 } // END - if
208
209                 // Does the instance exist?
210                 if (!$this->isInfoRegistered($infoInstance)) {
211                         // No, not found so we create a sub registry (not needed to configure!)
212                         $registryInstance = SubRegistry::createSubRegistry();
213
214                         // Now we can create the sub-registry for this info
215                         $this->addInstance($this->getRegistryKeyFromInfo($infoInstance), $registryInstance);
216                 } else {
217                         // Get the sub-registry back
218                         $registryInstance = $this->getInstance($this->getRegistryKeyFromInfo($infoInstance));
219                 }
220
221                 // Get a key for sub-registries
222                 $socketKey = $this->getSubRegistryKey($infoInstance);
223
224                 // Get a socket container
225                 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $infoInstance, $packageData));
226
227                 // We have a sub-registry, the socket key and the socket, now we need to put all together
228                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
229                 $registryInstance->addInstance($socketKey, $socketInstance);
230         }
231
232         /**
233          * Getter for given listener's socket resource
234          *
235          * @param       $listenerInstance       An instance of a Listenable class
236          * @return      $socketResource         A valid socket resource
237          * @throws      NoSocketRegisteredException             If the requested socket is not registered
238          */
239         public function getRegisteredSocketResource (Listenable $listenerInstance) {
240                 // Debug message
241                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - CALLED!');
242
243                 // The socket must be registered before we can return it
244                 if (!$this->isInfoRegistered($listenerInstance)) {
245                         // Throw the exception
246                         throw new NoSocketRegisteredException ($listenerInstance, self::SOCKET_NOT_REGISTERED);
247                 } // END - if
248
249                 // Now get the key from the listener
250                 $key = $this->getRegistryKeyFromInfo($listenerInstance);
251
252                 // And get the registry
253                 $registryInstance = $this->getInstance($key);
254
255                 // Get a socket key
256                 $socketKey = $this->getSubRegistryKey($listenerInstance);
257
258                 // And the final socket resource
259                 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
260
261                 // Debug message
262                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!');
263
264                 // Return the resource
265                 return $socketResource;
266         }
267
268         /**
269          * "Getter" for info instance from given package data
270          *
271          * @param       $packageData    Raw package data
272          * @return      $infoInstance   An instance of a ShareableInfo class
273          */
274         public function getInfoInstanceFromPackageData (array $packageData) {
275                 // Init info instance
276                 $infoInstance = NULL;
277                 //* DEBUG-DIE: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE));
278
279                 // Get all keys and check them
280                 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
281                         // Debug message
282                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
283
284                         // This is always a SubRegistry instance
285                         foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
286                                 // Debug message
287                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
288
289                                 // Is this a SocketContainer instance and is the address the same?
290                                 if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
291                                         // Debug die
292                                         //* DEBUG-DIE: */ die(__METHOD__ . ': containerInstance=' . print_r($containerInstance, TRUE));
293
294                                         // Get listener and helper instances
295                                         $listenerInstance = $containerInstance->getListenerInstance();
296                                         $helperInstance = $containerInstance->getHelperInstance();
297
298                                         // Debug message
299                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance));
300
301                                         // Is a listener or helper set?
302                                         if ($listenerInstance instanceof Listenable) {
303                                                 // Found a listener, so get the info instance first
304                                                 $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($listenerInstance->getProtocolName(), 'helper');
305
306                                                 // Fill info instance with listener data
307                                                 $infoInstance->fillWithListenerInformation($listenerInstance);
308                                         } elseif ($helperInstance instanceof ConnectionHelper) {
309                                                 // Found a helper, so get the info instance first
310                                                 $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($helperInstance->getProtocolName(), 'helper');
311
312                                                 // Helper is found
313                                                 $infoInstance->fillWithConnectionHelperInformation($helperInstance);
314                                         } else {
315                                                 // Not supported state!
316                                                 $this->debugInstance('[' . __METHOD__ . ':' . __LINE__ . ']: Invalid state found, please report this to the developers with full debug infos.' . PHP_EOL);
317                                         }
318
319                                         // Debug message
320                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',infoInstance[' . gettype($infoInstance) . ']=' . $infoInstance->__toString() . ' with protocol ' . $infoInstance->getProtocolName() . ' - FOUND!');
321                                         break;
322                                 } // END - if
323                         } // END - foreach
324
325                         // Is no longer NULL set?
326                         if (!is_null($infoInstance)) {
327                                 // Then skip here, too
328                                 break;
329                         } // END - if
330                 } // END - foreach
331
332                 // Return the info instance
333                 return $infoInstance;
334         }
335 }
336
337 // [EOF]
338 ?>