* @return $isBooting Whether this DHT is currently booting
*/
function ifDhtIsBooting ();
+
+ /**
+ * Finds DHT recipients by given key/value pair
+ *
+ * @param $key Key to search for
+ * @param $value Value to check on found key
+ * @return $recipientList Array with DHT recipients from given key/value pair
+ */
+ function findRecipientsByKey ($key, $value);
}
// [EOF]
* @return $recipients An indexed array with DHT recipients
*/
function getResultFromExcludedSender (array $packageData);
+
+ /**
+ * Find recopients by given key/value pair. First look for the key and if it
+ * matches, compare the value.
+ *
+ * @param $key Key to look for
+ * @param $value Value to compare if key matches
+ * @return $recipients An indexed array with DHT recipients
+ */
+ function getResultFromKeyValue ($key, $value);
}
// [EOF]
const DB_COLUMN_NODE_LIST = 'node_list';
const DB_COLUMN_PUBLICATION_STATUS = 'publication_status';
const DB_COLUMN_ANSWER_STATUS = 'answer_status';
+ const DB_COLUMN_ACCEPT_BOOTSTRAP = 'accept_bootstrap';
// Publication status'
const PUBLICATION_STATUS_PENDING = 'PENDING';
// Assert on required array field
assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
+ // Get max recipients
+ $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
+
// First creata a search instance
$searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
// Then exclude 'sender' field as the sender is the current (*this*) node
$searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+ // Set limit to maximum DHT recipients
+ $searchInstance->setLimit($maxRecipients);
+
+ // Get a result instance back from DHT database wrapper.
+ $resultInstance = $this->doSelectByCriteria($searchInstance);
+
+ // Return result instance
+ return $resultInstance;
+ }
+
+ /**
+ * Find recopients by given key/value pair. First look for the key and if it
+ * matches, compare the value.
+ *
+ * @param $key Key to look for
+ * @param $value Value to compare if key matches
+ * @return $recipients An indexed array with DHT recipients
+ */
+ public function getResultFromKeyValue ($key, $value) {
+ // Get max recipients
+ $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
+
+ // First creata a search instance
+ $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+ // Find the key/value pair
+ $searchInstance->addCriteria($key, $value);
+
// Get a result instance back from DHT database wrapper.
$resultInstance = $this->doSelectByCriteria($searchInstance);
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-abstract class BaseDht extends BaseHubSystem {
+abstract class BaseDht extends BaseHubSystem implements Distributable {
/**
* "Cached" instance of a publish helper
*/
// Get result instance
$resultInstance = $this->getWrapperInstance()->getUnpublishedEntriesInstance();
- // Must still be valid
+ // Make sure the result instance is valid
+ assert($resultInstance instanceof SearchableResult);
assert($resultInstance->valid());
// "Walk" through all entries
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
+class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable {
/**
* Protected constructor
*
*/
$resultInstance = $this->getWrapperInstance()->findNodeLocalBySessionId($sessionId);
+ // Make sure the result instance is valid
+ assert($resultInstance instanceof SearchableResult);
+ assert($resultInstance->valid());
+
// Is the next entry valid?
if (($resultInstance->valid()) && ($resultInstance->next())) {
/*
// Run the query
$resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
+ // Make sure the result instance is valid
+ assert($resultInstance instanceof SearchableResult);
+ assert($resultInstance->valid());
+
// Is there already an entry?
if ($resultInstance->valid()) {
// Entry found, so update it
// Run the query
$resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
- // Get node list
+ // Make sure the result instance is valid
+ assert($resultInstance instanceof SearchableResult);
+ assert($resultInstance->valid());
+
+ // Init array
$nodeList = array();
+
+ // Get node list
while ($resultInstance->next()) {
// Get current element (it should be an array, and have at least 1 entry)
$current = $resultInstance->current();
* @return $recipients An indexed array with DHT recipients
*/
public function findRecipientsByPackageData (array $packageData) {
- // Get max recipients
- $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
-
// Query get a result instance back from DHT database wrapper.
$resultInstance = $this->getWrapperInstance()->getResultFromExcludedSender($packageData);
+ // Make sure the result instance is valid
+ assert($resultInstance instanceof SearchableResult);
+ assert($resultInstance->valid());
+
// Init array
$recipients = array();
// Add instance to recipient list
array_push($recipients, $current);
+ } // END - while
- // Has the maximum been reached?
- if (count($recipients) == $maxRecipients) {
- // Stop search here
- break;
- } // END - if
+ // Return filled array
+ return $recipients;
+ }
+
+ /**
+ * Finds DHT recipients by given key/value pair
+ *
+ * @param $key Key to search for
+ * @param $value Value to check on found key
+ * @return $recipiens Array with DHT recipients from given key/value pair
+ */
+ public function findRecipientsByKey ($key, $value) {
+ // Look for all suitable nodes
+ $resultInstance = $this->getWrapperInstance()->getResultFromKeyValue($key, $value);
+
+ // Make sure the result instance is valid
+ assert($resultInstance instanceof SearchableResult);
+ assert($resultInstance->valid());
+
+ // Init array
+ $recipients = array();
+
+ // "Walk" through all entries
+ while ($resultInstance->next()) {
+ // Get current entry
+ $current = $resultInstance->current();
+ } // END - while
+
+ // Add instance to recipient list
+ array_push($recipients, $current);
} // END - while
// Return filled array
$discoveryInstance = new DhtRecipientDiscovery();
// Get a DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Set it here
$discoveryInstance->setDhtInstance($dhtInstance);
* @param $prefix Prefix for DHT class name and registry key
* @return $dhtInstance An instance of a DHT object class
*/
- public static final function createDhtObjectInstance ($prefix) {
+ public static final function createDhtInstance ($prefix) {
// Set instance name
$name = $prefix . '_dht';
parent::__construct($className);
// Get a DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Set it here
$this->setDhtInstance($dhtInstance);
*/
public function handleMessageData (array $messageData, Receivable $packageInstance) {
// Get DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Has this DHT attempted to bootstrap?
if (!$dhtInstance->ifDhtIsBooting()) {
// ... and set it here
$handlerInstance->setNodeInstance($nodeInstance);
+ // Get a DHT instance
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
+
+ // Set the DHT instance here
+ $handlerInstance->setDhtInstance($dhtInstance);
+
// Return the prepared instance
return $handlerInstance;
}
// Set it in configuration (temporarily)
$this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
+
+ /*
+ * Use the DHT instance to get a list of recipients. This means that all
+ * DHT nodes that accept bootstrap requests are read from the DHT
+ * database.
+ */
+ $nodeList = $this->getDhtInstance()->findRecipientsByKey(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPT_BOOTSTRAP, 'Y');
+
+ // Make sure it is an array and has at least one entry
+ assert(is_array($nodeList));
+ assert(count($nodeList) > 0);
+
+ // Set it in configuration
+ $this->getConfigInstance()->setConfigEntry('dht_nodes', base64_encode(serialize($nodeList)));
}
/**
$taskInstance = new NodeDhtBootstrapTask();
// Get a DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Set the DHT instance here
$taskInstance->setDhtInstance($dhtInstance);
$taskInstance = new NodeDhtInitializationTask();
// Get a DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Set the DHT instance here
$taskInstance->setDhtInstance($dhtInstance);
$taskInstance = new NodeDhtPublicationCheckTask();
// Get a DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Set the DHT instance here
$taskInstance->setDhtInstance($dhtInstance);
$taskInstance = new NodeDhtPublicationTask();
// Get a DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Set the DHT instance here
$taskInstance->setDhtInstance($dhtInstance);
$taskInstance = new NodeDhtQueryTask();
// Get a DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Set the DHT instance here
$taskInstance->setDhtInstance($dhtInstance);
parent::__construct(__CLASS__);
// Get a DHT instance
- $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+ $dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Set it here
$this->setDhtInstance($dhtInstance);