]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/wrapper/node/class_NodeInformationDatabaseWrapper.php
Introduced enableAcceptDhtBootstrap() + updated core.
[hub.git] / application / hub / main / wrapper / node / class_NodeInformationDatabaseWrapper.php
diff --git a/application/hub/main/wrapper/node/class_NodeInformationDatabaseWrapper.php b/application/hub/main/wrapper/node/class_NodeInformationDatabaseWrapper.php
new file mode 100644 (file)
index 0000000..820102f
--- /dev/null
@@ -0,0 +1,184 @@
+<?php
+/**
+ * A database wrapper for node informations
+ *
+ * @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.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
+ * 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 implements NodeInformationWrapper, Registerable {
+       // 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_SESSION_ID       = 'session_id';
+       const DB_COLUMN_PRIVATE_KEY      = 'private_key';
+       const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
+       const DB_COLUMN_NODE_MODE        = 'node_mode';
+
+       /**
+        * 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 static final 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;
+       }
+
+       /**
+        * Checks whether there is an entry for given node instance
+        *
+        * @param       $nodeInstance   An instance of a NodeHelper class
+        * @return      $isFound                Whether a node id has been found for this node
+        */
+       public function ifNodeDataIsFound (NodeHelper $nodeInstance) {
+               // Is there cache?
+               if (!isset($GLOBALS[__METHOD__])) {
+                       // Now get a search criteria instance
+                       $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+                       // Search for the node number one which is hard-coded the default
+                       $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR  , 1);
+                       $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_MODE, $nodeInstance->getRequestInstance()->getRequestElement('mode'));
+                       $searchInstance->setLimit(1);
+
+                       // Get a result back
+                       $resultInstance = $this->doSelectByCriteria($searchInstance);
+
+                       // Set result instance in node instance
+                       $nodeInstance->setResultInstance($resultInstance);
+
+                       // Is it valid?
+                       $GLOBALS[__METHOD__] = $resultInstance->next();
+               } // END - if
+
+               // Return it
+               return $GLOBALS[__METHOD__];
+       }
+
+       /**
+        * '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
+        * @param       $requestInstance        An instance of a Requestable class
+        * @return      void
+        */
+       public function registerNodeId (BaseHubNode $nodeInstance, Requestable $requestInstance) {
+               // 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, $requestInstance);
+
+               // "Insert" this dataset instance completely into the database
+               $this->queryInsertDataSet($dataSetInstance);
+       }
+
+       /**
+        * 'Registers' a new session id along with data provided in the node instance.
+        * This may sound confusing but avoids double code very nicely...
+        *
+        * @param       $nodeInstance           An instance of a BaseHubNode class
+        * @param       $requestInstance        An instance of a Requestable class
+        * @param       $searchInstance         An instance of a LocalSearchCriteria class
+        * @return      void
+        */
+       public function registerSessionId (BaseHubNode $nodeInstance, Requestable $requestInstance, LocalSearchCriteria $searchInstance) {
+               // Get a dataset instance
+               $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
+
+               // Set search instance
+               $dataSetInstance->setSearchInstance($searchInstance);
+
+               // Set the primary key
+               $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
+
+               // Add registration elements to the dataset
+               $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
+
+               // Update database record
+               $this->queryUpdateDataSet($dataSetInstance);
+       }
+
+       /**
+        * 'Registers' a private key along with data provided in the node instance.
+        * This may sound confusing but avoids double code very nicely...
+        *
+        * @param       $nodeInstance           An instance of a BaseHubNode class
+        * @param       $requestInstance        An instance of a Requestable class
+        * @param       $searchInstance         An instance of a LocalSearchCriteria class
+        * @return      void
+        */
+       public function registerPrivateKey (BaseHubNode $nodeInstance, Requestable $requestInstance, LocalSearchCriteria $searchInstance) {
+               // 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);
+
+               // Set search instance
+               $dataSetInstance->setSearchInstance($searchInstance);
+
+               // Add registration elements to the dataset
+               $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
+
+               // Update database record
+               $this->queryUpdateDataSet($dataSetInstance);
+       }
+
+       /**
+        * Removes non-public data from given array.
+        *
+        * @param       $data   An array with possible non-public data that needs to be removed.
+        * @return      $data   A cleaned up array with only public data.
+        */
+       public function removeNonPublicDataFromArray(array $data) {
+               // Currently call only inner method
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE-WRAPPER[' . $this->__toString() . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
+               $data = parent::removeNonPublicDataFromArray($data);
+
+               // Return cleaned data
+               return $data;
+       }
+}
+
+// [EOF]
+?>