]> git.mxchange.org Git - hub.git/commitdiff
Hub-Id creation basicly finished
authorRoland Häder <roland@mxchange.org>
Wed, 25 Mar 2009 09:02:51 +0000 (09:02 +0000)
committerRoland Häder <roland@mxchange.org>
Wed, 25 Mar 2009 09:02:51 +0000 (09:02 +0000)
.gitattributes
application/hub/class_ApplicationHelper.php
application/hub/config.php
application/hub/main/database/.htaccess [new file with mode: 0644]
application/hub/main/database/wrapper/.htaccess [new file with mode: 0644]
application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php [new file with mode: 0644]
application/hub/main/nodes/class_
application/hub/main/nodes/class_BaseHubNode.php
db/node_data/.htaccess [new file with mode: 0644]

index 70fd9a236d7d2174b454387ba430beb82ad50eb4..b58ad545d4dc13d3beda2487bdb67a279765f350 100644 (file)
@@ -14,6 +14,9 @@ application/hub/interfaces/nodes/.htaccess -text
 application/hub/interfaces/nodes/class_NodeHelper.php -text
 application/hub/loader.php -text
 application/hub/main/.htaccess -text
+application/hub/main/database/.htaccess -text
+application/hub/main/database/wrapper/.htaccess -text
+application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php -text
 application/hub/main/nodes/.htaccess -text
 application/hub/main/nodes/boot/.htaccess -text
 application/hub/main/nodes/class_ -text
@@ -25,6 +28,7 @@ application/hub/main/nodes/regular/class_HubRegularNode.php -text
 application/hub/starter.php -text
 /clear-cache.sh -text
 db/.htaccess -text
+db/node_data/.htaccess -text
 docs/COPYING -text
 docs/COPYING.documents -text
 docs/COPYING.software -text
index 6f29c8d9219ef148e2d048b1106fcc50464313ef..1c5cb9decee07740fc0790e7d70bdfdba36824a0 100644 (file)
@@ -172,8 +172,16 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica
                // And try to instance it
                $nodeInstance = ObjectFactory::createObjectByName($className);
 
+               // --------------------- Hub-id aquirement phase ---------------------
+               // Aquire a hub-id. This step generates on first launch anew one and
+               // on any later launches it will restore the hub-id from the database.
+               // A passed 'nickname=xxx' argument will be used to add some
+               // 'personality' to the hub.
+               $nodeInstance->aquireHubId($requestInstance);
+
                // --------------------- Bootstrapping phase ---------------------
-               // Try to bootstrap the node and pass the request instance to it
+               // Try to bootstrap the node and pass the request instance to it for
+               // extra arguments which mostly override config entries
                $nodeInstance->doBootstrapping($requestInstance);
        }
 
index 490f38f285e0099cb5cdbe252ad1a96f064185db..c589259df041eeef64220b2ccb6e4e9a8b6fa3bf 100644 (file)
@@ -40,7 +40,10 @@ $cfg->setConfigEntry('tpl_engine', "ConsoleOutput");
 $cfg->setConfigEntry('output_class', "DebugConsoleOutput");
 
 // CFG: DEBUG-CLASS
-$cfg->setConfigEntry('debug_class', "DebugErrorLogOutput");
+$cfg->setConfigEntry('debug_class', "DebugConsoleOutput");
+
+// CFG: NODE-INFO-DB-WRAPPER-CLASS
+$cfg->setConfigEntry('node_info_db_wrapper_class', "NodeInformationDatabaseWrapper");
 
 // CFG: WEB-CONTENT-TYPE
 $cfg->setConfigEntry('web_content_type', "");
diff --git a/application/hub/main/database/.htaccess b/application/hub/main/database/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/application/hub/main/database/wrapper/.htaccess b/application/hub/main/database/wrapper/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php b/application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php
new file mode 100644 (file)
index 0000000..0cab92f
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+/**
+ * A database wrapper for node informations
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * 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 NodeInformationDatabaseWrapper extends BaseDatabaseWrapper {
+       // Constants for database table names
+       const DB_TABLE_NODE_INFORMATION = 'node_data';
+
+       // Constants for database column names
+       const DB_COLUMN_NODE_NR   = 'node_nr';
+       const DB_COLUMN_NODE_ID   = 'node_id';
+       const DB_COLUMN_NODE_TYPE = 'node_type';
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct() {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this database wrapper by a provided user class
+        *
+        * @return      $wrapperInstance        An instance of the created wrapper class
+        */
+       public final static function createNodeInformationDatabaseWrapper () {
+               // Get a new instance
+               $wrapperInstance = new NodeInformationDatabaseWrapper();
+
+               // Set (primary!) table name
+               $wrapperInstance->setTableName(self::DB_TABLE_NODE_INFORMATION);
+
+               // Return the instance
+               return $wrapperInstance;
+       }
+
+       /**
+        * Getter for index key
+        *
+        * @return      $indexKey       Index key
+        */
+       public final function getIndexKey () {
+               return $this->getDatabaseInstance()->getIndexKey();
+       }
+
+       /**
+        * 'Registers' a new node id along with data provided in the node instance.
+        * This may sound confusing but avoids double code very nicely...
+        *
+        * @param       $nodeInstance   A node instance
+        * @return      void
+        */
+       public function registerNodeId (BaseHubNode $nodeInstance) {
+               // Get a dataset instance
+               $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
+
+               // Set the primary key
+               $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
+
+               // Add registration elements to the dataset
+               $nodeInstance->addElementsToDataSet($dataSetInstance);
+
+               // "Insert" this request instance completely into the database
+               $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
+       }
+}
+
+// [EOF]
+?>
index 0951d0188a22b342679948173cddc89a0e9044ab..90a9484502f69ae5150ed5b9f2c89a808d686f2e 100644 (file)
@@ -44,6 +44,30 @@ class Hub???Node extends BaseHubNode implements NodeHelper {
                // Return the instance
                return $nodeInstance;
        }
+
+       /**
+        * Method to "bootstrap" the node. This step does also apply provided
+        * command-line arguments stored in the request instance. The regular node
+        * should communicate with the bootstrap-nodes at this point.
+        *
+        * @param       $requestInstance        An instance of a Requestable class
+        * @return      void
+        */
+       public function doBootstrapping (Requestable $requestInstance) {
+               $this->partialStub();
+       }
+
+       /**
+        * Method to aquire a hub-id. On first run this generates a new one
+        * based on many pseudo-random data. On any later run, unless the id
+        * got not removed from database, it will be restored from the database.
+        *
+        * @param       $requestInstance        An instance of a Requestable class
+        * @return      void
+        */
+       public function aquireHubId (Requestable $requestInstance) {
+               $this->partialStub();
+       }
 }
 
 // [EOF]
index 8fb34e6b6dfc28660ecb18e16b6e4f59e49ac3b0..c82ca908a4d1a18686ea3f09f0b9fbf15ad57776 100644 (file)
  * 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 BaseHubNode extends BaseFrameworkSystem {
+class BaseHubNode extends BaseFrameworkSystem implements Updateable {
+       /**
+        * Node id
+        */
+       private $nodeId = "";
+
        /**
         * Protected constructor
         *
@@ -36,6 +41,125 @@ class BaseHubNode extends BaseFrameworkSystem {
                $this->removeNumberFormaters();
                $this->removeSystemArray();
        }
+
+       /**
+        * Setter for node id
+        *
+        * @param       $nodeId         Our new node id
+        * @return      void
+        */
+       private final function setNodeId ($nodeId) {
+               $this->nodeId = (string) $nodeId;
+       }
+
+       /**
+        * Getter for node id
+        *
+        * @return      $nodeId         Our new node id
+        */
+       private final function getNodeId () {
+               return $this->nodeId;
+       }
+
+       /**
+        * Method to aquire a hub-id. On first run this generates a new one
+        * based on many pseudo-random data. On any later run, unless the id
+        * got not removed from database, it will be restored from the database.
+        *
+        * @param       $requestInstance        An instance of a Requestable class
+        * @return      void
+        */
+       public function aquireHubId (Requestable $requestInstance) {
+               // Get a wrapper instance
+               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class');
+
+               // Now get a search criteria instance
+               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+               // Search for the node number zero which is hard-coded the default
+               $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
+               $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->__toString());
+               $searchInstance->setLimit(1);
+
+               // Get a result back
+               $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
+
+               // Is it valid?
+               if ($resultInstance->next()) {
+                       // Save the result instance in this class
+                       $this->setResultInstance($resultInstance);
+
+                       // Get the node id from result and set it
+                       $this->setNodeId($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID));
+               } else {
+                       // Get an RNG instance (Random Number Generator)
+                       $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
+
+                       // Generate a pseudo-random string
+                       $randomString = $rngInstance->randomString(255);
+
+                       // Get a crypto instance
+                       $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
+
+                       // Hash and encrypt the string so we become a "node id" aka Hub-Id
+                       $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
+
+                       // Register the node id with our wrapper
+                       $wrapperInstance->registerNodeId($this);
+               }
+       }
+
+       /**
+        * Adds registration elements to a given dataset instance
+        *
+        * @param       $criteriaInstance       An instance of a storeable criteria
+        * @return      void
+        */
+       public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
+               // Add node number and type
+               $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
+               $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->__toString());
+
+               // Add the node id
+               $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, $this->getNodeId());
+       }
+
+       /**
+        * Updates a given field with new value
+        *
+        * @param       $fieldName              Field to update
+        * @param       $fieldValue             New value to store
+        * @return      void
+        * @throws      DatabaseUpdateSupportException  If this class does not support database updates
+        * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
+        */
+       public function updateDatabaseField ($fieldName, $fieldValue) {
+               // Unfinished
+               $this->partialStub("Unfinished!");
+               return;
+
+               // Get a critieria instance
+               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+               // Add search criteria
+               $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
+               $searchInstance->setLimit(1);
+
+               // Now get another criteria
+               $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
+
+               // Add criteria entry which we shall update
+               $updateInstance->addCriteria($fieldName, $fieldValue);
+
+               // Add the search criteria for searching for the right entry
+               $updateInstance->setSearchInstance($searchInstance);
+
+               // Set wrapper class name
+               $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
+
+               // Remember the update in database result
+               $this->getResultInstance()->add2UpdateQueue($updateInstance);
+       }
 }
 
 // [EOF]
diff --git a/db/node_data/.htaccess b/db/node_data/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all