X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fnodes%2Fclass_BaseHubNode.php;h=8a091325e91c7ab85a4ef569f78462620f3d1279;hb=f8fb89d6b4f9b4bfbd5498f784043542ea23ec88;hp=b6ec9d84ed9e802e8a6f34061f2fdd1cadc5d15d;hpb=623221e8b97d0bb7a044265ce7418d0fe7894712;p=hub.git diff --git a/application/hub/main/nodes/class_BaseHubNode.php b/application/hub/main/nodes/class_BaseHubNode.php index b6ec9d84e..8a091325e 100644 --- a/application/hub/main/nodes/class_BaseHubNode.php +++ b/application/hub/main/nodes/class_BaseHubNode.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @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 * @@ -22,6 +22,14 @@ * along with this program. If not, see . */ class BaseHubNode extends BaseHubSystem implements Updateable { + /** + * Node types + */ + const NODE_TYPE_BOOT = 'boot'; + const NODE_TYPE_MASTER = 'master'; + const NODE_TYPE_LIST = 'list'; + const NODE_TYPE_REGULAR = 'regular'; + // Exception constants const EXCEPTION_HUB_ALREADY_ANNOUNCED = 0xe00; @@ -33,23 +41,28 @@ class BaseHubNode extends BaseHubSystem implements Updateable { /** * Query connector instance */ - private $connectorInstance = null; + private $queryConnectorInstance = NULL; /** - * Listener pool instance + * Queue connector instance */ - private $listenerPoolInstance = null; + private $queueConnectorInstance = NULL; /** - * Wether this node is anncounced (KEEP ON false!) + * Whether this node is anncounced (KEEP ON false!) * @deprecated */ private $hubIsAnnounced = false; /** - * Node state instance + * Whether this hub is active (default: false) */ - private $nodeStateInstance = null; + private $isActive = false; + + /** + * Whether this node accepts announcements (default: false) + */ + private $acceptAnnouncements = false; /** * Protected constructor @@ -61,8 +74,8 @@ class BaseHubNode extends BaseHubSystem implements Updateable { // Call parent constructor parent::__construct($className); - // Init node state which sets the state to 'init' - $this->initNodeState(); + // Init state which sets the state to 'init' + $this->initState(); } /** @@ -70,107 +83,50 @@ class BaseHubNode extends BaseHubSystem implements Updateable { * * @return void */ - private function initNodeState() { - // Get the state factory and create the initial state, we don't need - // the state instance here + private function initState() { + /* + * Get the state factory and create the initial state, we don't need + * the state instance here + */ NodeStateFactory::createNodeStateInstanceByName('init', $this); } /** - * Setter for node id - * - * @param $nodeId Our new node id - * @return void - */ - private final function setNodeId ($nodeId) { - // Set it config now - $this->getConfigInstance()->setConfigEntry('node_id', (string) $nodeId); - } - - /** - * Getter for node id - * - * @return $nodeId Current node id - */ - private final function getNodeId () { - // Get it from config - return $this->getConfigInstance()->getConfigEntry('node_id'); - } - - /** - * Setter for listener pool instance - * - * @param $listenerPoolInstance Our new listener pool instance - * @return void - */ - private final function setListenerPoolInstance (PoolableListener $listenerPoolInstance) { - $this->listenerPoolInstance = $listenerPoolInstance; - } - - /** - * Getter for listener pool instance - * - * @return $listenerPoolInstance Our current listener pool instance - */ - public final function getListenerPoolInstance () { - return $this->listenerPoolInstance; - } - - /** - * Setter for node state instance - * - * @param $nodeStateInstance Node's current state instance - * @return void - */ - public final function setNodeStateInstance (NodeStateable $nodeStateInstance) { - $this->nodeStateInstance = $nodeStateInstance; - } - - /** - * Getter for node state instance - * - * @return $nodeStateInstance Node's current state instance - */ - public final function getNodeStateInstance () { - return $this->nodeStateInstance; - } - - /** - * Setter for session id + * Setter for query instance * - * @param $sessionId Our new session id + * @param $connectorInstance Our new query instance * @return void */ - private final function setSessionId ($sessionId) { - $this->getConfigInstance()->setConfigEntry('session_id', (string) $sessionId); + private final function setQueryConnectorInstance (Connectable $connectorInstance) { + $this->queryConnectorInstance = $connectorInstance; } /** - * Getter for session id + * Getter for query instance * - * @return $sessionId Current session id + * @return $connectorInstance Our new query instance */ - public final function getSessionId () { - return $this->getConfigInstance()->getConfigEntry('session_id'); + public final function getQueryConnectorInstance () { + return $this->queryConnectorInstance; } /** - * Setter for query instance + * Setter for queue instance * - * @param $connectorInstance Our new query instance + * @param $connectorInstance Our new queue instance * @return void */ - private final function setQueryConnectorInstance (Connectable $connectorInstance) { - $this->connectorInstance = $connectorInstance; + private final function setQueueConnectorInstance (Connectable $connectorInstance) { + $this->queueConnectorInstance = $connectorInstance; } /** - * Getter for query instance + * Getter for queue instance * - * @return $connectorInstance Our new query instance + * @return $connectorInstance Our new queue instance */ - public final function getQueryConnectorInstance () { - return $this->connectorInstance; + public final function getQueueConnectorInstance () { + return $this->queueConnectorInstance; } /** @@ -183,37 +139,17 @@ class BaseHubNode extends BaseHubSystem implements Updateable { } /** - * "Getter" for a printable state name - */ - public final function getPrintableState () { - // Default is 'null' - $stateName = 'null'; - - // Get the state instance - $stateInstance = $this->getNodeStateInstance(); - - // Is it an instance of NodeStateable? - if ($stateInstance instanceof NodeStateable) { - // Then use that state name - $stateName = $stateInstance->getStateName(); - } // END - if - - // Return result - return $stateName; - } - - /** - * Checks wether the given IP address matches one of the bootstrapping nodes + * Checks whether the given IP address matches one of the bootstrapping nodes * * @param $remoteAddr IP address to checkout against our bootstrapping list - * @return $isFound Wether the IP is found + * @return $isFound Whether the IP is found */ protected function ifAddressMatchesBootstrappingNodes ($remoteAddr) { // By default nothing is found $isFound = false; // Run through all configured IPs - foreach (explode(',', $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $ipPort) { + foreach (explode(BaseHubSystem::BOOTSTRAP_NODES_SEPARATOR, $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $ipPort) { // Split it up in IP/port $ipPortArray = explode(':', $ipPort); @@ -232,7 +168,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable { break; } elseif ($ipPortArray[0] == $this->getConfigInstance()->getConfigEntry('node_listen_addr')) { // IP matches listen address. At this point we really don't care - // if we can also listen on that address! + // if we can really listen on that address $isFound = true; // Remember the port number @@ -263,7 +199,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable { // Output all lines $this->debugOutput(' '); $this->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' mode active'); - $this->debugOutput('Copyright (c) 2007 - 2008 Roland Haeder, 2009, 2010 Hub Developer Team'); + $this->debugOutput('Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2012 Hub Developer Team'); $this->debugOutput(' '); $this->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.'); $this->debugOutput('This is free software, and you are welcome to redistribute it under certain'); @@ -280,7 +216,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable { * @param $responseInstance A Responseable class * @return void */ - public function bootstrapAcquireHubId (Requestable $requestInstance, Responseable $responseInstance) { + public function bootstrapAcquireNodeId (Requestable $requestInstance, Responseable $responseInstance) { // Get a wrapper instance $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class'); @@ -315,7 +251,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable { // Get a crypto instance $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class'); - // Hash and encrypt the string so we become a "node id" aka Hub-Id + // Hash and encrypt the string so we become a node id (also documented as "hub id") $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString))); // Register the node id with our wrapper @@ -327,7 +263,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable { } /** - * Generates a session id which will be sent to the other hubs and clients + * Generates a session id which will be sent to the other hubs and peers * * @return void */ @@ -354,7 +290,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable { $this->debugOutput('BOOTSTRAP: Created new session-id: ' . $this->getSessionId() . ''); // Change the state because the node has auired a hub id - $this->getNodeStateInstance()->nodeGeneratedSessionId(); + $this->getStateInstance()->nodeGeneratedSessionId(); } /** @@ -372,6 +308,12 @@ class BaseHubNode extends BaseHubSystem implements Updateable { // Run a test query $this->getQueryConnectorInstance()->doTestQuery(); + // Set the queue connector instance + $this->setQueueConnectorInstance(ObjectFactory::createObjectByConfiguredName('queue_connector_class', array($this))); + + // Run a test queue + $this->getQueueConnectorInstance()->doTestQueue(); + // Debug message $this->debugOutput('BOOTSTRAP: Initialize queues: FINISHED'); } @@ -436,7 +378,10 @@ class BaseHubNode extends BaseHubSystem implements Updateable { /** * Announces this hub to the upper (bootstrap or list) hubs. After this is - * successfully done the given task is unregistered from the handler. + * successfully done the given task is unregistered from the handler. This + * might look a bit overloaded here but the announcement phase isn't a + * simple "Hello there" message, it may later on also contain more + * informations like the object list. * * @param $taskInstance The task instance running this announcement * @return void @@ -450,30 +395,57 @@ class BaseHubNode extends BaseHubSystem implements Updateable { throw new HubAlreadyAnnouncedException($this, self::EXCEPTION_HUB_ALREADY_ANNOUNCED); } // END - if + // Set some dummy configuration entries, e.g. node_status + $this->getConfigInstance()->setConfigEntry('node_status', $this->getStateInstance()->getStateName()); + // Debug output - $this->debugOutput('HUB: Self-announcement: START (taskInstance=' . $taskInstance->__toString(). ')'); + $this->debugOutput('HUB-Announcement: START (taskInstance=' . $taskInstance->__toString(). ')'); // Get a helper instance - $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_descriptor_class', array($this)); + $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_announcement_helper_class'); // Load the announcement descriptor - $helperInstance->loadAnnouncementDescriptor(); - - // Set some dummy configuration entries, e.g. node_status - $this->getConfigInstance()->setConfigEntry('node_status', $this->getNodeStateInstance()->getStateName()); + $helperInstance->loadDescriptorXml(); // Compile all variables $helperInstance->getTemplateInstance()->compileConfigInVariables(); // "Publish" the descriptor by sending it to the bootstrap/list nodes - $helperInstance->publishAnnouncementDescriptor(); + $helperInstance->sendPackage($this); // Change the state, this should be the last line except debug output - $this->getNodeStateInstance()->nodeAnnouncedToUpperHubs(); - die("OK!\n"); + $this->getStateInstance()->nodeAnnouncedToUpperHubs(); // Debug output - $this->debugOutput('HUB: Self-announcement: FINISHED'); + $this->debugOutput('HUB-Announcement: FINISHED'); + } + + /** + * Does a self-connect attempt on the public IP address. This should make + * it sure, we are reachable from outside world. For this kind of package we + * don't need that overload we have in the announcement phase. + * + * @param $taskInstance The task instance running this announcement + * @return void + */ + public function doSelfConnection (Taskable $taskInstance) { + // Debug output + $this->debugOutput('HUB: Self Connection: START (taskInstance=' . $taskInstance->__toString(). ')'); + + // Get a helper instance + $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_self_connect_helper_class', array($this)); + + // Load the descriptor (XML) file + $helperInstance->loadDescriptorXml(); + + // Compile all variables + $helperInstance->getTemplateInstance()->compileConfigInVariables(); + + // And send the package away + $helperInstance->sendPackage($this); + + // Debug output + $this->debugOutput('HUB: Self Connection: FINISHED'); } /** @@ -484,10 +456,10 @@ class BaseHubNode extends BaseHubSystem implements Updateable { * @param $responseInstance A Responseable class * @return void */ - public function activateHub (Requestable $requestInstance, Responseable $responseInstance) { - // Checks wether a listener is still active and shuts it down if one + public function activateNode (Requestable $requestInstance, Responseable $responseInstance) { + // Checks whether a listener is still active and shuts it down if one // is still listening. - if (($this->determineIfListenerIsActive()) && ($this->determineIfHubIsActive())) { + if (($this->determineIfListenerIsActive()) && ($this->isNodeActive()())) { // Shutdown them down before they can hurt anything $this->shutdownListenerPool(); } // END - if @@ -500,7 +472,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable { // ----------------------- Last step from here ------------------------ // Activate the hub. This is ALWAYS the last step in this method - $this->getNodeStateInstance()->nodeIsActivated(); + $this->getStateInstance()->nodeIsActivated(); // ---------------------- Last step until here ------------------------ } @@ -521,6 +493,11 @@ class BaseHubNode extends BaseHubSystem implements Updateable { // Setup address and port $listenerInstance->setListenAddressByConfiguration('node_listen_addr'); + + /* + * All nodes can now use the same configuration entry because it can be + * customized in config-local.php. + */ $listenerInstance->setListenPortByConfiguration('node_tcp_listen_port'); // Initialize the listener @@ -543,6 +520,11 @@ class BaseHubNode extends BaseHubSystem implements Updateable { // Setup address and port $listenerInstance->setListenAddressByConfiguration('node_listen_addr'); + + /* + * All nodes can now use the same configuration entry because it can be + * customized in config-local.php. + */ $listenerInstance->setListenPortByConfiguration('node_udp_listen_port'); // Initialize the listener @@ -603,17 +585,63 @@ class BaseHubNode extends BaseHubSystem implements Updateable { } /** - * Determines wether the hub is active by checking its current state + * Getter for isActive attribute + * + * @return $isActive Whether the hub is active + */ + public final function isNodeActive() () { + return $this->isActive; + } + + /** + * Enables (default) or disables isActive flag + * + * @param $isActive Whether the hub is active + * @return void + */ + public final function enableIsActive ($isActive = true) { + $this->isActive = (bool) $isActive; + } + + /** + * Checks whether this node accepts announcements * - * @return $isActive Wether the hub is active + * @return $acceptsAnnouncements Whether this node accepts announcements */ - public function determineIfHubIsActive () { - // Check the state - // @TODO Add more states e.g. 'firewalled', 'senior' - $isActive = ($this->getNodeStateInstance() instanceof NodeActiveState); + public final function isAcceptingAnnouncements () { + // Check it (this node must be active and not shutdown!) + $acceptsAnnouncements = (($this->acceptsAnnouncements === true) && ($this->isNodeActive()()); + + // Return it + return $acceptsAnnouncements; + } + + /** + * Enables whether this node accepts announcements + * + * @param $acceptsAnnouncements Whether this node accepts announcements (default: true) + * @return void + */ + protected final function enableAcceptingAnnouncements ($acceptsAnnouncements = true) { + $this->acceptAnnouncements = $acceptsAnnouncements; + } + + + /** + * "Getter for address:port combination + * + * @param $handlerInstance A valid Networkable instance + * @return $addressPort A address:port combination for this node + */ + public final function getAddressPort (Networkable $handlerInstance) { + // Construct config entry + $configEntry = 'node_' . $handlerInstance->getHandlerName() . '_listen_port'; + + // Get IP and port + $addressPort = $this->getConfigInstance()->detectServerAddress() . ':' . $this->getConfigInstance()->getConfigEntry($configEntry); - // Return value - return $isActive; + // Return it + return $addressPort; } }