]> git.mxchange.org Git - hub.git/blob - application/hub/main/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
Continued with refacturing:
[hub.git] / application / hub / main / wrapper / node / class_NodeDistributedHashTableDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for distributed hash tables
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.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 NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implements NodeDhtWrapper, Registerable {
25         /**
26          * "Cached" results for dabase for looking for unpublished entries
27          */
28         private $unpublishedEntriesInstance = NULL;
29
30         // Constants for database table names
31         const DB_TABLE_NODE_DHT = 'node_dht';
32
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_ADDRESS   = 'external_address';
37         const DB_COLUMN_PRIVATE_KEY_HASH   = 'private_key_hash';
38         const DB_COLUMN_NODE_MODE          = 'node_mode';
39         const DB_COLUMN_ACCEPTED_OBJECTS   = 'accepted_object_types';
40         const DB_COLUMN_NODE_LIST          = 'node_list';
41         const DB_COLUMN_PUBLICATION_STATUS = 'publication_status';
42         const DB_COLUMN_ANSWER_STATUS      = 'answer_status';
43         const DB_COLUMN_ACCEPT_BOOTSTRAP   = 'accept_bootstrap';
44
45         // Publication status'
46         const PUBLICATION_STATUS_PENDING = 'PENDING';
47
48         // Exception codes
49         const EXCEPTION_NODE_ALREADY_REGISTERED = 0x800;
50         const EXCEPTION_NODE_NOT_REGISTERED     = 0x801;
51
52         /**
53          * Protected constructor
54          *
55          * @return      void
56          */
57         protected function __construct () {
58                 // Call parent constructor
59                 parent::__construct(__CLASS__);
60         }
61
62         /**
63          * Creates an instance of this database wrapper by a provided user class
64          *
65          * @return      $wrapperInstance        An instance of the created wrapper class
66          */
67         public static final function createNodeDistributedHashTableDatabaseWrapper () {
68                 // Get a new instance
69                 $wrapperInstance = new NodeDistributedHashTableDatabaseWrapper();
70
71                 // Set (primary!) table name
72                 $wrapperInstance->setTableName(self::DB_TABLE_NODE_DHT);
73
74                 // Return the instance
75                 return $wrapperInstance;
76         }
77
78         /**
79          * Static getter for an array of all DHT database entries
80          *
81          * @return      $elements       All elements for the DHT dabase
82          */
83         public static final function getAllElements () {
84                 // Create array and ...
85                 $elements = array(
86                         self::DB_COLUMN_NODE_ID,
87                         self::DB_COLUMN_SESSION_ID,
88                         self::DB_COLUMN_EXTERNAL_ADDRESS,
89                         self::DB_COLUMN_PRIVATE_KEY_HASH,
90                         self::DB_COLUMN_NODE_MODE,
91                         self::DB_COLUMN_ACCEPTED_OBJECTS,
92                         self::DB_COLUMN_NODE_LIST
93                 );
94
95                 // ... return it
96                 return $elements;
97         }
98
99         /**
100          * Prepares a search instance for given node data
101          *
102          * @param       $nodeData                       An array with valid node data
103          * @return      $searchInstance         An instance of a SearchCriteria class
104          */
105         private function prepareSearchInstance (array $nodeData) {
106                 // Assert on array elements
107                 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
108
109                 // Get instance
110                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
111
112                 // Search for node id and limit it to one entry
113                 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]);
114                 $searchInstance->setLimit(1);
115
116                 // Return it
117                 return $searchInstance;
118         }
119
120         /**
121          * Getter for result instance for unpublished entries
122          *
123          * @return      $unpublishedEntriesInstance             Result instance
124          */
125         public final function getUnpublishedEntriesInstance () {
126                 return $this->unpublishedEntriesInstance;
127         }
128
129         /**
130          * Prepares a "local" instance of a StoreableCriteria class with all node
131          * data for insert/update queries. This data set contains data from *this*
132          * (local) node.
133          *
134          * @return      $dataSetInstance        An instance of a StoreableCriteria class
135          */
136         private function prepareLocalDataSetInstance () {
137                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
138
139                 // Get node/request instances
140                 $nodeInstance = NodeObjectFactory::createNodeInstance();
141                 $requestInstance = ApplicationHelper::getSelfInstance()->getRequestInstance();
142
143                 // Get a dataset instance
144                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
145
146                 // Set the primary key
147                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
148
149                 // Get Universal Node Locator and "explode" it
150                 $unlInstance = $nodeInstance->determineUniversalNodeLocator();
151
152                 // Make sure both is valid
153                 assert(($unl[0] !== 'invalid') && ($unl[1] !== 'invalid') && ($unl[2] !== 'invalid'));
154
155                 // Get an array of all accepted object types
156                 $objectList = $nodeInstance->getListFromAcceptedObjectTypes();
157
158                 // Make sure this is an array
159                 assert(is_array($objectList));
160
161                 // Add public node data
162                 $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_MODE       , $requestInstance->getRequestElement('mode'));
163                 $dataSetInstance->addCriteria(self::DB_COLUMN_EXTERNAL_ADDRESS, $unl[0]);
164                 $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_ID         , $nodeInstance->getNodeId());
165                 $dataSetInstance->addCriteria(self::DB_COLUMN_SESSION_ID      , $nodeInstance->getSessionId());
166                 $dataSetInstance->addCriteria(self::DB_COLUMN_PRIVATE_KEY_HASH, $nodeInstance->getPrivateKeyHash());
167                 $dataSetInstance->addCriteria(self::DB_COLUMN_ACCEPTED_OBJECTS, implode(BaseHubNode::OBJECT_LIST_SEPARATOR, $objectList));
168                 $dataSetInstance->addCriteria(self::DB_COLUMN_ACCEPT_BOOTSTRAP, $this->translateBooleanToYesNo($nodeInstance->isAcceptingDhtBootstrap()));
169
170                 // Return it
171                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
172                 return $dataSetInstance;
173         }
174
175         /**
176          * Checks whether the local (*this*) node is registered in the DHT by
177          * checking if the external address is found.
178          *
179          * @return      $isRegistered   Whether *this* node is registered in the DHT
180          */
181         public function isLocalNodeRegistered () {
182                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
183
184                 // Get a search criteria instance
185                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
186
187                 // Get node instance
188                 $nodeInstance = NodeObjectFactory::createNodeInstance();
189
190                 // Get Universal Node Locator and "explode" it
191                 $unlData = $nodeInstance->getUniversalNodeLocatorArray();
192
193                 /*
194                  * Make sure both is not 'invalid' which means that the resolver
195                  * didn't work.
196                  */
197                 die(__METHOD__ . ':unlData[' . gettype($unlData) . ']=' . print_r($unlData, TRUE) . PHP_EOL);
198
199                 // Add Universal Node Locator/node id as criteria
200                 $searchInstance->addCriteria(self::DB_COLUMN_EXTERNAL_ADDRESS, $unl[0]);
201                 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID         , $nodeInstance->getNodeId());
202                 $searchInstance->addCriteria(self::DB_COLUMN_SESSION_ID      , $nodeInstance->getSessionId());
203                 $searchInstance->setLimit(1);
204
205                 // Query database and get a result instance back
206                 $resultInstance = $this->doSelectByCriteria($searchInstance);
207
208                 // Cache result of if there is an entry, valid() will tell us if an entry is there
209                 $isRegistered = $resultInstance->valid();
210
211                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: isRegistered=' . intval($isRegistered) . ' - EXIT!');
212
213                 // Return result
214                 return $isRegistered;
215         }
216
217         /**
218          * Registeres the local (*this*) node with its data in the DHT.
219          *
220          * @return      void
221          */
222         public function registerLocalNode () {
223                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
224
225                 // Assert to make sure this method is called with no record in DB (the actual backend of the DHT)
226                 assert(!$this->isLocalNodeRegistered());
227
228                 // Get prepared data set instance
229                 $dataSetInstance = $this->prepareLocalDataSetInstance();
230
231                 // "Insert" this dataset instance completely into the database
232                 $this->queryInsertDataSet($dataSetInstance);
233
234                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
235         }
236
237         /**
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.
240          *
241          * @return      void
242          */
243         public function updateLocalNode () {
244                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
245
246                 // Assert to make sure this method is called with one record in DB (the actual backend of the DHT)
247                 assert($this->isLocalNodeRegistered());
248
249                 // Get node instance
250                 $nodeInstance = NodeObjectFactory::createNodeInstance();
251
252                 // Get search criteria
253                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
254
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);
258
259                 // Get a prepared dataset instance
260                 $dataSetInstance = $this->prepareLocalDataSetInstance();
261
262                 // Set search instance
263                 $dataSetInstance->setSearchInstance($searchInstance);
264
265                 // Update DHT database record
266                 $this->queryUpdateDataSet($dataSetInstance);
267
268                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
269         }
270
271         /**
272          * Finds a node locally by given session id
273          *
274          * @param       $sessionId      Session id to lookup
275          * @return      $nodeData       Node data array
276          */
277         public function findNodeLocalBySessionId ($sessionId) {
278                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: sessionId=' . $sessionId . ' - CALLED!');
279
280                 // Get search criteria
281                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
282
283                 // Search for session id and limit it to one entry
284                 $searchInstance->addCriteria(self::DB_COLUMN_SESSION_ID, $sessionId);
285                 $searchInstance->setLimit(1);
286
287                 // Query database and get a result instance back
288                 $resultInstance = $this->doSelectByCriteria($searchInstance);
289
290                 // Return result instance
291                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: resultInstance->valid()=' . intval($resultInstance->valid()) . ' - EXIT!');
292                 return $resultInstance;
293         }
294
295         /**
296          * Registeres a node by given message data.
297          *
298          * @param       $messageData            An array of all message data
299          * @param       $handlerInstance        An instance of a HandleableDataSet class
300          * @return      void
301          */
302         public function registerNodeByMessageData (array $messageData, HandleableDataSet $handlerInstance) {
303                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: handlerInstance=' . $handlerInstance->__toString() . ' - CALLED!');
304
305                 // Get a data set instance
306                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
307
308                 // Set primary key (session id)
309                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
310
311                 // Add all array elements
312                 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
313
314                 // Remove 'node_list'
315                 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
316
317                 // Run the "INSERT" query
318                 $this->queryInsertDataSet($dataSetInstance);
319
320                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . '] - EXIT!');
321         }
322
323         /**
324          * Updates an existing entry in node list
325          *
326          * @param       $messageData            An array of all message data
327          * @param       $handlerInstance        An instance of a HandleableDataSet class
328          * @param       $searchInstance         An instance of LocalSearchCriteria class
329          * @return      void
330          */
331         public function updateNodeByMessageData (array $messageData, HandleableDataSet $handlerInstance, LocalSearchCriteria $searchInstance) {
332                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
333
334                 // Get a data set instance
335                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
336
337                 // Add search instance
338                 $dataSetInstance->setSearchInstance($searchInstance);
339
340                 // Set primary key (session id)
341                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
342
343                 // Add all array elements
344                 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
345
346                 // Remove 'node_list'
347                 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
348
349                 // Run the "UPDATE" query
350                 $this->queryUpdateDataSet($dataSetInstance);
351
352                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
353         }
354
355         /**
356          * Determines whether the given node data is already inserted in the DHT
357          *
358          * @param       $nodeData               An array with valid node data
359          * @return      $isRegistered   Whether the given node data is already inserted
360          */
361         public function isNodeRegistered (array $nodeData) {
362                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
363
364                 // Assert on array elements
365                 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
366
367                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: node-id=' . $nodeData[self::DB_COLUMN_NODE_ID]);
368
369                 // Get search criteria
370                 $searchInstance = $this->prepareSearchInstance($nodeData);
371
372                 // Query database and get a result instance back
373                 $resultInstance = $this->doSelectByCriteria(
374                         // Search instance
375                         $searchInstance,
376                         // Only look for these array elements ("keys")
377                         array(
378                                 self::DB_COLUMN_NODE_ID          => TRUE,
379                                 self::DB_COLUMN_EXTERNAL_ADDRESS => TRUE
380                         )
381                 );
382
383                 // Check if there is an entry
384                 $isRegistered = $resultInstance->valid();
385
386                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: isRegistered=' . intval($isRegistered) . ' - EXIT!');
387
388                 // Return registration status
389                 return $isRegistered;
390         }
391
392         /**
393          * Registers a node with given data in the DHT. If the node is already
394          * registered this method shall throw an exception.
395          *
396          * @param       $nodeData       An array with valid node data
397          * @return      void
398          * @throws      NodeAlreadyRegisteredException  If the node is already registered
399          */
400         public function registerNode (array $nodeData) {
401                 // Assert on array elements
402                 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
403
404                 // Is the node registered?
405                 if ($this->isNodeRegistered($nodeData)) {
406                         // Throw an exception
407                         throw new NodeAlreadyRegisteredException(array($this, $nodeData), self::EXCEPTION_NODE_ALREADY_REGISTERED);
408                 } // END - if
409
410                 // @TODO Unimplemented part
411                 $this->partialStub('nodeData=' . print_r($nodeData, TRUE));
412         }
413
414         /**
415          * Updates a node's entry in the DHT with given data. This will enrich or
416          * just update already exsiting data. If the node is not found this method
417          * shall throw an exception.
418          *
419          * @param       $nodeData       An array with valid node data
420          * @return      void
421          * @throws      NodeDataMissingException        If the node's data is missing
422          */
423         public function updateNode (array $nodeData) {
424                 // Assert on array elements
425                 assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
426
427                 // Debug message
428                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Updating DHT entry for node-id=' . $nodeData[self::DB_COLUMN_NODE_ID] . ' ...');
429
430                 // Is the node registered?
431                 if (!$this->isNodeRegistered($nodeData)) {
432                         // No, then throw an exception
433                         throw new NodeDataMissingException(array($this, $nodeData), self::EXCEPTION_NODE_NOT_REGISTERED);
434                 } // END - if
435
436                 // Get a search instance
437                 $searchInstance = $this->prepareSearchInstance($nodeData);
438
439                 // Get a data set instance
440                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
441
442                 // Add search instance
443                 $dataSetInstance->setSearchInstance($searchInstance);
444
445                 // Set primary key (session id)
446                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
447
448                 // Get node instance
449                 $nodeInstance = NodeObjectFactory::createNodeInstance();
450
451                 // Debug message
452                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: nodeData=' . print_r($nodeData, TRUE));
453
454                 // Add all array elements
455                 $nodeInstance->addArrayToDataSet($dataSetInstance, $nodeData);
456
457                 // Remove 'node_list'
458                 $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
459
460                 // Run the "UPDATE" query
461                 $this->queryUpdateDataSet($dataSetInstance);
462         }
463
464         /**
465          * Checks whether there are unpublished entries
466          *
467          * @return      $hasUnpublished         Whether there are unpublished entries
468          * @todo        Add minimum/maximum age limitations
469          */
470         public function hasUnpublishedEntries () {
471                 // Get search instance
472                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
473
474                 // Add exclusion key which is the publish status
475                 $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PUBLICATION_STATUS, NodeDistributedHashTableDatabaseWrapper::PUBLICATION_STATUS_PENDING);
476
477                 // Remember search instance
478                 $this->setSearchInstance($searchInstance);
479
480                 // Run the query
481                 $this->unpublishedEntriesInstance = $this->doSelectByCriteria($searchInstance);
482
483                 // Check pending entries
484                 $hasUnpublished = $this->unpublishedEntriesInstance->valid();
485
486                 // Return it
487                 return $hasUnpublished;
488         }
489
490         /**
491          * Initializes publication of DHT entries. This does only prepare
492          * publication. The next step is to pickup such prepared entries and publish
493          * them by uploading to other (recently appeared) DHT members.
494          *
495          * @return      void
496          * @todo        Add timestamp to dataset instance
497          */
498         public function initEntryPublication () {
499                 /*
500                  * Make sure that hasUnpublishedEntries() has been called first by
501                  * asserting on the "cached" object instance. This "caching" saves some
502                  * needless queries as this method shall be called immediately after
503                  * hasUnpublishedEntries() returns TRUE.
504                  */
505                 assert($this->unpublishedEntriesInstance instanceof SearchableResult);
506
507                 // Result is still okay?
508                 assert($this->unpublishedEntriesInstance->valid());
509
510                 // Remove 'publication_status'
511                 $this->getSearchInstance()->unsetCriteria(self::DB_COLUMN_PUBLICATION_STATUS);
512
513                 // Make sure all entries are marked as pending, first get a dataset instance.
514                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
515
516                 // Add search instance
517                 $dataSetInstance->setSearchInstance($this->getSearchInstance());
518
519                 // Set primary key (node id)
520                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
521
522                 // Add criteria (that should be set)
523                 $dataSetInstance->addCriteria(self::DB_COLUMN_PUBLICATION_STATUS, self::PUBLICATION_STATUS_PENDING);
524
525                 // Run the "UPDATE" query
526                 $this->queryUpdateDataSet($dataSetInstance);
527         }
528
529         /**
530          * Removes non-public data from given array.
531          *
532          * @param       $data   An array with possible non-public data that needs to be removed.
533          * @return      $data   A cleaned up array with only public data.
534          */
535         public function removeNonPublicDataFromArray(array $data) {
536                 // Currently call only inner method
537                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
538                 $data = parent::removeNonPublicDataFromArray($data);
539                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: data[]=' . gettype($data));
540
541                 // Return cleaned data
542                 return $data;
543         }
544
545         /**
546          * Find recipients for given package data and exclude the sender
547          *
548          * @param       $packageData    An array of valid package data
549          * @return      $recipients             An indexed array with DHT recipients
550          */
551         public function getResultFromExcludedSender (array $packageData) {
552                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
553
554                 // Assert on required array field
555                 assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
556
557                 // Get max recipients
558                 $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
559
560                 // First creata a search instance
561                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
562
563                 // Then exclude 'sender' field as the sender is the current (*this*) node
564                 $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
565
566                 // Set limit to maximum DHT recipients
567                 $searchInstance->setLimit($maxRecipients);
568
569                 // Get a result instance back from DHT database wrapper.
570                 $resultInstance = $this->doSelectByCriteria($searchInstance);
571
572                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
573
574                 // Return result instance
575                 return $resultInstance;
576         }
577
578         /**
579          * Find recopients by given key/value pair. First look for the key and if it
580          * matches, compare the value.
581          *
582          * @param       $key                    Key to look for
583          * @param       $value                  Value to compare if key matches
584          * @return      $recipients             An indexed array with DHT recipients
585          */
586         public function getResultFromKeyValue ($key, $value) {
587                 // Get max recipients
588                 $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
589
590                 // First creata a search instance
591                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
592
593                 // Find the key/value pair
594                 $searchInstance->addCriteria($key, $value);
595
596                 // Get a result instance back from DHT database wrapper.
597                 $resultInstance = $this->doSelectByCriteria($searchInstance);
598
599                 // Return result instance
600                 return $resultInstance;
601         }
602
603         /**
604          * Enable DHT bootstrap request acceptance for local node
605          *
606          * @return      void
607          */
608         public function enableAcceptDhtBootstrap () {
609                 // Debug message
610                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Enabling DHT bootstrap requests ...');
611
612                 // Is the node already registered?
613                 if ($this->isLocalNodeRegistered()) {
614                         // Just update our record
615                         $this->updateLocalNode();
616                 } else {
617                         // Register it
618                         $this->registerLocalNode();
619                 }
620         }
621 }
622
623 // [EOF]
624 ?>