]> git.mxchange.org Git - hub.git/commitdiff
Some rewrites towards more flexible code:
authorRoland Häder <roland@mxchange.org>
Tue, 12 Feb 2013 05:37:42 +0000 (05:37 +0000)
committerRoland Häder <roland@mxchange.org>
Tue, 12 Feb 2013 05:37:42 +0000 (05:37 +0000)
- Got rid of 'ip_port' field and converted it into 'external_ip' (as it is) and
  'listen_port' fields
- Rewrote private method for session id -> ip:port resolving

application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
application/hub/main/tools/class_HubTools.php

index d880df4b8453e8fa0bb53a81af7b381b30b982f9..dbf3c66c3075d639e82b25f575432afddb0295b3 100644 (file)
@@ -28,7 +28,8 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
        // Constants for database column names
        const DB_COLUMN_NODE_ID          = 'node_id';
        const DB_COLUMN_SESSION_ID       = 'session_id';
-       const DB_COLUMN_IP_PORT          = 'ip_port';
+       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_TYPE        = 'node_type';
@@ -77,9 +78,16 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                // Set the primary key
                $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
 
+               // Get ip:port combination and "explode" it
+               $ipPort = explode(':' . $nodeInstance->getAddressPort());
+
+               // Make sure both is valid
+               assert(($ipPort[0] !== 'invalid') && ($ipPort[1] !== 'invalid'));
+
                // Add public node data
                $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_TYPE       , $requestInstance->getRequestElement('mode'));
-               $dataSetInstance->addCriteria(self::DB_COLUMN_IP_PORT         , $nodeInstance->getAddressPort());
+               $dataSetInstance->addCriteria(self::DB_COLUMN_EXTERNAL_IP     , $ipPort[0]);
+               $dataSetInstance->addCriteria(self::DB_COLUMN_LISTEN_PORT     , $ipPort[1]);
                $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_ID         , $nodeInstance->getNodeId());
                $dataSetInstance->addCriteria(self::DB_COLUMN_SESSION_ID      , $nodeInstance->getSessionId());
                $dataSetInstance->addCriteria(self::DB_COLUMN_PRIVATE_KEY_HASH, $nodeInstance->getPrivateKeyHash());
@@ -103,8 +111,15 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                        // Get node instance
                        $nodeInstance = Registry::getRegistry()->getInstance('node');
 
+                       // Get ip:port combination and "explode" it
+                       $ipPort = explode(':' . $nodeInstance->getAddressPort());
+
+                       // Make sure both is valid
+                       assert(($ipPort[0] !== 'invalid') && ($ipPort[1] !== 'invalid'));
+
                        // Add ip:port as criteria
-                       $searchInstance->addCriteria(self::DB_COLUMN_IP_PORT, $nodeInstance->getAddressPort());
+                       $searchInstance->addCriteria(self::DB_COLUMN_EXTERNAL_IP, $ipPort[0]);
+                       $searchInstance->addCriteria(self::DB_COLUMN_LISTEN_PORT, $ipPort[1]);
                        $searchInstance->setLimit(1);
 
                        // Query database and get a result instance back
index 1d203c8eff26b47ea39391e1aa26dfe76720b130..e2cad84c67f29ab28ff05d45dc23a7d2dde2cf56 100644 (file)
@@ -89,35 +89,33 @@ class HubTools extends BaseHubSystem {
         * Resolves a session id into an ip:port combination. The opposite method
         * is resolveSessionIdByIpPort()
         *
-        * @param       $sessionId      A valid session id
-        * @return      $recipient      Recipient as ip:port combination
+        * @param       $sessionId                      A valid session id
+        * @return      $recipientIpPort        Recipient as ip:port combination
         */
        protected function resolveIpPortBySessionId ($sessionId) {
+               // Init variable
+               $recipientIpPort = 'invalid:invalid';
+
                // And ask it for ip:port by given session id
                $recipient = $this->getDhtInstance()->findNodeBySessionId($sessionId);
 
-               // Is the recipient invalid?
-               if ((!isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_IP_PORT])) || ($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_IP_PORT] == 'invalid:invalid')) {
+               // Is the recipient valid?
+               if ((isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_IP])) && (isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_LISTEN_PORT])) {
+                       // Then use this
+                       $recipientIpPort = $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_IP] . ':' . $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_LISTEN_PORT];
+               } else {
                        // Get the instance, this might throw a NPE
                        $nodeInstance = Registry::getRegistry()->getInstance('node');
 
                        // Is the session id the same?
                        if ($nodeInstance->getSessionId() == $sessionId) {
                                // Then get the ip:port from it, assume TCP by default
-                               $recipient = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_listen_port');
+                               $recipientIpPort = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_listen_port');
                        } // 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[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_IP_PORT];
+               return $recipientIpPort;
        }
 
        /**
@@ -157,25 +155,27 @@ class HubTools extends BaseHubSystem {
                // Does it match a direct ip:port? (hint: see www.regexlib.com for the regular expression)
                if (preg_match('/((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])):([0-9]{3,5})/', $sessionId)) {
                        // Direct ip:port found
-                       $selfInstance->debugOutput('HUB-TOOLS: Direct ip:port ' . $sessionId . ' detected.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS: Direct ip:port ' . $sessionId . ' detected.');
                } elseif (isset($selfInstance->sessionIdCache[$sessionId])) {
                        // Debug message
-                       $selfInstance->debugOutput('HUB-TOOLS: Using entry from sessionIdCache[] array.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS: Using entry from sessionIdCache[] array.');
 
                        // Found in cache!
                        $recipient = $selfInstance->sessionIdCache[$sessionId];
 
                        // Debug message
-                       $selfInstance->debugOutput('HUB-TOOLS: sessionIdCache[' . $sessionId . ']=' . $recipient);
+                       self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS: sessionIdCache[' . $sessionId . ']=' . $recipient);
                } elseif (preg_match('/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}:([0-9]{3,5})/', $sessionId)) {
                        // Hostname:port found
-                       $selfInstance->debugOutput('HUB-TOOLS: hostname:port ' . $sessionId . ' detected.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS: hostname:port ' . $sessionId . ' detected.');
 
                        // Hostname:port found
                        $hostnameArray = explode(':', $sessionId);
 
-                       // Try to resolve it and add port again
-                       // @TODO Please try to encapsulate this PHP call into an own class
+                       /*
+                        * Try to resolve it and add port again
+                        * @TODO Please try to encapsulate this PHP call into an own class
+                        */
                        $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1];
 
                        // Is it valid?
@@ -185,16 +185,16 @@ class HubTools extends BaseHubSystem {
                        } // END - if
 
                        // Debug message
-                       $selfInstance->debugOutput('HUB-TOOLS: hostname:port ' . $sessionId . ' resolved to ' . $recipient);
+                       self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS: hostname:port ' . $sessionId . ' resolved to ' . $recipient);
                } elseif (preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) {
                        // Debug message
-                       $selfInstance->debugOutput('HUB-TOOLS: Using internal session id resolver.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS: Using internal session id resolver.');
 
                        // Resolve session id into a ip:port combination
                        $recipient = $selfInstance->resolveIpPortBySessionId($sessionId);
 
                        // Debug message
-                       $selfInstance->debugOutput('HUB-TOOLS: session id ' . $sessionId . ' resolved to ' . $recipient);
+                       self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS: Session id ' . $sessionId . ' resolved to ' . $recipient);
                } else {
                        // Invalid session id
                        throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);