]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/nodes/class_BaseHubNode.php
Hub-Id creation basicly finished
[hub.git] / application / hub / main / nodes / class_BaseHubNode.php
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]