5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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() . ' - CALLED!');
70 // Get address and port
71 $address = $infoInstance->getAddress();
72 $port = $infoInstance->getPort();
75 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: address=' . $address . ',port=' . $port);
77 // Get connection type and port number and add both together
78 $key = sprintf('%s:%s:%s:%s',
79 $infoInstance->__toString(),
80 $infoInstance->getProtocolName(),
86 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ' - EXIT!');
88 // Return resulting key
93 * "Getter" to get a string respresentation of the listener
95 * @param $infoInstance An instance of a ShareableInfo class
96 * @return $key A string representation of the listener for the registry
98 private function getRegistryKeyFromInfo (ShareableInfo $infoInstance) {
100 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',infoInstance=' . $infoInstance->__toString() . ' - CALLED!');
103 $key = $infoInstance->getProtocolName();
106 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ' - EXIT!');
108 // Return resulting key
113 * Checks whether the shared connection info is registered
115 * @param $infoInstance An instance of a ShareableInfo class
116 * @return $isRegistered Whether the listener is registered
118 private function isInfoRegistered (ShareableInfo $infoInstance) {
120 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ' - CALLED!');
123 $key = $this->getRegistryKeyFromInfo($infoInstance);
126 $isRegistered = $this->instanceExists($key);
129 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
132 return $isRegistered;
136 * Checks whether given socket resource is registered. If $socketResource is
137 * FALSE only the instance will be checked.
139 * @param $infoInstance An instance of a ShareableInfo class
140 * @param $socketResource A valid socket resource
141 * @return $isRegistered Whether the given socket resource is registered
143 public function isSocketRegistered (ShareableInfo $infoInstance, $socketResource) {
145 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - CALLED!');
147 // Default is not registered
148 $isRegistered = FALSE;
150 // First, check for the instance, there can be only once
151 if ($this->isInfoRegistered($infoInstance)) {
152 // That one is found so "get" a registry key from it
153 $key = $this->getRegistryKeyFromInfo($infoInstance);
156 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
159 $registryInstance = $this->getInstance($key);
161 // "Get" a key for the socket
162 $socketKey = $this->getSubRegistryKey($infoInstance);
165 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
168 if ($registryInstance->instanceExists($socketKey)) {
170 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey));
173 $registeredInstance = $registryInstance->getInstance($socketKey);
175 // Is it SocketContainer and same socket?
176 $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource)));
179 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered));
184 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
187 return $isRegistered;
191 * Registeres given socket for listener or throws an exception if it is already registered
193 * @param $infoInstance An instance of a ShareableInfo class
194 * @param $socketResource A valid socket resource
195 * @param $packageData Optional raw package data
196 * @throws SocketAlreadyRegisteredException If the given socket is already registered
199 public function registerSocket (ShareableInfo $infoInstance, $socketResource, array $packageData = array()) {
201 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - CALLED!');
203 // Is the socket already registered?
204 if ($this->isSocketRegistered($infoInstance, $socketResource)) {
205 // Throw the exception
206 throw new SocketAlreadyRegisteredException(array($infoInstance, $socketResource), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
209 // Does the instance exist?
210 if (!$this->isInfoRegistered($infoInstance)) {
211 // No, not found so we create a sub registry (not needed to configure!)
212 $registryInstance = SubRegistry::createSubRegistry();
214 // Now we can create the sub-registry for this info
215 $this->addInstance($this->getRegistryKeyFromInfo($infoInstance), $registryInstance);
217 // Get the sub-registry back
218 $registryInstance = $this->getInstance($this->getRegistryKeyFromInfo($infoInstance));
221 // Get a key for sub-registries
222 $socketKey = $this->getSubRegistryKey($infoInstance);
224 // Get a socket container
225 $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $infoInstance, $packageData));
227 // We have a sub-registry, the socket key and the socket, now we need to put all together
228 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
229 $registryInstance->addInstance($socketKey, $socketInstance);
233 * Getter for given listener's socket resource
235 * @param $listenerInstance An instance of a Listenable class
236 * @return $socketResource A valid socket resource
237 * @throws NoSocketRegisteredException If the requested socket is not registered
239 public function getRegisteredSocketResource (Listenable $listenerInstance) {
241 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ' - CALLED!');
243 // The socket must be registered before we can return it
244 if (!$this->isInfoRegistered($listenerInstance)) {
245 // Throw the exception
246 throw new NoSocketRegisteredException ($listenerInstance, self::SOCKET_NOT_REGISTERED);
249 // Now get the key from the listener
250 $key = $this->getRegistryKeyFromInfo($listenerInstance);
252 // And get the registry
253 $registryInstance = $this->getInstance($key);
256 $socketKey = $this->getSubRegistryKey($listenerInstance);
258 // And the final socket resource
259 $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
262 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:listener=' . $listenerInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!');
264 // Return the resource
265 return $socketResource;
269 * "Getter" for info instance from given package data
271 * @param $packageData Raw package data
272 * @return $infoInstance An instance of a ShareableInfo class
274 public function getInfoInstanceFromPackageData (array $packageData) {
275 // Init info instance
276 $infoInstance = NULL;
277 //* DEBUG-DIE: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE));
279 // Get all keys and check them
280 foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
282 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
284 // This is always a SubRegistry instance
285 foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
287 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
289 // Is this a SocketContainer instance and is the address the same?
290 if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
292 //* DEBUG-DIE: */ die(__METHOD__ . ': containerInstance=' . print_r($containerInstance, TRUE));
294 // Get listener and helper instances
295 $listenerInstance = $containerInstance->getListenerInstance();
296 $helperInstance = $containerInstance->getHelperInstance();
299 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance));
301 // Is a listener or helper set?
302 if ($listenerInstance instanceof Listenable) {
303 // Found a listener, so get the info instance first
304 $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($listenerInstance->getProtocolName(), 'helper');
306 // Fill info instance with listener data
307 $infoInstance->fillWithListenerInformation($listenerInstance);
308 } elseif ($helperInstance instanceof ConnectionHelper) {
309 // Found a helper, so get the info instance first
310 $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($helperInstance->getProtocolName(), 'helper');
313 $infoInstance->fillWithConnectionHelperInformation($helperInstance);
315 // Not supported state!
316 $this->debugInstance('[' . __METHOD__ . ':' . __LINE__ . ']: Invalid state found, please report this to the developers with full debug infos.' . PHP_EOL);
320 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',infoInstance[' . gettype($infoInstance) . ']=' . $infoInstance->__toString() . ' with protocol ' . $infoInstance->getProtocolName() . ' - FOUND!');
325 // Is no longer NULL set?
326 if (!is_null($infoInstance)) {
327 // Then skip here, too
332 // Return the info instance
333 return $infoInstance;