]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/tools/class_HubTools.php
Updated 'core'.
[hub.git] / application / hub / main / tools / class_HubTools.php
index c2c6197e58e866e46e0250b2f466884b2aca7330..988fa7a4d2ece5a8f8f40da31fb47e5891aef722 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -94,16 +94,16 @@ class HubTools extends BaseHubSystem {
         */
        protected function resolveUniversalNodeLocatorBySessionId ($sessionId) {
                // Init variable
-               die(__METHOD__ . ': Unfinished' . PHP_EOL);
                $recipientUniversalNodeLocator = 'invalid://invalid:invalid';
 
                // And ask it for Universal Node Locator by given session id
                $recipient = $this->getDhtInstance()->findNodeLocalBySessionId($sessionId);
+               //* DEBUG-DIE: */ die(__METHOD__ . ': UNFINISHED: recipient[' . gettype($recipient) . ']=' . print_r($recipient, TRUE) . ',sessionId=' . $sessionId . PHP_EOL);
 
                // Is the recipient valid?
-               if ((isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_IP])) && (isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_LISTEN_PORT]))) {
+               if (isset($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS])) {
                        // Then use this
-                       $recipientUniversalNodeLocator = $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_IP] . ':' . $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_LISTEN_PORT];
+                       $recipientUniversalNodeLocator = $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS];
                } else {
                        // Get the instance, this might throw a NPE
                        $nodeInstance = NodeObjectFactory::createNodeInstance();
@@ -111,7 +111,7 @@ class HubTools extends BaseHubSystem {
                        // Is the session id the same?
                        if ($nodeInstance->getSessionId() == $sessionId) {
                                // Then get an instance of a LocateableNode class from it, assume TCP by default
-                               $recipientUniversalNodeLocator = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_listen_port');
+                               $recipientUniversalNodeLocator = self::determineOwnExternalAddress() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_listen_port');
                        } // END - if
                }
 
@@ -119,6 +119,27 @@ class HubTools extends BaseHubSystem {
                return $recipientUniversalNodeLocator;
        }
 
+       /**
+        * Resolves a session id into a node id by asking local DHT.
+        *
+        * @param       $sessionId      Session id
+        * @return      $nodeId         Node id
+        */
+       public static function resolveNodeIdBySessionId ($sessionId) {
+               // Get an own instance
+               $selfInstance = self::getSelfInstance();
+
+               // And ask it for session id by given Universal Node Locator
+               $nodeData = $selfInstance->getDhtInstance()->findNodeLocalBySessionId($sessionId);
+
+               // Make sure the node id is there
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: sessionId=' . $sessionId . ', nodeData[' . gettype($nodeData) . ']=' . print_r($nodeData, TRUE));
+               assert(isset($nodeData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID]));
+
+               // Return it
+               return $nodeData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID];
+       }
+
        /**
         * Resolves a Universal Node Locator into a session id. The "opposite" method
         * is resolveUniversalNodeLocatorBySessionId().
@@ -186,44 +207,115 @@ class HubTools extends BaseHubSystem {
        }
 
        /**
-        * Determine IP or 'external_ip' if set
+        * Determine UNL or 'external_address' if set
         *
-        * @return      $ip             The determined external ip of this node
+        * @return      $unl    The determined external UNL of this node
         */
-       public static function determineOwnExternalIp () {
-               // Is the external_ip config entry set?
-               if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_ip') != '') {
-                       // Use it as external ip
-                       $ip = FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_ip');
+       public static function determineOwnExternalAddress () {
+               // Is the external_address config entry set?
+               if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_address') != '') {
+                       // Use it as external address
+                       $unl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('external_address');
                } else {
-                       // Determine own external ip by connecting to my (coder) server at 188.138.90.169
-                       $ip = ConsoleTools::determineExternalIp();
+                       // Determine own external address by connecting to my (coder) server at 188.138.90.169
+                       $unl = self::determineExternalUniversalNodeLocator();
                }
 
                // Return it
-               return $ip;
+               return $unl;
        }
 
        /**
-        * Determine IP or 'internal_ip' if set
+        * Determine UNL or 'internal_address' if set
         *
-        * @return      $ip             The determined external ip of this node
+        * @return      $unl    The determined internal UNL of this node
         */
-       public static function determineOwnInternalIp () {
-               // Is the internal_ip config entry set?
-               if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('allow_publish_internal_ip') == 'N') {
-                       // Not allowed to publish internal IP, so use external
-                       $ip = self::determineOwnExternalIp();
-               } elseif (FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_ip') != '') {
-                       // Use it as internal ip
-                       $ip = FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_ip');
+       public static function determineOwnInternalAddress () {
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
+
+               // Is the internal_address config entry set?
+               if (FrameworkConfiguration::getSelfInstance()->getConfigEntry('allow_publish_internal_address') == 'N') {
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: Calling self::determineOwnExternalAddress() as allow_publish_internal_address=N is set ...');
+
+                       // Not allowed to publish internal address, so use external
+                       $unl = self::determineOwnExternalAddress();
+               } elseif (FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_address') != '') {
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: Getting config entry internal_address ...');
+
+                       // Use it as internal address
+                       $unl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('internal_address');
                } else {
-                       // Determine own internal ip by connecting to my (coder) server at 188.138.90.169
-                       $ip = ConsoleTools::acquireSelfIPAddress();
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: Calling self::determineInternalUniversalNodeLocator() ...');
+
+                       // Determine own internal address
+                       $unl = self::determineInternalUniversalNodeLocator();
                }
 
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - EXIT!');
+
+               // Return it
+               return $unl;
+       }
+
+       /**
+        * Determines the UNL (Universal Node Locator) for the internal address
+        *
+        * @return      $internalUnl    Internal UNL
+        */
+       public static function determineInternalUniversalNodeLocator () {
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
+
+               // Is there cache? (This shortens a lot calls)
+               if (!isset($GLOBALS[__METHOD__])) {
+                       // Determine UNL based on this node:
+                       // 1) Get discovery class
+                       $discoveryInstance = ObjectFactory::createObjectByConfiguredName('unl_discovery_class');
+
+                       // 2) "Determine" it
+                       $GLOBALS[__METHOD__] = $discoveryInstance->discoverUniversalNodeLocatorByConfiguredAddress('internal');
+
+                       // Make sure it is valid
+                       // @TODO Find a better validation than empty()
+                       assert(!empty($GLOBALS[__METHOD__]));
+               } // END - if
+
+               // Return it
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $GLOBALS[__METHOD__] . ' - EXIT!');
+               return $GLOBALS[__METHOD__];
+       }
+
+       /**
+        * Determines the UNL (Universal Node Locator) for the external address
+        *
+        * @return      $externalUnl    External UNL
+        */
+       public static function determineExternalUniversalNodeLocator () {
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
+
+               // Is there cache? (This shortens a lot calls)
+               if (!isset($GLOBALS[__METHOD__])) {
+                       // Determine UNL based on this node:
+                       // 1) Get discovery class
+                       $discoveryInstance = ObjectFactory::createObjectByConfiguredName('unl_discovery_class');
+
+                       // 2) "Determine" it
+                       $GLOBALS[__METHOD__] = $discoveryInstance->discoverUniversalNodeLocatorByConfiguredAddress('external');
+
+                       // Make sure it is valid
+                       // @TODO Find a better validation than empty()
+                       assert(!empty($GLOBALS[__METHOD__]));
+               } // END - if
+
                // Return it
-               return $ip;
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $GLOBALS[__METHOD__] . ' - EXIT!');
+               return $GLOBALS[__METHOD__];
        }
 }