3 * A general hub node class
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class BaseHubNode extends BaseFrameworkSystem implements Updateable {
31 * Protected constructor
33 * @param $className Name of the class
36 protected function __construct ($className) {
37 // Call parent constructor
38 parent::__construct($className);
41 $this->removeNumberFormaters();
42 $this->removeSystemArray();
48 * @param $nodeId Our new node id
51 private final function setNodeId ($nodeId) {
52 $this->nodeId = (string) $nodeId;
58 * @return $nodeId Our new node id
60 private final function getNodeId () {
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.
69 * @param $requestInstance An instance of a Requestable class
72 public function aquireHubId (Requestable $requestInstance) {
73 // Get a wrapper instance
74 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class');
76 // Now get a search criteria instance
77 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
85 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
88 if ($resultInstance->next()) {
89 // Save the result instance in this class
90 $this->setResultInstance($resultInstance);
92 // Get the node id from result and set it
93 $this->setNodeId($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID));
95 // Get an RNG instance (Random Number Generator)
96 $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
98 // Generate a pseudo-random string
99 $randomString = $rngInstance->randomString(255);
101 // Get a crypto instance
102 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
104 // Hash and encrypt the string so we become a "node id" aka Hub-Id
105 $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
107 // Register the node id with our wrapper
108 $wrapperInstance->registerNodeId($this);
113 * Adds registration elements to a given dataset instance
115 * @param $criteriaInstance An instance of a storeable criteria
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());
124 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, $this->getNodeId());
128 * Updates a given field with new value
130 * @param $fieldName Field to update
131 * @param $fieldValue New value to store
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
136 public function updateDatabaseField ($fieldName, $fieldValue) {
138 $this->partialStub("Unfinished!");
141 // Get a critieria instance
142 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
144 // Add search criteria
145 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
146 $searchInstance->setLimit(1);
148 // Now get another criteria
149 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
151 // Add criteria entry which we shall update
152 $updateInstance->addCriteria($fieldName, $fieldValue);
154 // Add the search criteria for searching for the right entry
155 $updateInstance->setSearchInstance($searchInstance);
157 // Set wrapper class name
158 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
160 // Remember the update in database result
161 $this->getResultInstance()->add2UpdateQueue($updateInstance);