]> git.mxchange.org Git - hub.git/blob - application/hub/main/registry/socket/class_SocketRegistry.php
e20b0f98a04fad37b0a341d76bd9595bdd9f6793
[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                 // Default address is invalid
68                 $address = '*invalid*';
69                 $port    = 0;
70
71                 // Which instance is it?
72                 // @TODO Tested again base class, rewrite it to a generic interface!
73                 if ($connectionInstance instanceof BaseConnectionHelper) {
74                         // Get address
75                         $address = $connectionInstance->getAddress();
76                         $port    = $connectionInstance->getConnectionPort();
77                 } elseif ($connectionInstance instanceof BaseListener) {
78                         // Get listen address
79                         $address = $connectionInstance->getListenAddress();
80                         $port    = $connectionInstance->getListenPort();
81                 } else {
82                         // Not supported!
83                         $this->debugBackTrace('Unsupported connectionInstance=' . $connectionInstance->__toString() . ' detected. Please fix this!');
84                 }
85
86                 // Get connection type and port number and add both together
87                 $key = sprintf('%s:%s:%s:%s',
88                         $connectionInstance->__toString(),
89                         $connectionInstance->getConnectionType(),
90                         $address,
91                         $port
92                 );
93
94                 // Return resulting key
95                 return $key;
96         }
97
98         /**
99          * "Getter" to get a string respresentation of the listener
100          *
101          * @param       $listenerInstance       An instance of a Listenable class
102          * @return      $key                            A string representation of the listener for the registry
103          */
104         private function getRegistryKeyFromListener (Listenable $listenerInstance) {
105                 // Get the key
106                 $key = $listenerInstance->getConnectionType();
107
108                 // Return resulting key
109                 return $key;
110         }
111
112         /**
113          * Checks whether the given listener is registered
114          *
115          * @param       $listenerInstance       An instance of a Listenable class
116          * @return      $isRegistered           Whether the listener is registered
117          */
118         private function isListenerRegistered (Listenable $listenerInstance) {
119                 // Debug message
120                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
121
122                 // Get the key
123                 $key = $this->getRegistryKeyFromListener($listenerInstance);
124
125                 // Determine it
126                 $isRegistered = $this->instanceExists($key);
127
128                 // Debug message
129                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',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       $listenerInstance       An instance of a Listenable class
140          * @param       $socketResource         A valid socket resource
141          * @return      $isRegistered           Whether the given socket resource is registered
142          */
143         public function isSocketRegistered (Listenable $listenerInstance, $socketResource) {
144                 // Debug message
145                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
146
147                 // Default is not registered
148                 $isRegistered = FALSE;
149
150                 // First, check for the instance, there can be only once
151                 if ($this->isListenerRegistered($listenerInstance)) {
152                         // That one is found so "get" a registry key from it
153                         $key = $this->getRegistryKeyFromListener($listenerInstance);
154
155                         // Debug message
156                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: listener=' . $listenerInstance->getConnectionType() . ',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($listenerInstance);
163
164                         // Debug message
165                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: listener=' . $listenerInstance->getConnectionType() . ',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__ . ']:listener=' . $listenerInstance->getConnectionType() . ',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       $listenerInstance       An instance of a Listenable 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 (Listenable $listenerInstance, $socketResource, array $packageData = array()) {
200                 // Debug message
201                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
202
203                 // Is the socket already registered?
204                 if ($this->isSocketRegistered($listenerInstance, $socketResource)) {
205                         // Throw the exception
206                         throw new SocketAlreadyRegisteredException(array($listenerInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
207                 } // END - if
208
209                 // Does the instance exist?
210                 if (!$this->isListenerRegistered($listenerInstance)) {
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 listener
215                         $this->addInstance($this->getRegistryKeyFromListener($listenerInstance), $registryInstance);
216                 } else {
217                         // Get the sub-registry back
218                         $registryInstance = $this->getInstance($this->getRegistryKeyFromListener($listenerInstance));
219                 }
220
221                 // Get a key for sub-registries
222                 $socketKey = $this->getSubRegistryKey($listenerInstance);
223
224                 // Get a socket container
225                 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $listenerInstance, $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() . ' - ENTERED!');
242
243                 // The socket must be registered before we can return it
244                 if (!$this->isListenerRegistered($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->getRegistryKeyFromListener($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 protocol/connection instance from given package data
270          *
271          * @param       $packageData            Raw package data
272          * @return      $protocolInstance       An instance of a HandleableProtocol class
273          */
274         public function getHandlerInstanceFromPackageData (array $packageData) {
275                 // Init protocol instance
276                 $protocolInstance = NULL;
277
278                 // Get all keys and check them
279                 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
280                         // Debug message
281                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
282
283                         // This is always a SubRegistry instance
284                         foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
285                                 // Debug message
286                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
287
288                                 // Is this a SocketContainer instance and is the address the same?
289                                 if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
290                                         // Found one, so get the protocol instance and abort any further search
291                                         $protocolInstance = $containerInstance->getProtocolInstance();
292                                         break;
293                                 } // END - if
294                         } // END - foreach
295                 } // END - foreach
296
297                 // Return the protocol instance
298                 return $protocolInstance;
299         }
300 }
301
302 // [EOF]
303 ?>