From 00b44aa1d827369e6c72caa6a3a5f23c23e009bf Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 30 Jan 2015 01:34:18 +0100 Subject: [PATCH] Continued with refacturing: - Deleted deprecated stuff, such as the ConnectionRegistry class as there is now only a SocketRegistry. - More cleanups (e.g. no longer used config entry and many more) - Use more ShareableInfo class which main contain a Listenable or ConnectionHelper instance. If both is missing, please report this. - Updated 'core' Signed-off-by: Roland Haeder --- application/hub/config.php | 3 - .../protocol/class_DiscoverableProtocol.php | 8 - .../shareable/info/class_ShareableInfo.php | 10 +- application/hub/main/class_BaseHubSystem.php | 24 ++ .../protocol/class_ProtocolDiscovery.php | 25 +- .../class_BaseRecipientDiscovery.php | 23 -- .../class_PackageRecipientDiscovery.php | 10 +- .../socket/class_PackageSocketDiscovery.php | 2 +- .../handler/class_ProtocolHandlerFactory.php | 5 +- .../info/class_ConnectionInfoFactory.php | 13 +- .../ipv4/tcp/class_TcpConnectionHelper.php | 2 +- .../info/connection/class_ConnectionInfo.php | 38 ++- .../hub/main/listener/class_BaseListener.php | 4 +- .../main/listener/tcp/class_TcpListener.php | 2 +- .../hub/main/nodes/boot/class_HubBootNode.php | 2 +- .../hub/main/package/class_NetworkPackage.php | 12 +- .../hub/main/registry/connection/.htaccess | 1 - .../connection/class_ConnectionRegistry.php | 250 ------------------ .../registry/socket/class_SocketRegistry.php | 77 ++++-- core | 2 +- 20 files changed, 174 insertions(+), 339 deletions(-) delete mode 100644 application/hub/main/registry/connection/.htaccess delete mode 100644 application/hub/main/registry/connection/class_ConnectionRegistry.php diff --git a/application/hub/config.php b/application/hub/config.php index f84c45302..3afe1f12b 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -732,9 +732,6 @@ $cfg->setConfigEntry('dht_recipient_discovery_class', 'DhtRecipientDiscovery'); // CFG: UNL-DISCOVERY-CLASS $cfg->setConfigEntry('unl_discovery_class', 'UniversalNodeLocatorDiscovery'); -// CFG: PROTOCOL-DISCOVERY-CLASS -$cfg->setConfigEntry('protocol_discovery_class', 'ProtocolDiscovery'); - // CFG: TCP-PROTOCOL-HANDLER-CLASS $cfg->setConfigEntry('tcp_protocol_handler_class', 'TcpProtocolHandler'); diff --git a/application/hub/interfaces/discovery/protocol/class_DiscoverableProtocol.php b/application/hub/interfaces/discovery/protocol/class_DiscoverableProtocol.php index 97c894f86..e2c22d982 100644 --- a/application/hub/interfaces/discovery/protocol/class_DiscoverableProtocol.php +++ b/application/hub/interfaces/discovery/protocol/class_DiscoverableProtocol.php @@ -22,14 +22,6 @@ * along with this program. If not, see . */ interface DiscoverableProtocol extends Discoverable { - /** - * "Discovers" the protocol type from given raw package data. This is done - * by looking at the 'recipient' field and extract the first part from it. - * - * @param $packageData Raw package data - * @return $protocolType Type of protocol, e.g. 'tcp' for TCP/IPv4 connections - */ - function discoverProtocolByPackageData (array $packageData); } // [EOF] diff --git a/application/hub/interfaces/shareable/info/class_ShareableInfo.php b/application/hub/interfaces/shareable/info/class_ShareableInfo.php index 89b3d248a..0b37a28ac 100644 --- a/application/hub/interfaces/shareable/info/class_ShareableInfo.php +++ b/application/hub/interfaces/shareable/info/class_ShareableInfo.php @@ -25,10 +25,18 @@ interface ShareableInfo extends FrameworkInterface { /** * Fills the information class with data from a Listenable instance * - * @param $listenerInstanc An instance of a Listenable class + * @param $listenerInstance An instance of a Listenable class * @return void */ function fillWithListenerInformation (Listenable $listenerInstance); + + /** + * Fills the information class with data from a ConnectionHelper instance + * + * @param $helperInstance An instance of a ConnectionHelper class + * @return void + */ + function fillWithConnectionHelperInformation (ConnectionHelper $helperInstance); } // [EOF] diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index bba43c512..bf644d940 100644 --- a/application/hub/main/class_BaseHubSystem.php +++ b/application/hub/main/class_BaseHubSystem.php @@ -104,6 +104,11 @@ class BaseHubSystem extends BaseFrameworkSystem { */ private $assemblerInstance = NULL; + /** + * Info instance + */ + private $infoInstance = NULL; + /** * Name of used protocol */ @@ -348,6 +353,25 @@ class BaseHubSystem extends BaseFrameworkSystem { return $this->assemblerInstance; } + /** + * Setter for info instance + * + * @param $infoInstance A ShareableInfo instance + * @return void + */ + protected final function setInfoInstance (ShareableInfo $infoInstance) { + $this->infoInstance = $infoInstance; + } + + /** + * Getter for info instance + * + * @return $infoInstance A Decodeable instance + */ + public final function getInfoInstance () { + return $this->infoInstance; + } + /** * Setter for node id * diff --git a/application/hub/main/discovery/protocol/class_ProtocolDiscovery.php b/application/hub/main/discovery/protocol/class_ProtocolDiscovery.php index e95a8914e..e6f8feabc 100644 --- a/application/hub/main/discovery/protocol/class_ProtocolDiscovery.php +++ b/application/hub/main/discovery/protocol/class_ProtocolDiscovery.php @@ -45,6 +45,29 @@ class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtoco return $discoveryInstance; } + /** + * Determines the protoctol name + * + * @param $packageData Valid package data + * @return $protocolInstance An instance of a HandleableProtocol class + */ + public static final function determineProtocolByPackageData (array $packageData) { + // First we need a tags instance + $tagsInstance = PackageTagsFactory::createPackageTagsInstance(); + + /* + * We need to decide here which socket (TCP or UDP) should be used for + * the actual data transmission. In this process we will find out if + * the recipient of this package has already a known (registered) socket + * and if so we can re-use it. If there is no socket registered, we try + * to make a new connection to the given Universal Node Locator. + */ + $protocolInstance = $tagsInstance->chooseProtocolFromPackageData($packageData); + + // Return it + return $protocolInstance; + } + /** * "Discovers" the protocol type from given raw package data. This is done * by looking at the 'recipient' field and extract the first part from it. @@ -52,7 +75,7 @@ class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtoco * @param $packageData Raw package data * @return $protocolType Type of protocol, e.g. 'tcp' for TCP/IPv4 connections */ - public function discoverProtocolByPackageData (array $packageData) { + public static final function discoverProtocolByPackageData (array $packageData) { //* DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE)); /* diff --git a/application/hub/main/discovery/recipient/class_BaseRecipientDiscovery.php b/application/hub/main/discovery/recipient/class_BaseRecipientDiscovery.php index 56d3db4d3..b0fb44262 100644 --- a/application/hub/main/discovery/recipient/class_BaseRecipientDiscovery.php +++ b/application/hub/main/discovery/recipient/class_BaseRecipientDiscovery.php @@ -40,29 +40,6 @@ class BaseRecipientDiscovery extends BaseNodeDiscovery implements DiscoverableRe $this->setListInstance($listInstance); } - /** - * Determines the protoctol name - * - * @param $packageData Valid package data - * @return $protocolInstance An instance of a HandleableProtocol class - */ - protected function determineProtocolByPackageData (array $packageData) { - // First we need a tags instance - $tagsInstance = PackageTagsFactory::createPackageTagsInstance(); - - /* - * We need to decide here which socket (TCP or UDP) should be used for - * the actual data transmission. In this process we will find out if - * the recipient of this package has already a known (registered) socket - * and if so we can re-use it. If there is no socket registered, we try - * to make a new connection to the given Universal Node Locator. - */ - $protocolInstance = $tagsInstance->chooseProtocolFromPackageData($packageData); - - // Return it - return $protocolInstance; - } - /** * "Getter" for recipient iterator * diff --git a/application/hub/main/discovery/recipient/package/class_PackageRecipientDiscovery.php b/application/hub/main/discovery/recipient/package/class_PackageRecipientDiscovery.php index dbde1508d..367d21c91 100644 --- a/application/hub/main/discovery/recipient/package/class_PackageRecipientDiscovery.php +++ b/application/hub/main/discovery/recipient/package/class_PackageRecipientDiscovery.php @@ -88,17 +88,16 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov */ public function discoverRawRecipients (array $decodedData) { // This must be available - die(__METHOD__ . ': Unfinished' . PHP_EOL); assert(isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); // First clear all recipients $this->clearRecipients(); - // Explode 'recipient', first element is the IP/hostname - $recipient = explode(':', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + // Get a protocol handler back from decoded data + $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($decodedData); // Is the 'recipient' field same as this peer's IP? - if ((($recipient[0] == HubTools::determineOwnExternalAddress()) && ($recipient[1] == $this->getConfigInstance()->getConfigEntry('node_listen_port'))) || ($recipient[0] == $this->getConfigInstance()->getServerAddress())) { + if ($handlerInstance->isOwnAddress()) { /* * Is same as own external address + TCP/UDP listen port or * internal address, don't do anything here so other classes found @@ -113,8 +112,9 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient ' . $recipient[0] . ' is different than own external address (' . HubTools::determineOwnExternalAddress() . ') nor internal address (' . $this->getConfigInstance()->getServerAddress() . '), need to forward (not yet implemented)!'); // This package is to be delivered to someone else, so add it - $this->getListInstance()->addEntry('unl', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + //$this->getListInstance()->addEntry('unl', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); } + die(__METHOD__ . ': Unfinished!' . PHP_EOL); } } diff --git a/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php index f7724cffd..cda8dcd4f 100644 --- a/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php +++ b/application/hub/main/discovery/recipient/socket/class_PackageSocketDiscovery.php @@ -120,7 +120,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); // Determine protocol instance - $protocolInstance = $this->determineProtocolByPackageData($packageData); + $protocolInstance = ProtocolDiscovery::determineProtocolByPackageData($packageData); // Is it valid? assert($protocolInstance instanceof HandleableProtocol); diff --git a/application/hub/main/factories/handler/class_ProtocolHandlerFactory.php b/application/hub/main/factories/handler/class_ProtocolHandlerFactory.php index 12ada2e94..b5b54bccb 100644 --- a/application/hub/main/factories/handler/class_ProtocolHandlerFactory.php +++ b/application/hub/main/factories/handler/class_ProtocolHandlerFactory.php @@ -79,11 +79,8 @@ class ProtocolHandlerFactory extends ObjectFactory { * @return $handlerInstance A protocol handler instance */ public static final function createProtocolHandlerFromPackageData (array $packageData) { - // Now that there is no direct node instance available, a protocol discovery class is required - $discoveryInstance = ObjectFactory::createObjectByConfiguredName('protocol_discovery_class'); - // "Discover" the protocol type - $protocolType = $discoveryInstance->discoverProtocolByPackageData($packageData); + $protocolType = ProtocolDiscovery::discoverProtocolByPackageData($packageData); // Call super factory method return self::createProtocolHandlerByType($protocolType); diff --git a/application/hub/main/factories/info/class_ConnectionInfoFactory.php b/application/hub/main/factories/info/class_ConnectionInfoFactory.php index a5d049d2c..05700060e 100644 --- a/application/hub/main/factories/info/class_ConnectionInfoFactory.php +++ b/application/hub/main/factories/info/class_ConnectionInfoFactory.php @@ -35,19 +35,24 @@ class ConnectionInfoFactory extends ObjectFactory { /** * Returns a singleton (registry-based) ShareableInfo instance * + * @param $protocolName Name of protocol (e.g. 'tcp') + * @param $type Connection type ('listener' or 'helper') * @return $infoInstance An instance of a ShareableInfo class */ - public static final function createConnectionInfoInstance () { + public static final function createConnectionInfoInstance ($protocolName, $type) { + // Generate key + $key = 'connection_info_' . $protocolName . '_' . $type; + // If there is no info? - if (Registry::getRegistry()->instanceExists('connection_info')) { + if (Registry::getRegistry()->instanceExists($key)) { // Get info from registry - $infoInstance = Registry::getRegistry()->getInstance('connection_info'); + $infoInstance = Registry::getRegistry()->getInstance($key); } else { // Get the info instance $infoInstance = self::createObjectByConfiguredName('connection_info_class'); // Add it to the registry - Registry::getRegistry()->addInstance('connection_info', $infoInstance); + Registry::getRegistry()->addInstance($key, $infoInstance); } // Return the instance diff --git a/application/hub/main/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php b/application/hub/main/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php index 0e879ef29..0c749946f 100644 --- a/application/hub/main/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/main/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -92,7 +92,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Try to solve the recipient try { - // Resolve any session ids; 0 = IP, 1 = Port + // Get protocol handler back from package data $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData); // Get UNL data diff --git a/application/hub/main/info/connection/class_ConnectionInfo.php b/application/hub/main/info/connection/class_ConnectionInfo.php index 3ec070a45..f9751b569 100644 --- a/application/hub/main/info/connection/class_ConnectionInfo.php +++ b/application/hub/main/info/connection/class_ConnectionInfo.php @@ -48,17 +48,45 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { /** * Fills the information class with data from a Listenable instance * - * @param $listenerInstanc An instance of a Listenable class + * @param $listenerInstance An instance of a Listenable class * @return void */ public function fillWithListenerInformation (Listenable $listenerInstance) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: protocolName=' . $listenerInstance->getProtocolName() . ',listenerInstance=' . $listenerInstance->__toString() . ' - CALLED!'); + // Fill the generic array with several data from the listener: $this->setProtocolName($listenerInstance->getProtocolName()); - $this->setGenericArrayElement('listen', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_ADDRESS , $listenerInstance->getListenAddress()); - $this->setGenericArrayElement('listen', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_PORT , $listenerInstance->getListenPort()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_ADDRESS , $listenerInstance->getListenAddress()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_PORT , $listenerInstance->getListenPort()); // Set listener here $this->setListenerInstance($listenerInstance); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!'); + } + + /** + * Fills the information class with data from a ConnectionHelper instance + * + * @param $helperInstance An instance of a ConnectionHelper class + * @return void + */ + public function fillWithConnectionHelperInformation (ConnectionHelper $helperInstance) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: protocolName=' . $helperInstance->getProtocolName() . ',helperInstance=' . $helperInstance->__toString() . ',socketResource=' . $helperInstance->getSocketResource() . ' - CALLED!'); + + // Fill the generic array with several data from the listener: + $this->setProtocolName($helperInstance->getProtocolName()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_ADDRESS , $helperInstance->getAddress()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_PORT , $helperInstance->getConnectionPort()); + + // Set helper here + $this->setHelperInstance($helperInstance); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!'); } /** @@ -68,7 +96,7 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { */ public final function getAddress () { // Return it from generic array - return $this->getGenericArrayElement('listen', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_ADDRESS); + return $this->getGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_ADDRESS); } /** @@ -78,7 +106,7 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { */ public final function getPort () { // Return it from generic array - return $this->getGenericArrayElement('listen', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_PORT); + return $this->getGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_PORT); } } diff --git a/application/hub/main/listener/class_BaseListener.php b/application/hub/main/listener/class_BaseListener.php index adf252d1a..f541d30c9 100644 --- a/application/hub/main/listener/class_BaseListener.php +++ b/application/hub/main/listener/class_BaseListener.php @@ -218,7 +218,7 @@ class BaseListener extends BaseHubSystem implements Visitable { $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); // Get a connection info instance - $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance(); + $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener'); // Will the info instance with listener data $infoInstance->fillWithListenerInformation($this); @@ -241,7 +241,7 @@ class BaseListener extends BaseHubSystem implements Visitable { $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); // Get a connection info instance - $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance(); + $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener'); // Will the info instance with listener data $infoInstance->fillWithListenerInformation($this); diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index faacca976..6900a6288 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -290,7 +290,7 @@ class TcpListener extends BaseListener implements Listenable { ); // Get a connection info instance - $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance(); + $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener'); // Will the info instance with listener data $infoInstance->fillWithListenerInformation($this); diff --git a/application/hub/main/nodes/boot/class_HubBootNode.php b/application/hub/main/nodes/boot/class_HubBootNode.php index de02f5659..55767b0c6 100644 --- a/application/hub/main/nodes/boot/class_HubBootNode.php +++ b/application/hub/main/nodes/boot/class_HubBootNode.php @@ -62,7 +62,7 @@ class HubBootNode extends BaseHubNode implements NodeHelper, Registerable { $unl = $this->detectOwnUniversalNodeLocator(); // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl); // Now check if the IP address matches one of the bootstrap nodes if ($this->ifAddressMatchesBootstrapNodes($unl)) { diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 4561e08e2..b76e6e818 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -529,7 +529,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R assert($helperInstance instanceof ConnectionHelper); // Get connection info class - $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance(); + $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($helperInstance->getProtocolName(), 'helper'); // Will the info instance with connection helper data $infoInstance->fillWithConnectionHelperInformation($helperInstance); @@ -576,10 +576,18 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R $sentBytes = 0; // Get the right connection instance - $helperInstance = SocketRegistryFactory::createSocketRegistryInstance()->getHandlerInstanceFromPackageData($packageData); + $infoInstance = SocketRegistryFactory::createSocketRegistryInstance()->getInfoInstanceFromPackageData($packageData); // Test helper instance + assert($infoInstance instanceof ShareableInfo); + + // Get helper instance + $helperInstance = $infoInstance->getHelperInstance(); + + // Some sanity-checks on the object + //* DEBUG-DIE: */ die('[' . __METHOD__ . ':' . __LINE__ . ']: p1=' . $infoInstance->getProtocolName() . ',p2=' . $helperInstance->getProtocolName() . ',infoInstance=' . print_r($infoInstance, TRUE)); assert($helperInstance instanceof ConnectionHelper); + assert($infoInstance->getProtocolName() == $helperInstance->getProtocolName()); // Is this connection still alive? if ($helperInstance->isShuttedDown()) { diff --git a/application/hub/main/registry/connection/.htaccess b/application/hub/main/registry/connection/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/main/registry/connection/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/main/registry/connection/class_ConnectionRegistry.php b/application/hub/main/registry/connection/class_ConnectionRegistry.php deleted file mode 100644 index e6aa06d7f..000000000 --- a/application/hub/main/registry/connection/class_ConnectionRegistry.php +++ /dev/null @@ -1,250 +0,0 @@ - - * @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 . - */ -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] -?> diff --git a/application/hub/main/registry/socket/class_SocketRegistry.php b/application/hub/main/registry/socket/class_SocketRegistry.php index 737faf8c2..37cbda2eb 100644 --- a/application/hub/main/registry/socket/class_SocketRegistry.php +++ b/application/hub/main/registry/socket/class_SocketRegistry.php @@ -67,10 +67,6 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: infoInstance=' . $infoInstance->__toString() . ' - ENTERED!'); - // Default address is invalid - $address = '*invalid*'; - $port = 0; - // Get address and port $address = $infoInstance->getAddress(); $port = $infoInstance->getPort(); @@ -121,7 +117,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke */ private function isInfoRegistered (ShareableInfo $infoInstance) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ' - ENTERED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ' - ENTERED!'); // Get the key $key = $this->getRegistryKeyFromInfo($infoInstance); @@ -130,7 +126,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke $isRegistered = $this->instanceExists($key); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); // Return result return $isRegistered; @@ -146,7 +142,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke */ public function isSocketRegistered (ShareableInfo $infoInstance, $socketResource) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!'); // Default is not registered $isRegistered = FALSE; @@ -157,7 +153,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke $key = $this->getRegistryKeyFromInfo($infoInstance); // Debug message - //* 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 ...'); + //* 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 ...'); // Get the registry $registryInstance = $this->getInstance($key); @@ -166,7 +162,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke $socketKey = $this->getSubRegistryKey($infoInstance); // Debug message - //* 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 ...'); + //* 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 ...'); // Is it there? if ($registryInstance->instanceExists($socketKey)) { @@ -185,7 +181,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke } // END - if // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); // Return the result return $isRegistered; @@ -202,7 +198,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke */ public function registerSocket (ShareableInfo $infoInstance, $socketResource, array $packageData = array()) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getConnectionType() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']:info=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!'); // Is the socket already registered? if ($this->isSocketRegistered($infoInstance, $socketResource)) { @@ -270,40 +266,71 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke } /** - * "Getter" for protocol/connection instance from given package data + * "Getter" for info instance from given package data * - * @param $packageData Raw package data - * @return $protocolInstance An instance of a HandleableProtocol class + * @param $packageData Raw package data + * @return $infoInstance An instance of a ShareableInfo class */ - public function getHandlerInstanceFromPackageData (array $packageData) { - // Init protocol instance - $protocolInstance = NULL; - //* DIE-DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE)); + public function getInfoInstanceFromPackageData (array $packageData) { + // Init info instance + $infoInstance = NULL; + //* DEBUG-DIE: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE)); // Get all keys and check them foreach ($this->getInstanceRegistry() as $key => $registryInstance) { // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',registryInstance=' . $registryInstance->__toString()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-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('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString()); // Is this a SocketContainer instance and is the address the same? if (($containerInstance instanceof SocketContainer) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) { - // Found one, so get the protocol instance and abort any further search - $protocolInstance = $containerInstance->getProtocolInstance(); + // Debug die + //* DEBUG-DIE: */ die(__METHOD__ . ': containerInstance=' . print_r($containerInstance, TRUE)); + + // Get listener and helper instances + $listenerInstance = $containerInstance->getListenerInstance(); + $helperInstance = $containerInstance->getHelperInstance(); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance)); + + // Is a listener or helper set? + if ($listenerInstance instanceof Listenable) { + // Found a listener, so get the info instance first + $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($listenerInstance->getProtocolName(), 'helper'); + + // Fill info instance with listener data + $infoInstance->fillWithListenerInformation($listenerInstance); + } elseif ($helperInstance instanceof ConnectionHelper) { + // Found a helper, so get the info instance first + $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($helperInstance->getProtocolName(), 'helper'); + + // Helper is found + $infoInstance->fillWithConnectionHelperInformation($helperInstance); + } else { + // Not supported state! + $this->debugInstance('[' . __METHOD__ . ':' . __LINE__ . ']: Invalid state found, please report this to the developers with full debug infos.' . PHP_EOL); + } // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',protocolInstance[]=' . gettype($protocolInstance) . ' - FOUND!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-REGISTRY[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',subKey=' . $subKey . ',infoInstance[' . gettype($infoInstance) . ']=' . $infoInstance->__toString() . ' with protocol ' . $infoInstance->getProtocolName() . ' - FOUND!'); break; } // END - if } // END - foreach + + // Is no longer NULL set? + if (!is_null($infoInstance)) { + // Then skip here, too + break; + } // END - if } // END - foreach - // Return the protocol instance - return $protocolInstance; + // Return the info instance + return $infoInstance; } } diff --git a/core b/core index fd8bfc0a9..12417a738 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit fd8bfc0a9fdc898d7158029673b9dc42ad7fdf01 +Subproject commit 12417a7382d7ab76dacb985819e30b57db01f1fd -- 2.39.2