3 namespace Org\Shipsimu\Hub\Database\Frontend\Node\Dht;
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Database\Frontend\BaseHubDatabaseFrontend;
7 use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory;
8 use Org\Shipsimu\Hub\Locator\Node\LocateableNode;
9 use Org\Shipsimu\Hub\Network\Message\DeliverableMessage;
10 use Org\Shipsimu\Hub\Network\Package\DeliverablePackage;
11 use Org\Shipsimu\Hub\Node\BaseHubNode;
13 // Import framework stuff
14 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
15 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
16 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
17 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
18 use Org\Mxchange\CoreFramework\Handler\DataSet\HandleableDataSet;
19 use Org\Mxchange\CoreFramework\Registry\Registerable;
20 use Org\Mxchange\CoreFramework\Result\Search\SearchableResult;
23 use \InvalidArgumentException;
26 * A database frontend for distributed hash tables
28 * @author Roland Haeder <webmaster@shipsimu.org>
30 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
31 * @license GNU GPL 3.0 or any newer version
32 * @link http://www.shipsimu.org
34 * This program is free software: you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation, either version 3 of the License, or
37 * (at your option) any later version.
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with this program. If not, see <http://www.gnu.org/licenses/>.
47 class NodeDistributedHashTableDatabaseFrontend extends BaseHubDatabaseFrontend implements NodeDhtFrontend, Registerable {
49 * "Cached" results for dabase for looking for unpublished entries
51 private $unpublishedEntriesInstance = NULL;
53 // Constants for database table names
54 const DB_TABLE_NODE_DHT = 'node_dht';
56 // Constants for database column names
57 const DB_COLUMN_NODE_ID = 'node_id';
58 const DB_COLUMN_SESSION_ID = 'session_id';
59 const DB_COLUMN_EXTERNAL_ADDRESS = 'external_address';
60 const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
61 const DB_COLUMN_NODE_MODE = 'node_mode';
62 const DB_COLUMN_ACCEPTED_OBJECTS = 'accepted_object_types';
63 const DB_COLUMN_NODE_LIST = 'node_list';
64 const DB_COLUMN_PUBLICATION_STATUS = 'publication_status';
65 const DB_COLUMN_ANSWER_STATUS = 'answer_status';
66 const DB_COLUMN_ACCEPT_BOOTSTRAP = 'accept_bootstrap';
68 // Publication status'
69 const PUBLICATION_STATUS_PENDING = 'PENDING';
72 const EXCEPTION_NODE_ALREADY_REGISTERED = 0x800;
73 const EXCEPTION_NODE_NOT_REGISTERED = 0x801;
76 * Protected constructor
80 protected function __construct () {
81 // Call parent constructor
82 parent::__construct(__CLASS__);
86 * Creates an instance of this database frontend by a provided user class
88 * @return $frontendInstance An instance of the created frontend class
90 public static final function createNodeDistributedHashTableDatabaseFrontend () {
92 $frontendInstance = new NodeDistributedHashTableDatabaseFrontend();
94 // Set (primary!) table name
95 $frontendInstance->setTableName(self::DB_TABLE_NODE_DHT);
98 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: Creating node instance ...');
99 $nodeInstance = NodeObjectFactory::createNodeInstance();
102 $frontendInstance->setNodeInstance($nodeInstance);
104 // Return the instance
105 return $frontendInstance;
109 * Static getter for an array of all DHT database entries
111 * @return $elements All elements for the DHT dabase
113 public static final function getAllElements () {
114 // Create array and ...
116 self::DB_COLUMN_NODE_ID,
117 self::DB_COLUMN_SESSION_ID,
118 self::DB_COLUMN_EXTERNAL_ADDRESS,
119 self::DB_COLUMN_PRIVATE_KEY_HASH,
120 self::DB_COLUMN_NODE_MODE,
121 self::DB_COLUMN_ACCEPTED_OBJECTS,
122 self::DB_COLUMN_NODE_LIST
130 * Prepares a search instance for given node data
132 * @param $nodeData An array with valid node data
133 * @return $searchInstance An instance of a SearchCriteria class
135 private function prepareSearchInstance (array $nodeData) {
136 // Assert on array elements
137 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
138 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
141 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
143 // Search for node id and limit it to one entry
144 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]);
145 $searchInstance->setLimit(1);
148 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
149 return $searchInstance;
153 * Prepares a "local" instance of a StoreableCriteria class with all node
154 * data for insert/update queries. This data set contains data from *this*
157 * @return $dataSetInstance An instance of a StoreableCriteria class
159 private function prepareLocalDataSetInstance () {
161 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
163 // Get request instances
164 $requestInstance = FrameworkBootstrap::getRequestInstance();
166 // Get a dataset instance
167 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
169 // Set the primary key
170 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
172 // Get Universal Node Locator and "explode" it
173 $locatorInstance = $this->getNodeInstance()->determineUniversalNodeLocator();
176 $externalUnl = $locatorInstance->getExternalUnl();
178 // Make sure both is valid
179 // @TODO Bad check on UNL, better use a proper validator
180 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: externalUnl=' . $externalUnl);
181 assert($externalUnl !== 'invalid');
183 // Get an array of all accepted object types
184 $objectList = $this->getNodeInstance()->getListFromAcceptedObjectTypes();
186 // Make sure this is an array
187 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: objectList()=' . count($objectList));
188 assert(is_array($objectList));
190 // Add public node data
191 $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_MODE , $requestInstance->getRequestElement('mode'));
192 $dataSetInstance->addCriteria(self::DB_COLUMN_EXTERNAL_ADDRESS, $externalUnl);
193 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: nodeInstance->nodeId=' . $this->getNodeInstance()->getNodeId());
194 $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_ID , $this->getNodeInstance()->getNodeId());
195 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: nodeInstance->sessionId=' . $this->getNodeInstance()->getSessionId());
196 $dataSetInstance->addCriteria(self::DB_COLUMN_SESSION_ID , $this->getNodeInstance()->getSessionId());
197 $dataSetInstance->addCriteria(self::DB_COLUMN_PRIVATE_KEY_HASH, $this->getNodeInstance()->getNodePrivateKeyHash());
198 $dataSetInstance->addCriteria(self::DB_COLUMN_ACCEPTED_OBJECTS, implode(BaseHubNode::OBJECT_LIST_SEPARATOR, $objectList));
199 $dataSetInstance->addCriteria(self::DB_COLUMN_ACCEPT_BOOTSTRAP, $this->translateBooleanToYesNo($this->getNodeInstance()->isAcceptingDhtBootstrap()));
202 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: dataSetInstance=' . $dataSetInstance->__toString() . ' - EXIT!');
203 return $dataSetInstance;
207 * Getter for result instance for unpublished entries
209 * @return $unpublishedEntriesInstance Result instance
211 public final function getUnpublishedEntriesInstance () {
212 return $this->unpublishedEntriesInstance;
216 * Checks whether the local (*this*) node is registered in the DHT by
217 * checking if the external address is found.
219 * @return $isRegistered Whether *this* node is registered in the DHT
221 public function isLocalNodeRegistered () {
222 // Get a search criteria instance
223 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
224 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
226 // Get Universal Node Locator and "explode" it
227 $locatorInstance = $this->getNodeInstance()->determineUniversalNodeLocator();
229 // Make sure the external address is set and not invalid
230 // @TODO Bad check on UNL, better use a proper validator
231 $externalUnl = $locatorInstance->getExternalUnl();
232 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: externalUnl=%s', $externalUnl));
233 assert($externalUnl != 'invalid');
235 // Add Universal Node Locator/node id as criteria
236 $searchInstance->addCriteria(self::DB_COLUMN_EXTERNAL_ADDRESS, $externalUnl);
237 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID , $this->getNodeInstance()->getNodeId());
238 $searchInstance->addCriteria(self::DB_COLUMN_SESSION_ID , $this->getNodeInstance()->getSessionId());
239 $searchInstance->setLimit(1);
241 // Query database and get a result instance back
242 $resultInstance = $this->doSelectByCriteria($searchInstance);
244 // Cache result of if there is an entry, valid() will tell us if an entry is there
245 $isRegistered = $resultInstance->valid();
248 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: isRegistered=%d - EXIT!', intval($isRegistered)));
249 return $isRegistered;
253 * Registeres the local (*this*) node with its data in the DHT.
257 public function registerLocalNode () {
259 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
261 // Assert to make sure this method is called with no record in DB (the actual backend of the DHT)
262 assert(!$this->isLocalNodeRegistered());
264 // Get prepared data set instance
265 $dataSetInstance = $this->prepareLocalDataSetInstance();
267 // "Insert" this dataset instance completely into the database
268 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: Calling this->queryInsertDataSet(' . $dataSetInstance->__toString() . ') ...');
269 $this->queryInsertDataSet($dataSetInstance);
272 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
276 * Updates local (*this*) node's data in DHT, this is but not limited to the
277 * session id, ip number (and/or hostname) and port number.
281 public function updateLocalNode () {
283 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
285 // Assert to make sure this method is called with one record in DB (the actual backend of the DHT)
286 assert($this->isLocalNodeRegistered());
288 // Get search criteria
289 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
291 // Search for node id and limit it to one entry
292 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $this->getNodeInstance()->getNodeId());
293 $searchInstance->setLimit(1);
295 // Get a prepared dataset instance
296 $dataSetInstance = $this->prepareLocalDataSetInstance();
298 // Set search instance
299 $dataSetInstance->setSearchInstance($searchInstance);
301 // Update DHT database record
302 $this->queryUpdateDataSet($dataSetInstance);
305 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
309 * Finds a node locally by given session id
311 * @param $sessionId Session id to lookup
312 * @return $resultInstance An instance of a SearchableResult class
313 * @throws InvalidArgumentException If parameter $sessionId is not valid
315 public function findNodeLocalBySessionId ($sessionId) {
316 // Validate parameter
317 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: sessionId=%s - CALLED!', $sessionId));
318 if (empty($sessionId)) {
320 throw new InvalidArgumentException('Parameter "sessionId" is empty.');
323 // Get search criteria
324 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
326 // Search for session id and limit it to one entry
327 $searchInstance->addCriteria(self::DB_COLUMN_SESSION_ID, $sessionId);
328 $searchInstance->setLimit(1);
330 // Query database and get a result instance back
331 $resultInstance = $this->doSelectByCriteria($searchInstance);
333 // Return result instance
334 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: resultInstance->valid()=%d - EXIT!', intval($resultInstance->valid())));
335 return $resultInstance;
339 * Finds a node locally by given UNL instance
341 * @param $locatorInstance An instance of a LocateableNode class
342 * @return $resultInstance An instance of a SearchableResult class
344 public function findNodeLocalByLocatorInstance (LocateableNode $locatorInstance) {
345 // Get search criteria
346 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: locatorInstance=%s - CALLED!', $locatorInstance->__toString()));
347 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
349 // Search for session id and limit it to one entry
350 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: externalUnl=%s', $locatorInstance->getExternalUnl()));
351 $searchInstance->addCriteria(self::DB_COLUMN_EXTERNAL_ADDRESS, $locatorInstance->getExternalUnl());
352 $searchInstance->setLimit(1);
354 // Query database and get a result instance back
355 $resultInstance = $this->doSelectByCriteria($searchInstance);
357 // Return result instance
358 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: resultInstance->valid()=%d - EXIT!', intval($resultInstance->valid())));
359 return $resultInstance;
363 * Registeres a node by given message data.
365 * @param $messageInstance An instance of a DeliverableMessage class
366 * @param $handlerInstance An instance of a HandleableDataSet class
369 public function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance) {
371 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: handlerInstance=' . $handlerInstance->__toString() . ' - CALLED!');
373 // Get a data set instance
374 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
376 // Set primary key (session id)
377 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
379 // Add all array elements
380 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageInstance);
382 // Remove 'node_list'
383 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
385 // Run the "INSERT" query
386 $this->queryInsertDataSet($dataSetInstance);
389 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER - EXIT!');
393 * Updates an existing entry in node list
395 * @param $messageInstance An instance of a DeliverableMessage class
396 * @param $handlerInstance An instance of a HandleableDataSet class
397 * @param $searchInstance An instance of LocalSearchCriteria class
400 public function updateNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, LocalSearchCriteria $searchInstance) {
402 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
404 // Get a data set instance
405 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
407 // Add search instance
408 $dataSetInstance->setSearchInstance($searchInstance);
410 // Set primary key (session id)
411 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
413 // Add all array elements
414 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageInstance);
416 // Remove 'node_list'
417 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
419 // Run the "UPDATE" query
420 $this->queryUpdateDataSet($dataSetInstance);
423 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
427 * Determines whether the given node data is already inserted in the DHT
429 * @param $nodeData An array with valid node data
430 * @return $isRegistered Whether the given node data is already inserted
432 public function isNodeRegistered (array $nodeData) {
434 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
436 // Assert on array elements
437 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
440 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: node-id=' . $nodeData[self::DB_COLUMN_NODE_ID]);
442 // Get search criteria
443 $searchInstance = $this->prepareSearchInstance($nodeData);
445 // Query database and get a result instance back
446 $resultInstance = $this->doSelectByCriteria(
449 // Only look for these array elements ("keys")
451 self::DB_COLUMN_NODE_ID => TRUE,
452 self::DB_COLUMN_EXTERNAL_ADDRESS => TRUE
456 // Check if there is an entry
457 $isRegistered = $resultInstance->valid();
460 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: isRegistered=' . intval($isRegistered) . ' - EXIT!');
462 // Return registration status
463 return $isRegistered;
467 * Registers a node with given data in the DHT. If the node is already
468 * registered this method shall throw an exception.
470 * @param $nodeData An array with valid node data
472 * @throws NodeAlreadyRegisteredException If the node is already registered
474 public function registerNode (array $nodeData) {
476 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
478 // Assert on array elements
479 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
481 // Is the node registered?
482 if ($this->isNodeRegistered($nodeData)) {
483 // Throw an exception
484 throw new NodeAlreadyRegisteredException(array($this, $nodeData), self::EXCEPTION_NODE_ALREADY_REGISTERED);
487 // @TODO Unimplemented part
488 $this->partialStub('nodeData=' . print_r($nodeData, TRUE));
491 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
495 * Updates a node's entry in the DHT with given data. This will enrich or
496 * just update already exsiting data. If the node is not found this method
497 * shall throw an exception.
499 * @param $nodeData An array with valid node data
501 * @throws NodeDataMissingException If the node's data is missing
503 public function updateNode (array $nodeData) {
505 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: nodeData=' . print_r($nodeData, TRUE));
506 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
508 // Assert on array elements
509 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
512 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: Updating DHT entry for node-id=' . $nodeData[self::DB_COLUMN_NODE_ID] . ' ...');
514 // Is the node registered?
515 if (!$this->isNodeRegistered($nodeData)) {
516 // No, then throw an exception
517 throw new NodeDataMissingException(array($this, $nodeData), self::EXCEPTION_NODE_NOT_REGISTERED);
520 // Get a search instance
521 $searchInstance = $this->prepareSearchInstance($nodeData);
523 // Get a data set instance
524 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
526 // Add search instance
527 $dataSetInstance->setSearchInstance($searchInstance);
529 // Set primary key (session id)
530 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
532 // Add all array elements
533 $this->getNodeInstance()->addArrayToDataSet($dataSetInstance, $nodeData);
535 // Remove 'node_list'
536 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
538 // Run the "UPDATE" query
539 $this->queryUpdateDataSet($dataSetInstance);
542 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
546 * Checks whether there are unpublished entries
548 * @return $hasUnpublished Whether there are unpublished entries
549 * @todo Add minimum/maximum age limitations
551 public function hasUnpublishedEntries () {
552 // Get search instance
553 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
555 // Add exclusion key which is the publish status
556 $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseFrontend::DB_COLUMN_PUBLICATION_STATUS, NodeDistributedHashTableDatabaseFrontend::PUBLICATION_STATUS_PENDING);
558 // Remember search instance
559 $this->setSearchInstance($searchInstance);
562 $this->unpublishedEntriesInstance = $this->doSelectByCriteria($searchInstance);
564 // Check pending entries
565 $hasUnpublished = $this->unpublishedEntriesInstance->valid();
568 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
571 return $hasUnpublished;
575 * Initializes publication of DHT entries. This does only prepare
576 * publication. The next step is to pickup such prepared entries and publish
577 * them by uploading to other (recently appeared) DHT members.
580 * @todo Add timestamp to dataset instance
582 public function initEntryPublication () {
584 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
587 * Make sure that hasUnpublishedEntries() has been called first by
588 * asserting on the "cached" object instance. This "caching" saves some
589 * needless queries as this method shall be called immediately after
590 * hasUnpublishedEntries() returns TRUE.
592 assert($this->unpublishedEntriesInstance instanceof SearchableResult);
594 // Result is still okay?
595 assert($this->unpublishedEntriesInstance->valid());
597 // Remove 'publication_status'
598 $this->getSearchInstance()->unsetCriteria(self::DB_COLUMN_PUBLICATION_STATUS);
600 // Make sure all entries are marked as pending, first get a dataset instance.
601 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
603 // Add search instance
604 $dataSetInstance->setSearchInstance($this->getSearchInstance());
606 // Set primary key (node id)
607 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
609 // Add criteria (that should be set)
610 $dataSetInstance->addCriteria(self::DB_COLUMN_PUBLICATION_STATUS, self::PUBLICATION_STATUS_PENDING);
612 // Run the "UPDATE" query
613 $this->queryUpdateDataSet($dataSetInstance);
616 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
620 * Removes non-public data from given array.
622 * @param $data An array with possible non-public data that needs to be removed.
623 * @return $data A cleaned up array with only public data.
625 public function removeNonPublicDataFromArray(array $data) {
626 // Currently call only inner method
627 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: Calling parent::removeNonPublicDataFromArray(data) ...');
628 $data = parent::removeNonPublicDataFromArray($data);
629 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: data[]=' . gettype($data));
631 // Return cleaned data
636 * Find recipients for given package data and exclude the sender
638 * @param $packageInstance An instance of a DeliverablePackage class
639 * @return $recipients An indexed array with DHT recipients
641 public function getResultFromExcludedSender (DeliverablePackage $packageInstance) {
643 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
645 // Get max recipients
646 $maxRecipients = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('max_dht_recipients');
648 // First creata a search instance
649 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
651 // Then exclude 'sender' field as the sender is the current (*this*) node
652 $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseFrontend::DB_COLUMN_SESSION_ID, $packageInstance->getSenderAddress());
654 // Set limit to maximum DHT recipients
655 $searchInstance->setLimit($maxRecipients);
657 // Get a result instance back from DHT database frontend.
658 $resultInstance = $this->doSelectByCriteria($searchInstance);
661 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
663 // Return result instance
664 return $resultInstance;
668 * Find recopients by given key/value pair. First look for the key and if it
669 * matches, compare the value.
671 * @param $key Key to look for
672 * @param $value Value to compare if key matches
673 * @return $recipients An indexed array with DHT recipients
674 * @throws InvalidArgumentException If $key is empty
676 public function getResultFromKeyValue (string $key, $value) {
677 // Is key parameter valid?
678 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: key=%s,value[%s]=%s - CALLED!', $key, gettype($value), $value));
681 throw new InvalidArgumentException('Parameter key is empty');
684 // Get max recipients
685 $maxRecipients = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('max_dht_recipients');
687 // First creata a search instance
688 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
690 // Find the key/value pair
691 $searchInstance->addCriteria($key, $value);
693 // Get a result instance back from DHT database frontend.
694 $resultInstance = $this->doSelectByCriteria($searchInstance);
696 // Return result instance
697 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: resultInstance=%s - EXIT!', $resultInstance->__toString()));
698 return $resultInstance;
702 * Enable DHT bootstrap request acceptance for local node
706 public function enableAcceptDhtBootstrap () {
708 /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: Enabling DHT bootstrap requests ...');
710 // Is the node already registered?
711 if ($this->isLocalNodeRegistered()) {
712 // Just update our record
713 $this->updateLocalNode();
716 $this->registerLocalNode();