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