]> git.mxchange.org Git - hub.git/blob - application/hub/main/nodes/class_BaseHubNode.php
c0dfac164a2723ba3b383afbda0a37c4627fd9a1
[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 Hub 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          * IP/port number of bootstrapping node
32          */
33         private $bootIpPort = '';
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48         }
49
50         /**
51          * Setter for node id
52          *
53          * @param       $nodeId         Our new node id
54          * @return      void
55          */
56         private final function setNodeId ($nodeId) {
57                 $this->nodeId = (string) $nodeId;
58         }
59
60         /**
61          * Getter for node id
62          *
63          * @return      $nodeId         Our new node id
64          */
65         private final function getNodeId () {
66                 return $this->nodeId;
67         }
68
69         /**
70          * Checks wether the given IP address matches one of the bootstrapping nodes
71          *
72          * @param       $remoteAddr             IP address to checkout against our bootstrapping list
73          * @return      $isFound                Wether the IP is found
74          */
75         protected function ifAddressMatchesBootstrappingNodes ($remoteAddr) {
76                 // By default nothing is found
77                 $isFound = false;
78
79                 // Run through all configured IPs
80                 foreach (explode(',', $this->getConfigInstance()->readConfig('hub_bootstrap_nodes')) as $ipPort) {
81                         // Does it match?
82                         if (substr($ipPort, 0, strlen($remoteAddr)) == $remoteAddr) {
83                                 // Found it!
84                                 $isFound = true;
85
86                                 // Remember the port number
87                                 $this->bootIpPort = $ipPort;
88
89                                 // Output message
90                                 $this->getDebugInstance()->output(__FUNCTION__.'['.__LINE__.']: IP matches remote address ' . $ipPort . '.');
91
92                                 // Stop further searching
93                                 break;
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!
97                                 $isFound = true;
98
99                                 // Remember the port number
100                                 $this->bootIpPort = $ipPort;
101
102                                 // Output message
103                                 $this->getDebugInstance()->output(__FUNCTION__.'['.__LINE__.']: IP matches listen address ' . $ipPort . '.');
104
105                                 // Stop further searching
106                                 break;
107                         }
108                 } // END - foreach
109
110                 // Return the result
111                 return $isFound;
112         }
113
114         /**
115          * Outputs the console teaser. This should only be executed on startup or
116          * full restarts. This method generates some space around the teaser.
117          *
118          * @return      void
119          */
120         private function outputConsoleTeaser () {
121                 // Get the app instance (for shortening our code)
122                 $app = $this->getApplicationInstance();
123
124                 // Output all lines
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(' ');
133         }
134
135         /**
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.
138          *
139          * @todo        This method is maybe not yet finished.
140          * @return      void
141          */
142         protected function doGenericBootstrapping () {
143                 // Finally output our teaser. This should be the last line!
144                 $this->outputConsoleTeaser();
145         }
146
147         /**
148          * Generic method to acquire a hub-id. On first run this generates a new one
149          * based on many pseudo-random data. On any later run, unless the id
150          * got not removed from database, it will be restored from the database.
151          *
152          * @param       $requestInstance        An instance of a Requestable class
153          * @return      void
154          */
155         public function acquireHubId (Requestable $requestInstance) {
156                 // Get a wrapper instance
157                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class');
158
159                 // Now get a search criteria instance
160                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
161
162                 // Search for the node number zero which is hard-coded the default
163                 $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
164                 $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $requestInstance->getRequestElement('mode'));
165                 $searchInstance->setLimit(1);
166
167                 // Get a result back
168                 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
169
170                 // Is it valid?
171                 if ($resultInstance->next()) {
172                         // Save the result instance in this class
173                         $this->setResultInstance($resultInstance);
174
175                         // Get the node id from result and set it
176                         $this->setNodeId($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID));
177
178                         // Output message
179                         $this->getDebugInstance()->output('Re-using found node-id: ' . $this->getNodeId() . '');
180                 } else {
181                         // Get an RNG instance (Random Number Generator)
182                         $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
183
184                         // Generate a pseudo-random string
185                         $randomString = $rngInstance->randomString(255) . ':' . $requestInstance->getRequestElement('mode');
186
187                         // Get a crypto instance
188                         $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
189
190                         // Hash and encrypt the string so we become a "node id" aka Hub-Id
191                         $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
192
193                         // Register the node id with our wrapper
194                         $wrapperInstance->registerNodeId($this, $requestInstance);
195
196                         // Output message
197                         $this->getDebugInstance()->output('Created new node-id: ' . $this->getNodeId() . '');
198                 }
199         }
200
201         /**
202          * Adds hub data elements to a given dataset instance
203          *
204          * @param       $criteriaInstance       An instance of a storeable criteria
205          * @param       $requestInstance        An instance of a Requestable class
206          * @return      void
207          */
208         public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance) {
209                 // Add node number and type
210                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
211                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $requestInstance->getRequestElement('mode'));
212
213                 // Add the node id
214                 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, $this->getNodeId());
215         }
216
217         /**
218          * Updates a given field with new value
219          *
220          * @param       $fieldName              Field to update
221          * @param       $fieldValue             New value to store
222          * @return      void
223          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
224          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
225          */
226         public function updateDatabaseField ($fieldName, $fieldValue) {
227                 // Unfinished
228                 $this->partialStub("Unfinished!");
229                 return;
230
231                 // Get a critieria instance
232                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
233
234                 // Add search criteria
235                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
236                 $searchInstance->setLimit(1);
237
238                 // Now get another criteria
239                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
240
241                 // Add criteria entry which we shall update
242                 $updateInstance->addCriteria($fieldName, $fieldValue);
243
244                 // Add the search criteria for searching for the right entry
245                 $updateInstance->setSearchInstance($searchInstance);
246
247                 // Set wrapper class name
248                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
249
250                 // Remember the update in database result
251                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
252         }
253 }
254
255 // [EOF]
256 ?>