/**
* A database wrapper for distributed hash tables
*
- * @author Roland Haeder <webmaster@ship-simu.org>
+ * @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.ship-simu.org
+ * @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
const DB_COLUMN_SESSION_ID = 'session_id';
const DB_COLUMN_EXTERNAL_IP = 'external_ip';
const DB_COLUMN_LISTEN_PORT = 'listen_port';
- const DB_COLUMN_PRIVATE_KEY = 'private_key';
const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
const DB_COLUMN_NODE_MODE = 'node_mode';
const DB_COLUMN_ACCEPTED_OBJECTS = 'accepted_object_types';
+ const DB_COLUMN_NODE_LIST = 'node_list';
+
+ // Exception codes
+ const EXCEPTION_NODE_ALREADY_REGISTERED = 0x800;
+ const EXCEPTION_NODE_NOT_REGISTERED = 0x801;
/**
* Protected constructor
return $wrapperInstance;
}
+ /**
+ * Static getter for an array of all DHT database entries
+ *
+ * @return $elements All elements for the DHT dabase
+ */
+ public static final function getAllElements () {
+ // Create array and ...
+ $elements = array(
+ self::DB_COLUMN_NODE_ID,
+ self::DB_COLUMN_SESSION_ID,
+ self::DB_COLUMN_EXTERNAL_IP,
+ self::DB_COLUMN_LISTEN_PORT,
+ self::DB_COLUMN_PRIVATE_KEY_HASH,
+ self::DB_COLUMN_NODE_MODE,
+ self::DB_COLUMN_ACCEPTED_OBJECTS,
+ self::DB_COLUMN_NODE_LIST
+ );
+
+ // ... return it
+ return $elements;
+ }
+
+ /**
+ * Prepares a search instance for given node data
+ *
+ * @param $nodeData An array with valid node data
+ * @return $searchInstance An instance of a SearchCriteria class
+ */
+ private function prepareSearchInstance (array $nodeData) {
+ // Assert on array elements
+ assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+ // Get instance
+ $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+ // Search for node id and limit it to one entry
+ $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]);
+ $searchInstance->setLimit(1);
+
+ // Return it
+ return $searchInstance;
+ }
+
/**
* Prepares a "local" instance of a StoreableCriteria class with all node
* data for insert/update queries. This data set contains data from *this*
// Get an array of all accepted object types
$objectList = $nodeInstance->getListFromAcceptedObjectTypes();
+ // Make sure this is an array
+ assert(is_array($objectList));
+
// Add public node data
$dataSetInstance->addCriteria(self::DB_COLUMN_NODE_MODE , $requestInstance->getRequestElement('mode'));
$dataSetInstance->addCriteria(self::DB_COLUMN_EXTERNAL_IP , $ipPort[0]);
// Get ip:port combination and "explode" it
$ipPort = $nodeInstance->getAddressPortArray();
- // Make sure both is valid
+ /*
+ * Make sure both is not 'invalid' which means that the resolver
+ * didn't work.
+ */
assert(($ipPort[0] !== 'invalid') && ($ipPort[1] !== 'invalid'));
// Add ip:port/node id as criteria
// Query database and get a result instance back
$resultInstance = $this->doSelectByCriteria($searchInstance);
- // Cache result of if there is an entry, next() will tell us if the next entry is valid
- $GLOBALS[__METHOD__] = $resultInstance->next();
+ // Cache result of if there is an entry, valid() will tell us if an entry is there
+ $GLOBALS[__METHOD__] = $resultInstance->valid();
} // END - if
// Return result
* Registeres a node by given message data.
*
* @param $messageData An array of all message data
- * @param $handlerInstance An instance of a HandleableMessage class
+ * @param $handlerInstance An instance of a Handleable class
* @return void
*/
public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance) {
$handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
// Remove 'node_list'
- $dataSetInstance->unsetCriteria('node_list');
+ $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
// Run the "INSERT" query
$this->queryInsertDataSet($dataSetInstance);
* Updates an existing entry in node list
*
* @param $messageData An array of all message data
- * @param $handlerInstance An instance of a HandleableMessage class
+ * @param $handlerInstance An instance of a Handleable class
* @param $searchInstance An instance of LocalSearchCriteria class
* @return void
*/
$handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
// Remove 'node_list'
- $dataSetInstance->unsetCriteria('node_list');
+ $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
+
+ // Run the "UPDATE" query
+ $this->queryUpdateDataSet($dataSetInstance);
+ }
+
+ /**
+ * Determines whether the given node data is already inserted in the DHT
+ *
+ * @param $nodeData An array with valid node data
+ * @return $isRegistered Whether the given node data is already inserted
+ */
+ public function isNodeRegistered (array $nodeData) {
+ // Assert on array elements
+ assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+ // Get search criteria
+ $searchInstance = $this->prepareSearchInstance($nodeData);
+
+ // Query database and get a result instance back
+ $resultInstance = $this->doSelectByCriteria(
+ // Search instance
+ $searchInstance,
+ // Only look for these array elements ("keys")
+ array(
+ self::DB_COLUMN_NODE_ID => TRUE,
+ self::DB_COLUMN_EXTERNAL_IP => TRUE,
+ self::DB_COLUMN_LISTEN_PORT => TRUE,
+ )
+ );
+
+ // Check if there is an entry
+ $isRegistered = $resultInstance->valid();
+
+ // Return registration status
+ return $isRegistered;
+ }
+
+ /**
+ * Registers a node with given data in the DHT. If the node is already
+ * registered this method shall throw an exception.
+ *
+ * @param $nodeData An array with valid node data
+ * @return void
+ * @throws NodeAlreadyRegisteredException If the node is already registered
+ */
+ public function registerNode (array $nodeData) {
+ // Assert on array elements
+ assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+ // Is the node registered?
+ if ($this->isNodeRegistered($nodeData)) {
+ // Throw an exception
+ throw new NodeAlreadyRegisteredException(array($this, $nodeData), self::EXCEPTION_NODE_ALREADY_REGISTERED);
+ } // END - if
+
+ // @TODO Unimplemented part
+ $this->partialStub('nodeData=' . print_r($nodeData, TRUE));
+ }
+
+ /**
+ * Updates a node's entry in the DHT with given data. This will enrich or
+ * just update already exsiting data. If the node is not found this method
+ * shall throw an exception.
+ *
+ * @param $nodeData An array with valid node data
+ * @return void
+ * @throws NodeDataMissingException If the node's data is missing
+ */
+ public function updateNode (array $nodeData) {
+ // Assert on array elements
+ assert(isset($nodeData[self::DB_COLUMN_NODE_ID]));
+
+ // Is the node registered?
+ if (!$this->isNodeRegistered($nodeData)) {
+ // No, then throw an exception
+ throw new NodeDataMissingException(array($this, $nodeData), self::EXCEPTION_NODE_NOT_REGISTERED);
+ } // END - if
+
+ // Get a search instance
+ $searchInstance = $this->prepareSearchInstance($nodeData);
+
+ // Get a data set instance
+ $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
+
+ // Add search instance
+ $dataSetInstance->setSearchInstance($searchInstance);
+
+ // Set primary key (session id)
+ $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
+
+ // Get node instance
+ $nodeInstance = Registry::getRegistry()->getInstance('node');
+
+ // Add all array elements
+ $nodeInstance->addArrayToDataSet($dataSetInstance, $nodeData);
+
+ // Remove 'node_list'
+ $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST);
// Run the "UPDATE" query
$this->queryUpdateDataSet($dataSetInstance);