]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/dht/node/class_NodeDhtFacade.php
Continued with DHT stuff:
[hub.git] / application / hub / main / dht / node / class_NodeDhtFacade.php
index a80a3625905bdb6414c48a8594dba19452937f41..a3d1524b88919a9875d8c12fd1ce3091693e0869 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
+class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable {
        /**
         * Protected constructor
         *
@@ -137,6 +137,10 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                 */
                $resultInstance = $this->getWrapperInstance()->findNodeLocalBySessionId($sessionId);
 
+               // Make sure the result instance is valid
+               assert($resultInstance instanceof SearchableResult);
+               assert($resultInstance->valid());
+
                // Is the next entry valid?
                if (($resultInstance->valid()) && ($resultInstance->next())) {
                        /*
@@ -192,6 +196,10 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                // Run the query
                $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
 
+               // Make sure the result instance is valid
+               assert($resultInstance instanceof SearchableResult);
+               assert($resultInstance->valid());
+
                // Is there already an entry?
                if ($resultInstance->valid()) {
                        // Entry found, so update it
@@ -251,8 +259,14 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                // Run the query
                $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
 
-               // Get node list
+               // Make sure the result instance is valid
+               assert($resultInstance instanceof SearchableResult);
+               assert($resultInstance->valid());
+
+               // Init array
                $nodeList = array();
+
+               // Get node list
                while ($resultInstance->next()) {
                        // Get current element (it should be an array, and have at least 1 entry)
                        $current = $resultInstance->current();
@@ -310,12 +324,13 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
         * @return      $recipients             An indexed array with DHT recipients
         */
        public function findRecipientsByPackageData (array $packageData) {
-               // Get max recipients
-               $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
-
                // Query get a result instance back from DHT database wrapper.
                $resultInstance = $this->getWrapperInstance()->getResultFromExcludedSender($packageData);
 
+               // Make sure the result instance is valid
+               assert($resultInstance instanceof SearchableResult);
+               assert($resultInstance->valid());
+
                // Init array
                $recipients = array();
 
@@ -327,12 +342,38 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
 
                        // Add instance to recipient list
                        array_push($recipients, $current);
+               } // END - while
 
-                       // Has the maximum been reached?
-                       if (count($recipients) == $maxRecipients) {
-                               // Stop search here
-                               break;
-                       } // END - if
+               // Return filled array
+               return $recipients;
+       }
+
+       /**
+        * Finds DHT recipients by given key/value pair
+        *
+        * @param       $key            Key to search for
+        * @param       $value          Value to check on found key
+        * @return      $recipiens      Array with DHT recipients from given key/value pair
+        */
+       public function findRecipientsByKey ($key, $value) {
+               // Look for all suitable nodes
+               $resultInstance = $this->getWrapperInstance()->getResultFromKeyValue($key, $value);
+
+               // Make sure the result instance is valid
+               assert($resultInstance instanceof SearchableResult);
+               assert($resultInstance->valid());
+
+               // Init array
+               $recipients = array();
+
+               // "Walk" through all entries
+               while ($resultInstance->next()) {
+                       // Get current entry
+                       $current = $resultInstance->current();
+               } // END - while
+
+                       // Add instance to recipient list
+                       array_push($recipients, $current);
                } // END - while
 
                // Return filled array