3 * A general hub node class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub 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 * IP/port number of bootstrapping node
33 private $bootIpPort = '';
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
46 $this->removeNumberFormaters();
47 $this->removeSystemArray();
53 * @param $nodeId Our new node id
56 private final function setNodeId ($nodeId) {
57 $this->nodeId = (string) $nodeId;
63 * @return $nodeId Our new node id
65 private final function getNodeId () {
70 * Checks wether the given IP address matches one of the bootstrapping nodes
72 * @param $remoteAddr IP address to checkout against our bootstrapping list
73 * @return $isFound Wether the IP is found
75 private function ifAddressMatchesBootstrappingNodes ($remoteAddr) {
76 // By default nothing is found
79 // Run through all configured IPs
80 foreach (explode(',', $this->getConfigInstance()->readConfig('hub_bootstrap_nodes')) as $ipPort) {
82 if (substr($ipPort, 0, strlen($remoteAddr)) == $remoteAddr) {
86 // Remember the port number
87 $this->bootIpPort = $ipPort;
90 $this->getDebugInstance()->output(__FUNCTION__.': IP matches remote address ' . $ipPort . '.');
92 // Stop further searching
94 } elseif (substr($ipPort, 0, strlen($this->getConfigInstance()->readConfig('node_listen_addr'))) == $this->getConfigInstance()->readConfig('node_listen_addr')) {
95 // IP matches listen address. At this point we really don't care
96 // if we can also listen on that address!
99 // Remember the port number
100 $this->bootIpPort = $ipPort;
103 $this->getDebugInstance()->output(__FUNCTION__.': IP matches listen address ' . $ipPort . '.');
105 // Stop further searching
115 * Outputs the console teaser. This should only be executed on startup or
116 * full restarts. This method generates some space around the teaser.
120 private function outputConsoleTeaser () {
121 // Get the app instance (for shortening our code)
122 $app = $this->getApplicationInstance();
125 $this->getDebugInstance()->output(' ');
126 $this->getDebugInstance()->output($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' mode active');
127 $this->getDebugInstance()->output('Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team');
128 $this->getDebugInstance()->output(' ');
129 $this->getDebugInstance()->output('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
130 $this->getDebugInstance()->output('This is free software, and you are welcome to redistribute it under certain');
131 $this->getDebugInstance()->output('conditions; see docs/COPYING for details.');
132 $this->getDebugInstance()->output(' ');
136 * Do generic things for bootup phase. This can be e.g. checking if the
137 * right node mode is selected for this hub's IP number.
139 * @todo This method is maybe not yet finished.
142 protected function doGenericBootstrapping () {
143 // Now check if the IP address matches one of the bootstrap nodes
144 if ($this->ifAddressMatchesBootstrappingNodes($_SERVER['REMOTE_ADDR'])) {
146 $ourPort = $this->getConfigInstance()->readConfig('node_listen_port');
148 // Is the port the same?
149 if (substr($this->bootIpPort, -strlen($ourPort), strlen($ourPort)) == $ourPort) {
151 $this->getDebugInstance()->output(__FUNCTION__.': IP/port matches bootstrapping node ' . $this->bootIpPort . '.');
153 // Now, does the mode match (should be 'boot'!)
154 if ($this->getRequestInstance()->getRequestElement('mode') == 'boot') {
155 // Output debug message
156 $this->getDebugInstance()->output(__FUNCTION__.': Our node is a valid boot-strapping node.');
159 $this->getDebugInstance()->output(__FUNCTION__.': WARNING! Mismatching mode ' . $this->getRequestInstance()->getRequestElement('mode') . '!=boot detected.');
164 // Finally output our teaser. This should be the last line!
165 $this->outputConsoleTeaser();
169 * Generic method to aquire a hub-id. On first run this generates a new one
170 * based on many pseudo-random data. On any later run, unless the id
171 * got not removed from database, it will be restored from the database.
173 * @param $requestInstance An instance of a Requestable class
176 public function aquireHubId (Requestable $requestInstance) {
177 // Get a wrapper instance
178 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class');
180 // Now get a search criteria instance
181 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
183 // Search for the node number zero which is hard-coded the default
184 $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
185 $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $requestInstance->getRequestElement('mode'));
186 $searchInstance->setLimit(1);
189 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
192 if ($resultInstance->next()) {
193 // Save the result instance in this class
194 $this->setResultInstance($resultInstance);
196 // Get the node id from result and set it
197 $this->setNodeId($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID));
200 $this->getDebugInstance()->output('Re-using found node-id: ' . $this->getNodeId() . '');
202 // Get an RNG instance (Random Number Generator)
203 $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
205 // Generate a pseudo-random string
206 $randomString = $rngInstance->randomString(255) . ':' . $requestInstance->getRequestElement('mode');
208 // Get a crypto instance
209 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
211 // Hash and encrypt the string so we become a "node id" aka Hub-Id
212 $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
214 // Register the node id with our wrapper
215 $wrapperInstance->registerNodeId($this, $requestInstance);
218 $this->getDebugInstance()->output('Created new node-id: ' . $this->getNodeId() . '');
223 * Adds hub data elements to a given dataset instance
225 * @param $criteriaInstance An instance of a storeable criteria
226 * @param $requestInstance An instance of a Requestable class
229 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance) {
230 // Add node number and type
231 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
232 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $requestInstance->getRequestElement('mode'));
235 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, $this->getNodeId());
239 * Updates a given field with new value
241 * @param $fieldName Field to update
242 * @param $fieldValue New value to store
244 * @throws DatabaseUpdateSupportException If this class does not support database updates
245 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
247 public function updateDatabaseField ($fieldName, $fieldValue) {
249 $this->partialStub("Unfinished!");
252 // Get a critieria instance
253 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
255 // Add search criteria
256 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
257 $searchInstance->setLimit(1);
259 // Now get another criteria
260 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
262 // Add criteria entry which we shall update
263 $updateInstance->addCriteria($fieldName, $fieldValue);
265 // Add the search criteria for searching for the right entry
266 $updateInstance->setSearchInstance($searchInstance);
268 // Set wrapper class name
269 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
271 // Remember the update in database result
272 $this->getResultInstance()->add2UpdateQueue($updateInstance);