]> git.mxchange.org Git - hub.git/blob - application/hub/main/nodes/class_BaseHubNode.php
Hub-Id creation basicly finished
[hub.git] / application / hub / main / nodes / class_BaseHubNode.php
1 <?php
2 /**
3  * A general hub node class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseHubNode extends BaseFrameworkSystem implements Updateable {
25         /**
26          * Node id
27          */
28         private $nodeId = "";
29
30         /**
31          * Protected constructor
32          *
33          * @param       $className      Name of the class
34          * @return      void
35          */
36         protected function __construct ($className) {
37                 // Call parent constructor
38                 parent::__construct($className);
39
40                 // Clean up a little
41                 $this->removeNumberFormaters();
42                 $this->removeSystemArray();
43         }
44
45         /**
46          * Setter for node id
47          *
48          * @param       $nodeId         Our new node id
49          * @return      void
50          */
51         private final function setNodeId ($nodeId) {
52                 $this->nodeId = (string) $nodeId;
53         }
54
55         /**
56          * Getter for node id
57          *
58          * @return      $nodeId         Our new node id
59          */
60         private final function getNodeId () {
61                 return $this->nodeId;
62         }
63
64         /**
65          * Method to aquire a hub-id. On first run this generates a new one
66          * based on many pseudo-random data. On any later run, unless the id
67          * got not removed from database, it will be restored from the database.
68          *
69          * @param       $requestInstance        An instance of a Requestable class
70          * @return      void
71          */
72         public function aquireHubId (Requestable $requestInstance) {
73                 // Get a wrapper instance
74                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class');
75
76                 // Now get a search criteria instance
77                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
78
79                 // Search for the node number zero which is hard-coded the default
80                 $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
81                 $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->__toString());
82                 $searchInstance->setLimit(1);
83
84                 // Get a result back
85                 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
86
87                 // Is it valid?
88                 if ($resultInstance->next()) {
89                         // Save the result instance in this class
90                         $this->setResultInstance($resultInstance);
91
92                         // Get the node id from result and set it
93                         $this->setNodeId($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID));
94                 } else {
95                         // Get an RNG instance (Random Number Generator)
96                         $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
97
98                         // Generate a pseudo-random string
99                         $randomString = $rngInstance->randomString(255);
100
101                         // Get a crypto instance
102                         $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
103
104                         // Hash and encrypt the string so we become a "node id" aka Hub-Id
105                         $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
106
107                         // Register the node id with our wrapper
108                         $wrapperInstance->registerNodeId($this);
109                 }
110         }
111
112         /**
113          * Adds registration elements to a given dataset instance
114          *
115          * @param       $criteriaInstance       An instance of a storeable criteria
116          * @return      void
117          */
118         public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
119                 // Add node number and type
120                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
121                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->__toString());
122
123                 // Add the node id
124                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, $this->getNodeId());
125         }
126
127         /**
128          * Updates a given field with new value
129          *
130          * @param       $fieldName              Field to update
131          * @param       $fieldValue             New value to store
132          * @return      void
133          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
134          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
135          */
136         public function updateDatabaseField ($fieldName, $fieldValue) {
137                 // Unfinished
138                 $this->partialStub("Unfinished!");
139                 return;
140
141                 // Get a critieria instance
142                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
143
144                 // Add search criteria
145                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
146                 $searchInstance->setLimit(1);
147
148                 // Now get another criteria
149                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
150
151                 // Add criteria entry which we shall update
152                 $updateInstance->addCriteria($fieldName, $fieldValue);
153
154                 // Add the search criteria for searching for the right entry
155                 $updateInstance->setSearchInstance($searchInstance);
156
157                 // Set wrapper class name
158                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
159
160                 // Remember the update in database result
161                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
162         }
163 }
164
165 // [EOF]
166 ?>