From: Roland Häder Date: Thu, 29 Oct 2020 11:56:01 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=eb3129b939e6e51fdd164c75188d367590935f2e;p=hub.git Continued: - implemented NodeDhtWrapper::findNodeLocalByLocatorInstance() - updated core framework Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php index b5479aa19..55d52a8e6 100644 --- a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -5,6 +5,7 @@ namespace Org\Shipsimu\Hub\Database\Frontend\Node\Dht; // Import application-specific stuff use Org\Shipsimu\Hub\Database\Frontend\BaseHubDatabaseWrapper; use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory; +use Org\Shipsimu\Hub\Locator\Node\LocateableNode; use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Node\BaseHubNode; @@ -224,7 +225,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp // Make sure the external address is set and not invalid // @TODO Bad check on UNL, better use a proper validator $externalUnl = $locatorInstance->getExternalUnl(); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: externalUnl=' . $externalUnl); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: externalUnl=%s', $externalUnl)); assert($externalUnl != 'invalid'); // Add Universal Node Locator/node id as criteria @@ -240,7 +241,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp $isRegistered = $resultInstance->valid(); // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: isRegistered=' . intval($isRegistered) . ' - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: isRegistered=%d - EXIT!', intval($isRegistered))); return $isRegistered; } @@ -304,7 +305,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp * Finds a node locally by given session id * * @param $sessionId Session id to lookup - * @return $nodeData Node data array + * @return $resultInstance An instance of a SearchableResult class */ public function findNodeLocalBySessionId ($sessionId) { // Get search criteria @@ -319,7 +320,31 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp $resultInstance = $this->doSelectByCriteria($searchInstance); // Return result instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: resultInstance->valid()=' . intval($resultInstance->valid()) . ' - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: resultInstance->valid()=%d - EXIT!', intval($resultInstance->valid()))); + return $resultInstance; + } + + /** + * Finds a node locally by given UNL instance + * + * @param $locatorInstance An instance of a LocateableNode class + * @return $resultInstance An instance of a SearchableResult class + */ + public function findNodeLocalByLocatorInstance (LocateableNode $locatorInstance) { + // Get search criteria + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: locatorInstance=%s - CALLED!', $locatorInstance->__toString())); + $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + + // Search for session id and limit it to one entry + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: externalUnl=%s', $locatorInstance->getExternalUnl())); + $searchInstance->addCriteria(self::DB_COLUMN_EXTERNAL_ADDRESS, $locatorInstance->getExternalUnl()); + $searchInstance->setLimit(1); + + // Query database and get a result instance back + $resultInstance = $this->doSelectByCriteria($searchInstance); + + // Return result instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: resultInstance->valid()=%d - EXIT!', intval($resultInstance->valid()))); return $resultInstance; } diff --git a/application/hub/classes/dht/node/class_NodeDhtFacade.php b/application/hub/classes/dht/node/class_NodeDhtFacade.php index fd25b163b..5e17db99a 100644 --- a/application/hub/classes/dht/node/class_NodeDhtFacade.php +++ b/application/hub/classes/dht/node/class_NodeDhtFacade.php @@ -202,13 +202,23 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { $nodeData = array(); // Query database - $resultInstance = $this->getWrapperInstance()->findNodeLocalByUnlInstance($locatorInstance); + $resultInstance = $this->getWrapperInstance()->findNodeLocalByLocatorInstance($locatorInstance); // Make sure the result instance is valid + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: resultInstance=%s', __METHOD__, __LINE__, print_r($resultInstance, TRUE))); assert($resultInstance instanceof SearchableResult); + // Is the next entry valid? + if (($resultInstance->valid()) && ($resultInstance->next())) { + /* + * Then load the first entry (more entries are being ignored and + * should not happen). + */ + $nodeData = $resultInstance->current(); + } // END - if + // Return node data - /* DEBUG-DIE: */ die(sprintf('[%s:%d]: nodeData=%s', __METHOD__, __LINE__, print_r($nodeData, TRUE))); + //* 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; } diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index af8b8a3b3..11421344d 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -1024,7 +1024,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei /* 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())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: recipientUnl=%s is being solved into a session id ...', $packageInstance->getRecipientUnl())); $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl()); // Get session id and set it back diff --git a/application/hub/exceptions.php b/application/hub/exceptions.php index 1b74a7286..a343589d8 100644 --- a/application/hub/exceptions.php +++ b/application/hub/exceptions.php @@ -1,6 +1,7 @@