]> git.mxchange.org Git - hub.git/blob - application/hub/main/registry/socket/class_SocketRegistry.php
9b55d9cd79ca6e4171a609f40dc760c0625ea07e
[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       $connectionInstance     An instance of a Listenable class
64          * @return      $key                            A string representation of the socket for the registry
65          */
66         private function getSubRegistryKey (Listenable $connectionInstance) {
67                 // Debug message
68                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: connectionInstance=' . $connectionInstance->__toString() . ' - ENTERED!');
69
70                 // Default address is invalid
71                 $address = '*invalid*';
72                 $port    = 0;
73
74                 // Which instance is it?
75                 // @TODO Tested again base class, rewrite it to a generic interface!
76                 if ($connectionInstance instanceof BaseConnectionHelper) {
77                         // Get address
78                         $address = $connectionInstance->getAddress();
79                         $port    = $connectionInstance->getConnectionPort();
80                 } elseif ($connectionInstance instanceof BaseListener) {
81                         // Get listen address
82                         $address = $connectionInstance->getListenAddress();
83                         $port    = $connectionInstance->getListenPort();
84                 } else {
85                         // Not supported!
86                         $this->debugBackTrace('Unsupported connectionInstance=' . $connectionInstance->__toString() . ' detected. Please fix this!');
87                 }
88
89                 // Debug message
90                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: address=' . $address . ',port=' . $port);
91
92                 // Get connection type and port number and add both together
93                 $key = sprintf('%s:%s:%s:%s',
94                         $connectionInstance->__toString(),
95                         $connectionInstance->getConnectionType(),
96                         $address,
97                         $port
98                 );
99
100                 // Debug message
101                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' .  $key . ' - EXIT!');
102
103                 // Return resulting key
104                 return $key;
105         }
106
107         /**
108          * "Getter" to get a string respresentation of the listener
109          *
110          * @param       $listenerInstance       An instance of a Listenable class
111          * @return      $key                            A string representation of the listener for the registry
112          */
113         private function getRegistryKeyFromListener (Listenable $listenerInstance) {
114                 // Debug message
115                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: listenerInstance=' . $listenerInstance->__toString() . ' - ENTERED!');
116
117                 // Get the key
118                 $key = $listenerInstance->getConnectionType();
119
120                 // Debug message
121                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' .  $key . ' - EXIT!');
122
123                 // Return resulting key
124                 return $key;
125         }
126
127         /**
128          * Checks whether the given listener is registered
129          *
130          * @param       $listenerInstance       An instance of a Listenable class
131          * @return      $isRegistered           Whether the listener is registered
132          */
133         private function isListenerRegistered (Listenable $listenerInstance) {
134                 // Debug message
135                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
136
137                 // Get the key
138                 $key = $this->getRegistryKeyFromListener($listenerInstance);
139
140                 // Determine it
141                 $isRegistered = $this->instanceExists($key);
142
143                 // Debug message
144                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
145
146                 // Return result
147                 return $isRegistered;
148         }
149
150         /**
151          * Checks whether given socket resource is registered. If $socketResource is
152          * FALSE only the instance will be checked.
153          *
154          * @param       $listenerInstance       An instance of a Listenable class
155          * @param       $socketResource         A valid socket resource
156          * @return      $isRegistered           Whether the given socket resource is registered
157          */
158         public function isSocketRegistered (Listenable $listenerInstance, $socketResource) {
159                 // Debug message
160                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
161
162                 // Default is not registered
163                 $isRegistered = FALSE;
164
165                 // First, check for the instance, there can be only once
166                 if ($this->isListenerRegistered($listenerInstance)) {
167                         // That one is found so "get" a registry key from it
168                         $key = $this->getRegistryKeyFromListener($listenerInstance);
169
170                         // Debug message
171                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
172
173                         // Get the registry
174                         $registryInstance = $this->getInstance($key);
175
176                         // "Get" a key for the socket
177                         $socketKey = $this->getSubRegistryKey($listenerInstance);
178
179                         // Debug message
180                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
181
182                         // Is it there?
183                         if ($registryInstance->instanceExists($socketKey)) {
184                                 // Debug message
185                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey));
186
187                                 // Get the instance
188                                 $registeredInstance = $registryInstance->getInstance($socketKey);
189
190                                 // Is it SocketContainer and same socket?
191                                 $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource)));
192
193                                 // Debug message
194                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered));
195                         } // END - if
196                 } // END - if
197
198                 // Debug message
199                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
200
201                 // Return the result
202                 return $isRegistered;
203         }
204
205         /**
206          * Registeres given socket for listener or throws an exception if it is already registered
207          *
208          * @param       $listenerInstance       An instance of a Listenable class
209          * @param       $socketResource         A valid socket resource
210          * @param       $packageData            Optional raw package data
211          * @throws      SocketAlreadyRegisteredException        If the given socket is already registered
212          * @return      void
213          */
214         public function registerSocket (Listenable $listenerInstance, $socketResource, array $packageData = array()) {
215                 // Debug message
216                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
217
218                 // Is the socket already registered?
219                 if ($this->isSocketRegistered($listenerInstance, $socketResource)) {
220                         // Throw the exception
221                         throw new SocketAlreadyRegisteredException(array($listenerInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
222                 } // END - if
223
224                 // Does the instance exist?
225                 if (!$this->isListenerRegistered($listenerInstance)) {
226                         // No, not found so we create a sub registry (not needed to configure!)
227                         $registryInstance = SubRegistry::createSubRegistry();
228
229                         // Now we can create the sub-registry for this listener
230                         $this->addInstance($this->getRegistryKeyFromListener($listenerInstance), $registryInstance);
231                 } else {
232                         // Get the sub-registry back
233                         $registryInstance = $this->getInstance($this->getRegistryKeyFromListener($listenerInstance));
234                 }
235
236                 // Get a key for sub-registries
237                 $socketKey = $this->getSubRegistryKey($listenerInstance);
238
239                 // Get a socket container
240                 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $listenerInstance, $packageData));
241
242                 // We have a sub-registry, the socket key and the socket, now we need to put all together
243                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
244                 $registryInstance->addInstance($socketKey, $socketInstance);
245         }
246
247         /**
248          * Getter for given listener's socket resource
249          *
250          * @param       $listenerInstance       An instance of a Listenable class
251          * @return      $socketResource         A valid socket resource
252          * @throws      NoSocketRegisteredException             If the requested socket is not registered
253          */
254         public function getRegisteredSocketResource (Listenable $listenerInstance) {
255                 // Debug message
256                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
257
258                 // The socket must be registered before we can return it
259                 if (!$this->isListenerRegistered($listenerInstance)) {
260                         // Throw the exception
261                         throw new NoSocketRegisteredException ($listenerInstance, self::SOCKET_NOT_REGISTERED);
262                 } // END - if
263
264                 // Now get the key from the listener
265                 $key = $this->getRegistryKeyFromListener($listenerInstance);
266
267                 // And get the registry
268                 $registryInstance = $this->getInstance($key);
269
270                 // Get a socket key
271                 $socketKey = $this->getSubRegistryKey($listenerInstance);
272
273                 // And the final socket resource
274                 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
275
276                 // Debug message
277                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!');
278
279                 // Return the resource
280                 return $socketResource;
281         }
282
283         /**
284          * "Getter" for protocol/connection instance from given package data
285          *
286          * @param       $packageData            Raw package data
287          * @return      $protocolInstance       An instance of a HandleableProtocol class
288          */
289         public function getHandlerInstanceFromPackageData (array $packageData) {
290                 // Init protocol instance
291                 $protocolInstance = NULL;
292
293                 // Get all keys and check them
294                 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
295                         // Debug message
296                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
297
298                         // This is always a SubRegistry instance
299                         foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
300                                 // Debug message
301                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
302
303                                 // Is this a SocketContainer instance and is the address the same?
304                                 if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
305                                         // Found one, so get the protocol instance and abort any further search
306                                         $protocolInstance = $containerInstance->getProtocolInstance();
307                                         break;
308                                 } // END - if
309                         } // END - foreach
310                 } // END - foreach
311
312                 // Return the protocol instance
313                 return $protocolInstance;
314         }
315 }
316
317 // [EOF]
318 ?>