X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Ftools%2Fclass_HubTools.php;h=f8ef190621577ab8fe5dca3b792505432a4a688b;hb=ea369fbf3b5ab38fa5616561502015855e024b53;hp=f0ba8beccbc5497fa3f6b5eeecb2d9678f7efd70;hpb=4fb0a9cb42950c86303f83288f7c7a90cddcf684;p=hub.git diff --git a/application/hub/main/tools/class_HubTools.php b/application/hub/main/tools/class_HubTools.php index f0ba8becc..f8ef19062 100644 --- a/application/hub/main/tools/class_HubTools.php +++ b/application/hub/main/tools/class_HubTools.php @@ -2,11 +2,11 @@ /** * This class contains static helper functions for our hub * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -50,6 +50,12 @@ class HubTools extends BaseHubSystem { // Call parent constructor parent::__construct(__CLASS__); + // Get a DHT instance + $dhtInstance = DhtObjectFactory::createDhtInstance('node'); + + // Set it here + $this->setDhtInstance($dhtInstance); + // Init salt length $this->sessionIdLength = 32 + $this->getConfigInstance()->getConfigEntry('salt_length'); } @@ -83,38 +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) { - // Get a DHT instance - $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node'); + // Init variable + $recipientIpPort = 'invalid:invalid'; // And ask it for ip:port by given session id - $recipient = $dhtInstance->findNodeBySessionId($sessionId); + $recipient = $this->getDhtInstance()->findNodeLocalBySessionId($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'); + $nodeInstance = NodeObjectFactory::createNodeInstance(); // 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; } /** @@ -125,12 +126,12 @@ class HubTools extends BaseHubSystem { * @return $sessionId Valid session id */ public static function resolveSessionIdByIpPort ($ipPort) { - // Get a DHT instance - $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node'); + // Get an own instance + $selfInstance = self::getSelfInstance(); // And ask it for session id by given ip:port - $recipient = $dhtInstance->findNodeByIpPort($ipPort); - die(__METHOD__.':recipient=
'.print_r($recipient, true).'
' . PHP_EOL); + $recipient = $selfInstance->getDhtInstance()->findNodeByIpPort($ipPort); + die(__METHOD__.':recipient=
'.print_r($recipient, TRUE).'
' . PHP_EOL); // Return result return $sessionId; @@ -154,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[' . __METHOD__ . ':' . __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[' . __METHOD__ . ':' . __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[' . __METHOD__ . ':' . __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[' . __METHOD__ . ':' . __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? @@ -182,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[' . __METHOD__ . ':' . __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[' . __METHOD__ . ':' . __LINE__ . ']: 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[' . __METHOD__ . ':' . __LINE__ . ']: Session id ' . $sessionId . ' resolved to ' . $recipient); } else { // Invalid session id throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);