* @return $nodeData Node data array
*/
function findNodeBySessionId ($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 $messageArray An array with all minimum message data
+ * @param $handlerInstance An instance of a HandleableMessage class
+ * @return void
+ */
+ function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance);
}
// [EOF]
}
/**
- * Prepares an instance of a StoreableCriteria class with all node data for
- * insert/update queries.
+ * Prepares a "local" instance of a StoreableCriteria class with all node
+ * data for insert/update queries. This data set contains data from *this*
+ * (local) node.
*
* @return $dataSetInstance An instance of a StoreableCriteria class
*/
- private function prepareDataSetInstance () {
+ private function prepareLocalDataSetInstance () {
// Get node/request instances
$nodeInstance = Registry::getRegistry()->getInstance('node');
$requestInstance = ApplicationHelper::getSelfInstance()->getRequestInstance();
assert(!$this->isLocalNodeRegistered());
// Get prepared data set instance
- $dataSetInstance = $this->prepareDataSetInstance();
+ $dataSetInstance = $this->prepareLocalDataSetInstance();
// "Insert" this dataset instance completely into the database
$this->queryInsertDataSet($dataSetInstance);
$searchInstance->setLimit(1);
// Get a prepared dataset instance
- $dataSetInstance = $this->prepareDataSetInstance();
+ $dataSetInstance = $this->prepareLocalDataSetInstance();
// Set search instance
$dataSetInstance->setSearchInstance($searchInstance);
// Return result instance
return $resultInstance;
}
+
+ /**
+ * Registeres a node by given message data.
+ *
+ * @param $messageData An array of all message data
+ * @param $handlerInstance An instance of a HandleableMessage class
+ * @return void
+ */
+ public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance) {
+ // Get a data set instance
+ $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
+
+ // Set primary key (session id)
+ $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID);
+
+ // Add all array elements
+ $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
+
+ // 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 $searchInstance An instance of LocalSearchCriteria class
+ * @return void
+ */
+ public function updateNodeByMessageData (array $messageData, Handleable $handlerInstance, LocalSearchCriteria $searchInstance) {
+ // 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);
+
+ // Add all array elements
+ $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
+
+ // Run the "UPDATE" query
+ $this->queryUpdateDataSet($dataSetInstance);
+ }
}
// [EOF]
// Return node data
return $nodeData;
}
+
+ /**
+ * 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 $messageArray An array with all minimum message data
+ * @param $handlerInstance An instance of a HandleableMessage class
+ * @return void
+ */
+ public function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance) {
+ // Get a search criteria class
+ $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+ // Debug message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE: messageData=' . print_r($messageData, true));
+
+ // Search for the node's session id and external IP/hostname + TCP/UDP listen port
+ foreach ($handlerInstance->getSearchData() as $key) {
+ // Debug message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE: messageData[' . $key . ']=' . $messageData[$key]);
+
+ // Is it there?
+ assert(isset($messageData[$key]));
+
+ // Add criteria
+ $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]);
+ } // END - foreach
+
+ // Only one entry is fine
+ $searchInstance->setLimit(1);
+
+ // Run the query
+ $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
+
+ // Is there already an entry?
+ if ($resultInstance->next()) {
+ // Entry found, so update it
+ $this->getWrapperInstance()->updateNodeByMessageData($messageData, $handlerInstance, $searchInstance);
+ } else {
+ // Nothing found, so register it
+ $this->getWrapperInstance()->registerNodeByMessageData($messageData, $handlerInstance);
+ }
+
+ // Save last exception
+ $handlerInstance->setLastException($this->getWrapperInstance()->getLastException());
+ }
}
// [EOF]
parent::__construct($className);
}
+ /**
+ * Getter for search data array
+ *
+ * @return $searchData Search data array
+ */
+ public final function getSearchData () {
+ return $this->searchData;
+ }
+
/**
* Getter for last exception
*
* @return $lastException Last thrown exception
*/
- protected final function getLastException () {
+ public final function getLastException () {
return $this->lastException;
}
* @param $lastException Last thrown exception
* @return void
*/
- protected final function setLastException (FrameworkException $exceptionInstance = NULL) {
+ public final function setLastException (FrameworkException $exceptionInstance = NULL) {
$this->lastException = $exceptionInstance;
}
*
* @param $messageArray An array with all minimum message data
* @return void
- * @todo Rewrite this to use DHT
*/
protected function registerNodeByMessageData (array $messageData) {
// Check if searchData has entries
- assert(count($this->searchData) > 0);
+ assert(count($this->getSearchData()) > 0);
- // Get a wrapper instance
- $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
-
- // Get a search criteria class
- $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER: messageData=' . print_r($messageData, true));
-
- // Search for the node's session id and external IP/hostname + TCP/UDP listen port
- foreach ($this->searchData as $key) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER: messageData[' . $key . ']=' . $messageData[$key]);
-
- // Is it there?
- assert(isset($messageData[$key]));
-
- // Add criteria
- $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]);
- } // END - foreach
-
- // Only one entry is fine
- $searchInstance->setLimit(1);
-
- // Run the query
- $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
-
- // Is there already an entry?
- if ($resultInstance->next()) {
- // Entry found, so update it
- $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance);
- } else {
- // Nothing found, so register it
- $wrapperInstance->registerNodeByMessageData($messageData, $this);
- }
-
- // Save last exception
- $this->setLastException($wrapperInstance->getLastException());
+ // Let the DHT facade do the work
+ $this->getDhtInstance()->registerNodeByMessageData($messageData, $this);
}
}