]> git.mxchange.org Git - hub.git/commitdiff
Continued with DHT stuff:
authorRoland Haeder <roland@mxchange.org>
Tue, 18 Mar 2014 22:46:41 +0000 (23:46 +0100)
committerRoland Haeder <roland@mxchange.org>
Tue, 18 Mar 2014 22:46:41 +0000 (23:46 +0100)
- added a very generic way to query the local DHT for key/value matches
- used this way to look for DHT nodes accepting bootstrap requests.
- renamed method DhtObjectFactory::createDhtObjectInstance to createDhtInstance
  to match with its getter/setter mates.

Signed-off-by: Roland Haeder <roland@mxchange.org>
16 files changed:
application/hub/interfaces/distributable/class_Distributable.php
application/hub/interfaces/wrapper/class_NodeDhtWrapper.php
application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
application/hub/main/dht/class_BaseDht.php
application/hub/main/dht/node/class_NodeDhtFacade.php
application/hub/main/discovery/dht/class_DhtRecipientDiscovery.php
application/hub/main/factories/dht/class_DhtObjectFactory.php
application/hub/main/handler/class_BaseDataHandler.php
application/hub/main/handler/message-types/answer/class_NodeMessageDhtBootstrapAnswerHandler.php
application/hub/main/handler/message-types/dht/class_NodeMessageDhtBootstrapHandler.php
application/hub/main/tasks/node/dht/class_NodeDhtBootstrapTask.php
application/hub/main/tasks/node/dht/class_NodeDhtInitializationTask.php
application/hub/main/tasks/node/dht/class_NodeDhtPublicationCheckTask.php
application/hub/main/tasks/node/dht/class_NodeDhtPublicationTask.php
application/hub/main/tasks/node/dht/class_NodeDhtQueryTask.php
application/hub/main/tools/class_HubTools.php

index 8adbc8d64b822eef96c3b40e86a24a9f7aacf22e..0743d683e0e0421640ff228b011291981d3ced53 100644 (file)
@@ -97,6 +97,15 @@ interface Distributable extends FrameworkInterface {
         * @return      $isBooting      Whether this DHT is currently booting
         */
        function ifDhtIsBooting ();
+
+       /**
+        * Finds DHT recipients by given key/value pair
+        *
+        * @param       $key                    Key to search for
+        * @param       $value                  Value to check on found key
+        * @return      $recipientList  Array with DHT recipients from given key/value pair
+        */
+       function findRecipientsByKey ($key, $value);
 }
 
 // [EOF]
index 938a3189ff2897cdb2647ed5d4750fe7f7d44ba9..b962cc9de0558d928e17342495820221878414da 100644 (file)
@@ -148,6 +148,16 @@ interface NodeDhtWrapper extends DatabaseWrapper {
         * @return      $recipients             An indexed array with DHT recipients
         */
        function getResultFromExcludedSender (array $packageData);
+
+       /**
+        * Find recopients by given key/value pair. First look for the key and if it
+        * matches, compare the value.
+        *
+        * @param       $key                    Key to look for
+        * @param       $value                  Value to compare if key matches
+        * @return      $recipients             An indexed array with DHT recipients
+        */
+       function getResultFromKeyValue ($key, $value);
 }
 
 // [EOF]
index 8c3a8a5e3f8bb81606988f39f95e3ba6f60b8c3a..08c8a77164778f7660debb5848e6a8d1ba28f5a8 100644 (file)
@@ -41,6 +41,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
        const DB_COLUMN_NODE_LIST          = 'node_list';
        const DB_COLUMN_PUBLICATION_STATUS = 'publication_status';
        const DB_COLUMN_ANSWER_STATUS      = 'answer_status';
+       const DB_COLUMN_ACCEPT_BOOTSTRAP   = 'accept_bootstrap';
 
        // Publication status'
        const PUBLICATION_STATUS_PENDING = 'PENDING';
@@ -519,12 +520,43 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                // Assert on required array field
                assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
 
+               // Get max recipients
+               $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
+
                // First creata a search instance
                $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
                // Then exclude 'sender' field as the sender is the current (*this*) node
                $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
 
+               // Set limit to maximum DHT recipients
+               $searchInstance->setLimit($maxRecipients);
+
+               // Get a result instance back from DHT database wrapper.
+               $resultInstance = $this->doSelectByCriteria($searchInstance);
+
+               // Return result instance
+               return $resultInstance;
+       }
+
+       /**
+        * Find recopients by given key/value pair. First look for the key and if it
+        * matches, compare the value.
+        *
+        * @param       $key                    Key to look for
+        * @param       $value                  Value to compare if key matches
+        * @return      $recipients             An indexed array with DHT recipients
+        */
+       public function getResultFromKeyValue ($key, $value) {
+               // Get max recipients
+               $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
+
+               // First creata a search instance
+               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+               // Find the key/value pair
+               $searchInstance->addCriteria($key, $value);
+
                // Get a result instance back from DHT database wrapper.
                $resultInstance = $this->doSelectByCriteria($searchInstance);
 
index d81cbcd2934286114453df0a1554a6367cedb345..7a667811aa99df7167befcb1698875bb367079fb 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/>.
  */
-abstract class BaseDht extends BaseHubSystem {
+abstract class BaseDht extends BaseHubSystem implements Distributable {
        /**
         * "Cached" instance of a publish helper
         */
@@ -150,7 +150,8 @@ abstract class BaseDht extends BaseHubSystem {
                // Get result instance
                $resultInstance = $this->getWrapperInstance()->getUnpublishedEntriesInstance();
 
-               // Must still be valid
+               // Make sure the result instance is valid
+               assert($resultInstance instanceof SearchableResult);
                assert($resultInstance->valid());
 
                // "Walk" through all entries
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
index f9c2a46304dd7726dd8e7c5a4d4d911ef42a2f09..35809b67f7f22244a08b30ae43c1e022c2fd3354 100644 (file)
@@ -42,7 +42,7 @@ class DhtRecipientDiscovery extends BaseNodeDiscovery implements DiscoverableDht
                $discoveryInstance = new DhtRecipientDiscovery();
 
                // Get a DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Set it here
                $discoveryInstance->setDhtInstance($dhtInstance);
index 4c7ff621f0a6fb3f2db6141f2a9de2ade57ac99b..11f35a8bbf6428534c1113237713397aed2690db 100644 (file)
@@ -38,7 +38,7 @@ class DhtObjectFactory extends ObjectFactory {
         * @param       $prefix                 Prefix for DHT class name and registry key
         * @return      $dhtInstance    An instance of a DHT object class
         */
-       public static final function createDhtObjectInstance ($prefix) {
+       public static final function createDhtInstance ($prefix) {
                // Set instance name
                $name = $prefix . '_dht';
 
index 902c66783d62bdaa6d5e243f7088b18966bfe475..8af6712819c1757a30ad1a79c7c37581f047acc2 100644 (file)
@@ -59,7 +59,7 @@ abstract class BaseDataHandler extends BaseHandler {
                parent::__construct($className);
 
                // Get a DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Set it here
                $this->setDhtInstance($dhtInstance);
index 23fc0f5f7acc254e8110591abdb577cab502d60f..cf4fa42c9f6c86844dafa2c2b1d5a945d3b8ee20 100644 (file)
@@ -94,7 +94,7 @@ class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements
         */
        public function handleMessageData (array $messageData, Receivable $packageInstance) {
                // Get DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Has this DHT attempted to bootstrap?
                if (!$dhtInstance->ifDhtIsBooting()) {
index 5356cb6a423ac59e0205f0cacb750a50e4272daa..e6344ca18c42ee015bf0154c8d905f4f9c8a9b85 100644 (file)
@@ -82,6 +82,12 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
                // ... and set it here
                $handlerInstance->setNodeInstance($nodeInstance);
 
+               // Get a DHT instance
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
+
+               // Set the DHT instance here
+               $handlerInstance->setDhtInstance($dhtInstance);
+
                // Return the prepared instance
                return $handlerInstance;
        }
@@ -164,6 +170,20 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
 
                // Set it in configuration (temporarily)
                $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
+
+               /*
+                * Use the DHT instance to get a list of recipients. This means that all
+                * DHT nodes that accept bootstrap requests are read from the DHT
+                * database.
+                */
+               $nodeList = $this->getDhtInstance()->findRecipientsByKey(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPT_BOOTSTRAP, 'Y');
+
+               // Make sure it is an array and has at least one entry
+               assert(is_array($nodeList));
+               assert(count($nodeList) > 0);
+
+               // Set it in configuration
+               $this->getConfigInstance()->setConfigEntry('dht_nodes', base64_encode(serialize($nodeList)));
        }
 
        /**
index b4614ab6da84e5b95d46fd1924ea901c75f12edf..73dedd4d400f0a1b0df6e0579d46a9a2afd02afa 100644 (file)
@@ -42,7 +42,7 @@ class NodeDhtBootstrapTask extends BaseTask implements Taskable, Visitable {
                $taskInstance = new NodeDhtBootstrapTask();
 
                // Get a DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Set the DHT instance here
                $taskInstance->setDhtInstance($dhtInstance);
index 25f3414910c8ff0d15a3db04b62605650444cfec..7daf578413f0332ade27407072c14cafc440ea4a 100644 (file)
@@ -42,7 +42,7 @@ class NodeDhtInitializationTask extends BaseTask implements Taskable, Visitable
                $taskInstance = new NodeDhtInitializationTask();
 
                // Get a DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Set the DHT instance here
                $taskInstance->setDhtInstance($dhtInstance);
index 7037e08ceaa8ef672c7dc540dc6805bc0e6dcddb..cfa8b4753e2bba66a3f5c0ea3b229f403c9ec032 100644 (file)
@@ -42,7 +42,7 @@ class NodeDhtPublicationCheckTask extends BaseTask implements Taskable, Visitabl
                $taskInstance = new NodeDhtPublicationCheckTask();
 
                // Get a DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Set the DHT instance here
                $taskInstance->setDhtInstance($dhtInstance);
index 437b050d86a8d301ec57911018e57d44009f88c8..d4486239513e67f8b0aba30dce6b953177c7a1c9 100644 (file)
@@ -42,7 +42,7 @@ class NodeDhtPublicationTask extends BaseTask implements Taskable, Visitable {
                $taskInstance = new NodeDhtPublicationTask();
 
                // Get a DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Set the DHT instance here
                $taskInstance->setDhtInstance($dhtInstance);
index f8daa16d34c0aafc0412a0c223c98339af95fe96..55576e718edc52094d0650aa1c3614c421d1b031 100644 (file)
@@ -42,7 +42,7 @@ class NodeDhtQueryTask extends BaseTask implements Taskable, Visitable {
                $taskInstance = new NodeDhtQueryTask();
 
                // Get a DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Set the DHT instance here
                $taskInstance->setDhtInstance($dhtInstance);
index 122ce3a185b82c11a3eadaabd2211f85bcfd9979..d59db66541245e3eee2abeebcde2674c6a514a4c 100644 (file)
@@ -51,7 +51,7 @@ class HubTools extends BaseHubSystem {
                parent::__construct(__CLASS__);
 
                // Get a DHT instance
-               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
 
                // Set it here
                $this->setDhtInstance($dhtInstance);