]> git.mxchange.org Git - hub.git/commitdiff
Implemented (very basic) registerLocalNode() and updateLocalNode()
authorRoland Häder <roland@mxchange.org>
Sun, 10 Feb 2013 02:51:30 +0000 (02:51 +0000)
committerRoland Häder <roland@mxchange.org>
Sun, 10 Feb 2013 02:51:30 +0000 (02:51 +0000)
application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
application/hub/main/database/wrapper/node/class_NodeInformationDatabaseWrapper.php
application/hub/main/nodes/class_BaseHubNode.php

index 70b5a58a380bb2d44621c54d5e6997c352b96db8..67fd53b26c7a7e949f067552167f538417056502 100644 (file)
@@ -26,9 +26,12 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
        const DB_TABLE_NODE_DHT = 'node_dht';
 
        // Constants for database column names
-       const DB_COLUMN_NODE_ID    = 'node_id';
-       const DB_COLUMN_SESSION_ID = 'session_id';
-       const DB_COLUMN_IP_PORT    = 'ip_port';
+       const DB_COLUMN_NODE_ID          = 'node_id';
+       const DB_COLUMN_SESSION_ID       = 'session_id';
+       const DB_COLUMN_IP_PORT          = 'ip_port';
+       const DB_COLUMN_PRIVATE_KEY      = 'private_key';
+       const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
+       const DB_COLUMN_NODE_TYPE        = 'node_type';
 
        /**
         * Protected constructor
@@ -56,6 +59,34 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                return $wrapperInstance;
        }
 
+       /**
+        * Prepares an instance of a StoreableCriteria class with all node data for
+        * insert/update queries.
+        *
+        * @return      $dataSetInstance        An instance of a StoreableCriteria class
+        */
+       private function prepareDataSetInstance () {
+               // Get node/request instances
+               $nodeInstance = Registry::getRegistry()->getInstance('node');
+               $requestInstance = ApplicationHelper::getSelfInstance()->getRequestInstance();
+
+               // Get a dataset instance
+               $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
+
+               // Set the primary key
+               $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
+
+               // Add public node data
+               $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_TYPE       , $requestInstance->getRequestElement('mode'));
+               $dataSetInstance->addCriteria(self::DB_COLUMN_IP_PORT         , $nodeInstance->getAddressPort());
+               $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_ID         , $nodeInstance->getNodeId());
+               $dataSetInstance->addCriteria(self::DB_COLUMN_SESSION_ID      , $nodeInstance->getSessionId());
+               $dataSetInstance->addCriteria(self::DB_COLUMN_PRIVATE_KEY_HASH, $nodeInstance->getPrivateKeyHash());
+
+               // Return it
+               return $dataSetInstance;
+       }
+
        /**
         * Checks whether the local (*this*) node is registered in the DHT by
         * checking if the external ip/port is found.
@@ -92,7 +123,14 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * @return      void
         */
        public function registerLocalNode () {
-               $this->partialStub('Not implemented yet.');
+               // Assert to make sure this method is called with no record in DB (the actual backend of the DHT)
+               assert(!$this->isLocalNodeRegistered());
+
+               // Get prepared data set instance
+               $dataSetInstance = $this->prepareDataSetInstance();
+
+               // "Insert" this dataset instance completely into the database
+               $this->queryInsertDataSet($dataSetInstance);
        }
 
        /**
@@ -102,7 +140,27 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
         * @return      void
         */
        public function updateLocalNode () {
-               $this->partialStub('Not implemented yet.');
+               // Assert to make sure this method is called with one record in DB (the actual backend of the DHT)
+               assert($this->isLocalNodeRegistered());
+
+               // Get node instance
+               $nodeInstance = Registry::getRegistry()->getInstance('node');
+
+               // Get search criteria
+               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+               // Search for node id and limit it to one entry
+               $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeInstance->getNodeId());
+               $searchInstance->setLimit(1);
+
+               // Get a prepared dataset instance
+               $dataSetInstance = $this->prepareDataSetInstance();
+
+               // Set search instance
+               $dataSetInstance->setSearchInstance($searchInstance);
+
+               // Update DHT database record
+               $this->queryUpdateDataSet($dataSetInstance);
        }
 }
 
index 4491ef22284e9b0290a2b67a4fa538aabcda137b..e9a4c1cd9a417d62b79392b9ba3130c111a86bc5 100644 (file)
@@ -134,7 +134,7 @@ class NodeInformationDatabaseWrapper extends BaseDatabaseWrapper implements Node
                // Add registration elements to the dataset
                $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
 
-               // "Insert" this dataset instance completely into the database
+               // Update database record
                $this->queryUpdateDataSet($dataSetInstance);
        }
 
@@ -160,7 +160,7 @@ class NodeInformationDatabaseWrapper extends BaseDatabaseWrapper implements Node
                // Add registration elements to the dataset
                $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
 
-               // "Insert" this dataset instance completely into the database
+               // Update database record
                $this->queryUpdateDataSet($dataSetInstance);
        }
 }
index 842c85a1331ba8cd234f710dbdde83644751fd91..5d9d9848c194afcbf13409a874b0c1c514938a8f 100644 (file)
@@ -370,6 +370,9 @@ class BaseHubNode extends BaseHubSystem implements Updateable, AddableCriteria {
         * @return      void
         */
        public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
+               // Make sure request instance is set as it is not optional
+               assert($requestInstance instanceof Requestable);
+
                // Add node number and type
                $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
                $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $requestInstance->getRequestElement('mode'));