3 * A PackageRecipient discovery class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class PackageRecipientDiscovery extends BaseRecipientDiscovery implements DiscoverableNodeRecipient, Registerable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Create an instance of this class
38 * @return $discoveryInstance An instance of this discovery class
40 public static final function createPackageRecipientDiscovery () {
41 // Get an instance of this class
42 $discoveryInstance = new PackageRecipientDiscovery();
44 // Return the prepared instance
45 return $discoveryInstance;
49 * Tries to discover all recipients for given package data
51 * @param $packageData Raw package data array
54 public function discoverRecipients (array $packageData) {
55 // This must be available
56 //* DEBUG: */ print $this->__toString() . ': packageData=' . print_r($packageData, TRUE);
57 assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
59 // First try out the direct recipient (session id)
61 // Get instance (should not break)
62 $recipientInstance = ObjectFactory::createObjectByConfiguredName('direct_recipient_class');
65 $recipientInstance->resolveRecipient($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $this->getListInstance(), $packageData);
66 } catch (FrameworkException $e) {
67 // Didn't work, so try the non-generic, depending recipient field itself (this may fail)
69 // Try to find the right class
70 $recipientInstance = ObjectFactory::createObjectByConfiguredName(strtolower($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]) . '_recipient_class');
72 // And try to solve again
73 $recipientInstance->resolveRecipient($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $this->getListInstance(), $packageData);
74 } catch (FrameworkException $e) {
75 // Could not find class, what ever failed
76 $this->debugInstance($e->getMessage());
82 * Tries to discover all recipients by given decoded package data.
84 * @param $decodedData Raw raw package data array
86 * @todo Add some validation of recipient field, e.g. an Universal Node Locator is found
87 * @todo Enrich both messages with recipient data
89 public function discoverRawRecipients (array $decodedData) {
90 // This must be available
91 assert(isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
93 // First clear all recipients
94 $this->clearRecipients();
96 // Get a protocol handler back from decoded data
97 $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($decodedData);
99 // Is the 'recipient' field same as this peer's IP?
100 if ($handlerInstance->isOwnAddress()) {
102 * Is same as own external address + TCP/UDP listen port or
103 * internal address, don't do anything here so other classes found
104 * an empty recipient list for internal (own) handling of the
108 // Debug output (may flood)
109 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient matches own ip (external=' . HubTools::determineOwnExternalAddress() . ' or internal=' . HubTools::determineOwnInternalAddress() . ')');
111 // Debug output (may flood)
112 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY[' . __METHOD__ . ':' . __LINE__ . ']: Recipient is different than own external address (' . HubTools::determineOwnExternalAddress() . ') nor internal address (' . HubTools::determineOwnInternalAddress() . '), need to forward (not yet implemented)!');
114 // This package is to be delivered to someone else, so add it
115 // @TODO Unfinished: $this->getListInstance()->addEntry('unl', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);