]> git.mxchange.org Git - hub.git/blob - application/hub/main/discovery/package/class_PackageRecipientDiscovery.php
Added missing XML, config entries for DHT bootstrap
[hub.git] / application / hub / main / discovery / package / class_PackageRecipientDiscovery.php
1 <?php
2 /**
3  * A PackageRecipient discovery class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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 BaseHubDiscovery implements DiscoverableRecipient, 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                 // Get recipients list instance and set it
45                 $listInstance = RecipientListFactory::createRecipientListInstance();
46                 $discoveryInstance->setListInstance($listInstance);
47
48                 // Output debug message
49                 self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY: Initialized.');
50
51                 // Return the prepared instance
52                 return $discoveryInstance;
53         }
54
55         /**
56          * Tries to discover all recipients for given package data
57          *
58          * @param       $packageData    Raw package data array
59          * @return      void
60          * @todo        Find a better way than this switch/case/default thing
61          */
62         public function discoverRecipients (array $packageData) {
63                 // We do some rudimentary checks but this might be a bit ugly
64                 switch ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]) {
65                         case NetworkPackage::NETWORK_TARGET_UPPER_NODES: // All upper hubs, these are currently the bootstrap nodes and later on prepended list-nodes
66                                 // Get all bootstrap nodes
67                                 foreach (explode(BaseHubSystem::BOOTSTRAP_NODES_SEPARATOR, $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $ipPort) {
68                                         // Is maximum reached?
69                                         if ($this->getListInstance()->count() == $this->getConfigInstance()->getConfigEntry('package_recipient_max_count')) {
70                                                 // Debug message
71                                                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DISCOVERY: Going to abort at maximum of ' . $this->getConfigInstance()->getConfigEntry('package_recipient_max_count') . ' recipients!');
72
73                                                 // Then stop adding more
74                                                 break;
75                                         } // END - if
76
77                                         // Debug message
78                                         /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DISCOVERY: Adding node ' . $ipPort . ' as recipient.');
79
80                                         // Add the entry
81                                         $this->getListInstance()->addEntry('ip_port', $ipPort);
82                                 } // END - foreach
83                                 break;
84
85                         case NetworkPackage::NETWORK_TARGET_SELF: // The target 'self' is always the external IP address!
86                                 // Determine own port
87                                 $port = $this->getConfigInstance()->getConfigEntry('node_listen_port');
88
89                                 // Determine IP or 'external_ip' if set
90                                 $ip = HubTools::determineOwnExternalIp();
91
92                                 // Is it not empty?
93                                 if (!empty($ip)) {
94                                         // Add it to the list
95                                         $this->getListInstance()->addEntry('ip_port', $ip . ':' . $port);
96                                 } // END - if
97                                 break;
98
99                         case NetworkPackage::NETWORK_TARGET_DHT: // The target 'dht' is a list from local DHT query
100                                 $this->partialStub('DHT recipient unimplemented: packageData=' . print_r($packageData, true));
101                                 break;
102
103                         default: // This may be a direct recipient (node's session id)
104                                 /*
105                                  * Try to solve it, if an exception comes back, it is not a
106                                  * session-id, nor IP:port and not a hostname:port combination.
107                                  */
108                                 try {
109                                         // "Explode" all recipients
110                                         $recipients = explode(NetworkPackage::PACKAGE_RECIPIENT_SEPARATOR, $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
111
112                                         // Is maximum reached?
113                                         assert(count($recipients) <= $this->getConfigInstance()->getConfigEntry('package_recipient_max_count'));
114
115                                         // Try it on all
116                                         foreach ($recipients as $recipient) {
117                                                 // Try to sole a single recipient
118                                                 $ipPort = HubTools::resolveSessionId($recipient);
119
120                                                 // Add it as recipient
121                                                 $this->getListInstance()->addEntry('ip_port', $ipPort);
122                                         } // END - foreach
123                                 } catch (FrameworkException $e) {
124                                         // Didn't work, pleace add more code
125                                         $this->partialStub('Please add code handling recipients ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',packageData=' . print_r($packageData, true) . ',exception=' . $e->__toString() . ',message=' . $e->getMessage());
126                                 }
127                                 break;
128                 } // END - switch
129         }
130
131         /**
132          * Tries to discover all recipients by given decoded package data.
133          *
134          * @param       $decodedData    Raw raw package data array
135          * @return      void
136          * @todo        Add some validation of recipient field, e.g. ip:port is found
137          * @todo        The if() does only check for TCP, not UDP, e.g. try to get a $handlerInstance here
138          */
139         public function discoverRawRecipients (array $decodedData) {
140                 // First clear all recipients
141                 $this->clearRecipients();
142
143                 // Explode 'recipient', first element is the IP/hostname
144                 $recipient = explode(':', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
145
146                 // Is the 'recipient' field same as this peer's IP?
147                 if ((($recipient[0] == HubTools::determineOwnExternalIp()) && ($recipient[1] == $this->getConfigInstance()->getConfigEntry('node_listen_port'))) || ($recipient[0] == $this->getConfigInstance()->getServerAddress())) {
148                         /*
149                          * Is same as own external IP + TCP/UDP listen port or internal IP, don't do anything here so other
150                          * classes found an empty recipient list for internal (own) handling
151                          * of the original content.
152                          */
153
154                         // Debug output (may flood)
155                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $recipient[0] . ' matches own ip (' .  HubTools::determineOwnExternalIp() . ' or ' . $this->getConfigInstance()->getServerAddress() . ')');
156                 } else {
157                         // Debug output (may flood)
158                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->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)!');
159
160                         // This package is to be delivered to someone else, so add it
161                         $this->getListInstance()->addEntry('ip_port', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
162                 }
163         }
164
165         /**
166          * "Getter" for recipient iterator
167          *
168          * @return      $iteratorInstance       An instance of a Iterateable object
169          */
170         public function getIterator () {
171                 // Get iterator from it
172                 $iteratorInstance = $this->getListInstance()->getIterator();
173
174                 // Return it
175                 return $iteratorInstance;
176         }
177
178         /**
179          * Checks whether the recipient list is empty
180          *
181          * @return      $isEmpty        Whether the recipient list is empty
182          */
183         public function isRecipientListEmpty () {
184                 // Check it ...
185                 $isEmpty = ($this->getListInstance()->count() == 0);
186
187                 // Return it
188                 return $isEmpty;
189         }
190
191         /**
192          * Clears all recipients for e.g. another package to deliver. This method
193          * simply clears the inner list instance.
194          *
195          * @return      void
196          */
197         public function clearRecipients () {
198                 // Clear the list
199                 $this->getListInstance()->clearList();
200         }
201 }
202
203 // [EOF]
204 ?>