* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements DiscoverableNodeRecipient, Registerable { /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Create an instance of this class * * @return $discoveryInstance An instance of this discovery class */ public static final function createPackageRecipientDiscovery () { // Get an instance of this class $discoveryInstance = new PackageRecipientDiscovery(); // Return the prepared instance return $discoveryInstance; } /** * Tries to discover all recipients for given package data * * @param $packageData Raw package data array * @return void */ public function discoverRecipients (array $packageData) { // This must be available //* DEBUG: */ print $this->__toString() . ': packageData=' . print_r($packageData, TRUE); assert(isset($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(), $packageData); } 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($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $this->getListInstance(), $packageData); } catch (FrameworkException $e) { // Could not find class, what ever failed $this->debugInstance($e->getMessage()); } } } /** * Tries to discover all recipients by given decoded package data. * * @param $decodedData Raw raw package data array * @return void * @todo Add some validation of recipient field, e.g. an Universal Node Locator is found * @todo Enrich both messages with recipient data */ public function discoverRawRecipients (array $decodedData) { // This must be available assert(isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); // First clear all recipients $this->clearRecipients(); // Get a protocol handler back from decoded data $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($decodedData); // Is the 'recipient' field same as this peer's IP? if ($handlerInstance->isOwnAddress($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) { /* * Is same as own external address + TCP/UDP listen port or * internal address, 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: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient ' . $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' matches own ip (external=' . HubTools::determineOwnExternalAddress() . ' or internal=' . HubTools::determineOwnInternalAddress() . ')'); } else { // Debug output (may flood) /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient ' . $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' is different than own external address (' . HubTools::determineOwnExternalAddress() . ') nor internal address (' . HubTools::determineOwnInternalAddress() . '), need to forward (not yet implemented)!'); // This package is to be delivered to someone else, so add it // @TODO Unfinished: $this->getListInstance()->addEntry('unl', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); } } } // [EOF] ?>