From: Roland Häder Date: Sun, 10 Feb 2013 03:17:27 +0000 (+0000) Subject: Introduced and implemented findNodeBySessionId() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4fb0a9cb42950c86303f83288f7c7a90cddcf684;p=hub.git Introduced and implemented findNodeBySessionId() --- diff --git a/application/hub/interfaces/distributable/class_Distributable.php b/application/hub/interfaces/distributable/class_Distributable.php index 9ddba1f53..4b1053ccd 100644 --- a/application/hub/interfaces/distributable/class_Distributable.php +++ b/application/hub/interfaces/distributable/class_Distributable.php @@ -27,7 +27,15 @@ interface Distributable extends FrameworkInterface { * * @return void */ - function initDht(); + function initDht (); + + /** + * Finds a node by given session id + * + * @param $sessionId Session id to lookup + * @return $nodeData Node data array + */ + function findNodeBySessionId ($sessionId); } // [EOF] diff --git a/application/hub/interfaces/wrapper/class_NodeDhtWrapper.php b/application/hub/interfaces/wrapper/class_NodeDhtWrapper.php index 26b84e2d3..04ab69df9 100644 --- a/application/hub/interfaces/wrapper/class_NodeDhtWrapper.php +++ b/application/hub/interfaces/wrapper/class_NodeDhtWrapper.php @@ -44,6 +44,14 @@ interface NodeDhtWrapper extends DatabaseWrapper { * @return void */ function updateLocalNode (); + + /** + * Finds a node by given session id + * + * @param $sessionId Session id to lookup + * @return $nodeData Node data array + */ + function findNodeBySessionId ($sessionId); } // [EOF] diff --git a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php index 67fd53b26..106b87b36 100644 --- a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -162,6 +162,30 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem // Update DHT database record $this->queryUpdateDataSet($dataSetInstance); } + + /** + * Finds a node by given session id + * + * @param $sessionId Session id to lookup + * @return $nodeData Node data array + */ + public function findNodeBySessionId ($sessionId) { + // Get search criteria + $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + + // Get node instance + $nodeInstance = Registry::getRegistry()->getInstance('node'); + + // Search for session id and limit it to one entry + $searchInstance->addCriteria(self::DB_COLUMN_SESSION_ID, $nodeInstance->getSessionId()); + $searchInstance->setLimit(1); + + // Query database and get a result instance back + $resultInstance = $this->doSelectByCriteria($searchInstance); + + // Return result instance + return $resultInstance; + } } // [EOF] diff --git a/application/hub/main/dht/node/class_NodeDhtFacade.php b/application/hub/main/dht/node/class_NodeDhtFacade.php index dd6d13a1c..ea908b3f4 100644 --- a/application/hub/main/dht/node/class_NodeDhtFacade.php +++ b/application/hub/main/dht/node/class_NodeDhtFacade.php @@ -67,6 +67,29 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable { $this->getWrapperInstance()->registerLocalNode(); } } + + /** + * Finds a node by given session id + * + * @param $sessionId Session id to lookup + * @return $nodeData Node data array + */ + public function findNodeBySessionId ($sessionId) { + // Default is empty data array + $nodeData = array(); + + // Call the wrapper to do the job and get back a result instance + $resultInstance = $this->getWrapperInstance()->findNodeBySessionId($sessionId); + + // Is the next entry valid? + if ($resultInstance->next()) { + // Then load the entry + $nodeData = $resultInstance->current(); + } // END - if + + // Return node data + return $nodeData; + } } // [EOF] diff --git a/application/hub/main/tools/class_HubTools.php b/application/hub/main/tools/class_HubTools.php index ede037985..f0ba8becc 100644 --- a/application/hub/main/tools/class_HubTools.php +++ b/application/hub/main/tools/class_HubTools.php @@ -92,10 +92,9 @@ class HubTools extends BaseHubSystem { // And ask it for ip:port by given session id $recipient = $dhtInstance->findNodeBySessionId($sessionId); - die(__METHOD__.':recipient=
'.print_r($recipient, true).'
' . PHP_EOL); // Is the recipient invalid? - if ($recipient == 'invalid:invalid') { + if ((!isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_IP_PORT])) || ($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_IP_PORT] == 'invalid:invalid')) { // Get the instance, this might throw a NPE $nodeInstance = Registry::getRegistry()->getInstance('node'); @@ -106,8 +105,16 @@ class HubTools extends BaseHubSystem { } // END - if } // END - if + // Is $recipient an array? + if ((!is_array($recipient)) || (!isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_IP_PORT]))) { + // Fake array with invalid data + $recipient = array( + NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_IP_PORT => 'invalid:invalid' + ); + } // END - if + // Return result - return $recipient; + return $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_IP_PORT]; } /**