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, $requestInstance->getRequestElement('mode'));
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));
96 $this->getDebugInstance()->output('Re-using found node-id: ' . $this->getNodeId() . '');
98 // Get an RNG instance (Random Number Generator)
99 $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
101 // Generate a pseudo-random string
102 $randomString = $rngInstance->randomString(255) . ':' . $requestInstance->getRequestElement('mode');
104 // Get a crypto instance
105 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
107 // Hash and encrypt the string so we become a "node id" aka Hub-Id
108 $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
110 // Register the node id with our wrapper
111 $wrapperInstance->registerNodeId($this, $requestInstance);
114 $this->getDebugInstance()->output('Created new node-id: ' . $this->getNodeId() . '');
119 * Adds registration elements to a given dataset instance
121 * @param $criteriaInstance An instance of a storeable criteria
122 * @param $requestInstance An instance of a Requestable class
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'));
131 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, $this->getNodeId());
135 * Updates a given field with new value
137 * @param $fieldName Field to update
138 * @param $fieldValue New value to store
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
143 public function updateDatabaseField ($fieldName, $fieldValue) {
145 $this->partialStub("Unfinished!");
148 // Get a critieria instance
149 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
151 // Add search criteria
152 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
153 $searchInstance->setLimit(1);
155 // Now get another criteria
156 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
158 // Add criteria entry which we shall update
159 $updateInstance->addCriteria($fieldName, $fieldValue);
161 // Add the search criteria for searching for the right entry
162 $updateInstance->setSearchInstance($searchInstance);
164 // Set wrapper class name
165 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
167 // Remember the update in database result
168 $this->getResultInstance()->add2UpdateQueue($updateInstance);