]> git.mxchange.org Git - hub.git/blob - application/hub/main/discovery/package/class_PackageRecipientDiscovery.php
5567d36ed280b68c6afa7794e2f4c7aa38f5bc76
[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          */
61         public function discoverRecipients (array $packageData) {
62                 // First try out the direct recipient (session id)
63                 try {
64                         // Get instance (should not break)
65                         $recipientInstance = ObjectFactory::createObjectByConfiguredName('direct_recipient_class');
66
67                         // Try to solve it
68                         $recipientInstance->resolveRecipient($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $this->getListInstance());
69                 } catch (FrameworkException $e) {
70                         // Didn't work, so try the non-generic, depending recipient field itself (this may fail)
71                         try {
72                                 // Try to find the right class
73                                 $recipientInstance = ObjectFactory::createObjectByConfiguredName(strtolower($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]) . '_recipient_class');
74
75                                 // And try to solve again
76                                 $recipientInstance->resolveRecipient('', $this->getListInstance());
77                         } catch (FrameworkException $e) {
78                                 // Could not find class, what ever failed
79                                 $this->debugInstance($e->getMessage());
80                         }
81                 }
82         }
83
84         /**
85          * Tries to discover all recipients by given decoded package data.
86          *
87          * @param       $decodedData    Raw raw package data array
88          * @return      void
89          * @todo        Add some validation of recipient field, e.g. ip:port is found
90          * @todo        The if() does only check for TCP, not UDP, e.g. try to get a $handlerInstance here
91          */
92         public function discoverRawRecipients (array $decodedData) {
93                 // First clear all recipients
94                 $this->clearRecipients();
95
96                 // Explode 'recipient', first element is the IP/hostname
97                 $recipient = explode(':', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
98
99                 // Is the 'recipient' field same as this peer's IP?
100                 if ((($recipient[0] == HubTools::determineOwnExternalIp()) && ($recipient[1] == $this->getConfigInstance()->getConfigEntry('node_listen_port'))) || ($recipient[0] == $this->getConfigInstance()->getServerAddress())) {
101                         /*
102                          * Is same as own external IP + TCP/UDP listen port or internal IP, don't do anything here so other
103                          * classes found an empty recipient list for internal (own) handling
104                          * of the original content.
105                          */
106
107                         // Debug output (may flood)
108                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $recipient[0] . ' matches own ip (' .  HubTools::determineOwnExternalIp() . ' or ' . $this->getConfigInstance()->getServerAddress() . ')');
109                 } else {
110                         // Debug output (may flood)
111                         /* 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)!');
112
113                         // This package is to be delivered to someone else, so add it
114                         $this->getListInstance()->addEntry('ip_port', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
115                 }
116         }
117
118         /**
119          * "Getter" for recipient iterator
120          *
121          * @return      $iteratorInstance       An instance of a Iterateable object
122          */
123         public function getIterator () {
124                 // Get iterator from it
125                 $iteratorInstance = $this->getListInstance()->getIterator();
126
127                 // Return it
128                 return $iteratorInstance;
129         }
130
131         /**
132          * Checks whether the recipient list is empty
133          *
134          * @return      $isEmpty        Whether the recipient list is empty
135          */
136         public function isRecipientListEmpty () {
137                 // Check it ...
138                 $isEmpty = ($this->getListInstance()->count() == 0);
139
140                 // Return it
141                 return $isEmpty;
142         }
143
144         /**
145          * Clears all recipients for e.g. another package to deliver. This method
146          * simply clears the inner list instance.
147          *
148          * @return      void
149          */
150         public function clearRecipients () {
151                 // Clear the list
152                 $this->getListInstance()->clearList();
153         }
154 }
155
156 // [EOF]
157 ?>