]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
Updating node data in local DHT (not yet distributed here, that will do an other...
[hub.git] / application / hub / main / database / wrapper / node / class_NodeDistributedHashTableDatabaseWrapper.php
index 55f182f7f50683a7fb901dbf42e854e25f544e89..daea599f608bb99568d1d27a9814e21ed42177eb 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A database wrapper for distributed hash tables
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
  * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -30,10 +30,14 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
        const DB_COLUMN_SESSION_ID       = 'session_id';
        const DB_COLUMN_EXTERNAL_IP      = 'external_ip';
        const DB_COLUMN_LISTEN_PORT      = 'listen_port';
-       const DB_COLUMN_PRIVATE_KEY      = 'private_key';
        const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
        const DB_COLUMN_NODE_MODE        = 'node_mode';
        const DB_COLUMN_ACCEPTED_OBJECTS = 'accepted_object_types';
+       const DB_COLUMN_NODE_LIST        = 'node_list';
+
+       // Exception codes
+       const EXCEPTION_NODE_ALREADY_REGISTERED = 0x800;
+       const EXCEPTION_NODE_NOT_REGISTERED     = 0x801;
 
        /**
         * Protected constructor
@@ -61,6 +65,49 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                return $wrapperInstance;
        }
 
+       /**
+        * Static getter for an array of all DHT database entries
+        *
+        * @return      $elements       All elements for the DHT dabase
+        */
+       public static final function getAllElements () {
+               // Create array and ...
+               $elements = array(
+                       self::DB_COLUMN_NODE_ID,
+                       self::DB_COLUMN_SESSION_ID,
+                       self::DB_COLUMN_EXTERNAL_IP,
+                       self::DB_COLUMN_LISTEN_PORT,
+                       self::DB_COLUMN_PRIVATE_KEY_HASH,
+                       self::DB_COLUMN_NODE_MODE,
+                       self::DB_COLUMN_ACCEPTED_OBJECTS,
+                       self::DB_COLUMN_NODE_LIST
+               );
+
+               // ... return it
+               return $elements;
+       }
+
+       /**
+        * Prepares a search instance for given node data
+        *
+        * @param       $nodeData                       An array with valid node data
+        * @return      $searchInstance         An instance of a SearchCriteria class
+        */
+       private function prepareSearchInstance (array $nodeData) {
+               // Assert on array elements
+               assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+               // Get instance
+               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+               // Search for node id and limit it to one entry
+               $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]);
+               $searchInstance->setLimit(1);
+
+               // Return it
+               return $searchInstance;
+       }
+
        /**
         * Prepares a "local" instance of a StoreableCriteria class with all node
         * data for insert/update queries. This data set contains data from *this*
@@ -88,6 +135,9 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                // Get an array of all accepted object types
                $objectList = $nodeInstance->getListFromAcceptedObjectTypes();
 
+               // Make sure this is an array
+               assert(is_array($objectList));
+
                // Add public node data
                $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_MODE       , $requestInstance->getRequestElement('mode'));
                $dataSetInstance->addCriteria(self::DB_COLUMN_EXTERNAL_IP     , $ipPort[0]);
@@ -119,7 +169,10 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                        // Get ip:port combination and "explode" it
                        $ipPort = $nodeInstance->getAddressPortArray();
 
-                       // Make sure both is valid
+                       /*
+                        * Make sure both is not 'invalid' which means that the resolver
+                        * didn't work.
+                        */
                        assert(($ipPort[0] !== 'invalid') && ($ipPort[1] !== 'invalid'));
 
                        // Add ip:port/node id as criteria
@@ -131,8 +184,8 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                        // Query database and get a result instance back
                        $resultInstance = $this->doSelectByCriteria($searchInstance);
 
-                       // Cache result of if there is an entry, next() will tell us if the next entry is valid
-                       $GLOBALS[__METHOD__] = $resultInstance->next();
+                       // Cache result of if there is an entry, valid() will tell us if an entry is there
+                       $GLOBALS[__METHOD__] = $resultInstance->valid();
                } // END - if
 
                // Return result
@@ -210,7 +263,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * Registeres a node by given message data.
         *
         * @param       $messageData            An array of all message data
-        * @param       $handlerInstance        An instance of a HandleableMessage class
+        * @param       $handlerInstance        An instance of a Handleable class
         * @return      void
         */
        public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance) {
@@ -224,7 +277,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
 
                // Remove 'node_list'
-               $dataSetInstance->unsetCriteria('node_list');
+               $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
 
                // Run the "INSERT" query
                $this->queryInsertDataSet($dataSetInstance);
@@ -234,7 +287,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * Updates an existing entry in node list
         *
         * @param       $messageData            An array of all message data
-        * @param       $handlerInstance        An instance of a HandleableMessage class
+        * @param       $handlerInstance        An instance of a Handleable class
         * @param       $searchInstance         An instance of LocalSearchCriteria class
         * @return      void
         */
@@ -252,7 +305,105 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
 
                // Remove 'node_list'
-               $dataSetInstance->unsetCriteria('node_list');
+               $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
+
+               // Run the "UPDATE" query
+               $this->queryUpdateDataSet($dataSetInstance);
+       }
+
+       /**
+        * Determines whether the given node data is already inserted in the DHT
+        *
+        * @param       $nodeData               An array with valid node data
+        * @return      $isRegistered   Whether the given node data is already inserted
+        */
+       public function isNodeRegistered (array $nodeData) {
+               // Assert on array elements
+               assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+               // Get search criteria
+               $searchInstance = $this->prepareSearchInstance($nodeData);
+
+               // Query database and get a result instance back
+               $resultInstance = $this->doSelectByCriteria(
+                       // Search instance
+                       $searchInstance,
+                       // Only look for these array elements ("keys")
+                       array(
+                               self::DB_COLUMN_NODE_ID     => TRUE,
+                               self::DB_COLUMN_EXTERNAL_IP => TRUE,
+                               self::DB_COLUMN_LISTEN_PORT => TRUE,
+                       )
+               );
+
+               // Check if there is an entry
+               $isRegistered = $resultInstance->valid();
+
+               // Return registration status
+               return $isRegistered;
+       }
+
+       /**
+        * Registers a node with given data in the DHT. If the node is already
+        * registered this method shall throw an exception.
+        *
+        * @param       $nodeData       An array with valid node data
+        * @return      void
+        * @throws      NodeAlreadyRegisteredException  If the node is already registered
+        */
+       public function registerNode (array $nodeData) {
+               // Assert on array elements
+               assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+               // Is the node registered?
+               if ($this->isNodeRegistered($nodeData)) {
+                       // Throw an exception
+                       throw new NodeAlreadyRegisteredException(array($this, $nodeData), self::EXCEPTION_NODE_ALREADY_REGISTERED);
+               } // END - if
+
+               // @TODO Unimplemented part
+               $this->partialStub('nodeData=' . print_r($nodeData, TRUE));
+       }
+
+       /**
+        * Updates a node's entry in the DHT with given data. This will enrich or
+        * just update already exsiting data. If the node is not found this method
+        * shall throw an exception.
+        *
+        * @param       $nodeData       An array with valid node data
+        * @return      void
+        * @throws      NodeDataMissingException        If the node's data is missing
+        */
+       public function updateNode (array $nodeData) {
+               // Assert on array elements
+               assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+               // Is the node registered?
+               if (!$this->isNodeRegistered($nodeData)) {
+                       // No, then throw an exception
+                       throw new NodeDataMissingException(array($this, $nodeData), self::EXCEPTION_NODE_NOT_REGISTERED);
+               } // END - if
+
+               // Get a search instance
+               $searchInstance = $this->prepareSearchInstance($nodeData);
+
+               // Get a data set instance
+               $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
+
+               // Add search instance
+               $dataSetInstance->setSearchInstance($searchInstance);
+
+               // Set primary key (session id)
+               $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
+
+               // Get node instance
+               $nodeInstance = Registry::getRegistry()->getInstance('node');
+
+               // Add all array elements
+               $nodeInstance->addArrayToDataSet($dataSetInstance, $nodeData);
+
+               // Remove 'node_list'
+               $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
 
                // Run the "UPDATE" query
                $this->queryUpdateDataSet($dataSetInstance);