X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fdiscovery%2Fpackage%2Fclass_PackageRecipientDiscovery.php;h=a5cd4e144f0ebd63ce2512d199e5d6e001c9fd4a;hb=e10aa2b808ead315a4b8924f7b2ec2cc349ff414;hp=4a687ddbc77b2abf558d0491eb17e15aa41d3769;hpb=2ab36c5181304d0d0c17755f546cb625df4f172f;p=hub.git diff --git a/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php b/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php index 4a687ddbc..a5cd4e144 100644 --- a/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php +++ b/application/hub/main/discovery/package/class_PackageRecipientDiscovery.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -41,6 +41,10 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable // Get an instance of this class $discoveryInstance = new PackageRecipientDiscovery(); + // Get recipients list instance and set it + $listInstance = RecipientListFactory::createRecipientListInstance(); + $discoveryInstance->setListInstance($listInstance); + // Output debug message $discoveryInstance->debugOutput('RECIPIENT-DISCOVERY: Initialized.'); @@ -55,20 +59,17 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable * @return void */ public function discoverRecipients (array $packageData) { - // Get recipients list instance - $listInstance = RecipientListFactory::createRecipientListInstance(); - // 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_HUBS: // Get all bootstrap nodes - foreach (explode(BaseHubSystem::BOOTSTRAP_NODES_SEPERATOR, $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $node) { + foreach (explode(BaseHubSystem::BOOTSTRAP_NODES_SEPARATOR, $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $node) { // Debug message /* DEBUG: */ $this->debugOutput('DISCOVERY: Adding node ' . $node . ' as recipient.'); // Add the entry - $listInstance->addEntry('ip_port', $node); + $this->getListInstance()->addEntry('ip_port', $node); } // END - foreach break; @@ -81,7 +82,7 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable $ipPort = $ip . ':' . $this->getConfigInstance()->getConfigEntry('node_' . $this->determineProtocolByPackageData($packageData) . '_listen_port'); // Add it to the list - $listInstance->addEntry('ip_port', $ipPort); + $this->getListInstance()->addEntry('ip_port', $ipPort); break; // This may be a direct recipient (node's session id) @@ -91,21 +92,76 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable } // END - switch } + /** + * Tries to discover all recipients by given decoded package data. + * + * @param $decodedData Decoded raw package data array + * @return void + * @todo Add some validation of recipient field, e.g. ip:port is found + * @todo The if() does only check for TCP, not UDP, e.g. try to get a $handlerInstance here + */ + public function discoverDecodedRecipients (array $decodedData) { + // First clear all recipients + $this->clearRecipients(); + + // Explode 'recipient', first element is the IP/hostname + $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())) { + /* + * Is same as own external IP+TCP 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. + */ + + // Debug output (may flood) + /* NOISY-DEBUG: */ $this->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $recipient[0] . ' matches own ip (' . HubTools::determineOwnExternalIp() . ' or ' . $this->getConfigInstance()->getServerAddress() . ')'); + } else { + // Debug output (may flood) + /* NOISY-DEBUG: */ $this->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $recipient[0] . ' is different than own external ip (' . HubTools::determineOwnExternalIp() . ') nor internal ip (' . $this->getConfigInstance()->getServerAddress() . '), need to forward (not yet implemented)!'); + + // This package is to be delivered to someone else, so add it + $this->getListInstance()->addEntry('ip_port', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + } + } + /** * "Getter" for recipient iterator * * @return $iteratorInstance An instance of a Iterateable object */ public function getIterator () { - // Get list instance - $listInstance = RecipientListFactory::createRecipientListInstance(); - // Get iterator from it - $iteratorInstance = $listInstance->getIterator(); + $iteratorInstance = $this->getListInstance()->getIterator(); // Return it return $iteratorInstance; } + + /** + * Checks whether the recipient list is empty + * + * @return $isEmpty Whether the recipient list is empty + */ + public function isRecipientListEmpty () { + // Check it ... + $isEmpty = ($this->getListInstance()->count() == 0); + + // Return it + return $isEmpty; + } + + /** + * Clears all recipients for e.g. another package to deliver. This method + * simply clears the inner list instance. + * + * @return void + */ + public function clearRecipients () { + // Clear the list + $this->getListInstance()->clearList(); + } } // [EOF]