*
* @author Roland Haeder <webmaster@shipsimu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.shipsimu.org
*
/**
* Universal node locator of bootstrap node
*/
- private $bootUniversalNodeLocator = '';
+ private $bootUnl = '';
/**
* Whether this node is anncounced (keep on FALSE!)
// Set it here
$this->setCryptoInstance($cryptoInstance);
+ // Set the node instance in registry
+ Registry::getRegistry()->addInstance('node', $this);
+
// Init state which sets the state to 'init'
$this->initState();
}
*/
private function initState() {
// Get the state factory and create the initial state.
- NodeStateFactory::createNodeStateInstanceByName('init', $this);
- }
-
- /**
- * Generates a random string from various data inluding UUID if PECL
- * extension uuid is installed.
- *
- * @param $length Length of the random part
- * @return $randomString Random string
- * @todo Make this code more generic and move it to CryptoHelper or
- */
- protected function generateRamdomString ($length) {
- // Get an RNG instance
- $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
-
- // Generate a pseudo-random string
- $randomString = $rngInstance->randomString($length) . ':' . $this->getBootUniversalNodeLocator() . ':' . $this->getRequestInstance()->getRequestElement('mode');
-
- // Add UUID for even more entropy for the hasher
- $randomString .= $this->getCryptoInstance()->createUuid();
-
- // Return it
- return $randomString;
+ NodeStateFactory::createNodeStateInstanceByName('init');
}
/**
* Generates a private key and hashes it (for speeding up things)
*
* @param $searchInstance An instance of a LocalSearchCriteria class
- * @return void
+ * @return void
*/
private function generatePrivateKeyAndHash (LocalSearchCriteria $searchInstance) {
// Get an RNG instance
}
/**
- * Getter for boot IP/port combination
+ * Generates a random string from various data inluding UUID if PECL
+ * extension uuid is installed.
*
- * @return $bootUniversalNodeLocator The IP/port combination of the boot node
+ * @param $length Length of the random part
+ * @return $randomString Random string
+ * @todo Make this code more generic and move it to CryptoHelper or
+ */
+ protected function generateRamdomString ($length) {
+ // Get an RNG instance
+ $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
+
+ // Generate a pseudo-random string
+ $randomString = $rngInstance->randomString($length) . ':' . $this->getBootUniversalNodeLocator() . ':' . $this->getRequestInstance()->getRequestElement('mode');
+
+ // Add UUID for even more entropy for the hasher
+ $randomString .= $this->getCryptoInstance()->createUuid();
+
+ // Return it
+ return $randomString;
+ }
+
+ /**
+ * Getter for boot UNL (Universal Node Locator)
+ *
+ * @return $bootUnl The UNL (Universal Node Locator) of the boot node
*/
protected final function getBootUniversalNodeLocator () {
- return $this->bootUniversalNodeLocator;
+ return $this->bootUnl;
}
/**
// Run through all configured IPs
foreach (explode(BaseHubSystem::BOOTSTRAP_NODES_SEPARATOR, $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $unl) {
- // Get back an array from the UNL
- $unlArray = HubTools::getArrayFromUniversalNodeLocator($unl);
- // @TODO Unfinished
- die(__METHOD__ . ':' . print_r($unlArray, TRUE));
-
// Does it match?
- if ($unlArray[0] == $remoteAddr) {
+ if ($unl == $remoteAddr) {
// Found it!
$isFound = TRUE;
- // Remember the port number
- $this->bootUniversalNodeLocator = $unl;
+ // Remember the UNL
+ $this->bootUnl = $unl;
// Output message
- self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: ' . __FUNCTION__ . '[' . __METHOD__ . ':' . __LINE__ . ']: IP matches remote address ' . $unl . '.');
+ self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: ' . __FUNCTION__ . '[' . __METHOD__ . ':' . __LINE__ . ']: UNL matches remote address ' . $unl . '.');
// Stop further searching
break;
- } elseif ($unlArray[0] == $this->getConfigInstance()->getConfigEntry('node_listen_addr')) {
+ } elseif ($unl == $this->getConfigInstance()->getConfigEntry('node_listen_addr')) {
/*
* IP matches listen address. At this point we really don't care
* if we can really listen on that address
$isFound = TRUE;
// Remember the port number
- $this->bootUniversalNodeLocator = $unl;
+ $this->bootUnl = $unl;
// Output message
- self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: ' . __FUNCTION__ . '[' . __METHOD__ . ':' . __LINE__ . ']: IP matches listen address ' . $unl . '.');
+ self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: ' . __FUNCTION__ . '[' . __METHOD__ . ':' . __LINE__ . ']: UNL matches listen address ' . $unl . '.');
// Stop further searching
break;
return $isFound;
}
+ /**
+ * Tries to detect own UNL (Universal Node Locator)
+ *
+ * @return $unl Node's own universal node locator
+ */
+ public function detectOwnUniversalNodeLocator () {
+ // Is "cache" set?
+ if (!isset($GLOBALS[__METHOD__])) {
+ // Get the UNL array back
+ $unlData = $this->getUniversalNodeLocatorArray();
+
+ // There are 2 UNLs, internal and external.
+ if ($this->getConfigInstance()->getConfigEntry('allow_publish_internal_address') == 'N') {
+ // Public "external" UNL address
+ $GLOBALS[__METHOD__] = $unlData[NodeInformationDatabaseWrapper::DB_COLUMN_EXTERNAL_UNL];
+ } else {
+ // Non-public "internal" UNL address
+ $GLOBALS[__METHOD__] = $unlData[NodeInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL];
+ }
+ } // END - if
+
+ // Return it
+ return $GLOBALS[__METHOD__];
+ }
+
/**
* Outputs the console teaser. This should only be executed on startup or
* full restarts. This method generates some space around the teaser.
// Output all lines
self::createDebugInstance(__CLASS__)->debugOutput(' ');
self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' mode active');
- self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2014 Hub Developer Team');
+ self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2015 Hub Developer Team');
self::createDebugInstance(__CLASS__)->debugOutput(' ');
self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
// Output message
self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: Created new session-id: ' . $this->getSessionId() . '');
- // Change the state because the node has auired a hub id
+ // Change the state because the node has aquired a session id
$this->getStateInstance()->nodeGeneratedSessionId();
}
* @return void
*/
public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
- // Make sure request instance is set as it is not optional
+ // Make sure the request instance is set as it is not optional.
assert($requestInstance instanceof Requestable);
// Add node number and type
$criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY, base64_encode($this->getPrivateKey()));
$criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH, $this->getPrivateKeyHash());
} // END - if
+
+ // Add own external and internal addresses as UNLs
+ $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL, HubTools::determineOwnInternalAddress());
+ $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_EXTERNAL_UNL, HubTools::determineOwnExternalAddress());
}
/**
}
/**
- * Determines the universal node locator
+ * Determines an instance of a LocateableNode class
*
- * @return $unl A an universal node locator for this node
+ * @return $unlInstance An instance of a LocateableNode class for this node
*/
- public final function determineUniversalNodeLocator () {
- // Get IP and port
- $unl = $this->getConfigInstance()->detectServerAddress() . ':' . $this->getConfigInstance()->getConfigEntry('node_listen_port');
+ public function determineUniversalNodeLocator () {
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
- // Return it
- return $unl;
+ // Determine UNL based on this node:
+ // 1) Get discovery class
+ $discoveryInstance = ObjectFactory::createObjectByConfiguredName('unl_discovery_class');
+
+ // 2) "Determine" it
+ $unlInstance = $discoveryInstance->discoverUniversalNodeLocatorByNode($this);
+
+ // 3) Return it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: unlInstance= ' . $unlInstance->__toString() . ' - EXIT!');
+ return $unlInstance;
}
/**
- * "Getter" for universal node locator array
+ * "Getter" for an array of an instance of a LocateableNode class
*
- * @return $unlArray An array of the universal node locator for this node
+ * @return $unlData An array from an instance of a LocateableNode class for this node
*/
public final function getUniversalNodeLocatorArray () {
- // Get IP and port
- // @TODO Unfinished
- die(__METHOD__ . ':' . print_r($this, TRUE));
- $unlArray = explode(':', $this->determineUniversalNodeLocator());
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
+
+ // Get the Universal Node Locator (UNL) instance
+ $unlInstance = $this->determineUniversalNodeLocator();
+
+ // Make sure the instance is valid
+ if (!$unlInstance instanceof LocateableNode) {
+ // No valid instance, so better debug this
+ $this->debugBackTrace('unlInstance[' . gettype($unlInstance) . ']=' . $unlInstance);
+ } // END - if
+
+ // ... and the array from it
+ $unlData = $unlInstance->getUnlData();
// Return it
- return $unlArray;
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
+ return $unlData;
}
/**