]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/discovery/package/class_PackageRecipientDiscovery.php
'hub' project continued:
[hub.git] / application / hub / main / discovery / package / class_PackageRecipientDiscovery.php
index d1de1423206502e795ab3adc259f13e5fc989384..6c70284b6c340dac4ab968a18db251e136f7cad6 100644 (file)
@@ -41,13 +41,13 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable
                // Get an instance of this class
                $discoveryInstance = new PackageRecipientDiscovery();
 
-               // Output debug message
-               $discoveryInstance->debugOutput('RECIPIENT-DISCOVERY: Initialized.');
-
                // Get recipients list instance and set it
                $listInstance = RecipientListFactory::createRecipientListInstance();
                $discoveryInstance->setListInstance($listInstance);
 
+               // Output debug message
+               $discoveryInstance->debugOutput('RECIPIENT-DISCOVERY: Initialized.');
+
                // Return the prepared instance
                return $discoveryInstance;
        }
@@ -92,6 +92,39 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable
                } // END - switch
        }
 
+       /**
+        * Tries to discover all recipients by given decoded package data.
+        *
+        * @param       $decodedData    Decoded raw package data array
+        * @return      void
+        * @todo        Add some validation of recipient field, e.g. ip:port is found
+        */
+       public function discoverDecodedRecipients (array $decodedData) {
+               // First clear all recipients
+               $this->clearRecipients();
+
+               // Explode 'recipient', first element is the IP/hostname
+               $recipient = explode(':', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
+
+               // Is the 'recipient' field same as this peer's IP?
+               if ($recipient[0] == HubTools::determineOwnExternalIp()) {
+                       /*
+                        * Is same as own external IP, 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: */ $this->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $recipient[0] . ' matches own ip (' .  HubTools::determineOwnExternalIp() . ')');
+               } else {
+                       // Debug output (may flood)
+                       /* NOISY-DEBUG: */ $this->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $recipient[0] . ' is different than own ip (' .  HubTools::determineOwnExternalIp() . '), need to forward (not yet implemented)!');
+
+                       // This package is to be delivered to someone else, so add it
+                       $this->getListInstance()->addEntry('ip_port', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]);
+               }
+       }
+
        /**
         * "Getter" for recipient iterator
         *
@@ -105,6 +138,19 @@ class PackageRecipientDiscovery extends BaseHubDiscovery implements Discoverable
                return $iteratorInstance;
        }
 
+       /**
+        * Checks whether the recipient list is empty
+        *
+        * @return      $isEmpty        Whether the recipient list is empty
+        */
+       public function isRecipientListEmpty () {
+               // Check it ...
+               $isEmpty = ($this->getListInstance()->count() == 0);
+
+               // Return it
+               return $isEmpty;
+       }
+
        /**
         * Clears all recipients for e.g. another package to deliver. This method
         * simply clears the inner list instance.