X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Ftools%2Fclass_HubTools.php;h=855a59210ba76a55f50dd65623c5d65aac0531f4;hb=57f03d1960de70517f197892375654d5b6dbeab7;hp=4d8227f2f80799908c20fcda9da453cf1936d780;hpb=81c90916f7a908c77f8844dff5adc6fae3aed422;p=hub.git diff --git a/application/hub/main/tools/class_HubTools.php b/application/hub/main/tools/class_HubTools.php index 4d8227f2f..855a59210 100644 --- a/application/hub/main/tools/class_HubTools.php +++ b/application/hub/main/tools/class_HubTools.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class HubTools extends BaseFrameworkSystem { +class HubTools extends BaseHubSystem { // Constants for exceptions const EXCEPTION_SESSION_ID_IS_INVALID = 0x200; const EXCEPTION_HOSTNAME_NOT_FOUND = 0x201; @@ -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 @@ -92,10 +92,39 @@ class HubTools extends BaseFrameworkSystem { // And ask it for the session id $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId); + // 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_tcp_listen_port'); + } // END - if + } // END - if + // Return result return $recipient; } + /** + * Resolves a ip:port combination into a session id + * + * @param $ipPort Ip:port combination + * @return $sessionId Valid session id + */ + public static function resolveSessionIdByIpPort ($ipPort) { + // Get a wrapper instance + $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class'); + + // And ask it for the session id + $sessionId = $wrapperInstance->resolveSessionIdByIpPort($ipPort); + + // Return result + return $sessionId; + } + /** * Resolves given session id into an ip:port combination, if ip:port is set, it won't be translated * @@ -106,7 +135,7 @@ class HubTools extends BaseFrameworkSystem { */ public static function resolveSessionId ($sessionId) { // Get an own instance - $selfInstance = self::getInstance(); + $selfInstance = self::getSelfInstance(); // Default is direct ip:port $recipient = $sessionId; @@ -121,12 +150,18 @@ class HubTools extends BaseFrameworkSystem { // Found in cache! $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)) { + + // 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 We may want to encapsulate this PHP call into an own class + // @TODO Please try to encapsulate this PHP call into an own class $recipient = gethostbyname($hostnameArray[0]) . ':' . $hostnameArray[1]; // Is it valid? @@ -134,23 +169,45 @@ class HubTools extends BaseFrameworkSystem { // 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); + + // 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); } - // Output message - $selfInstance->debugOutput('HUB-TOOLS: Session id ' . $sessionId . ' resolved to ' . $recipient); - // 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; + } } // [EOF]