]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/tools/console/class_ConsoleTools.php
Continued:
[core.git] / framework / main / classes / tools / console / class_ConsoleTools.php
index 02d298327d6066a9121c15c47373f4c69ffac608..e9d2fe5afaac52dde296d62c8f477a702dc0c74d 100644 (file)
@@ -47,6 +47,11 @@ class ConsoleTools extends BaseFrameworkSystem {
         */
        private static $isQuietResolver = FALSE;
 
+       /**
+        * Cached values
+        */
+       private static $cache = [];
+
        /**
         * Protected constructor
         *
@@ -269,61 +274,109 @@ class ConsoleTools extends BaseFrameworkSystem {
        }
 
        /**
-        * Aquires the IP address of this host by reading the /etc/hostname file
-        * and solving it. It is now stored in configuration
+        * acquires this host's LAN name. It tries a varity of different source.
         *
-        * @return      $ipAddress      Aquired IPv4 address
+        * @return      $hostName       The acquired name of this host or something invalid
         */
-       public static function acquireSelfIpAddress () {
-               // Local IP by default
-               $ipAddress = '127.0.0.1';
+       public static function acquireHostname () {
+               // Is cache there?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!');
+               if (!isset(self::$cache[__METHOD__])) {
+                       // Get a new instance
+                       $toolsInstance = new ConsoleTools();
 
-               // Get a new instance
-               $helperInstance = new ConsoleTools();
+                       // Get SplFileInfo instance
+                       $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('hostname_file'));
 
-               // Get SplFileInfo instance
-               $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('hostname_file'));
+                       // Init host name
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: infoInstance=%s', $infoInstance->__toString()));
+                       $hostname = 'host.invalid';
 
-               try {
-                       // Get a file pointer
-                       $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_class', array($infoInstance));
+                       // Try to check /etc/hostname first
+                       try {
+                               // Get a file pointer
+                               $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_class', [$infoInstance]);
 
-                       // Read the file
-                       $rawData = trim($fileInstance->readFromFile());
+                               // Read the file
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: fileInstance=%s', $fileInstance->__toString()));
+                               $rawData = trim($fileInstance->readFromFile());
 
-                       // Close the file
-                       $fileInstance->closeFile();
+                               // Close the file
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: rawData=%s', $rawData));
+                               $fileInstance->closeFile();
 
-                       // Extract hostname from it
-                       $hostname = $helperInstance->extractHostnameFromRawData($rawData);
+                               // Extract hostname from it
+                               $hostname = $toolsInstance->extractHostnameFromRawData($rawData);
 
-                       // Resolve the IP number
-                       $ipAddress = self::resolveIpAddress($hostname);
-               } catch (FileNotFoundException $e) {
-                       // Fall-back to 'SESSION_SVR' which found on my Sun Station
-                       if (isset($_SERVER['SESSION_SVR'])) {
-                               // Resolve it
-                               $ipAddress = self::resolveIpAddress($_SERVER['SESSION_SVR']);
-                       } elseif (isset($_SERVER['COMPUTERNAME'])) {
-                               // May happen on some Windows XP systems, so also try this
-                               $ipAddress = self::resolveIpAddress($_SERVER['COMPUTERNAME']);
-                       } else {
-                               // Could not find our hostname
-                               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] WARNING: Cannot resolve my own IP address.',
-                                       $helperInstance->__toString()
+                               // Debug message
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from /etc/hostname', $hostname));
+                       } catch (FileNotFoundException $e) {
+                               // Fall-back to 'SESSION_SVR' which found on my Sun Station
+                               if (isset($_SERVER['SESSION_SVR'])) {
+                                       // Resolve it
+                                       $hostname = $_SERVER['SESSION_SVR'];
+                               } elseif (isset($_SERVER['COMPUTERNAME'])) {
+                                       // May happen on some Windows XP systems, so also try this
+                                       $hostname = $_SERVER['COMPUTERNAME'];
+                               } else {
+                                       // Could not find our hostname
+                                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] WARNING: Cannot acquire my own host name.',
+                                               $toolsInstance->__toString()
+                                       ));
+                               }
+
+                               // Debug message
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from _SERVER array', $hostname));
+                       } catch (FrameworkException $e) {
+                               // Output debug message
+                               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Problem while resolving own IP address: [%s|%s]:%s',
+                                       $toolsInstance->__toString(),
+                                       $e->__toString(),
+                                       $e->getHexCode(),
+                                       $e->getMessage()
                                ));
                        }
-               } catch (FrameworkException $e) {
-                       // Output debug message
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Problem while resolving own IP address: [%s|%s]:%s',
-                               $helperInstance->__toString(),
-                               $e->__toString(),
-                               $e->getHexCode(),
-                               $e->getMessage()
-                       ));
+
+                       // Set cache
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Setting hostname=%s ...', $hostname));
+                       self::$cache[__METHOD__] = $hostname;
+               } else {
+                       // Get from cache
+                       $hostname = self::$cache[__METHOD__];
+               }
+
+               // Return it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: hostname=%s - EXIT!', $hostname));
+               return $hostname;
+       }
+
+       /**
+        * acquires the IP address of this host by reading the /etc/hostname file
+        * and solving it. It is now stored in configuration
+        *
+        * @return      $ipAddress      acquired IPv4 address
+        */
+       public static function acquireSelfIpAddress () {
+               // Is cache there?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!');
+               if (!isset(self::$cache[__METHOD__])) {
+                       // Get host name
+                       $hostname = self::acquireHostname();
+
+                       // Resolve the IP number
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s', $hostname));
+                       $ipAddress = self::resolveIpAddress($hostname);
+
+                       // Set cache
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Setting ipAddress=%s ...', $ipAddress));
+                       self::$cache[__METHOD__] = $ipAddress;
+               } else {
+                       // Get it from cache
+                       $ipAddress = self::$cache[__METHOD__];
                }
 
                // Return it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: ipAddress=%s - EXIT!', $ipAddress));
                return $ipAddress;
        }
 
@@ -346,13 +399,13 @@ class ConsoleTools extends BaseFrameworkSystem {
         */
        public static function determineExternalAddress () {
                // Get helper instance
-               $helperInstance = new ConsoleTools();
+               $toolsInstance = new ConsoleTools();
 
                // First get a socket
                // @TODO Add some DNS caching here
 
                // Open connection
-               if ($helperInstance->isProxyUsed() === true) {
+               if ($toolsInstance->isProxyUsed() === true) {
                        // Resolve hostname into IP address
                        $ipAddress = self::resolveIpAddress(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_host'));
 
@@ -366,26 +419,26 @@ class ConsoleTools extends BaseFrameworkSystem {
                // Check if there was an error else
                if ($errorNo > 0) {
                        // Then throw again
-                       throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), BaseFrameworkSystem::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($toolsInstance, $socketResource, $errorNo, $errorStr), BaseFrameworkSystem::EXCEPTION_INVALID_SOCKET);
                }
 
                // Prepare the GET request
-               $request  = 'GET ' . ($helperInstance->isProxyUsed() === true ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL;
+               $request  = 'GET ' . ($toolsInstance->isProxyUsed() === true ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL;
                $request .= 'Host: shipsimu.org' . self::HTTP_EOL;
                $request .= 'User-Agent: ' . self::HTTP_USER_AGENT . self::HTTP_EOL;
                $request .= 'Connection: close' . self::HTTP_EOL;
 
                // Do we use proxy?
-               if ($helperInstance->isProxyUsed() === true) {
+               if ($toolsInstance->isProxyUsed() === true) {
                        // CONNECT method?
                        if (FrameworkBootstrap::getConfigurationInstance()->isEnabled('proxy_connect_method')) {
                                // Setup proxy tunnel
-                               $response = $helperInstance->setupProxyTunnel('shipsimu.org', 80, $socketResource);
+                               $response = $toolsInstance->setupProxyTunnel('shipsimu.org', 80, $socketResource);
 
                                // If the response is invalid, abort
                                if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) {
                                        // Invalid response!
-                                       $helperInstance->debugBackTrace('Proxy tunnel not working: response=' . print_r($response, true));
+                                       $toolsInstance->debugBackTrace('Proxy tunnel not working: response=' . print_r($response, true));
                                }
                        } else {
                                // Add header for proxy