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