3 * A Connection registry
5 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
24 class ConnectionRegistry extends BaseRegistry implements Register, RegisterableConnection {
25 // Exception constants
26 const CONNECTION_NOT_REGISTERED = 0xd100;
29 * Instance of this class
31 private static $registryInstance = NULL;
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates a singleton instance of this registry class
46 * @return $registryInstance An instance of this class
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();
55 // Return the instance
56 return self::$registryInstance;
60 * "Getter" to get a string respresentation for a key for the sub-registry
61 * in this format: class:type:port
63 * @param $connectionInstance An instance of a ConnectionHelper class
64 * @return $key A string representation of the socket for the registry
66 private function getSubRegistryKey (ConnectionHelper $connectionInstance) {
68 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: connectionInstance=' . $connectionInstance->__toString() . ' CALLED!');
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()
78 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ' - EXIT!');
80 // Return resulting key
85 * "Getter" to get a string respresentation of the protocol
87 * @param $connectionInstance An instance of a ConnectionHelper class
88 * @return $key A string representation of the protocol for the registry
90 private function getRegistryKeyFromConnection (ConnectionHelper $connectionInstance) {
92 $key = $connectionInstance->getProtocolName();
94 // Return resulting key
99 * Checks whether the given protocol is registered
101 * @param $connectionInstance An instance of a ConnectionHelper class
102 * @return $isRegistered Whether the protocol is registered
104 private function isConnectionRegistered (ConnectionHelper $connectionInstance) {
106 $key = $this->getRegistryKeyFromConnection($connectionInstance);
109 $isRegistered = $this->instanceExists($key);
112 return $isRegistered;
116 * Checks whether given socket resource is registered. If $socketResource is
117 * FALSE only the instance will be checked.
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
123 public function isConnectionRegistered (ConnectionHelper $connectionInstance, $socketResource) {
124 // Default is not registered
125 $isRegistered = FALSE;
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);
133 $registryInstance = $this->getInstance($key);
135 // "Get" a key for the socket
136 $socketKey = $this->getSubRegistryKey($connectionInstance);
139 $isRegistered = $registryInstance->instanceExists($socketKey);
143 return $isRegistered;
147 * Registeres given socket for listener or throws an exception if it is already registered
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
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);
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();
167 // Now we can create the sub-registry for this protocol
168 $this->addInstance($this->getRegistryKeyFromConnection($connectionInstance), $registryInstance);
170 // Get the sub-registry back
171 $registryInstance = $this->getInstance($this->getRegistryKeyFromConnection($connectionInstance));
174 // Get a key for sub-registries
175 $socketKey = $this->getSubRegistryKey($connectionInstance);
177 // Get a socket container
178 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $connectionInstance, $packageData));
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);
186 * Getter for given listener's socket resource
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
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);
199 // Now get the key from the protocol
200 $key = $this->getRegistryKeyFromConnection($connectionInstance);
202 // And get the registry
203 $registryInstance = $this->getInstance($key);
206 $socketKey = $this->getSubRegistryKey($connectionInstance);
208 // And the final socket resource
209 $socketResource = $registryInstance->getInstance($socketKey)->getConnectionResource();
211 // Return the resource
212 return $socketResource;
216 * "Getter" for protocol/connection instance from given package data
218 * @param $packageData Raw package data
219 * @return $connectionInstance An instance of a ConnectionHelper class
221 public function getHandlerInstanceFromPackageData (array $packageData) {
222 // Init protocol instance
223 $connectionInstance = NULL;
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) {
230 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
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();
241 // Return the protocol instance
242 return $connectionInstance;