From 613741911a48e25622d0558f6a2c89a8cdfd2bef Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 27 Oct 2020 21:18:40 +0100 Subject: [PATCH] Continued: - renamed $unlInstance to $locatorInstance - implemented findNodeByUniversalNodeLocator() - WIP: started implementing looking up session id by UNL (internal or external) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../hub/classes/class_BaseHubSystem.php | 6 +- .../container/class_BaseHubContainer.php | 6 +- .../socket/class_SocketContainer.php | 14 +- .../frontend/class_BaseHubDatabaseWrapper.php | 6 +- ...odeDistributedHashTableDatabaseWrapper.php | 8 +- .../class_PeerStateLookupDatabaseWrapper.php | 2 +- .../classes/dht/node/class_NodeDhtFacade.php | 25 +++ .../class_UniversalNodeLocatorDiscovery.php | 10 +- .../class_PackageRecipientDiscovery.php | 2 +- .../class_UniversalNodeLocatorFactory.php | 20 +- .../classes/handler/class_BaseHubHandler.php | 6 +- .../package/class_NetworkPackageHandler.php | 18 +- .../protocol/class_BaseProtocolHandler.php | 4 +- .../ipv4/class_BaseIpV4ProtocolHandler.php | 6 +- .../ipv4/tcp/class_TcpConnectionHelper.php | 8 +- .../classes/listener/class_BaseListener.php | 6 +- .../listener/class_BaseListenerDecorator.php | 4 +- .../locator/class_UniversalNodeLocator.php | 203 ++++++++++-------- .../hub/classes/nodes/class_BaseHubNode.php | 12 +- .../package/deliverable/class_PackageData.php | 4 +- .../fragmenter/class_PackageFragmenter.php | 25 ++- .../pools/peer/class_DefaultPeerPool.php | 8 +- .../direct/class_DirectRecipient.php | 2 +- .../tcp/class_TcpProtocolResolver.php | 6 +- .../hub/classes/tools/hub/class_HubTools.php | 40 ++-- .../hub/interfaces/class_HubInterface.php | 2 +- application/hub/interfaces/dht/.htaccess | 1 - ...class_DiscoverableUniversalNodeLocator.php | 4 +- .../node/class_DistributableNode.php | 9 + .../hub/interfaces/nodes/class_Node.php | 2 +- .../resolver/class_ProtocolResolver.php | 2 +- 31 files changed, 275 insertions(+), 196 deletions(-) delete mode 100644 application/hub/interfaces/dht/.htaccess diff --git a/application/hub/classes/class_BaseHubSystem.php b/application/hub/classes/class_BaseHubSystem.php index 8857121af..098b0bb8a 100644 --- a/application/hub/classes/class_BaseHubSystem.php +++ b/application/hub/classes/class_BaseHubSystem.php @@ -426,12 +426,12 @@ abstract class BaseHubSystem extends BaseFrameworkSystem implements HubInterface /** * Setter for UNL instance * - * @para $unlInstance An instance of a LocateableNode class + * @para $locatorInstance An instance of a LocateableNode class * @return void */ - protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + protected final function setUniversalNodeLocatorInstance (LocateableNode $locatorInstance) { // Set new UNL data array - $this->universalNodeLocatorInstance = $unlInstance; + $this->universalNodeLocatorInstance = $locatorInstance; } /** diff --git a/application/hub/classes/container/class_BaseHubContainer.php b/application/hub/classes/container/class_BaseHubContainer.php index 2a33da9dc..52b04714b 100644 --- a/application/hub/classes/container/class_BaseHubContainer.php +++ b/application/hub/classes/container/class_BaseHubContainer.php @@ -156,12 +156,12 @@ abstract class BaseHubContainer extends BaseContainer implements HubInterface { /** * Setter for UNL instance * - * @para $unlInstance An instance of a LocateableNode class + * @para $locatorInstance An instance of a LocateableNode class * @return void */ - protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + protected final function setUniversalNodeLocatorInstance (LocateableNode $locatorInstance) { // Set new UNL data array - $this->universalNodeLocatorInstance = $unlInstance; + $this->universalNodeLocatorInstance = $locatorInstance; } /** diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index aa8ab84fb..bb84047b6 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -383,10 +383,10 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $recipient = $this->getSocketRecipient(); // Generate UNL instance for it - $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient); + $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient); // Get address part - $recipientAddress = $unlInstance->getUnlAddress(); + $recipientAddress = $locatorInstance->getUnlAddress(); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientAddress=%s - EXIT!', strtoupper($this->getSocketProtocol()), $recipientAddress)); @@ -415,10 +415,10 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $recipient = $this->getSocketRecipient(); // Generate UNL instance for it - $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient); + $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient); // Get port part - $recipientPort = $unlInstance->getUnlPort(); + $recipientPort = $locatorInstance->getUnlPort(); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientPort=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($recipientPort))); @@ -691,13 +691,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlRecipient=%s', strtoupper($this->getSocketProtocol()), $unlRecipient)); // Create UNL instance from it. This will validate the connection - $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($unlRecipient); + $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($unlRecipient); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlAddress=%s,unlPort=%d', strtoupper($this->getSocketProtocol()), $unlInstance->getUnlAddress(), $unlInstance->getUnlPort())); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlAddress=%s,unlPort=%d', strtoupper($this->getSocketProtocol()), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort())); // Try to connect to it - $result = socket_connect($this->getSocketResource(), $unlInstance->getUnlAddress(), $unlInstance->getUnlPort()); + $result = socket_connect($this->getSocketResource(), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort()); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); diff --git a/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php b/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php index bad6fb596..f4902aeb2 100644 --- a/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php @@ -156,12 +156,12 @@ abstract class BaseHubDatabaseWrapper extends BaseDatabaseWrapper implements Hub /** * Setter for UNL instance * - * @para $unlInstance An instance of a LocateableNode class + * @para $locatorInstance An instance of a LocateableNode class * @return void */ - protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + protected final function setUniversalNodeLocatorInstance (LocateableNode $locatorInstance) { // Set new UNL data array - $this->universalNodeLocatorInstance = $unlInstance; + $this->universalNodeLocatorInstance = $locatorInstance; } /** diff --git a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php index cc07f0bc5..6bfa6fb15 100644 --- a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -174,10 +174,10 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID); // Get Universal Node Locator and "explode" it - $unlInstance = $this->getNodeInstance()->determineUniversalNodeLocator(); + $locatorInstance = $this->getNodeInstance()->determineUniversalNodeLocator(); // Get external UNL - $externalUnl = $unlInstance->getExternalUnl(); + $externalUnl = $locatorInstance->getExternalUnl(); // Make sure both is valid // @TODO Bad check on UNL, better use a proper validator @@ -219,11 +219,11 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Get Universal Node Locator and "explode" it - $unlInstance = $this->getNodeInstance()->determineUniversalNodeLocator(); + $locatorInstance = $this->getNodeInstance()->determineUniversalNodeLocator(); // Make sure the external address is set and not invalid // @TODO Bad check on UNL, better use a proper validator - $externalUnl = $unlInstance->getExternalUnl(); + $externalUnl = $locatorInstance->getExternalUnl(); /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: externalUnl=' . $externalUnl); assert($externalUnl != 'invalid'); diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php index 1a9c356ff..3ff61b539 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php @@ -198,7 +198,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_STATE , $stateInstance->getStateName()); // Try to resolve sender's session id - $senderData = explode(':', HubTools::resolveSessionId($packageInstance->getSenderAddress())); + $senderData = explode(':', HubTools::resolveSessionIdToUnl($packageInstance->getSenderAddress())); // Just make sure that 'invalid:invalid' is not being processed assert(($senderData[0] != 'invalid') && ($senderData[1] != 'invalid') && ($senderData[2] != 'invalid')); diff --git a/application/hub/classes/dht/node/class_NodeDhtFacade.php b/application/hub/classes/dht/node/class_NodeDhtFacade.php index 9d042119b..66f7ba1d6 100644 --- a/application/hub/classes/dht/node/class_NodeDhtFacade.php +++ b/application/hub/classes/dht/node/class_NodeDhtFacade.php @@ -6,6 +6,7 @@ namespace Org\Shipsimu\Hub\Dht\Node; use Org\Shipsimu\Hub\Dht\BaseDht; use Org\Shipsimu\Hub\Dht\Distributable; use Org\Shipsimu\Hub\Generic\BaseHubSystem; +use Org\Shipsimu\Hub\Locator\Node\LocateableNode; use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff @@ -188,6 +189,30 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { return $nodeData; } + /** + * Finds a node locally by given UNL instance + * + * @param $locatorInstance An instance of a LocateableNode class + * @return $nodeData Node-data array + */ + public function findNodeByUniversalNodeLocator (LocateableNode $locatorInstance) { + // Default is empty data array + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: locatorInstance=%s - CALLED!', $locatorInstance->__toString())); + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: locatorInstance=%s', __METHOD__, __LINE__, print_r($locatorInstance, TRUE))); + $nodeData = array(); + + // Query database + $resultInstance = $this->getWrapperInstance()->findNodeLocalByUnlInstance($locatorInstance); + + // Make sure the result instance is valid + assert($resultInstance instanceof SearchableResult); + + // Return node data + /* DEBUG-DIE: */ die(sprintf('[%s:%d]: nodeData=%s', __METHOD__, __LINE__, print_r($nodeData, TRUE))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: nodeData()=%d - EXIT!', count($nodeData))); + return $nodeData; + } + /** * Registers an other node with this node by given message data. The * following data must always be present: diff --git a/application/hub/classes/discovery/node/class_UniversalNodeLocatorDiscovery.php b/application/hub/classes/discovery/node/class_UniversalNodeLocatorDiscovery.php index 792543efd..97c7f07ba 100644 --- a/application/hub/classes/discovery/node/class_UniversalNodeLocatorDiscovery.php +++ b/application/hub/classes/discovery/node/class_UniversalNodeLocatorDiscovery.php @@ -61,8 +61,8 @@ class UniversalNodeLocatorDiscovery extends BaseNodeDiscovery implements Discove * "Discovers" an instance of a LocateableNode class for given Node class * * @param $nodeInstance An instance of a Node class - * @return $unlInstance An instance of a LocateableNode class - * @throws NullPointerException If $unlInstance is NULL + * @return $locatorInstance An instance of a LocateableNode class + * @throws NullPointerException If $locatorInstance is NULL */ public function discoverUniversalNodeLocatorByNode (Node $nodeInstance) { /* @@ -72,16 +72,16 @@ class UniversalNodeLocatorDiscovery extends BaseNodeDiscovery implements Discove $resolverInstance = ProtocolResolverFactory::createResolverFromConfiguredProtocol(); // Then resolve the node instance into an UNL instance - $unlInstance = $resolverInstance->resolveUniversalNodeLocatorFromNode($nodeInstance); + $locatorInstance = $resolverInstance->resolveUniversalNodeLocatorFromNode($nodeInstance); // Is the instance given? - if (is_null($unlInstance)) { + if (is_null($locatorInstance)) { // Throw NPE here throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } // END - if // ... and return it - return $unlInstance; + return $locatorInstance; } /** diff --git a/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php b/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php index 6c2eebd36..7c19d7801 100644 --- a/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php +++ b/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php @@ -116,7 +116,7 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov } elseif ($packageInstance->getRecipientUnl() == '' && $packageInstance->getRecipientId() != '') { // Id is set, this might be resolved to an UNL /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RECIPIENT-DISCOVERY: Resolving recipientId=%s to UNL ...', $packageInstance->getRecipientId())); - $recipientUnl = HubTools::resolveSessionId($packageInstance->getRecipientId()); + $recipientUnl = HubTools::resolveSessionIdToUnl($packageInstance->getRecipientId()); // And set it in package instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RECIPIENT-DISCOVERY: recipientUnl=%s', $recipientUnl)); diff --git a/application/hub/classes/factories/locator/class_UniversalNodeLocatorFactory.php b/application/hub/classes/factories/locator/class_UniversalNodeLocatorFactory.php index e339ab9eb..ae2b769f6 100644 --- a/application/hub/classes/factories/locator/class_UniversalNodeLocatorFactory.php +++ b/application/hub/classes/factories/locator/class_UniversalNodeLocatorFactory.php @@ -43,7 +43,7 @@ class UniversalNodeLocatorFactory extends ObjectFactory { * Returns a singleton (registry-based) LocateableNode instance * * @param $unl UNL address - * @return $unlInstance An instance of a LocateableNode class + * @return $locatorInstance An instance of a LocateableNode class */ public static final function createUnlInstanceFromString ($unl) { // Trace message @@ -52,37 +52,37 @@ class UniversalNodeLocatorFactory extends ObjectFactory { // If there is no handler? if (GenericRegistry::getRegistry()->instanceExists('unl_' . $unl)) { // Get handler from registry - $unlInstance = GenericRegistry::getRegistry()->getInstance('unl_' . $unl); + $locatorInstance = GenericRegistry::getRegistry()->getInstance('unl_' . $unl); } else { // Get the handler instance - $unlInstance = self::createObjectByConfiguredName('universal_node_locator_class'); + $locatorInstance = self::createObjectByConfiguredName('universal_node_locator_class'); // Parse UNL string - $unlInstance->parseStringAsUnl($unl); + $locatorInstance->parseStringAsUnl($unl); // Add it to the registry - GenericRegistry::getRegistry()->addInstance('unl_' . $unl, $unlInstance); + GenericRegistry::getRegistry()->addInstance('unl_' . $unl, $locatorInstance); } // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNL-FACTORY: unlInstance[]=%s - EXIT!', gettype($unlInstance))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNL-FACTORY: locatorInstance[]=%s - EXIT!', gettype($locatorInstance))); // Return the instance - return $unlInstance; + return $locatorInstance; } /** * Creates a UNL instance from given array * * @param $unlData Array with valid UNL data - * @return $unlInstance An instance of a LocateableNode class + * @return $locatorInstance An instance of a LocateableNode class */ public static final function createUnlInstanceFromArray (array $unlData) { // No "caching" for now - $unlInstance = self::createObjectByConfiguredName('universal_node_locator_class', array($unlData)); + $locatorInstance = self::createObjectByConfiguredName('universal_node_locator_class', array($unlData)); // Return it - return $unlInstance; + return $locatorInstance; } } diff --git a/application/hub/classes/handler/class_BaseHubHandler.php b/application/hub/classes/handler/class_BaseHubHandler.php index e861c610d..63a65e59c 100644 --- a/application/hub/classes/handler/class_BaseHubHandler.php +++ b/application/hub/classes/handler/class_BaseHubHandler.php @@ -164,12 +164,12 @@ abstract class BaseHubHandler extends BaseHandler implements Handleable, HubInte /** * Setter for UNL instance * - * @para $unlInstance An instance of a LocateableNode class + * @para $locatorInstance An instance of a LocateableNode class * @return void */ - protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + protected final function setUniversalNodeLocatorInstance (LocateableNode $locatorInstance) { // Set new UNL data array - $this->universalNodeLocatorInstance = $unlInstance; + $this->universalNodeLocatorInstance = $locatorInstance; } /** diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index 82e777adb..af8b8a3b3 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -12,6 +12,7 @@ use Org\Shipsimu\Hub\Factory\Discovery\Socket\SocketDiscoveryFactory; use Org\Shipsimu\Hub\Factory\FilterChain\Package\PackageFilterChainFactory; use Org\Shipsimu\Hub\Factory\Handler\Message\MessageTypeHandlerFactory; use Org\Shipsimu\Hub\Factory\Information\Connection\ConnectionInfoFactory; +use Org\Shipsimu\Hub\Factory\Network\Locator\UniversalNodeLocatorFactory; use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory; use Org\Shipsimu\Hub\Generic\BaseHubSystem; use Org\Shipsimu\Hub\Handler\BaseHubHandler; @@ -128,7 +129,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei /** * Size of the decoded data array */ - const DECODED_DATA_ARRAY_SIZE = 6; + const DECODED_DATA_ARRAY_SIZE = 7; /** * Named array elements for decoded package content @@ -1019,6 +1020,19 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Get the package $packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_OUTGOING); + // The UNL should be set but not recipientId + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: recipientUnl(%d)=%s,recipientId(%d)=%s', strlen($packageInstance->getRecipientUnl()), $packageInstance->getRecipientUnl(), strlen($packageInstance->getRecipientId()), $packageInstance->getRecipientId())); + if ($packageInstance->getRecipientUnl() != '' && $packageInstance->getRecipientId() == '') { + // Need to resolve UNL -> session id ("recipient id"), so prepare UNL instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: recipientUnl=%s is being solved into session id ...', $packageInstance->getRecipientUnl())); + $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl()); + + // Get session id and set it back + $sessionId = HubTools::resolveSessionIdByUniversalNodeLocator($locatorInstance); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: sessionId=%s found, setting ...', $sessionId)); + $packageInstance->setRecipientId($sessionId); + } + try { // Now try to send it $this->sendOutgoingRawPackageData($packageInstance); @@ -1374,7 +1388,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Assert on count (should be always 6) /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedArray()=%d', count($decodedArray))); - /* DEBUG-DIE: */ die(sprintf('[%s:%d] decodedArray=%s', __METHOD__, __LINE__, print_r($decodedArray, TRUE))); + /* DEBUG-DIE: */ die(sprintf('[%s:%d]: decodedArray=%s', __METHOD__, __LINE__, print_r($decodedArray, TRUE))); if (count($decodedArray) != self::DECODED_DATA_ARRAY_SIZE) { // Count of array elements not expected throw new InvalidArgumentException(sprintf('decodedArray()=%d does not match expected size %d. rawPackageContent=%s', diff --git a/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php b/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php index 9df1c0714..5349b6fcd 100644 --- a/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php +++ b/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php @@ -73,8 +73,8 @@ abstract class BaseProtocolHandler extends BaseHubHandler implements HandleableP */ public function getAddressPart () { // Get UNL instance - $unlInstance = $this->getUniversalNodeLocatorInstance(); - die(__METHOD__.':'.print_r($unlInstance, TRUE)); + $locatorInstance = $this->getUniversalNodeLocatorInstance(); + die(__METHOD__.':'.print_r($locatorInstance, TRUE)); } /** diff --git a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php index 7eaf4863f..d46513216 100644 --- a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php +++ b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php @@ -92,14 +92,14 @@ abstract class BaseIpV4ProtocolHandler extends BaseProtocolHandler { // If this doesn't fail, continue validating the IP:port combination if ($isValid === TRUE) { // ... and validate IP:port, first "parse" the UNL - $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl()); + $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl()); // Set whole UNL instance // @TODO don't do this in a isValidFoo() method - $this->setUniversalNodeLocatorInstance($unlInstance); + $this->setUniversalNodeLocatorInstance($locatorInstance); // Set port - $this->setPort($unlInstance->getUnlPort()); + $this->setPort($locatorInstance->getUnlPort()); } // END - if // Return result diff --git a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php index 0f8931f3f..ed58f054e 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -70,7 +70,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // @TODO The whole resolving part should be moved out and made more configurable // Init recipient data - $unlInstance = NULL; + $locatorInstance = NULL; // Try to solve the recipient try { @@ -78,7 +78,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageInstance($packageInstance); // Get UNL instance - $unlInstance = $handlerInstance->getUniversalNodeLocatorInstance(); + $locatorInstance = $handlerInstance->getUniversalNodeLocatorInstance(); // Set handler instance $helperInstance->setHandlerInstance($handlerInstance); @@ -107,7 +107,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Create UNL instance $this->partialStub('Please rewrite this part.'); - //$unlInstance =; + //$locatorInstance =; } else { // It doesn't match, we need to take care of this later $helperInstance->debugInstance(sprintf( @@ -123,7 +123,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Now connect to it if (!$helperInstance->connectToPeerBySocketRecipient()) { // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: helperInstance=%s,unlInstance.unlData=%s', $helperInstance->__toString(), print_r($unlInstance->getUnlData(), TRUE))); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: helperInstance=%s,locatorInstance.unlData=%s', $helperInstance->__toString(), print_r($locatorInstance->getUnlData(), TRUE))); // Handle socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index 03f153f7f..33118d2b9 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -427,13 +427,13 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { $socketProtocol = $this->getSocketInstance()->getSocketProtocol(); // Get UNL instance - $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl()); + $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl()); // Get protocol from it - $unlProtocol = $unlInstance->getUnlProtocol(); + $unlProtocol = $locatorInstance->getUnlProtocol(); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: unlInstance.unlProtocol=%s,socketProtocol=%s', strtoupper($this->getProtocolName()), $unlProtocol, $socketProtocol)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: locatorInstance.unlProtocol=%s,socketProtocol=%s', strtoupper($this->getProtocolName()), $unlProtocol, $socketProtocol)); // Is same protocol? $accepts = ($unlProtocol == $socketProtocol); diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index 966d36e72..b0f3fc938 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -82,10 +82,10 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable /** * Setter for UNL instance * - * @param $unlInstance An instance of a LocateableNode class + * @param $locatorInstance An instance of a LocateableNode class * @return void */ - protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + protected final function setUniversalNodeLocatorInstance (LocateableNode $locatorInstance) { throw new UnsupportedOperationException('This method should not be called.'); } diff --git a/application/hub/classes/locator/class_UniversalNodeLocator.php b/application/hub/classes/locator/class_UniversalNodeLocator.php index 98d6a4363..8d89393b5 100644 --- a/application/hub/classes/locator/class_UniversalNodeLocator.php +++ b/application/hub/classes/locator/class_UniversalNodeLocator.php @@ -6,6 +6,7 @@ namespace Org\Shipsimu\Hub\Locator\Node; use Org\Shipsimu\Hub\Database\Frontend\Node\Information\NodeInformationDatabaseWrapper; use Org\Shipsimu\Hub\Generic\BaseHubSystem; use Org\Shipsimu\Hub\Locator\Node\Tools\NodeLocatorUtils; +use Org\Shipsimu\Hub\Tools\HubTools; // Import framework stuff use Org\Mxchange\CoreFramework\Registry\Registerable; @@ -56,17 +57,106 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi * Creates an instance of this class * * @param $current An array with "raw" data from the database layer for the UNL. This parameter is optional. - * @return $unlInstance An instance of a LocateableNode class + * @return $locatorInstance An instance of a LocateableNode class */ public final static function createUniversalNodeLocator (array $current = array()) { // Get new instance - $unlInstance = new UniversalNodeLocator(); + $locatorInstance = new UniversalNodeLocator(); // Init instance - $unlInstance->initUniversalNodeLocator($current); + $locatorInstance->initUniversalNodeLocator($current); // Return the prepared instance - return $unlInstance; + return $locatorInstance; + } + + /** + * Parses the given UNL by splitting it up in its components. The UNL ... + * + * protocol://address[:port] + * + * ... becomes: + * + * array( + * 'protocol' => 'value', + * 'address' => 'value', + * 'extra' => 'port' + * ) + * + * The value for 'extra' then must be handled by the overwriting + * parseUniversalNodeLocator() method of the individual protocol handler as + * this is protocol-specific. + * + * @param $unl Universal Node Locator (UNL) to "parse" + * @throws InvalidArgumentException If given UNL is not valid + */ + private function parseUniversalNodeLocator ($unl) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=[%s]=%s - CALLED!', gettype($unl), $unl)); + + // Make sure the UNL is valid + if (!NodeLocatorUtils::isValidUniversalNodeLocator($unl)) { + // UNL is not valid + throw new InvalidArgumentException(sprintf('unl[%s]=%s is not valid.', gettype($unl), $unl)); + } // END - if + + /* + * "Parse" the UNL "generically", sadly this cannot be done by using preg_match() :-( + * @TODO If you know why, please fix and explain it to me. + */ + $unlParts = explode('://', $unl); + + // Split again the last part as: address:port + $unlParts[1] = explode(':', $unlParts[1]); + + // Now there is an almost useable array which then can be copied to the "real" array. + $unlData = array( + LocateableNode::UNL_PART_PROTOCOL => $unlParts[0], + LocateableNode::UNL_PART_ADDRESS => $unlParts[1][0], + LocateableNode::UNL_PART_EXTRA => $unlParts[1][1], + ); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: unlData=' . print_r($unlData, TRUE) . ' - EXIT!'); + + // Call init method + $this->initUniversalNodeLocator($unlData); + } + + /** + * Initializes the UNL instance by givena array. If an entry is found, it + * will be copied, otherwise the entry is set empty. + * + * @param $unlData An array with "raw" data from the database layer for the UNL. This parameter is optional. + * @return void + */ + private function initUniversalNodeLocator (array $unlData = array()) { + // Init UNL array + $this->unlData = array(); + + // Copy all found entries + foreach (array( + LocateableNode::UNL_PART_PROTOCOL, + LocateableNode::UNL_PART_ADDRESS, + LocateableNode::UNL_PART_EXTRA, + NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, + NodeInformationDatabaseWrapper::DB_COLUMN_SESSION_ID, + NodeInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH, + NodeInformationDatabaseWrapper::DB_COLUMN_NODE_MODE, + NodeInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL, + NodeInformationDatabaseWrapper::DB_COLUMN_EXTERNAL_UNL) as $key) { + // Init entry + $this->unlData[$key] = NULL; + + // Is the key found? + if (isset($unlData[$key])) { + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: Copying unlData[' . $key . ']=' . $unlData[$key] . ' ...'); + + // The copy the entry + $this->unlData[$key] = $unlData[$key]; + } // END - if + } // END - foreach } /** @@ -190,7 +280,7 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi */ public function parseStringAsUnl ($unl) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=[%s]=%s - CALLED!', gettype($unl), $unl)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=[%s]=%s - CALLED!', gettype($unl), $unl)); // Is it basically valid? if (!is_string($unl)) { @@ -200,6 +290,20 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi // Parse it $this->parseUniversalNodeLocator($unl); + + // Is it matching own internal UNL? + if (HubTools::determineOwnInternalAddress() == $unl) { + // Yes, then set it here + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=%s matches own internal address, setting ...', $unl)); + $this->unlData[NodeInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL] = $unl; + } + + // Is it matching own external UNL? + if (HubTools::determineOwnExternalAddress() == $unl) { + // Yes, then set it here + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=%s matches own external address, setting ...', $unl)); + $this->unlData[NodeInformationDatabaseWrapper::DB_COLUMN_EXTERNAL_UNL] = $unl; + } } /** @@ -211,93 +315,4 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi return $this->unlData; } - /** - * Parses the given UNL by splitting it up in its components. The UNL ... - * - * protocol://address[:port] - * - * ... becomes: - * - * array( - * 'protocol' => 'value', - * 'address' => 'value', - * 'extra' => 'port' - * ) - * - * The value for 'extra' then must be handled by the overwriting - * parseUniversalNodeLocator() method of the individual protocol handler as - * this is protocol-specific. - * - * @param $unl Universal Node Locator (UNL) to "parse" - * @throws InvalidArgumentException If given UNL is not valid - */ - private function parseUniversalNodeLocator ($unl) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=[%s]=%s - CALLED!', gettype($unl), $unl)); - - // Make sure the UNL is valid - if (!NodeLocatorUtils::isValidUniversalNodeLocator($unl)) { - // UNL is not valid - throw new InvalidArgumentException(sprintf('unl[%s]=%s is not valid.', gettype($unl), $unl)); - } // END - if - - /* - * "Parse" the UNL "generically", sadly this cannot be done by using preg_match() :-( - * @TODO If you know why, please fix and explain it to me. - */ - $unlParts = explode('://', $unl); - - // Split again the last part as: address:port - $unlParts[1] = explode(':', $unlParts[1]); - - // Now there is an almost useable array which then can be copied to the "real" array. - $unlData = array( - LocateableNode::UNL_PART_PROTOCOL => $unlParts[0], - LocateableNode::UNL_PART_ADDRESS => $unlParts[1][0], - LocateableNode::UNL_PART_EXTRA => $unlParts[1][1], - ); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: unlData=' . print_r($unlData, TRUE) . ' - EXIT!'); - - // Call init method - $this->initUniversalNodeLocator($unlData); - } - - /** - * Initializes the UNL instance by givena array. If an entry is found, it - * will be copied, otherwise the entry is set empty. - * - * @param $unlData An array with "raw" data from the database layer for the UNL. This parameter is optional. - * @return void - */ - private function initUniversalNodeLocator (array $unlData = array()) { - // Init UNL array - $this->unlData = array(); - - // Copy all found entries - foreach (array( - LocateableNode::UNL_PART_PROTOCOL, - LocateableNode::UNL_PART_ADDRESS, - LocateableNode::UNL_PART_EXTRA, - NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, - NodeInformationDatabaseWrapper::DB_COLUMN_SESSION_ID, - NodeInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH, - NodeInformationDatabaseWrapper::DB_COLUMN_NODE_MODE, - NodeInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL, - NodeInformationDatabaseWrapper::DB_COLUMN_EXTERNAL_UNL) as $key) { - // Init entry - $this->unlData[$key] = NULL; - - // Is the key found? - if (isset($unlData[$key])) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: Copying unlData[' . $key . ']=' . $unlData[$key] . ' ...'); - - // The copy the entry - $this->unlData[$key] = $unlData[$key]; - } // END - if - } // END - foreach - } - } diff --git a/application/hub/classes/nodes/class_BaseHubNode.php b/application/hub/classes/nodes/class_BaseHubNode.php index 98f7395dd..749d15b9d 100644 --- a/application/hub/classes/nodes/class_BaseHubNode.php +++ b/application/hub/classes/nodes/class_BaseHubNode.php @@ -242,10 +242,10 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC // Is "cache" set? if (!isset($GLOBALS[__METHOD__])) { // Get the UNL array back - $unlInstance = $this->determineUniversalNodeLocator(); + $locatorInstance = $this->determineUniversalNodeLocator(); // Is the instance valid? - if (NULL == $unlInstance) { + if (NULL == $locatorInstance) { // Throw NPE throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } // END - if @@ -253,10 +253,10 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC // There are 2 UNLs, internal and external. if ($this->getConfigInstance()->getConfigEntry('allow_publish_internal_address') == 'N') { // Public "external" UNL address - $GLOBALS[__METHOD__] = $unlInstance->getExternalUnl(); + $GLOBALS[__METHOD__] = $locatorInstance->getExternalUnl(); } else { // Non-public "internal" UNL address - $GLOBALS[__METHOD__] = $unlInstance->getInternalUnl(); + $GLOBALS[__METHOD__] = $locatorInstance->getInternalUnl(); } } // END - if @@ -764,7 +764,7 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC /** * Determines an instance of a LocateableNode class * - * @return $unlInstance An instance of a LocateableNode class for this node + * @return $locatorInstance An instance of a LocateableNode class for this node */ public function determineUniversalNodeLocator () { // Debug message @@ -781,7 +781,7 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC } // END - if // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: unlInstance= ' . $GLOBALS[__METHOD__]->__toString() . ' - EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: locatorInstance= ' . $GLOBALS[__METHOD__]->__toString() . ' - EXIT!'); return $GLOBALS[__METHOD__]; } diff --git a/application/hub/classes/package/deliverable/class_PackageData.php b/application/hub/classes/package/deliverable/class_PackageData.php index f93693f0a..3ea63c3ae 100644 --- a/application/hub/classes/package/deliverable/class_PackageData.php +++ b/application/hub/classes/package/deliverable/class_PackageData.php @@ -293,7 +293,7 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera /** * Setter for private key hash * - * @param $senderPrivateKeyHash The new private key hash + * @param $senderPrivateKeyHash The new private key hash * @return void */ public final function setSenderPrivateKeyHash ($senderPrivateKeyHash) { @@ -303,7 +303,7 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera /** * Getter for private key hash * - * @return $senderPrivateKeyHash Current private key hash + * @return $senderPrivateKeyHash Current private key hash */ public final function getSenderPrivateKeyHash () { return $this->senderPrivateKeyHash; diff --git a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php index b58159191..b15b12621 100644 --- a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php @@ -489,7 +489,7 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera * to all chunks and prepends a chunk with all hashes only in it. It will * return the final hash for faster processing of packages. * - * @param $packageInstance An instance of a DeliverablePackage class array + * @param $packageInstance An instance of a DeliverablePackage class array * @param $helperInstance An instance of a ConnectionHelper class * @return $finalHash Final hash for faster processing * @todo $helperInstance is unused @@ -498,13 +498,24 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera // Is this package already fragmented? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: packageInstance=%s,helperInstance=%s - CALLED!', $packageInstance->__toString(), $helperInstance->__toString())); if (!$this->isPackageProcessed($packageInstance)) { - // First get all fields as array from PackageData class, implode() doesn't work as expected here. + // First put it the right way together again, implode on getAllAsArray() won't work! /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: Package is not processed., getting all raw data ...'); - $packageData = $packageInstance->getAllAsArray(); - - // Then put it the right way together again - /* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageData=%s', __METHOD__, __LINE__, print_r($packageData, TRUE))); - //$rawData = sprintf( + /* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance->allAsArray=%s', __METHOD__, __LINE__, print_r($packageInstance->getAllAsArray(), TRUE))); + $rawData = sprintf('%s%s%d%s%s%s%s%s%s%s%s%s%s', + $packageInstance->getSenderAddress(), + NetworkPackageHandler::PACKAGE_DATA_SEPARATOR, + $packageInstance->getSenderPort(), + NetworkPackageHandler::PACKAGE_DATA_SEPARATOR, + $packageInstance->getRecipientId(), + NetworkPackageHandler::PACKAGE_DATA_SEPARATOR, + $packageInstance->getPackageContent(), + NetworkPackageHandler::PACKAGE_DATA_SEPARATOR, + $packageInstance->getStatus(), + NetworkPackageHandler::PACKAGE_DATA_SEPARATOR, + $packageInstance->getContentHash(), + NetworkPackageHandler::PACKAGE_DATA_SEPARATOR, + $packageInstance->getSenderPrivateKeyHash() + ); // Generate the final hash from the raw data (not encoded!) /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: rawData(%d)=%s', strlen($rawData), $rawData)); diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index 0c6634b6c..fd5500c4d 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -237,10 +237,10 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageInstance($packageInstance); // Get UNL data - $unlInstance = $handlerInstance->getUniversalNodeLocatorInstance(); + $locatorInstance = $handlerInstance->getUniversalNodeLocatorInstance(); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),unlInstance.unl=' . $unlInstance->generateUnl() . ' ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),locatorInstance.unl=' . $locatorInstance->generateUnl() . ' ...'); // Default is all sockets $sockets = $this->getAllSockets(); @@ -281,10 +281,10 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { } // END - if // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s,peerPort=%d,unlInstance.addressPart=%s', $peerAddress, $peerPort, $unlInstance->getAddressPart())); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s,peerPort=%d,locatorInstance.addressPart=%s', $peerAddress, $peerPort, $locatorInstance->getAddressPart())); // If the "peer" IP and recipient is same, use it - if ($peerAddress == $unlInstance->getAddressPart()) { + if ($peerAddress == $locatorInstance->getAddressPart()) { // IPs match, so take the socket and quit this loop $socketInstance = $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]; diff --git a/application/hub/classes/recipient/direct/class_DirectRecipient.php b/application/hub/classes/recipient/direct/class_DirectRecipient.php index 8164e72e7..b82569b43 100644 --- a/application/hub/classes/recipient/direct/class_DirectRecipient.php +++ b/application/hub/classes/recipient/direct/class_DirectRecipient.php @@ -85,7 +85,7 @@ class DirectRecipient extends BaseRecipient implements Recipient { foreach ($recipients as $recipient) { // Try to sole a single recipient /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECT-RECIPIENT: recipient=' . $recipient); - $unlData = HubTools::resolveSessionId($recipient); + $unlData = HubTools::resolveSessionIdToUnl($recipient); // Add it as recipient $listInstance->addEntry('unl', $unlData); diff --git a/application/hub/classes/resolver/protocol/tcp/class_TcpProtocolResolver.php b/application/hub/classes/resolver/protocol/tcp/class_TcpProtocolResolver.php index f90fedb56..3c69aef0f 100644 --- a/application/hub/classes/resolver/protocol/tcp/class_TcpProtocolResolver.php +++ b/application/hub/classes/resolver/protocol/tcp/class_TcpProtocolResolver.php @@ -69,7 +69,7 @@ class TcpProtocolResolver extends BaseProtocolResolver implements ProtocolResolv * instance or null if it was not found. * * @param $nodeInstance An instance of a Node class - * @return $unlInstance An instance of a LocateableNode class + * @return $locatorInstance An instance of a LocateableNode class */ public function resolveUniversalNodeLocatorFromNode (Node $nodeInstance) { // Get search instance (to lookup database result) @@ -98,10 +98,10 @@ class TcpProtocolResolver extends BaseProtocolResolver implements ProtocolResolv assert($nodeInstance->getNodeId() == $current[NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID]); // Get UNL instance and handle over all data - $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromArray($current); + $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromArray($current); // Return resolved instance - return $unlInstance; + return $locatorInstance; } /** diff --git a/application/hub/classes/tools/hub/class_HubTools.php b/application/hub/classes/tools/hub/class_HubTools.php index 072912494..fba77c3fa 100644 --- a/application/hub/classes/tools/hub/class_HubTools.php +++ b/application/hub/classes/tools/hub/class_HubTools.php @@ -98,11 +98,12 @@ class HubTools extends BaseHubSystem { * Resolves a session id into an instance of a LocateableNode class. The opposite method * is resolveSessionIdByUniversalNodeLocator() * - * @param $sessionId A valid session id + * @param $sessionId A valid session id * @return $recipientUniversalNodeLocator Recipient as Universal Node Locator */ protected function resolveUniversalNodeLocatorBySessionId ($sessionId) { // Init variable + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - CALLED!', $sessionId)); $recipientUniversalNodeLocator = 'invalid://invalid:invalid'; // And ask it for Universal Node Locator by given session id @@ -112,7 +113,7 @@ class HubTools extends BaseHubSystem { // Is the recipient valid? if (isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS])) { // Then use this - $recipientUniversalNodeLocator = $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS]; + $recipientUnl = $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS]; } else { // Get the instance, this might throw a NPE /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Creating node instance ...'); @@ -121,12 +122,13 @@ class HubTools extends BaseHubSystem { // Is the session id the same? if ($nodeInstance->getSessionId() == $sessionId) { // Then get an instance of a LocateableNode class from it, assume TCP by default - $recipientUniversalNodeLocator = self::determineOwnExternalAddress() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_listen_port'); + $recipientUnl = self::determineOwnExternalAddress() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_listen_port'); } // END - if } // Return result - return $recipientUniversalNodeLocator; + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: recipientUnl=%s - EXIT!', $recipientUnl)); + return $recipientUnl; } /** @@ -137,6 +139,7 @@ class HubTools extends BaseHubSystem { */ public static function resolveNodeIdBySessionId ($sessionId) { // Get an own instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - CALLED!', $sessionId)); $selfInstance = self::getSelfInstance(); // And ask it for session id by given Universal Node Locator @@ -147,6 +150,7 @@ class HubTools extends BaseHubSystem { assert(isset($nodeData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID])); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: nodeId=%s - EXIT!', $nodeData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID])); return $nodeData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID]; } @@ -154,18 +158,20 @@ class HubTools extends BaseHubSystem { * Resolves a Universal Node Locator into a session id. The "opposite" method * is resolveUniversalNodeLocatorBySessionId(). * - * @param $unlInstance Universal Node Locator + * @param $locatorInstance An instance of a LocateableNode class * @return $sessionId Valid session id */ - public static function resolveSessionIdByUniversalNodeLocator (LocateableNode $unlInstance) { + public static function resolveSessionIdByUniversalNodeLocator (LocateableNode $locatorInstance) { // Get an own instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: locatorInstance=%s - CALLED!', $locatorInstance->__toString())); $selfInstance = self::getSelfInstance(); // And ask it for session id by given Universal Node Locator - $recipient = DhtObjectFactory::createDhtInstance('node')->findNodeByUniversalNodeLocator($unlInstance); + $recipient = DhtObjectFactory::createDhtInstance('node')->findNodeByUniversalNodeLocator($locatorInstance); die(__METHOD__.':recipient='.print_r($recipient, TRUE)); // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - EXIT!', $sessionId)); return $sessionId; } @@ -177,7 +183,7 @@ class HubTools extends BaseHubSystem { * @throws InvalidSessionIdException If the provided session id is invalid (and no Universal Node Locator) * @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address */ - public static function resolveSessionId ($address) { + public static function resolveSessionIdToUnl ($address) { // Get an own instance $selfInstance = self::getSelfInstance(); @@ -242,31 +248,31 @@ class HubTools extends BaseHubSystem { */ public static function determineOwnInternalAddress () { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: CALLED!'); // Is the internal_address config entry set? if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('allow_publish_internal_address') == 'N') { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: Calling self::determineOwnExternalAddress() as allow_publish_internal_address=N is set ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Calling self::determineOwnExternalAddress() as allow_publish_internal_address=N is set ...'); // Not allowed to publish internal address, so use external $unl = self::determineOwnExternalAddress(); } elseif (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('internal_address') != '') { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: Getting config entry internal_address ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Getting config entry internal_address ...'); // Use it as internal address $unl = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('internal_address'); } else { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: Calling self::determineInternalUniversalNodeLocator() ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Calling self::determineInternalUniversalNodeLocator() ...'); // Determine own internal address $unl = self::determineInternalUniversalNodeLocator(); } // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: unl=' . $unl . ' - EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $unl . ' - EXIT!'); // Return it return $unl; @@ -279,7 +285,7 @@ class HubTools extends BaseHubSystem { */ public static function determineInternalUniversalNodeLocator () { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: CALLED!'); // Is there cache? (This shortens a lot calls) if (!isset($GLOBALS[__METHOD__])) { @@ -296,7 +302,7 @@ class HubTools extends BaseHubSystem { } // END - if // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: unl=' . $GLOBALS[__METHOD__] . ' - EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $GLOBALS[__METHOD__] . ' - EXIT!'); return $GLOBALS[__METHOD__]; } @@ -307,7 +313,7 @@ class HubTools extends BaseHubSystem { */ public static function determineExternalUniversalNodeLocator () { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: CALLED!'); // Is there cache? (This shortens a lot calls) if (!isset($GLOBALS[__METHOD__])) { @@ -324,7 +330,7 @@ class HubTools extends BaseHubSystem { } // END - if // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: unl=' . $GLOBALS[__METHOD__] . ' - EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $GLOBALS[__METHOD__] . ' - EXIT!'); return $GLOBALS[__METHOD__]; } diff --git a/application/hub/interfaces/class_HubInterface.php b/application/hub/interfaces/class_HubInterface.php index 0eaa9f308..78356fe6f 100644 --- a/application/hub/interfaces/class_HubInterface.php +++ b/application/hub/interfaces/class_HubInterface.php @@ -101,7 +101,7 @@ interface HubInterface extends FrameworkInterface { /** * Getter for UNL instance * - * @return $unlInstance An instance of a LocateableNode class + * @return $locatorInstance An instance of a LocateableNode class */ function getUniversalNodeLocatorInstance (); diff --git a/application/hub/interfaces/dht/.htaccess b/application/hub/interfaces/dht/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/interfaces/dht/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/interfaces/discovery/unl/class_DiscoverableUniversalNodeLocator.php b/application/hub/interfaces/discovery/unl/class_DiscoverableUniversalNodeLocator.php index 64db85e05..975b84c48 100644 --- a/application/hub/interfaces/discovery/unl/class_DiscoverableUniversalNodeLocator.php +++ b/application/hub/interfaces/discovery/unl/class_DiscoverableUniversalNodeLocator.php @@ -35,8 +35,8 @@ interface DiscoverableUniversalNodeLocator extends Discoverable { * "Discovers" an instance of a LocateableNode class for given Node class * * @param $nodeInstance An instance of a Node class - * @return $unlInstance An instance of a LocateableNode class - * @throws NullPointerException If $unlInstance is NULL + * @return $locatorInstance An instance of a LocateableNode class + * @throws NullPointerException If $locatorInstance is NULL */ function discoverUniversalNodeLocatorByNode (Node $nodeInstance); diff --git a/application/hub/interfaces/distributable/node/class_DistributableNode.php b/application/hub/interfaces/distributable/node/class_DistributableNode.php index 1115a9d16..108ff06a7 100644 --- a/application/hub/interfaces/distributable/node/class_DistributableNode.php +++ b/application/hub/interfaces/distributable/node/class_DistributableNode.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Dht\Node; // Import application-specific stuff use Org\Shipsimu\Hub\Dht\Distributable; +use Org\Shipsimu\Hub\Locator\Node\LocateableNode; // Import framework stuff use Org\Mxchange\CoreFramework\Handler\DataSet\HandleableDataSet; @@ -39,6 +40,14 @@ interface DistributableNode extends Distributable { */ function findNodeLocalBySessionId ($sessionId); + /** + * Finds a node locally by given UNL instance + * + * @param $locatorInstance An instance of a LocateableNode class + * @return $nodeData Node-data array + */ + function findNodeByUniversalNodeLocator (LocateableNode $locatorInstance); + /** * Registers an other node with this node by given message data. The * following data must always be present: diff --git a/application/hub/interfaces/nodes/class_Node.php b/application/hub/interfaces/nodes/class_Node.php index 5854f0178..5ef756c2c 100644 --- a/application/hub/interfaces/nodes/class_Node.php +++ b/application/hub/interfaces/nodes/class_Node.php @@ -124,7 +124,7 @@ interface Node extends FrameworkInterface { /** * Determines an instance of a LocateableNode class * - * @return $unlInstance An instance of a LocateableNode class for this node + * @return $locatorInstance An instance of a LocateableNode class for this node */ function determineUniversalNodeLocator (); diff --git a/application/hub/interfaces/resolver/class_ProtocolResolver.php b/application/hub/interfaces/resolver/class_ProtocolResolver.php index 0f07d2f37..e056f29dd 100644 --- a/application/hub/interfaces/resolver/class_ProtocolResolver.php +++ b/application/hub/interfaces/resolver/class_ProtocolResolver.php @@ -34,7 +34,7 @@ interface ProtocolResolver extends HubInterface { * instance or null if it was not found. * * @param $nodeInstance An instance of a Node class - * @return $unlInstance An instance of a LocateableNode class + * @return $locatorInstance An instance of a LocateableNode class */ function resolveUniversalNodeLocatorFromNode (Node $nodeInstance); -- 2.39.5