]> git.mxchange.org Git - hub.git/commitdiff
Introduced and implemented findNodeBySessionId()
authorRoland Häder <roland@mxchange.org>
Sun, 10 Feb 2013 03:17:27 +0000 (03:17 +0000)
committerRoland Häder <roland@mxchange.org>
Sun, 10 Feb 2013 03:17:27 +0000 (03:17 +0000)
application/hub/interfaces/distributable/class_Distributable.php
application/hub/interfaces/wrapper/class_NodeDhtWrapper.php
application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
application/hub/main/dht/node/class_NodeDhtFacade.php
application/hub/main/tools/class_HubTools.php

index 9ddba1f538bdff89485a30ef66c7b69b95742526..4b1053ccdbbf25ebc7f70b381b21e248e8d2254f 100644 (file)
@@ -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]
index 26b84e2d3981f84eb805130992703566103a03cf..04ab69df9c6a0d00003d695cfb8dabe45dd0335f 100644 (file)
@@ -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]
index 67fd53b26c7a7e949f067552167f538417056502..106b87b36e04d4514c00ee9051649ca78dff7002 100644 (file)
@@ -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]
index dd6d13a1c8a05c299109b3687fa898b92cc8d457..ea908b3f4bba26fc3e54cdcc8c55953b71346d83 100644 (file)
@@ -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]
index ede037985d5386700d275cdd282010366b4815f2..f0ba8beccbc5497fa3f6b5eeecb2d9678f7efd70 100644 (file)
@@ -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=<pre>'.print_r($recipient, true).'</pre>' . 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];
        }
 
        /**