3 * A database wrapper for distributed hash tables
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implements NodeDhtWrapper, Registerable {
26 * "Cached" results for dabase for looking for unpublished entries
28 private $unpublishedEntriesInstance = NULL;
30 // Constants for database table names
31 const DB_TABLE_NODE_DHT = 'node_dht';
33 // Constants for database column names
34 const DB_COLUMN_NODE_ID = 'node_id';
35 const DB_COLUMN_SESSION_ID = 'session_id';
36 const DB_COLUMN_EXTERNAL_IP = 'external_ip';
37 const DB_COLUMN_LISTEN_PORT = 'listen_port';
38 const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
39 const DB_COLUMN_NODE_MODE = 'node_mode';
40 const DB_COLUMN_ACCEPTED_OBJECTS = 'accepted_object_types';
41 const DB_COLUMN_NODE_LIST = 'node_list';
42 const DB_COLUMN_PUBLICATION_STATUS = 'publication_status';
43 const DB_COLUMN_ANSWER_STATUS = 'answer_status';
44 const DB_COLUMN_ACCEPT_BOOTSTRAP = 'accept_bootstrap';
46 // Publication status'
47 const PUBLICATION_STATUS_PENDING = 'PENDING';
50 const EXCEPTION_NODE_ALREADY_REGISTERED = 0x800;
51 const EXCEPTION_NODE_NOT_REGISTERED = 0x801;
54 * Protected constructor
58 protected function __construct () {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
64 * Creates an instance of this database wrapper by a provided user class
66 * @return $wrapperInstance An instance of the created wrapper class
68 public static final function createNodeDistributedHashTableDatabaseWrapper () {
70 $wrapperInstance = new NodeDistributedHashTableDatabaseWrapper();
72 // Set (primary!) table name
73 $wrapperInstance->setTableName(self::DB_TABLE_NODE_DHT);
75 // Return the instance
76 return $wrapperInstance;
80 * Static getter for an array of all DHT database entries
82 * @return $elements All elements for the DHT dabase
84 public static final function getAllElements () {
85 // Create array and ...
87 self::DB_COLUMN_NODE_ID,
88 self::DB_COLUMN_SESSION_ID,
89 self::DB_COLUMN_EXTERNAL_IP,
90 self::DB_COLUMN_LISTEN_PORT,
91 self::DB_COLUMN_PRIVATE_KEY_HASH,
92 self::DB_COLUMN_NODE_MODE,
93 self::DB_COLUMN_ACCEPTED_OBJECTS,
94 self::DB_COLUMN_NODE_LIST
102 * Prepares a search instance for given node data
104 * @param $nodeData An array with valid node data
105 * @return $searchInstance An instance of a SearchCriteria class
107 private function prepareSearchInstance (array $nodeData) {
108 // Assert on array elements
109 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
112 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
114 // Search for node id and limit it to one entry
115 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]);
116 $searchInstance->setLimit(1);
119 return $searchInstance;
123 * Getter for result instance for unpublished entries
125 * @return $unpublishedEntriesInstance Result instance
127 public final function getUnpublishedEntriesInstance () {
128 return $this->unpublishedEntriesInstance;
132 * Prepares a "local" instance of a StoreableCriteria class with all node
133 * data for insert/update queries. This data set contains data from *this*
136 * @return $dataSetInstance An instance of a StoreableCriteria class
138 private function prepareLocalDataSetInstance () {
139 // Get node/request instances
140 $nodeInstance = NodeObjectFactory::createNodeInstance();
141 $requestInstance = ApplicationHelper::getSelfInstance()->getRequestInstance();
143 // Get a dataset instance
144 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
146 // Set the primary key
147 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
149 // Get ip:port combination and "explode" it
150 $ipPort = $nodeInstance->getAddressPortArray();
152 // Make sure both is valid
153 assert(($ipPort[0] !== 'invalid') && ($ipPort[1] !== 'invalid'));
155 // Get an array of all accepted object types
156 $objectList = $nodeInstance->getListFromAcceptedObjectTypes();
158 // Make sure this is an array
159 assert(is_array($objectList));
161 // Add public node data
162 $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_MODE , $requestInstance->getRequestElement('mode'));
163 $dataSetInstance->addCriteria(self::DB_COLUMN_EXTERNAL_IP , $ipPort[0]);
164 $dataSetInstance->addCriteria(self::DB_COLUMN_LISTEN_PORT , $ipPort[1]);
165 $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_ID , $nodeInstance->getNodeId());
166 $dataSetInstance->addCriteria(self::DB_COLUMN_SESSION_ID , $nodeInstance->getSessionId());
167 $dataSetInstance->addCriteria(self::DB_COLUMN_PRIVATE_KEY_HASH, $nodeInstance->getPrivateKeyHash());
168 $dataSetInstance->addCriteria(self::DB_COLUMN_ACCEPTED_OBJECTS, implode(BaseHubNode::OBJECT_LIST_SEPARATOR, $objectList));
169 $dataSetInstance->addCriteria(self::DB_COLUMN_ACCEPT_BOOTSTRAP, $this->translateBooleanToYesNo($nodeInstance->isAcceptingDhtBootstrap()));
172 return $dataSetInstance;
176 * Checks whether the local (*this*) node is registered in the DHT by
177 * checking if the external ip/port is found.
179 * @return $isRegistered Whether *this* node is registered in the DHT
181 public function isLocalNodeRegistered () {
182 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
184 // Get a search criteria instance
185 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
188 $nodeInstance = NodeObjectFactory::createNodeInstance();
190 // Get ip:port combination and "explode" it
191 $ipPort = $nodeInstance->getAddressPortArray();
194 * Make sure both is not 'invalid' which means that the resolver
197 assert(($ipPort[0] !== 'invalid') && ($ipPort[1] !== 'invalid'));
199 // Add ip:port/node id as criteria
200 $searchInstance->addCriteria(self::DB_COLUMN_EXTERNAL_IP, $ipPort[0]);
201 $searchInstance->addCriteria(self::DB_COLUMN_LISTEN_PORT, $ipPort[1]);
202 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID , $nodeInstance->getNodeId());
203 $searchInstance->setLimit(1);
205 // Query database and get a result instance back
206 $resultInstance = $this->doSelectByCriteria($searchInstance);
208 // Cache result of if there is an entry, valid() will tell us if an entry is there
209 $isRegistered = $resultInstance->valid();
211 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: isRegistered=' . intval($isRegistered) . ' - EXIT!');
214 return $isRegistered;
218 * Registeres the local (*this*) node with its data in the DHT.
222 public function registerLocalNode () {
223 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
225 // Assert to make sure this method is called with no record in DB (the actual backend of the DHT)
226 assert(!$this->isLocalNodeRegistered());
228 // Get prepared data set instance
229 $dataSetInstance = $this->prepareLocalDataSetInstance();
231 // "Insert" this dataset instance completely into the database
232 $this->queryInsertDataSet($dataSetInstance);
234 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
238 * Updates local (*this*) node's data in DHT, this is but not limited to the
239 * session id, ip number (and/or hostname) and port number.
243 public function updateLocalNode () {
244 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
246 // Assert to make sure this method is called with one record in DB (the actual backend of the DHT)
247 assert($this->isLocalNodeRegistered());
250 $nodeInstance = NodeObjectFactory::createNodeInstance();
252 // Get search criteria
253 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
255 // Search for node id and limit it to one entry
256 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeInstance->getNodeId());
257 $searchInstance->setLimit(1);
259 // Get a prepared dataset instance
260 $dataSetInstance = $this->prepareLocalDataSetInstance();
262 // Set search instance
263 $dataSetInstance->setSearchInstance($searchInstance);
265 // Update DHT database record
266 $this->queryUpdateDataSet($dataSetInstance);
268 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
272 * Finds a node locally by given session id
274 * @param $sessionId Session id to lookup
275 * @return $nodeData Node data array
277 public function findNodeLocalBySessionId ($sessionId) {
278 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: sessionId=' . $sessionId . ' - CALLED!');
280 // Get search criteria
281 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
283 // Search for session id and limit it to one entry
284 $searchInstance->addCriteria(self::DB_COLUMN_SESSION_ID, $sessionId);
285 $searchInstance->setLimit(1);
287 // Query database and get a result instance back
288 $resultInstance = $this->doSelectByCriteria($searchInstance);
290 // Return result instance
291 return $resultInstance;
295 * Registeres a node by given message data.
297 * @param $messageData An array of all message data
298 * @param $handlerInstance An instance of a Handleable class
301 public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance) {
302 // Get a data set instance
303 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
305 // Set primary key (session id)
306 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
308 // Add all array elements
309 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
311 // Remove 'node_list'
312 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
314 // Run the "INSERT" query
315 $this->queryInsertDataSet($dataSetInstance);
319 * Updates an existing entry in node list
321 * @param $messageData An array of all message data
322 * @param $handlerInstance An instance of a Handleable class
323 * @param $searchInstance An instance of LocalSearchCriteria class
326 public function updateNodeByMessageData (array $messageData, Handleable $handlerInstance, LocalSearchCriteria $searchInstance) {
327 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
329 // Get a data set instance
330 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
332 // Add search instance
333 $dataSetInstance->setSearchInstance($searchInstance);
335 // Set primary key (session id)
336 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
338 // Add all array elements
339 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
341 // Remove 'node_list'
342 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
344 // Run the "UPDATE" query
345 $this->queryUpdateDataSet($dataSetInstance);
347 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
351 * Determines whether the given node data is already inserted in the DHT
353 * @param $nodeData An array with valid node data
354 * @return $isRegistered Whether the given node data is already inserted
356 public function isNodeRegistered (array $nodeData) {
357 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
359 // Assert on array elements
360 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
362 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: node-id=' . $nodeData[self::DB_COLUMN_NODE_ID]);
364 // Get search criteria
365 $searchInstance = $this->prepareSearchInstance($nodeData);
367 // Query database and get a result instance back
368 $resultInstance = $this->doSelectByCriteria(
371 // Only look for these array elements ("keys")
373 self::DB_COLUMN_NODE_ID => TRUE,
374 self::DB_COLUMN_EXTERNAL_IP => TRUE,
375 self::DB_COLUMN_LISTEN_PORT => TRUE,
379 // Check if there is an entry
380 $isRegistered = $resultInstance->valid();
382 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: isRegistered=' . intval($isRegistered) . ' - EXIT!');
384 // Return registration status
385 return $isRegistered;
389 * Registers a node with given data in the DHT. If the node is already
390 * registered this method shall throw an exception.
392 * @param $nodeData An array with valid node data
394 * @throws NodeAlreadyRegisteredException If the node is already registered
396 public function registerNode (array $nodeData) {
397 // Assert on array elements
398 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
400 // Is the node registered?
401 if ($this->isNodeRegistered($nodeData)) {
402 // Throw an exception
403 throw new NodeAlreadyRegisteredException(array($this, $nodeData), self::EXCEPTION_NODE_ALREADY_REGISTERED);
406 // @TODO Unimplemented part
407 $this->partialStub('nodeData=' . print_r($nodeData, TRUE));
411 * Updates a node's entry in the DHT with given data. This will enrich or
412 * just update already exsiting data. If the node is not found this method
413 * shall throw an exception.
415 * @param $nodeData An array with valid node data
417 * @throws NodeDataMissingException If the node's data is missing
419 public function updateNode (array $nodeData) {
420 // Assert on array elements
421 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
424 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Updating DHT entry for node-id=' . $nodeData[self::DB_COLUMN_NODE_ID] . ' ...');
426 // Is the node registered?
427 if (!$this->isNodeRegistered($nodeData)) {
428 // No, then throw an exception
429 throw new NodeDataMissingException(array($this, $nodeData), self::EXCEPTION_NODE_NOT_REGISTERED);
432 // Get a search instance
433 $searchInstance = $this->prepareSearchInstance($nodeData);
435 // Get a data set instance
436 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
438 // Add search instance
439 $dataSetInstance->setSearchInstance($searchInstance);
441 // Set primary key (session id)
442 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
445 $nodeInstance = NodeObjectFactory::createNodeInstance();
448 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: nodeData=' . print_r($nodeData, TRUE));
450 // Add all array elements
451 $nodeInstance->addArrayToDataSet($dataSetInstance, $nodeData);
453 // Remove 'node_list'
454 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
456 // Run the "UPDATE" query
457 $this->queryUpdateDataSet($dataSetInstance);
461 * Checks whether there are unpublished entries
463 * @return $hasUnpublished Whether there are unpublished entries
464 * @todo Add minimum/maximum age limitations
466 public function hasUnpublishedEntries () {
467 // Get search instance
468 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
470 // Add exclusion key which is the publish status
471 $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PUBLICATION_STATUS, NodeDistributedHashTableDatabaseWrapper::PUBLICATION_STATUS_PENDING);
473 // Remember search instance
474 $this->setSearchInstance($searchInstance);
477 $this->unpublishedEntriesInstance = $this->doSelectByCriteria($searchInstance);
479 // Check pending entries
480 $hasUnpublished = $this->unpublishedEntriesInstance->valid();
483 return $hasUnpublished;
487 * Initializes publication of DHT entries. This does only prepare
488 * publication. The next step is to pickup such prepared entries and publish
489 * them by uploading to other (recently appeared) DHT members.
492 * @todo Add timestamp to dataset instance
494 public function initEntryPublication () {
496 * Make sure that hasUnpublishedEntries() has been called first by
497 * asserting on the "cached" object instance. This "caching" saves some
498 * needless queries as this method shall be called immediately after
499 * hasUnpublishedEntries() returns TRUE.
501 assert($this->unpublishedEntriesInstance instanceof SearchableResult);
503 // Result is still okay?
504 assert($this->unpublishedEntriesInstance->valid());
506 // Remove 'publication_status'
507 $this->getSearchInstance()->unsetCriteria(self::DB_COLUMN_PUBLICATION_STATUS);
509 // Make sure all entries are marked as pending, first get a dataset instance.
510 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
512 // Add search instance
513 $dataSetInstance->setSearchInstance($this->getSearchInstance());
515 // Set primary key (node id)
516 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
518 // Add criteria (that should be set)
519 $dataSetInstance->addCriteria(self::DB_COLUMN_PUBLICATION_STATUS, self::PUBLICATION_STATUS_PENDING);
521 // Run the "UPDATE" query
522 $this->queryUpdateDataSet($dataSetInstance);
526 * Removes non-public data from given array.
528 * @param $data An array with possible non-public data that needs to be removed.
529 * @return $data A cleaned up array with only public data.
531 public function removeNonPublicDataFromArray(array $data) {
532 // Currently call only inner method
533 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
534 $data = parent::removeNonPublicDataFromArray($data);
535 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: data[]=' . gettype($data));
537 // Return cleaned data
542 * Find recipients for given package data and exclude the sender
544 * @param $packageData An array of valid package data
545 * @return $recipients An indexed array with DHT recipients
547 public function getResultFromExcludedSender (array $packageData) {
548 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
550 // Assert on required array field
551 assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
553 // Get max recipients
554 $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
556 // First creata a search instance
557 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
559 // Then exclude 'sender' field as the sender is the current (*this*) node
560 $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
562 // Set limit to maximum DHT recipients
563 $searchInstance->setLimit($maxRecipients);
565 // Get a result instance back from DHT database wrapper.
566 $resultInstance = $this->doSelectByCriteria($searchInstance);
568 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
570 // Return result instance
571 return $resultInstance;
575 * Find recopients by given key/value pair. First look for the key and if it
576 * matches, compare the value.
578 * @param $key Key to look for
579 * @param $value Value to compare if key matches
580 * @return $recipients An indexed array with DHT recipients
582 public function getResultFromKeyValue ($key, $value) {
583 // Get max recipients
584 $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
586 // First creata a search instance
587 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
589 // Find the key/value pair
590 $searchInstance->addCriteria($key, $value);
592 // Get a result instance back from DHT database wrapper.
593 $resultInstance = $this->doSelectByCriteria($searchInstance);
595 // Return result instance
596 return $resultInstance;
600 * Enable DHT bootstrap request acceptance for local node
604 public function enableAcceptDhtBootstrap () {
606 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Enabling DHT bootstrap requests ...');
608 // Is the node already registered?
609 if ($this->isLocalNodeRegistered()) {
610 // Just update our record
611 $this->updateLocalNode();
614 $this->registerLocalNode();