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 $infoInstance An instance of a ShareableInfo class
64 * @return $key A string representation of the socket for the registry
66 private function getSubRegistryKey (ShareableInfo $infoInstance) {
68 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: infoInstance=' . $infoInstance->__toString() . ' - ENTERED!');
70 // Default address is invalid
71 $address = '*invalid*';
74 // Get address and port
75 $address = $infoInstance->getAddress();
76 $port = $infoInstance->getPort();
79 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: address=' . $address . ',port=' . $port);
81 // Get connection type and port number and add both together
82 $key = sprintf('%s:%s:%s:%s',
83 $infoInstance->__toString(),
84 $infoInstance->getProtocolName(),
90 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ' - EXIT!');
92 // Return resulting key
97 * "Getter" to get a string respresentation of the listener
99 * @param $infoInstance An instance of a ShareableInfo class
100 * @return $key A string representation of the listener for the registry
102 private function getRegistryKeyFromInfo (ShareableInfo $infoInstance) {
104 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',infoInstance=' . $infoInstance->__toString() . ' - ENTERED!');
107 $key = $infoInstance->getProtocolName();
110 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ' - EXIT!');
112 // Return resulting key
117 * Checks whether the shared connection info is registered
119 * @param $infoInstance An instance of a ShareableInfo class
120 * @return $isRegistered Whether the listener is registered
122 private function isInfoRegistered (ShareableInfo $infoInstance) {
124 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ' - ENTERED!');
127 $key = $this->getRegistryKeyFromInfo($infoInstance);
130 $isRegistered = $this->instanceExists($key);
133 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
136 return $isRegistered;
140 * Checks whether given socket resource is registered. If $socketResource is
141 * FALSE only the instance will be checked.
143 * @param $infoInstance An instance of a ShareableInfo class
144 * @param $socketResource A valid socket resource
145 * @return $isRegistered Whether the given socket resource is registered
147 public function isSocketRegistered (ShareableInfo $infoInstance, $socketResource) {
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
151 // Default is not registered
152 $isRegistered = FALSE;
154 // First, check for the instance, there can be only once
155 if ($this->isInfoRegistered($infoInstance)) {
156 // That one is found so "get" a registry key from it
157 $key = $this->getRegistryKeyFromInfo($infoInstance);
160 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
163 $registryInstance = $this->getInstance($key);
165 // "Get" a key for the socket
166 $socketKey = $this->getSubRegistryKey($infoInstance);
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
172 if ($registryInstance->instanceExists($socketKey)) {
174 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey));
177 $registeredInstance = $registryInstance->getInstance($socketKey);
179 // Is it SocketContainer and same socket?
180 $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource)));
183 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered));
188 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
191 return $isRegistered;
195 * Registeres given socket for listener or throws an exception if it is already registered
197 * @param $infoInstance An instance of a ShareableInfo class
198 * @param $socketResource A valid socket resource
199 * @param $packageData Optional raw package data
200 * @throws SocketAlreadyRegisteredException If the given socket is already registered
203 public function registerSocket (ShareableInfo $infoInstance, $socketResource, array $packageData = array()) {
205 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
207 // Is the socket already registered?
208 if ($this->isSocketRegistered($infoInstance, $socketResource)) {
209 // Throw the exception
210 throw new SocketAlreadyRegisteredException(array($infoInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
213 // Does the instance exist?
214 if (!$this->isInfoRegistered($infoInstance)) {
215 // No, not found so we create a sub registry (not needed to configure!)
216 $registryInstance = SubRegistry::createSubRegistry();
218 // Now we can create the sub-registry for this info
219 $this->addInstance($this->getRegistryKeyFromInfo($infoInstance), $registryInstance);
221 // Get the sub-registry back
222 $registryInstance = $this->getInstance($this->getRegistryKeyFromInfo($infoInstance));
225 // Get a key for sub-registries
226 $socketKey = $this->getSubRegistryKey($infoInstance);
228 // Get a socket container
229 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $infoInstance, $packageData));
231 // We have a sub-registry, the socket key and the socket, now we need to put all together
232 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
233 $registryInstance->addInstance($socketKey, $socketInstance);
237 * Getter for given listener's socket resource
239 * @param $listenerInstance An instance of a Listenable class
240 * @return $socketResource A valid socket resource
241 * @throws NoSocketRegisteredException If the requested socket is not registered
243 public function getRegisteredSocketResource (Listenable $listenerInstance) {
245 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - ENTERED!');
247 // The socket must be registered before we can return it
248 if (!$this->isInfoRegistered($listenerInstance)) {
249 // Throw the exception
250 throw new NoSocketRegisteredException ($listenerInstance, self::SOCKET_NOT_REGISTERED);
253 // Now get the key from the listener
254 $key = $this->getRegistryKeyFromInfo($listenerInstance);
256 // And get the registry
257 $registryInstance = $this->getInstance($key);
260 $socketKey = $this->getSubRegistryKey($listenerInstance);
262 // And the final socket resource
263 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
266 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!');
268 // Return the resource
269 return $socketResource;
273 * "Getter" for protocol/connection instance from given package data
275 * @param $packageData Raw package data
276 * @return $protocolInstance An instance of a HandleableProtocol class
278 public function getHandlerInstanceFromPackageData (array $packageData) {
279 // Init protocol instance
280 $protocolInstance = NULL;
281 //* DIE-DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE));
283 // Get all keys and check them
284 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
286 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
288 // This is always a SubRegistry instance
289 foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
291 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
293 // Is this a SocketContainer instance and is the address the same?
294 if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
295 // Found one, so get the protocol instance and abort any further search
296 $protocolInstance = $containerInstance->getProtocolInstance();
299 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',protocolInstance[]=' . gettype($protocolInstance) . ' - FOUND!');
305 // Return the protocol instance
306 return $protocolInstance;