// CFG: NODE-DHT-CLASS
$cfg->setConfigEntry('node_dht_class', 'NodeDhtFacade');
+// CFG: MAX-DHT-RECIPIENTS
+$cfg->setConfigEntry('max_dht_recipients', 10);
+
///////////////////////////////////////////////////////////////////////////////
// Node states
///////////////////////////////////////////////////////////////////////////////
*/
function bootstrapDht ();
- /**
- * Finds a node locally by given session id
- *
- * @param $sessionId Session id to lookup
- * @return $nodeData Node-data array
- */
- function findNodeLocalBySessionId ($sessionId);
-
- /**
- * Registers an other node with this node by given message data. The
- * following data must always be present:
- *
- * - session-id (for finding the node's record together with below data)
- * - external-ip (hostname or IP number)
- * - listen-port (TCP/UDP listen port for inbound connections)
- *
- * @param $messageData An array with all minimum message data
- * @param $handlerInstance An instance of a Handleable class
- * @param $forceUpdate Optionally force update, don't register (default: register if not found)
- * @return void
- */
- function registerNodeByMessageData (array $messageData, Handleable $handlerInstance, $forceUpdate = FALSE);
-
- /**
- * Queries the local DHT data(base) for a node list with all supported
- * object types except the node by given session id.
- *
- * @param $messageData An array with message data from a node_list request
- * @param $handlerInstance An instance of a Handleable class
- * @param $excludeKey Array key which should be excluded
- * @param $andKey Array of $separator-separated list of elements which all must match
- * @param $separator Sepator char (1st parameter for explode() call)
- * @return $nodeList An array with all found nodes
- */
- function queryLocalNodeListExceptByMessageData (array $messageData, Handleable $handlerInstance, $excludeKey, $andKey, $separator);
-
- /**
- * Inserts given node list array (from earlier database result produced by
- * an other node) into the DHT. This array origins from above method
- * queryLocalNodeListExceptByMessageData().
- *
- * @param $nodeList An array from an earlier database result instance
- * @return void
- */
- function insertNodeList (array $nodeList);
-
/**
* Updates/refreshes DHT data (e.g. status).
*
*/
function updateDhtData ();
- /**
- * Checks whether there are entries in "INSERT" node data stack
- *
- * @return $isPending Whether there are pending entries
- */
- function ifInsertNodeDataPending ();
-
- /**
- * Inserts a single entry of node data into the DHT
- *
- * @return void
- */
- function insertSingleNodeData ();
-
/**
* Checks whether there are unpublished entries
*
* @return void
*/
function publishEntry ();
+
+ /**
+ * Find recipients for given package data
+ *
+ * @param $packageData An array of valid package data
+ * @return $recipients An indexed array with DHT recipients
+ */
+ function findRecipientsByPackageData (array $packageData);
}
// [EOF]
--- /dev/null
+Deny from all
--- /dev/null
+<?php
+/**
+ * An interface for DHTs
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
+ * @license GNU GPL 3.0 or any newer version
+ * @link http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+interface DistributableNode extends Distributable {
+ /**
+ * Finds a node locally by given session id
+ *
+ * @param $sessionId Session id to lookup
+ * @return $nodeData Node-data array
+ */
+ function findNodeLocalBySessionId ($sessionId);
+
+ /**
+ * Registers an other node with this node by given message data. The
+ * following data must always be present:
+ *
+ * - session-id (for finding the node's record together with below data)
+ * - external-ip (hostname or IP number)
+ * - listen-port (TCP/UDP listen port for inbound connections)
+ *
+ * @param $messageData An array with all minimum message data
+ * @param $handlerInstance An instance of a Handleable class
+ * @param $forceUpdate Optionally force update, don't register (default: register if not found)
+ * @return void
+ */
+ function registerNodeByMessageData (array $messageData, Handleable $handlerInstance, $forceUpdate = FALSE);
+
+ /**
+ * Queries the local DHT data(base) for a node list with all supported
+ * object types except the node by given session id.
+ *
+ * @param $messageData An array with message data from a node_list request
+ * @param $handlerInstance An instance of a Handleable class
+ * @param $excludeKey Array key which should be excluded
+ * @param $andKey Array of $separator-separated list of elements which all must match
+ * @param $separator Sepator char (1st parameter for explode() call)
+ * @return $nodeList An array with all found nodes
+ */
+ function queryLocalNodeListExceptByMessageData (array $messageData, Handleable $handlerInstance, $excludeKey, $andKey, $separator);
+
+ /**
+ * Inserts given node list array (from earlier database result produced by
+ * an other node) into the DHT. This array origins from above method
+ * queryLocalNodeListExceptByMessageData().
+ *
+ * @param $nodeList An array from an earlier database result instance
+ * @return void
+ */
+ function insertNodeList (array $nodeList);
+
+ /**
+ * Checks whether there are entries in "INSERT" node data stack
+ *
+ * @return $isPending Whether there are pending entries
+ */
+ function ifInsertNodeDataPending ();
+
+ /**
+ * Checks whether there are unpublished entries
+ *
+ * @return $hasUnpublished Whether there are unpublished entries
+ */
+ function hasUnpublishedEntries ();
+
+ /**
+ * Initializes publication of DHT entries. This does only prepare
+ * publication. The next step is to pickup such prepared entries and publish
+ * them by uploading to other (recently appeared) DHT members.
+ *
+ * @return void
+ */
+ function initEntryPublication ();
+
+ /**
+ * Checks whether there are entries pending publication
+ *
+ * @return $isPending Whether there are entries pending publication
+ */
+ function hasEntriesPendingPublication ();
+
+ /**
+ * Publishes next entry found in stack. This method shall also update the
+ * corresponding dabase entry.
+ *
+ * @return void
+ */
+ function publishEntry ();
+}
+
+// [EOF]
+?>
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
interface NodeDhtWrapper extends DatabaseWrapper {
+ /**
+ * Static getter for an array of all DHT database entries
+ *
+ * @return $elements All elements for the DHT dabase
+ */
+ static function getAllElements ();
+
/**
* Getter for result instance for unpublished entries
*
function initEntryPublication ();
/**
- * Removes non-public data from given array.
+ * Removes non-data from given array.
+ *
+ * @param $data An array with possible non-data that needs to be removed.
+ * @return $data A cleaned up array with only data.
+ */
+ function removeNonPublicDataFromArray(array $data);
+
+ /**
+ * Find recipients for given package data and returns it as a result instance
*
- * @param $data An array with possible non-public data that needs to be removed.
- * @return $data A cleaned up array with only public data.
+ * @param $packageData An array of valid package data
+ * @return $recipients An indexed array with DHT recipients
*/
- function removeNonPublicDataFromArray (array $data);
+ function getResultFromExcludedSender (array $packageData);
}
// [EOF]
// Return cleaned data
return $data;
}
+
+ /**
+ * Find recipients for given package data and exclude the sender
+ *
+ * @param $packageData An array of valid package data
+ * @return $recipients An indexed array with DHT recipients
+ */
+ public function getResultFromExcludedSender (array $packageData) {
+ // Assert on required array field
+ assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
+
+ // 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]);
+
+ // Get a result instance back from DHT database wrapper.
+ $resultInstance = $this->doSelectByCriteria($searchInstance);
+
+ // Return result instance
+ return $resultInstance;
+ }
}
// [EOF]
$this->getStackerInstance()->pushNamed(self::STACKER_NAME_INSERT_NODE, $nodeData);
} // END - foreach
}
+
+ /**
+ * Find recipients for given package data
+ *
+ * @param $packageData An array of valid package data
+ * @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);
+
+ // Init array
+ $recipients = array();
+
+ // Search for all recipients
+ while ($resultInstance->next()) {
+ // Get current entry
+ $current = $resultInstance->current();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: current=' . print_r($current, TRUE));
+
+ // Add instance to recipient list
+ array_push($recipients, $current);
+
+ // Has the maximum been reached?
+ if (count($recipients) == $maxRecipients) {
+ // Stop search here
+ break;
+ } // END - if
+ } // END - while
+
+ // Return filled array
+ return $recipients;
+ }
}
// [EOF]
// Get an instance of this class
$discoveryInstance = new DhtRecipientDiscovery();
+ // Get a DHT instance
+ $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+
+ // Set it here
+ $discoveryInstance->setDhtInstance($dhtInstance);
+
// Return the prepared instance
return $discoveryInstance;
}
*
* @param $packageData Valid package data array
* @return $recipients An indexed array with DHT recipients
- * @todo 0% done
*/
public function resolveRecipientsByPackageData (array $packageData) {
- $this->partialStub('Please implement this method. packageData[]=' . print_r($packageData, TRUE));
+ // Use facade to get recipients back
+ $recipients = $this->getDhtInstance()->findRecipientsByPackageData($packageData);
+
+ // Return it
+ return $recipients;
}
}
* @return $dhtInstance An instance of a DHT object class
*/
public static final function createDhtObjectInstance ($prefix) {
+ // Set instance name
+ $name = $prefix . '_dht';
+
// If there is no handler?
- if (Registry::getRegistry()->instanceExists($prefix . '_dht')) {
+ if (Registry::getRegistry()->instanceExists($name)) {
// Get handler from registry
- $dhtInstance = Registry::getRegistry()->getInstance($prefix . '_dht');
+ $dhtInstance = Registry::getRegistry()->getInstance($name);
} else {
// Get the handler instance
- $dhtInstance = self::createObjectByConfiguredName($prefix . '_dht_class');
+ $dhtInstance = self::createObjectByConfiguredName($name . '_class');
// Add it to the registry
- Registry::getRegistry()->addInstance($prefix . '_dht', $dhtInstance);
+ Registry::getRegistry()->addInstance($name, $dhtInstance);
}
// Return the instance
* @param $packageData Valid package data array
* @return void
* @throws FrameworkException Could throw different exceptions depending on implementation
- * @todo 0% done
+ * @todo 30% done
*/
public function resolveRecipient ($recipient, Listable $listInstance, array $packageData) {
// Make sure the recipient is valid