]> git.mxchange.org Git - hub.git/blob - application/hub/main/registry/connection/class_ConnectionRegistry.php
Added line numbers to debug lines as this will become the 'norm'
[hub.git] / application / hub / main / registry / connection / class_ConnectionRegistry.php
1 <?php
2 /**
3  * A Connection 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 ConnectionRegistry extends BaseRegistry implements Register, RegisterableConnection {
25         // Exception constants
26         const CONNECTION_NOT_REGISTERED = 0xd100;
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 createConnectionRegistry () {
49                 // Is an instance there?
50                 if (is_null(self::$registryInstance)) {
51                         // Not yet, so create one
52                         self::$registryInstance = new ConnectionRegistry();
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       $connectionInstance             An instance of a ConnectionHelper class
64          * @return      $key                                    A string representation of the socket for the registry
65          */
66         private function getSubRegistryKey (ConnectionHelper $connectionInstance) {
67                 // Get protocol and port number and add both together
68                 $key = sprintf("%s:%s:%s",
69                         $connectionInstance->__toString(),
70                         $connectionInstance->getProtocol(),
71                         $connectionInstance->getPort()
72                 );
73
74                 // Return resulting key
75                 return $key;
76         }
77
78         /**
79          * "Getter" to get a string respresentation of the protocol
80          *
81          * @param       $connectionInstance     An instance of a ConnectionHelper class
82          * @return      $key                            A string representation of the protocol for the registry
83          */
84         private function getRegistryKeyFromProtocol (ConnectionHelper $connectionInstance) {
85                 // Get the key
86                 $key = $connectionInstance->getProtocol();
87
88                 // Return resulting key
89                 return $key;
90         }
91
92         /**
93          * Checks whether the given protocol is registered
94          *
95          * @param       $connectionInstance     An instance of a ConnectionHelper class
96          * @return      $isRegistered           Whether the protocol is registered
97          */
98         private function isProtocolRegistered (ConnectionHelper $connectionInstance) {
99                 // Get the key
100                 $key = $this->getRegistryKeyFromProtocol($connectionInstance);
101
102                 // Determine it
103                 $isRegistered = $this->instanceExists($key);
104
105                 // Return result
106                 return $isRegistered;
107         }
108
109         /**
110          * Checks whether given socket resource is registered. If $socketResource is
111          * false only the instance will be checked.
112          *
113          * @param       $connectionInstance             An instance of a ConnectionHelper class
114          * @param       $socketResource                 A valid socket resource
115          * @return      $isRegistered                   Whether the given socket resource is registered
116          */
117         public function isConnectionRegistered (ConnectionHelper $connectionInstance, $socketResource) {
118                 // Default is not registered
119                 $isRegistered = false;
120
121                 // First, check for the instance, there can be only once
122                 if ($this->isProtocolRegistered($connectionInstance)) {
123                         // That one is found so "get" a registry key from it
124                         $key = $this->getRegistryKeyFromProtocol($connectionInstance);
125
126                         // Get the registry
127                         $registryInstance = $this->getInstance($key);
128
129                         // "Get" a key for the socket
130                         $socketKey = $this->getSubRegistryKey($connectionInstance);
131
132                         // And simply ask it
133                         $isRegistered = $registryInstance->instanceExists($socketKey);
134                 } // END - if
135
136                 // Return the result
137                 return $isRegistered;
138         }
139
140         /**
141          * Registeres given socket for listener or throws an exception if it is already registered
142          *
143          * @param       $connectionInstance             An instance of a ConnectionHelper class
144          * @param       $socketResource                 A valid socket resource
145          * @param       $packageData                    Optional raw package data
146          * @throws      ConnectionAlreadyRegisteredException    If the given socket is already registered
147          * @return      void
148          */
149         public function registerConnection (ConnectionHelper $connectionInstance, $socketResource, array $packageData = array()) {
150                 // Is the socket already registered?
151                 if ($this->isConnectionRegistered($connectionInstance, $socketResource)) {
152                         // Throw the exception
153                         throw new ConnectionAlreadyRegisteredException(array($connectionInstance, $socketResource), BaseListener::EXCEPTION_CONNECTION_ALREADY_REGISTERED);
154                 } // END - if
155
156                 // Does the instance exist?
157                 if (!$this->isProtocolRegistered($connectionInstance)) {
158                         // No, not found so we create a sub registry (not needed to configure!)
159                         $registryInstance = SubRegistry::createSubRegistry();
160
161                         // Now we can create the sub-registry for this protocol
162                         $this->addInstance($this->getRegistryKeyFromProtocol($connectionInstance), $registryInstance);
163                 } else {
164                         // Get the sub-registry back
165                         $registryInstance = $this->getInstance($this->getRegistryKeyFromProtocol($connectionInstance));
166                 }
167
168                 // Get a key for sub-registries
169                 $socketKey = $this->getSubRegistryKey($connectionInstance);
170
171                 // Get a socket container
172                 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $connectionInstance, $packageData));
173
174                 // We have a sub-registry, the socket key and the socket, now we need to put all together
175                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource=' . $socketResource . ' - adding socket container instance ...');
176                 $registryInstance->addInstance($socketKey, $socketInstance);
177         }
178
179         /**
180          * Getter for given listener's socket resource
181          *
182          * @param       $connectionInstance             An instance of a ConnectionHelper class
183          * @return      $socketResource                 A valid socket resource
184          * @throws      NoConnectionRegisteredException         If the requested socket is not registered
185          */
186         public function getRegisteredConnectionResource (ConnectionHelper $connectionInstance) {
187                 // The socket must be registered before we can return it
188                 if (!$this->isConnectionRegistered($connectionInstance, false)) {
189                         // Throw the exception
190                         throw new NoConnectionRegisteredException ($connectionInstance, self::CONNECTION_NOT_REGISTERED);
191                 } // END - if
192
193                 // Now get the key from the protocol
194                 $key = $this->getRegistryKeyFromProtocol($connectionInstance);
195
196                 // And get the registry
197                 $registryInstance = $this->getInstance($key);
198
199                 // Get a socket key
200                 $socketKey = $this->getSubRegistryKey($connectionInstance);
201
202                 // And the final socket resource
203                 $socketResource = $registryInstance->getInstance($socketKey)->getConnectionResource();
204
205                 // Return the resource
206                 return $socketResource;
207         }
208
209         /**
210          * "Getter" for protocol/connection instance from given package data
211          *
212          * @param       $packageData                    Raw package data
213          * @return      $connectionInstance             An instance of a ConnectionHelper class
214          */
215         public function getHandlerInstanceFromPackageData (array $packageData) {
216                 // Init protocol instance
217                 $connectionInstance = NULL;
218
219                 // Get all keys and check them
220                 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
221                         // This is always a SubRegistry instance
222                         foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
223                                 // Debug message
224                                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
225
226                                 // This is a ConnectionContainer instance, so does the recipient match?
227                                 if ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) {
228                                         // Found one, so get the protocol instance and abort any further search
229                                         $connectionInstance = $containerInstance->getProtocolInstance();
230                                         break;
231                                 } // END - if
232                         } // END - foreach
233                 } // END - foreach
234
235                 // Return the protocol instance
236                 return $connectionInstance;
237         }
238 }
239
240 // [EOF]
241 ?>