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