From: Roland Häder Date: Tue, 27 Oct 2020 10:24:25 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=727ffdc91a2344c2cba64c8a5d0e7cc383d7c2f7;p=hub.git Continued: - assertitions are to soft, better throw hard exceptions - needed to solve recipientId to UNL after receiving and decoding a raw package Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php b/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php index 0a29c885f..6c2eebd36 100644 --- a/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php +++ b/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php @@ -15,6 +15,9 @@ use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Generic\FrameworkException; use Org\Mxchange\CoreFramework\Registry\Registerable; +// Import SPL stuff +use \InvalidArgumentException; + /** * A PackageRecipient discovery class * @@ -97,6 +100,7 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov * * @param $packageInstance An instance of a DeliverablePackage class * @return void + * @throws InvalidArgumentException If the package instance does not contain id and UNL * @todo Add some validation of recipient field, e.g. an Universal Node Locator is found * @todo Enrich both messages with recipient data */ @@ -105,7 +109,22 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RECIPIENT-DISCOVERY: packageInstance=%s - CALLED!', $packageInstance->__toString())); $this->clearRecipients(); + // Is the UNL empty but id given? + if ($packageInstance->getRecipientUnl() == '' && $packageInstance->getRecipientId() == '') { + // Uh, both is empty! + throw new InvalidArgumentException('packageInstance has both recipientUnl and recipientId empty.'); + } 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()); + + // And set it in package instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RECIPIENT-DISCOVERY: recipientUnl=%s', $recipientUnl)); + $packageInstance->setRecipientUnl($recipientUnl); + } + // Get a protocol handler back from decoded data + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE))); $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageInstance($packageInstance); // Is the 'recipient' field same as this peer's IP? diff --git a/application/hub/classes/recipient/dht/class_DhtRecipient.php b/application/hub/classes/recipient/dht/class_DhtRecipient.php index 83fb357c9..d6df5dd8e 100644 --- a/application/hub/classes/recipient/dht/class_DhtRecipient.php +++ b/application/hub/classes/recipient/dht/class_DhtRecipient.php @@ -13,6 +13,9 @@ use Org\Shipsimu\Hub\Network\Recipient\Recipient; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Lists\Listable; +// Import SPL stuff +use \InvalidArgumentException; + /** * A DHT recipient * @@ -72,12 +75,11 @@ class DhtRecipient extends BaseRecipient implements Recipient { // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: packageInstance=' . $packageInstance->__toString() . ',listInstance=' . $listInstance->__toString()); - // Get recipient UNL - $recipientUnl = $packageInstance->getRecipientUnl(); - - // Make sure the recipient is valid - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: recipientUnl=' . $recipientUnl); - assert($recipientUnl == NetworkPackageHandler::RECIPIENT_TYPE_DHT); + // Make sure the recipient type is valid + if ($packageInstance->getRecipientType() != NetworkPackageHandler::RECIPIENT_TYPE_DHT) { + // Not expected here + throw new InvalidArgumentException(sprintf('recipientType=%s is unexpected.', $packageInstance->getRecipientType())); + } // Get recipient discovery instance $discoverInstance = ObjectFactory::createObjectByConfiguredName('dht_recipient_discovery_class');