]> git.mxchange.org Git - hub.git/blob - application/hub/main/nodes/class_BaseHubNode.php
Updated documentation, typo fixed:
[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, $requestInstance->getRequestElement('mode'));
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
95                         // Output message
96                         $this->getDebugInstance()->output('Re-using found node-id: ' . $this->getNodeId() . '');
97                 } else {
98                         // Get an RNG instance (Random Number Generator)
99                         $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
100
101                         // Generate a pseudo-random string
102                         $randomString = $rngInstance->randomString(255) . ':' . $requestInstance->getRequestElement('mode');
103
104                         // Get a crypto instance
105                         $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
106
107                         // Hash and encrypt the string so we become a "node id" aka Hub-Id
108                         $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
109
110                         // Register the node id with our wrapper
111                         $wrapperInstance->registerNodeId($this, $requestInstance);
112
113                         // Output message
114                         $this->getDebugInstance()->output('Created new node-id: ' . $this->getNodeId() . '');
115                 }
116         }
117
118         /**
119          * Adds registration elements to a given dataset instance
120          *
121          * @param       $criteriaInstance       An instance of a storeable criteria
122          * @param       $requestInstance        An instance of a Requestable class
123          * @return      void
124          */
125         public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance) {
126                 // Add node number and type
127                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
128                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $requestInstance->getRequestElement('mode'));
129
130                 // Add the node id
131                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, $this->getNodeId());
132         }
133
134         /**
135          * Updates a given field with new value
136          *
137          * @param       $fieldName              Field to update
138          * @param       $fieldValue             New value to store
139          * @return      void
140          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
141          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
142          */
143         public function updateDatabaseField ($fieldName, $fieldValue) {
144                 // Unfinished
145                 $this->partialStub("Unfinished!");
146                 return;
147
148                 // Get a critieria instance
149                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
150
151                 // Add search criteria
152                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
153                 $searchInstance->setLimit(1);
154
155                 // Now get another criteria
156                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
157
158                 // Add criteria entry which we shall update
159                 $updateInstance->addCriteria($fieldName, $fieldValue);
160
161                 // Add the search criteria for searching for the right entry
162                 $updateInstance->setSearchInstance($searchInstance);
163
164                 // Set wrapper class name
165                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
166
167                 // Remember the update in database result
168                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
169         }
170 }
171
172 // [EOF]
173 ?>