+++ /dev/null
-<?php
-/**
- * A Connection registry
- *
- * @author Roland Haeder <webmaster@shipsimu.org>
- * @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
- * @license GNU GPL 3.0 or any newer version
- * @link http://www.shipsimu.org
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-class ConnectionRegistry extends BaseRegistry implements Register, RegisterableConnection {
- // Exception constants
- const CONNECTION_NOT_REGISTERED = 0xd100;
-
- /**
- * Instance of this class
- */
- private static $registryInstance = NULL;
-
- /**
- * Protected constructor
- *
- * @return void
- */
- protected function __construct () {
- // Call parent constructor
- parent::__construct(__CLASS__);
- }
-
- /**
- * Creates a singleton instance of this registry class
- *
- * @return $registryInstance An instance of this class
- */
- public static final function createConnectionRegistry () {
- // Is an instance there?
- if (is_null(self::$registryInstance)) {
- // Not yet, so create one
- self::$registryInstance = new ConnectionRegistry();
- } // END - if
-
- // Return the instance
- return self::$registryInstance;
- }
-
- /**
- * "Getter" to get a string respresentation for a key for the sub-registry
- * in this format: class:type:port
- *
- * @param $connectionInstance An instance of a ConnectionHelper class
- * @return $key A string representation of the socket for the registry
- */
- private function getSubRegistryKey (ConnectionHelper $connectionInstance) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: connectionInstance=' . $connectionInstance->__toString() . ' CALLED!');
-
- // Get connection type and port number and add both together
- $key = sprintf('%s:%s:%s',
- $connectionInstance->__toString(),
- $connectionInstance->getConnectionType(),
- $connectionInstance->getConnectionPort()
- );
-
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ' - EXIT!');
-
- // Return resulting key
- return $key;
- }
-
- /**
- * "Getter" to get a string respresentation of the protocol
- *
- * @param $infoInstance An instance of a ShareableInfo class
- * @return $key A string representation of the protocol for the registry
- */
- private function getRegistryKeyFromInfo (ShareableInfo $infoInstance) {
- // Get the key
- $key = $infoInstance->getProtocolName();
-
- // Return resulting key
- return $key;
- }
-
- /**
- * Checks whether the given protocol is registered
- *
- * @param $connectionInstance An instance of a ConnectionHelper class
- * @return $isRegistered Whether the protocol is registered
- */
- private function isConnectionRegistered (ConnectionHelper $connectionInstance) {
- // Get the key
- $key = $this->getRegistryKeyFromInfo($connectionInstance);
-
- // Determine it
- $isRegistered = $this->instanceExists($key);
-
- // Return result
- return $isRegistered;
- }
-
- /**
- * Checks whether given socket resource is registered. If $socketResource is
- * FALSE only the instance will be checked.
- *
- * @param $connectionInstance An instance of a ConnectionHelper class
- * @param $socketResource A valid socket resource
- * @return $isRegistered Whether the given socket resource is registered
- */
- public function isConnectionRegistered (ConnectionHelper $connectionInstance, $socketResource) {
- // Default is not registered
- $isRegistered = FALSE;
-
- // First, check for the instance, there can be only once
- if ($this->isConnectionRegistered($connectionInstance)) {
- // That one is found so "get" a registry key from it
- $key = $this->getRegistryKeyFromInfo($connectionInstance);
-
- // Get the registry
- $registryInstance = $this->getInstance($key);
-
- // "Get" a key for the socket
- $socketKey = $this->getSubRegistryKey($connectionInstance);
-
- // And simply ask it
- $isRegistered = $registryInstance->instanceExists($socketKey);
- } // END - if
-
- // Return the result
- return $isRegistered;
- }
-
- /**
- * Registeres given socket for listener or throws an exception if it is already registered
- *
- * @param $connectionInstance An instance of a ConnectionHelper class
- * @param $socketResource A valid socket resource
- * @param $packageData Optional raw package data
- * @throws ConnectionAlreadyRegisteredException If the given socket is already registered
- * @return void
- */
- public function registerConnection (ConnectionHelper $connectionInstance, $socketResource, array $packageData = array()) {
- // Is the socket already registered?
- if ($this->isConnectionRegistered($connectionInstance, $socketResource)) {
- // Throw the exception
- throw new ConnectionAlreadyRegisteredException(array($connectionInstance, $socketResource), BaseListener::EXCEPTION_CONNECTION_ALREADY_REGISTERED);
- } // END - if
-
- // Does the instance exist?
- if (!$this->isConnectionRegistered($connectionInstance)) {
- // No, not found so we create a sub registry (not needed to configure!)
- $registryInstance = SubRegistry::createSubRegistry();
-
- // Now we can create the sub-registry for this protocol
- $this->addInstance($this->getRegistryKeyFromInfo($connectionInstance), $registryInstance);
- } else {
- // Get the sub-registry back
- $registryInstance = $this->getInstance($this->getRegistryKeyFromInfo($connectionInstance));
- }
-
- // Get a key for sub-registries
- $socketKey = $this->getSubRegistryKey($connectionInstance);
-
- // Get a socket container
- $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $connectionInstance, $packageData));
-
- // We have a sub-registry, the socket key and the socket, now we need to put all together
- /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: socketKey=' . $socketKey . ',socketResource=' . $socketResource . ' - adding socket container instance ...');
- $registryInstance->addInstance($socketKey, $socketInstance);
- }
-
- /**
- * Getter for given listener's socket resource
- *
- * @param $connectionInstance An instance of a ConnectionHelper class
- * @return $socketResource A valid socket resource
- * @throws NoConnectionRegisteredException If the requested socket is not registered
- */
- public function getRegisteredConnectionResource (ConnectionHelper $connectionInstance) {
- // The socket must be registered before we can return it
- if (!$this->isConnectionRegistered($connectionInstance, FALSE)) {
- // Throw the exception
- throw new NoConnectionRegisteredException ($connectionInstance, self::CONNECTION_NOT_REGISTERED);
- } // END - if
-
- // Now get the key from the protocol
- $key = $this->getRegistryKeyFromInfo($connectionInstance);
-
- // And get the registry
- $registryInstance = $this->getInstance($key);
-
- // Get a socket key
- $socketKey = $this->getSubRegistryKey($connectionInstance);
-
- // And the final socket resource
- $socketResource = $registryInstance->getInstance($socketKey)->getConnectionResource();
-
- // Return the resource
- return $socketResource;
- }
-
- /**
- * "Getter" for protocol/connection instance from given package data
- *
- * @param $packageData Raw package data
- * @return $connectionInstance An instance of a ConnectionHelper class
- */
- public function getHandlerInstanceFromPackageData (array $packageData) {
- // Init protocol instance
- $connectionInstance = NULL;
-
- // Get all keys and check them
- foreach ($this->getInstanceRegistry() as $key => $registryInstance) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
-
- // This is always a SubRegistry instance
- foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
-
- // This is a ConnectionContainer instance, so does the recipient match?
- if ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) {
- // Found one, so get the protocol instance and abort any further search
- $connectionInstance = $containerInstance->getProtocolInstance();
- break;
- } // END - if
- } // END - foreach
- } // END - foreach
-
- // Return the protocol instance
- return $connectionInstance;
- }
-}
-
-// [EOF]
-?>