]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/discovery/package/class_PackageRecipientDiscovery.php
Introduced Recipient implentation classes:
[hub.git] / application / hub / main / discovery / package / class_PackageRecipientDiscovery.php
index 6bd982d83f09c8bb33a4930b8974dc469c2a6eb4..5567d36ed280b68c6afa7794e2f4c7aa38f5bc76 100644 (file)
@@ -57,75 +57,28 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable
         *
         * @param       $packageData    Raw package data array
         * @return      void
-        * @todo        Find a better way than this switch/case/default thing
         */
        public function discoverRecipients (array $packageData) {
-               // We do some rudimentary checks but this might be a bit ugly
-               switch ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]) {
-                       case NetworkPackage::NETWORK_TARGET_UPPER_NODES: // All upper hubs, these are currently the bootstrap nodes and later on prepended list-nodes
-                               // Get all bootstrap nodes
-                               foreach (explode(BaseHubSystem::BOOTSTRAP_NODES_SEPARATOR, $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $ipPort) {
-                                       // Is maximum reached?
-                                       if ($this->getListInstance()->count() == $this->getConfigInstance()->getConfigEntry('package_recipient_max_count')) {
-                                               // Debug message
-                                               /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DISCOVERY: Going to abort at maximum of ' . $this->getConfigInstance()->getConfigEntry('package_recipient_max_count') . ' recipients!');
-
-                                               // Then stop adding more
-                                               break;
-                                       } // END - if
-
-                                       // Debug message
-                                       /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DISCOVERY: Adding node ' . $ipPort . ' as recipient.');
-
-                                       // Add the entry
-                                       $this->getListInstance()->addEntry('ip_port', $ipPort);
-                               } // END - foreach
-                               break;
-
-                       case NetworkPackage::NETWORK_TARGET_SELF: // The target 'self' is always the external IP address!
-                               // Determine own port
-                               $port = $this->getConfigInstance()->getConfigEntry('node_listen_port');
-
-                               // Determine IP or 'external_ip' if set
-                               $ip = HubTools::determineOwnExternalIp();
-
-                               // Is it not empty?
-                               if (!empty($ip)) {
-                                       // Add it to the list
-                                       $this->getListInstance()->addEntry('ip_port', $ip . ':' . $port);
-                               } // END - if
-                               break;
-
-                       case NetworkPackage::NETWORK_TARGET_DHT: // The target 'dht' is a list from local DHT query
-                               $this->partialStub('DHT recipient unimplemented: packageData=' . print_r($packageData, true));
-                               break;
-
-                       default: // This may be a direct recipient (node's session id)
-                               /*
-                                * Try to solve it, if an exception comes back, it is not a
-                                * session-id, nor IP:port and not a hostname:port combination.
-                                */
-                               try {
-                                       // "Explode" all recipients
-                                       $recipients = explode(NetworkPackage::PACKAGE_RECIPIENT_SEPARATOR, $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
-
-                                       // Is maximum reached?
-                                       assert(count($recipients) <= $this->getConfigInstance()->getConfigEntry('package_recipient_max_count'));
-
-                                       // Try it on all
-                                       foreach ($recipients as $recipient) {
-                                               // Try to sole a single recipient
-                                               $ipPort = HubTools::resolveSessionId($recipient);
-
-                                               // Add it as recipient
-                                               $this->getListInstance()->addEntry('ip_port', $ipPort);
-                                       } // END - foreach
-                               } catch (FrameworkException $e) {
-                                       // Didn't work, pleace add more code
-                                       $this->partialStub('Please add code handling recipients ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',packageData=' . print_r($packageData, true) . ',exception=' . $e->__toString() . ',message=' . $e->getMessage());
-                               }
-                               break;
-               } // END - switch
+               // First try out the direct recipient (session id)
+               try {
+                       // Get instance (should not break)
+                       $recipientInstance = ObjectFactory::createObjectByConfiguredName('direct_recipient_class');
+
+                       // Try to solve it
+                       $recipientInstance->resolveRecipient($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $this->getListInstance());
+               } catch (FrameworkException $e) {
+                       // Didn't work, so try the non-generic, depending recipient field itself (this may fail)
+                       try {
+                               // Try to find the right class
+                               $recipientInstance = ObjectFactory::createObjectByConfiguredName(strtolower($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]) . '_recipient_class');
+
+                               // And try to solve again
+                               $recipientInstance->resolveRecipient('', $this->getListInstance());
+                       } catch (FrameworkException $e) {
+                               // Could not find class, what ever failed
+                               $this->debugInstance($e->getMessage());
+                       }
+               }
        }
 
        /**