Updated 'core'.
[hub.git] / application / hub / main / discovery / recipient / package / class_PackageRecipientDiscovery.php
1 <?php
2 /**
3  * A PackageRecipient discovery class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class PackageRecipientDiscovery extends BaseRecipientDiscovery implements DiscoverableNodeRecipient, Registerable {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33         }
34
35         /**
36          * Create an instance of this class
37          *
38          * @return      $discoveryInstance      An instance of this discovery class
39          */
40         public static final function createPackageRecipientDiscovery () {
41                 // Get an instance of this class
42                 $discoveryInstance = new PackageRecipientDiscovery();
43
44                 // Return the prepared instance
45                 return $discoveryInstance;
46         }
47
48         /**
49          * Tries to discover all recipients for given package data
50          *
51          * @param       $packageData    Raw package data array
52          * @return      void
53          */
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]));
58
59                 // First try out the direct recipient (session id)
60                 try {
61                         // Get instance (should not break)
62                         $recipientInstance = ObjectFactory::createObjectByConfiguredName('direct_recipient_class');
63
64                         // Try to solve it
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)
68                         try {
69                                 // Try to find the right class
70                                 $recipientInstance = ObjectFactory::createObjectByConfiguredName(strtolower($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]) . '_recipient_class');
71
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());
77                         }
78                 }
79         }
80
81         /**
82          * Tries to discover all recipients by given decoded package data.
83          *
84          * @param       $decodedData    Raw raw package data array
85          * @return      void
86          * @todo        Add some validation of recipient field, e.g. an Universal Node Locator is found
87          * @todo        Enrich both messages with recipient data
88          */
89         public function discoverRawRecipients (array $decodedData) {
90                 // This must be available
91                 assert(isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
92
93                 // First clear all recipients
94                 $this->clearRecipients();
95
96                 // Get a protocol handler back from decoded data
97                 $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($decodedData);
98
99                 // Is the 'recipient' field same as this peer's IP?
100                 if ($handlerInstance->isOwnAddress($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) {
101                         /*
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
105                          * original content.
106                          */
107
108                         // Debug output (may flood)
109                         /* 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() . ')');
110                 } else {
111                         // Debug output (may flood)
112                         /* 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)!');
113
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]);
116                 }
117         }
118 }
119
120 // [EOF]
121 ?>