]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/tools/class_HubTools.php
Changed all true/false to TRUE/FALSE respectively as PHP constants are better than...
[hub.git] / application / hub / main / tools / class_HubTools.php
index f0d1baf55ffacc0d2f857e6f91a340585fd397ca..33f1a9d83d8c67f7442ca69d388ae46ffdbb9e4e 100644 (file)
@@ -50,6 +50,12 @@ class HubTools extends BaseHubSystem {
                // Call parent constructor
                parent::__construct(__CLASS__);
 
+               // Get a DHT instance
+               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+
+               // Set it here
+               $this->setDhtInstance($dhtInstance);
+
                // Init salt length
                $this->sessionIdLength = 32 + $this->getConfigInstance()->getConfigEntry('salt_length');
        }
@@ -80,50 +86,52 @@ class HubTools extends BaseHubSystem {
        }
 
        /**
-        * Resolves a session id into an ip:port combination
+        * Resolves a session id into an ip:port combination. The opposite method
+        * is resolveSessionIdByIpPort()
         *
-        * @param       $sessionId      A valid session id
-        * @param       $protocol       Name of the used protocol: TCP/UDP
-        * @return      $recipient      Recipient as ip:port combination
-        * @todo        Rewrite this to use DHT
+        * @param       $sessionId                      A valid session id
+        * @return      $recipientIpPort        Recipient as ip:port combination
         */
-       protected function resolveIpPortBySessionId ($sessionId, $protocol) {
-               // Get a wrapper instance
-               $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
+       protected function resolveIpPortBySessionId ($sessionId) {
+               // Init variable
+               $recipientIpPort = 'invalid:invalid';
 
-               // And ask it for the session id
-               $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId, $protocol);
+               // And ask it for ip:port by given session id
+               $recipient = $this->getDhtInstance()->findNodeLocalBySessionId($sessionId);
 
-               // Is the recipient invalid?
-               if ($recipient == '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_' . strtolower($protocol) . '_listen_port');
+                               $recipientIpPort = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_listen_port');
                        } // END - if
-               } // END - if
+               }
 
                // Return result
-               return $recipient;
+               return $recipientIpPort;
        }
 
        /**
-        * Resolves a ip:port combination into a session id
+        * Resolves a ip:port combination into a session id. The "opposite" method
+        * is resolveIpPortBySessionId().
         *
         * @param       $ipPort         Ip:port combination
-        * @param       $protocol       Name of used protocol (TCP/UDP)
         * @return      $sessionId      Valid session id
-        * @todo        Rewrite this to use DHT
         */
-       public static function resolveSessionIdByIpPort ($ipPort, $protocol) {
-               // Get a wrapper instance
-               $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
+       public static function resolveSessionIdByIpPort ($ipPort) {
+               // Get an own instance
+               $selfInstance = self::getSelfInstance();
 
-               // And ask it for the session id
-               $sessionId = $wrapperInstance->resolveSessionIdByIpPort($ipPort, $protocol);
+               // And ask it for session id by given ip:port
+               $recipient = $selfInstance->getDhtInstance()->findNodeByIpPort($ipPort);
+               die(__METHOD__.':recipient=<pre>'.print_r($recipient, TRUE).'</pre>' . PHP_EOL);
 
                // Return result
                return $sessionId;
@@ -133,12 +141,11 @@ class HubTools extends BaseHubSystem {
         * Resolves given session id into an ip:port combination, if ip:port is set, it won't be translated
         *
         * @param       $sessionId      Session id or ip:port combination
-        * @param       $protocol       Name of the used protocol (TCP/UDP)
         * @return      $recipient      Recipient as ip:port combination
         * @throws      InvalidSessionIdException       If the provided session id is invalid (and no ip:port combination)
         * @throws      NoValidHostnameException        If the provided hostname cannot be resolved into an IP address
         */
-       public static function resolveSessionId ($sessionId, $protocol) {
+       public static function resolveSessionId ($sessionId) {
                // Get an own instance
                $selfInstance = self::getSelfInstance();
 
@@ -148,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[' . __LINE__ . ']: 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[' . __LINE__ . ']: 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[' . __LINE__ . ']: 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[' . __LINE__ . ']: 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?
@@ -176,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[' . __LINE__ . ']: 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[' . __LINE__ . ']: Using internal session id resolver.');
 
                        // Resolve session id into a ip:port combination
-                       $recipient = $selfInstance->resolveIpPortBySessionId($sessionId, $protocol);
+                       $recipient = $selfInstance->resolveIpPortBySessionId($sessionId);
 
                        // Debug message
-                       $selfInstance->debugOutput('HUB-TOOLS: session id ' . $sessionId . ' resolved to ' . $recipient);
+                       self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __LINE__ . ']: Session id ' . $sessionId . ' resolved to ' . $recipient);
                } else {
                        // Invalid session id
                        throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);