]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Tue, 27 Oct 2020 10:24:25 +0000 (11:24 +0100)
committerRoland Häder <roland@mxchange.org>
Tue, 27 Oct 2020 10:24:25 +0000 (11:24 +0100)
- 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 <roland@mxchange.org>
application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php
application/hub/classes/recipient/dht/class_DhtRecipient.php

index 0a29c885fbe8541cf14526b46aec4b8f8fcb3056..6c2eebd3692b4604b6daa6ff8272d066a32816c7 100644 (file)
@@ -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?
index 83fb357c9864b50b6b306a2156040d3748993a9c..d6df5dd8e065fd5f6d2794467bb1c4d0e61b28c9 100644 (file)
@@ -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');