X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fdiscovery%2Fpackage%2Fclass_PackageRecipientDiscovery.php;h=040c3083b65cf7e66da50295438ff3cfd85df532;hb=193e418fd76d06a69d5031b9702fa0c093c95cad;hp=4deb44a3788354eb25e7a73acce6a8419c257fe0;hpb=8e90b922c66aa345f5a6547b2535f90f49310857;p=hub.git diff --git a/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php b/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php index 4deb44a37..040c3083b 100644 --- a/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php +++ b/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php @@ -46,7 +46,7 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable $discoveryInstance->setListInstance($listInstance); // Output debug message - $discoveryInstance->debugOutput('RECIPIENT-DISCOVERY: Initialized.'); + self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY: Initialized.'); // Return the prepared instance return $discoveryInstance; @@ -59,71 +59,29 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable * @return void */ public function discoverRecipients (array $packageData) { - // We do some rudimentary checks but this might be a bit ugly - switch ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]) { - // All upper hubs, these are currently the bootstrap nodes and later on prepended list-nodes - case NetworkPackage::NETWORK_TARGET_UPPER_NODES: - // Get all bootstrap nodes - foreach (explode(BaseHubSystem::BOOTSTRAP_NODES_SEPARATOR, $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $node) { - // 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 ' . $node . ' as recipient.'); - - // Add the entry - $this->getListInstance()->addEntry('ip_port', $node); - } // END - foreach - break; - - // The target 'self' is always the external IP address! - case NetworkPackage::NETWORK_TARGET_SELF: - // Determine own port - $port = $this->getConfigInstance()->getConfigEntry('node_' . $this->determineProtocolByPackageData($packageData) . '_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; - - // This may be a direct recipient (node's session id) - default: - /* - * 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, $packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]); - - // 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 + // This must be available + assert($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + + // 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()); + } + } } /** @@ -135,6 +93,9 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable * @todo The if() does only check for TCP, not UDP, e.g. try to get a $handlerInstance here */ public function discoverRawRecipients (array $decodedData) { + // This must be available + assert($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + // First clear all recipients $this->clearRecipients(); @@ -142,9 +103,9 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable $recipient = explode(':', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); // Is the 'recipient' field same as this peer's IP? - if ((($recipient[0] == HubTools::determineOwnExternalIp()) && ($recipient[1] == $this->getConfigInstance()->getConfigEntry('node_tcp_listen_port'))) || ($recipient[0] == $this->getConfigInstance()->getServerAddress())) { + if ((($recipient[0] == HubTools::determineOwnExternalIp()) && ($recipient[1] == $this->getConfigInstance()->getConfigEntry('node_listen_port'))) || ($recipient[0] == $this->getConfigInstance()->getServerAddress())) { /* - * Is same as own external IP+TCP port or internal IP, don't do anything here so other + * Is same as own external IP + TCP/UDP listen port or internal IP, don't do anything here so other * classes found an empty recipient list for internal (own) handling * of the original content. */