5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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 SocketRegistry extends BaseRegistry implements Register, RegisterableSocket {
25 // Exception constants
26 const SOCKET_NOT_REGISTERED = 0xd00;
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 createSocketRegistry () {
49 // Is an instance there?
50 if (is_null(self::$registryInstance)) {
51 // Not yet, so create one
52 self::$registryInstance = new SocketRegistry();
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:protocol:port
63 * @param $protocolInstance An instance of a ProtocolHandler class
64 * @return $key A string representation of the socket for the registry
66 private function getSubRegistryKey (ProtocolHandler $protocolInstance) {
67 // Default address is invalid
68 $address = '*invalid*';
70 // Which instance is it?
71 // @TODO Tested again base class, rewrite it to a generic interface!
72 if ($protocolInstance instanceof BaseConnectionHelper) {
74 $address = $protocolInstance->getAddress();
75 } elseif ($protocolInstance instanceof BaseListener) {
77 $address = $protocolInstance->getListenAddress();
80 $this->debugBackTrace('Unsupported protocolInstance=' . $protocolInstance->__toString() . ' detected. Please fix this!');
83 // Get protocol and port number and add both together
84 $key = sprintf("%s:%s:%s:%s",
85 $protocolInstance->__toString(),
86 $protocolInstance->getProtocol(),
88 $protocolInstance->getPort()
91 // Return resulting key
96 * "Getter" to get a string respresentation of the protocol
98 * @param $protocolInstance An instance of a ProtocolHandler class
99 * @return $key A string representation of the protocol for the registry
101 private function getRegistryKeyFromProtocol (ProtocolHandler $protocolInstance) {
103 $key = $protocolInstance->getProtocol();
105 // Return resulting key
110 * Checks whether the given protocol is registered
112 * @param $protocolInstance An instance of a ProtocolHandler class
113 * @return $isRegistered Whether the protocol is registered
115 private function isProtocolRegistered (ProtocolHandler $protocolInstance) {
117 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ' - ENTERED!');
120 $key = $this->getRegistryKeyFromProtocol($protocolInstance);
123 $isRegistered = $this->instanceExists($key);
126 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
129 return $isRegistered;
133 * Checks whether given socket resource is registered. If $socketResource is
134 * false only the instance will be checked.
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
140 public function isSocketRegistered (ProtocolHandler $protocolInstance, $socketResource) {
142 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
144 // Default is not registered
145 $isRegistered = false;
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);
153 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY: protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
156 $registryInstance = $this->getInstance($key);
158 // "Get" a key for the socket
159 $socketKey = $this->getSubRegistryKey($protocolInstance);
162 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY: protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
165 if ($registryInstance->instanceExists($socketKey)) {
167 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey));
170 $registeredInstance = $registryInstance->getInstance($socketKey);
172 // Is it SocketContainer and same socket?
173 $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource)));
176 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered));
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
184 return $isRegistered;
188 * Registeres given socket for listener or throws an exception if it is already registered
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
196 public function registerSocket (ProtocolHandler $protocolInstance, $socketResource, array $packageData = array()) {
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
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);
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::createSubRegistry();
211 // Now we can create the sub-registry for this protocol
212 $this->addInstance($this->getRegistryKeyFromProtocol($protocolInstance), $registryInstance);
214 // Get the sub-registry back
215 $registryInstance = $this->getInstance($this->getRegistryKeyFromProtocol($protocolInstance));
218 // Get a key for sub-registries
219 $socketKey = $this->getSubRegistryKey($protocolInstance);
221 // Get a socket container
222 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $protocolInstance, $packageData));
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: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
226 $registryInstance->addInstance($socketKey, $socketInstance);
230 * Getter for given listener's socket resource
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
236 public function getRegisteredSocketResource (ProtocolHandler $protocolInstance) {
238 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ' - ENTERED!');
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);
246 // Now get the key from the protocol
247 $key = $this->getRegistryKeyFromProtocol($protocolInstance);
249 // And get the registry
250 $registryInstance = $this->getInstance($key);
253 $socketKey = $this->getSubRegistryKey($protocolInstance);
255 // And the final socket resource
256 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
259 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!');
261 // Return the resource
262 return $socketResource;
266 * "Getter" for protocol/connection instance from given package data
268 * @param $packageData Raw package data
269 * @return $protocolInstance An instance of a ProtocolHandler class
271 public function getHandlerInstanceFromPackageData (array $packageData) {
272 // Init protocol instance
273 $protocolInstance = NULL;
275 // Get all keys and check them
276 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
278 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
280 // This is always a SubRegistry instance
281 foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
283 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
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();
294 // Return the protocol instance
295 return $protocolInstance;