const EXCEPTION_HOSTNAME_NOT_FOUND = 0x201;
/**
- * Cache for session ids
+ * Cache for determined internal/external addresses to avoid "expensive"
+ * invocations on FrameworkConfiguration->getConfigEntry().
*/
- private $sessionIdCache = [];
+ private static $cachedAddresses = [];
/**
- * Length for session id (should be 32+salt_length
+ * Self instance
*/
- private $sessionIdLength = 0;
+ private static $selfInstance = NULL;
/**
- * Self instance
+ * Length for session id (should be 32+salt_length
*/
- private static $selfInstance = NULL;
+ private $sessionIdLength = 0;
/**
* Protected constructor
// And ask it for Universal Node Locator by given session id
$recipient = DhtObjectFactory::createDhtInstance('node')->findNodeLocalBySessionId($sessionId);
- //* DEBUG-DIE: */ die(__METHOD__ . ': UNFINISHED: recipient[' . gettype($recipient) . ']=' . print_r($recipient, TRUE) . ',sessionId=' . $sessionId . PHP_EOL);
// Is the recipient valid?
+ //* DEBUG-DIE: */ die(__METHOD__ . ': UNFINISHED: recipient[' . gettype($recipient) . ']=' . print_r($recipient, TRUE) . ',sessionId=' . $sessionId . PHP_EOL);
if (isset($recipient[NodeDistributedHashTableDatabaseFrontend::DB_COLUMN_EXTERNAL_ADDRESS])) {
// Then use this
$recipientUnl = $recipient[NodeDistributedHashTableDatabaseFrontend::DB_COLUMN_EXTERNAL_ADDRESS];
*
* @param $address Session id or Universal Node Locator
* @return $recipient Recipient as Universal Node Locator
+ * @throws InvalidArgumentException If a paramter is invalid
* @throws InvalidSessionIdException If the provided session id is invalid (and no Universal Node Locator)
* @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address
*/
public static function resolveSessionIdToUnl (string $address) {
+ // Is the parameter valid?
+ if (empty($address)) {
+ // No empty address
+ throw new InvalidArgumentException('Parameter "address" cannot be empty');
+ }
+
// Get an own instance
$selfInstance = self::getSelfInstance();
// @TODO ((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})
// Direct Universal Node Locator found
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Direct Universal Node Locator ' . $address . ' detected.');
- } elseif (isset($selfInstance->sessionIdCache[$address])) {
- // Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Using entry from sessionIdCache[] array.');
-
- // Found in cache!
- $recipient = $selfInstance->sessionIdCache[$address];
-
- // Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: sessionIdCache[' . $address . ']=' . $recipient);
} elseif (preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $address)) {
// Debug message
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Using internal session id resolver.');
*/
public static function determineOwnExternalAddress () {
// Is the external_address config entry set?
- if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('external_address') != '') {
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: CALLED!');
+ if (isset(self::$cachedAddresses['external'])) {
+ // Get entry from cache
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Getting external address from cache ...');
+ $unl = self::$cachedAddresses['external'];
+ } elseif (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('external_address') != '') {
// Use it as external address
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Getting config entry external_address ...');
$unl = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('external_address');
+ self::$cachedAddresses['external'] = $unl;
} else {
- // Determine own external address by connecting to my (coder) server at 188.138.90.169
+ // Determine own external address by connecting to home server at 188.138.90.169
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Calling self::determineExternalUniversalNodeLocator() ...');
$unl = self::determineExternalUniversalNodeLocator();
+ self::$cachedAddresses['external'] = $unl;
}
// Return it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $unl . ' - EXIT!');
return $unl;
}
* @return $unl The determined internal UNL of this node
*/
public static function determineOwnInternalAddress () {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: CALLED!');
-
// Is the internal_address config entry set?
- if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('allow_publish_internal_address') == 'N') {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Calling self::determineOwnExternalAddress() as allow_publish_internal_address=N is set ...');
-
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: CALLED!');
+ if (isset(self::$cachedAddresses['internal'])) {
+ // Get entry from cache
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Getting internal address from cache ...');
+ $unl = self::$cachedAddresses['internal'];
+ } elseif (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('allow_publish_internal_address') == 'N') {
// Not allowed to publish internal address, so use external
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Calling self::determineOwnExternalAddress() as allow_publish_internal_address=N is set ...');
$unl = self::determineOwnExternalAddress();
+ self::$cachedAddresses['internal'] = $unl;
} elseif (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('internal_address') != '') {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Getting config entry internal_address ...');
-
// Use it as internal address
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Getting config entry internal_address ...');
$unl = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('internal_address');
+ self::$cachedAddresses['internal'] = $unl;
} else {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Calling self::determineInternalUniversalNodeLocator() ...');
-
// Determine own internal address
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: Calling self::determineInternalUniversalNodeLocator() ...');
$unl = self::determineInternalUniversalNodeLocator();
+ self::$cachedAddresses['internal'] = $unl;
}
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $unl . ' - EXIT!');
-
// Return it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $unl . ' - EXIT!');
return $unl;
}
* @return $internalUnl Internal UNL
*/
public static function determineInternalUniversalNodeLocator () {
- // Debug message
+ // Determine UNL based on this node:
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: CALLED!');
+ // 1) Get discovery class
+ $discoveryInstance = ObjectFactory::createObjectByConfiguredName('unl_discovery_class');
- // 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__]));
- }
+ // 2) "Determine" it
+ $unl = $discoveryInstance->discoverUniversalNodeLocatorByConfiguredAddress('internal');
// Return it
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $GLOBALS[__METHOD__] . ' - EXIT!');
- return $GLOBALS[__METHOD__];
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $unl . ' - EXIT!');
+ return $unl;
}
/**
* @return $externalUnl External UNL
*/
public static function determineExternalUniversalNodeLocator () {
- // Debug message
+ // Determine UNL based on this node:
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: CALLED!');
+ // 1) Get discovery class
+ $discoveryInstance = ObjectFactory::createObjectByConfiguredName('unl_discovery_class');
- // 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__]));
- }
+ // 2) "Determine" it
+ $unl = $discoveryInstance->discoverUniversalNodeLocatorByConfiguredAddress('external');
// Return it
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $GLOBALS[__METHOD__] . ' - EXIT!');
- return $GLOBALS[__METHOD__];
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TOOLS: unl=' . $unl . ' - EXIT!');
+ return $unl;
}
}