]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/tools/class_HubTools.php
Rewrites, some more methods:
[hub.git] / application / hub / main / tools / class_HubTools.php
index 409739ec70fbbdf1cee2ffbd186ed922c42bd047..e69c4f7dcde1beacc24676c31e68dbbde5323015 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class HubTools extends BaseFrameworkSystem {
+class HubTools extends BaseHubSystem {
        // Constants for exceptions
        const EXCEPTION_SESSION_ID_IS_INVALID = 0x200;
        const EXCEPTION_HOSTNAME_NOT_FOUND    = 0x201;
@@ -39,7 +39,7 @@ class HubTools extends BaseFrameworkSystem {
        /**
         * Self instance
         */
-       private static $selfInstance = null;
+       private static $selfInstance = NULL;
 
        /**
         * Protected constructor
@@ -59,7 +59,7 @@ class HubTools extends BaseFrameworkSystem {
         *
         * @retuen      $selfInstance   An instance of this class
         */
-       public static final function getInstance () {
+       public static final function getSelfInstance () {
                // Is the instance set
                if (is_null(self::$selfInstance)) {
                        // Then set it
@@ -80,77 +80,163 @@ class HubTools extends BaseFrameworkSystem {
        }
 
        /**
-        * 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
         */
-       protected function resolveIpPortBySessionId ($sessionId) {
-               // Get a wrapper instance
-               $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
-
-               // And ask it for the session id
-               $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId);
+       protected function resolveIpPortBySessionId ($sessionId, $protocol) {
+               // Get a DHT instance
+               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+
+               // And ask it for ip:port by given session id
+               $recipient = $dhtInstance->findNodeBySessionId($sessionId, $protocol);
+               die(__METHOD__.':recipient=<pre>'.print_r($recipient, true).'</pre>' . PHP_EOL);
+
+               // Is the recipient invalid?
+               if ($recipient == 'invalid:invalid') {
+                       // 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');
+                       } // END - if
+               } // END - if
 
                // Return result
                return $recipient;
        }
 
+       /**
+        * 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
+        */
+       public static function resolveSessionIdByIpPort ($ipPort, $protocol) {
+               // Get a DHT instance
+               $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
+
+               // And ask it for session id by given ip:port
+               $recipient = $dhtInstance->findNodeByIpPort($ipPort, $protocol);
+               die(__METHOD__.':recipient=<pre>'.print_r($recipient, true).'</pre>' . PHP_EOL);
+
+               // Return result
+               return $sessionId;
+       }
+
        /**
         * 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) {
+       public static function resolveSessionId ($sessionId, $protocol) {
                // Get an own instance
-               $selfInstance = self::getInstance();
+               $selfInstance = self::getSelfInstance();
 
                // Default is direct ip:port
                $recipient = $sessionId;
 
                // Does it match a direct ip:port? (hint: see www.regexlib.com for the regular expression)
-               if (!preg_match('/((?:2[0-5]{2}|1\d{2}|[1-9]\d|[1-9])\.(?:(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)\.){2}(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)):(\d|[1-9]\d|[1-9]\d{2,3}|[1-5]\d{4}|6[0-4]\d{3}|654\d{2}|655[0-2]\d|6553[0-5])/', $sessionId)) {
-                       // Is it in cache?
-                       if (isset($selfInstance->sessionIdCache[$sessionId])) {
-                               // Debug message
-                               $selfInstance->debugOutput('HUB-TOOLS: Using entry from sessionIdCache[] array.');
-
-                               // Then use it
-                               $recipient = $selfInstance->sessionIdCache[$sessionId];
-                       } elseif (preg_match('/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}/', $sessionId)) {
-                               // Hostname:port found
-                               $hostnameArray = explode(':', $sessionId);
-
-                               // Try to resolve it and add port again
-                               // @TODO We may want to encapsulate this PHP call into an own class
-                               $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1];
-
-                               // Is it valid?
-                               if ($recipient == $sessionId) {
-                                       // Resolving hostname->IP failed!
-                                       throw new NoValidHostnameException($hostnameArray, self::EXCEPTION_HOSTNAME_NOT_FOUND);
-                               } // END - if
-                       } elseif (!preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) {
-                               // Invalid session id
-                               throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);
-                       } else {
-                               // Debug message
-                               $selfInstance->debugOutput('HUB-TOOLS: Using internal resolver.');
-
-                               // Resolve it here
-                               $recipient = $selfInstance->resolveIpPortBySessionId($sessionId);
-                       }
-               } // END - if
-
-               // Output message
-               $selfInstance->debugOutput('HUB-TOOLS: Session id ' . $sessionId . ' resolved to ' . $recipient);
+               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.');
+               } elseif (isset($selfInstance->sessionIdCache[$sessionId])) {
+                       // Debug message
+                       $selfInstance->debugOutput('HUB-TOOLS: Using entry from sessionIdCache[] array.');
+
+                       // Found in cache!
+                       $recipient = $selfInstance->sessionIdCache[$sessionId];
+
+                       // Debug message
+                       $selfInstance->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.');
+
+                       // 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
+                       $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1];
+
+                       // Is it valid?
+                       if ($recipient == $sessionId) {
+                               // Resolving hostname->IP failed!
+                               throw new NoValidHostnameException($hostnameArray, self::EXCEPTION_HOSTNAME_NOT_FOUND);
+                       } // END - if
+
+                       // Debug message
+                       $selfInstance->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.');
+
+                       // Resolve session id into a ip:port combination
+                       $recipient = $selfInstance->resolveIpPortBySessionId($sessionId, $protocol);
+
+                       // Debug message
+                       $selfInstance->debugOutput('HUB-TOOLS: session id ' . $sessionId . ' resolved to ' . $recipient);
+               } else {
+                       // Invalid session id
+                       throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);
+               }
 
                // Return it
                return $recipient;
        }
+
+       /**
+        * Determine IP or 'external_ip' if set
+        *
+        * @return      $ip             The determined external ip 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');
+               } else {
+                       // Determine own external ip by connecting to my (coder) server at 188.138.90.169
+                       $ip = ConsoleTools::determineExternalIp();
+               }
+
+               // Return it
+               return $ip;
+       }
+
+       /**
+        * Determine IP or 'internal_ip' if set
+        *
+        * @return      $ip             The determined external ip 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');
+               } else {
+                       // Determine own internal ip by connecting to my (coder) server at 188.138.90.169
+                       $ip = ConsoleTools::acquireSelfIPAddress();
+               }
+
+               // Return it
+               return $ip;
+       }
 }
 
 // [EOF]