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 SocketRegistry extends BaseRegistry implements Register, RegisterableSocket {
25 // Exception constants
26 const SOCKET_NOT_REGISTERED = 0xd200;
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:type:port
63 * @param $connectionInstance An instance of a Listenable class
64 * @return $key A string representation of the socket for the registry
66 private function getSubRegistryKey (Listenable $connectionInstance) {
68 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: connectionInstance=' . $connectionInstance->__toString() . ' - ENTERED!');
70 // Default address is invalid
71 $address = '*invalid*';
74 // Which instance is it?
75 // @TODO Tested again base class, rewrite it to a generic interface!
76 if ($connectionInstance instanceof BaseConnectionHelper) {
78 $address = $connectionInstance->getAddress();
79 $port = $connectionInstance->getConnectionPort();
80 } elseif ($connectionInstance instanceof BaseListener) {
82 $address = $connectionInstance->getListenAddress();
83 $port = $connectionInstance->getListenPort();
86 $this->debugBackTrace('Unsupported connectionInstance=' . $connectionInstance->__toString() . ' detected. Please fix this!');
90 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: address=' . $address . ',port=' . $port);
92 // Get connection type and port number and add both together
93 $key = sprintf('%s:%s:%s:%s',
94 $connectionInstance->__toString(),
95 $connectionInstance->getConnectionType(),
101 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ' - EXIT!');
103 // Return resulting key
108 * "Getter" to get a string respresentation of the listener
110 * @param $listenerInstance An instance of a Listenable class
111 * @return $key A string representation of the listener for the registry
113 private function getRegistryKeyFromListener (Listenable $listenerInstance) {
115 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',listenerInstance=' . $listenerInstance->__toString() . ' - ENTERED!');
118 $key = $listenerInstance->getConnectionType();
121 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ' - EXIT!');
123 // Return resulting key
128 * Checks whether the given listener is registered
130 * @param $listenerInstance An instance of a Listenable class
131 * @return $isRegistered Whether the listener is registered
133 private function isListenerRegistered (Listenable $listenerInstance) {
135 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
138 $key = $this->getRegistryKeyFromListener($listenerInstance);
141 $isRegistered = $this->instanceExists($key);
144 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
147 return $isRegistered;
151 * Checks whether given socket resource is registered. If $socketResource is
152 * FALSE only the instance will be checked.
154 * @param $listenerInstance An instance of a Listenable class
155 * @param $socketResource A valid socket resource
156 * @return $isRegistered Whether the given socket resource is registered
158 public function isSocketRegistered (Listenable $listenerInstance, $socketResource) {
160 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
162 // Default is not registered
163 $isRegistered = FALSE;
165 // First, check for the instance, there can be only once
166 if ($this->isListenerRegistered($listenerInstance)) {
167 // That one is found so "get" a registry key from it
168 $key = $this->getRegistryKeyFromListener($listenerInstance);
171 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
174 $registryInstance = $this->getInstance($key);
176 // "Get" a key for the socket
177 $socketKey = $this->getSubRegistryKey($listenerInstance);
180 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
183 if ($registryInstance->instanceExists($socketKey)) {
185 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey));
188 $registeredInstance = $registryInstance->getInstance($socketKey);
190 // Is it SocketContainer and same socket?
191 $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource)));
194 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered));
199 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
202 return $isRegistered;
206 * Registeres given socket for listener or throws an exception if it is already registered
208 * @param $listenerInstance An instance of a Listenable class
209 * @param $socketResource A valid socket resource
210 * @param $packageData Optional raw package data
211 * @throws SocketAlreadyRegisteredException If the given socket is already registered
214 public function registerSocket (Listenable $listenerInstance, $socketResource, array $packageData = array()) {
216 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
218 // Is the socket already registered?
219 if ($this->isSocketRegistered($listenerInstance, $socketResource)) {
220 // Throw the exception
221 throw new SocketAlreadyRegisteredException(array($listenerInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
224 // Does the instance exist?
225 if (!$this->isListenerRegistered($listenerInstance)) {
226 // No, not found so we create a sub registry (not needed to configure!)
227 $registryInstance = SubRegistry::createSubRegistry();
229 // Now we can create the sub-registry for this listener
230 $this->addInstance($this->getRegistryKeyFromListener($listenerInstance), $registryInstance);
232 // Get the sub-registry back
233 $registryInstance = $this->getInstance($this->getRegistryKeyFromListener($listenerInstance));
236 // Get a key for sub-registries
237 $socketKey = $this->getSubRegistryKey($listenerInstance);
239 // Get a socket container
240 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $listenerInstance, $packageData));
242 // We have a sub-registry, the socket key and the socket, now we need to put all together
243 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
244 $registryInstance->addInstance($socketKey, $socketInstance);
248 * Getter for given listener's socket resource
250 * @param $listenerInstance An instance of a Listenable class
251 * @return $socketResource A valid socket resource
252 * @throws NoSocketRegisteredException If the requested socket is not registered
254 public function getRegisteredSocketResource (Listenable $listenerInstance) {
256 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
258 // The socket must be registered before we can return it
259 if (!$this->isListenerRegistered($listenerInstance)) {
260 // Throw the exception
261 throw new NoSocketRegisteredException ($listenerInstance, self::SOCKET_NOT_REGISTERED);
264 // Now get the key from the listener
265 $key = $this->getRegistryKeyFromListener($listenerInstance);
267 // And get the registry
268 $registryInstance = $this->getInstance($key);
271 $socketKey = $this->getSubRegistryKey($listenerInstance);
273 // And the final socket resource
274 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
277 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!');
279 // Return the resource
280 return $socketResource;
284 * "Getter" for protocol/connection instance from given package data
286 * @param $packageData Raw package data
287 * @return $protocolInstance An instance of a HandleableProtocol class
289 public function getHandlerInstanceFromPackageData (array $packageData) {
290 // Init protocol instance
291 $protocolInstance = NULL;
293 // Get all keys and check them
294 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
296 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
298 // This is always a SubRegistry instance
299 foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
301 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
303 // Is this a SocketContainer instance and is the address the same?
304 if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
305 // Found one, so get the protocol instance and abort any further search
306 $protocolInstance = $containerInstance->getProtocolInstance();
312 // Return the protocol instance
313 return $protocolInstance;