]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/nodes/class_BaseHubNode.php
Fixed a typo, added an assert()
[hub.git] / application / hub / main / nodes / class_BaseHubNode.php
index b83b93c9743c3e6c2497ff88ec77e0b4289921f9..07974fad42ddec9c2d308b124c7a67bac54eef84 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 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,26 +41,21 @@ 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;
 
        /**
-        * State instance
-        */
-       private $stateInstance = null;
-
-       /**
-        * Wether this hub is active
+        * Whether this hub is active
         */
        private $isActive = false;
 
@@ -76,9 +79,11 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
         * @return      void
         */
        private function initState() {
-               // Get the state factory and create the initial state, we don't need
-               // the state instance here
-               StateFactory::createStateInstanceByName('init', $this);
+               /*
+                * Get the state factory and create the initial state, we don't need
+                * the state instance here
+                */
+               NodeStateFactory::createNodeStateInstanceByName('init', $this);
        }
 
        /**
@@ -102,44 +107,6 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                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 state instance
-        *
-        * @param       $stateInstance  Node's current state instance
-        * @return      void
-        */
-       public final function setStateInstance (Stateable $stateInstance) {
-               $this->stateInstance = $stateInstance;
-       }
-
-       /**
-        * Getter for state instance
-        *
-        * @return      $stateInstance  Node's current state instance
-        */
-       public final function getStateInstance () {
-               return $this->stateInstance;
-       }
-
        /**
         * Setter for session id
         *
@@ -166,7 +133,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
         * @return      void
         */
        private final function setQueryConnectorInstance (Connectable $connectorInstance) {
-               $this->connectorInstance = $connectorInstance;
+               $this->queryConnectorInstance = $connectorInstance;
        }
 
        /**
@@ -175,50 +142,49 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
         * @return      $connectorInstance              Our new query instance
         */
        public final function getQueryConnectorInstance () {
-               return $this->connectorInstance;
+               return $this->queryConnectorInstance;
        }
 
        /**
-        * Getter for boot IP/port combination
+        * Setter for queue instance
         *
-        * @return      $bootIpPort             The IP/port combination of the boot node
+        * @param       $connectorInstance              Our new queue instance
+        * @return      void
         */
-       protected final function getBootIpPort () {
-               return $this->bootIpPort;
+       private final function setQueueConnectorInstance (Connectable $connectorInstance) {
+               $this->queueConnectorInstance = $connectorInstance;
        }
 
        /**
-        * "Getter" for a printable state name
+        * Getter for queue instance
+        *
+        * @return      $connectorInstance              Our new queue instance
         */
-       public final function getPrintableState () {
-               // Default is 'null'
-               $stateName = 'null';
-
-               // Get the state instance
-               $stateInstance = $this->getStateInstance();
-
-               // Is it an instance of Stateable?
-               if ($stateInstance instanceof Stateable) {
-                       // Then use that state name
-                       $stateName = $stateInstance->getStateName();
-               } // END - if
+       public final function getQueueConnectorInstance () {
+               return $this->queueConnectorInstance;
+       }
 
-               // Return result
-               return $stateName;
+       /**
+        * Getter for boot IP/port combination
+        *
+        * @return      $bootIpPort             The IP/port combination of the boot node
+        */
+       protected final function getBootIpPort () {
+               return $this->bootIpPort;
        }
 
        /**
-        * 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);
 
@@ -237,7 +203,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
@@ -268,7 +234,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 - 2011 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');
@@ -285,7 +251,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');
 
@@ -320,7 +286,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
@@ -377,6 +343,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');
        }
@@ -459,7 +431,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                } // END - if
 
                // Debug output
-               $this->debugOutput('HUBAnnouncement: START (taskInstance=' . $taskInstance->__toString(). ')');
+               $this->debugOutput('HUB-Announcement: START (taskInstance=' . $taskInstance->__toString(). ')');
 
                // Get a helper instance
                $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_announcement_helper_class', array($this));
@@ -474,13 +446,13 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                $helperInstance->getTemplateInstance()->compileConfigInVariables();
 
                // "Publish" the descriptor by sending it to the bootstrap/list nodes
-               $helperInstance->sendPackage();
+               $helperInstance->sendPackage($this);
 
                // Change the state, this should be the last line except debug output
                $this->getStateInstance()->nodeAnnouncedToUpperHubs();
 
                // Debug output
-               $this->debugOutput('HUBAnnouncement: FINISHED');
+               $this->debugOutput('HUB-Announcement: FINISHED');
        }
 
        /**
@@ -502,7 +474,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                $helperInstance->loadDescriptorXml();
 
                // And send the package away
-               $helperInstance->sendPackage();
+               $helperInstance->sendPackage($this);
 
                // Debug output
                $this->debugOutput('HUB: Self Connection: FINISHED');
@@ -516,8 +488,8 @@ 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->getIsActive())) {
                        // Shutdown them down before they can hurt anything
@@ -553,13 +525,12 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
 
                // Setup address and port
                $listenerInstance->setListenAddressByConfiguration('node_listen_addr');
-               if ($this instanceof HubBootNode) {
-                       // Bootstrap have different listening port
-                       $listenerInstance->setListenPortByConfiguration('boot_node_tcp_listen_port');
-               } else {
-                       // All other nodes use the default port
-                       $listenerInstance->setListenPortByConfiguration('node_tcp_listen_port');
-               }
+
+               /*
+                * 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
                $listenerInstance->initListener();
@@ -571,7 +542,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                $this->getListenerPoolInstance()->addListener($decoratorInstance);
 
                // Get a decorator class
-               $decoratorInstance = ObjectFactory::createObjectByConfiguredName('peer_tcp_listener_class', array($listenerInstance));
+               $decoratorInstance = ObjectFactory::createObjectByConfiguredName('client_tcp_listener_class', array($listenerInstance));
 
                // Add this listener to the pool
                $this->getListenerPoolInstance()->addListener($decoratorInstance);
@@ -581,13 +552,12 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
 
                // Setup address and port
                $listenerInstance->setListenAddressByConfiguration('node_listen_addr');
-               if ($this instanceof HubBootNode) {
-                       // Bootstrap have different listening port
-                       $listenerInstance->setListenPortByConfiguration('boot_node_udp_listen_port');
-               } else {
-                       // All other nodes use the default port
-                       $listenerInstance->setListenPortByConfiguration('node_udp_listen_port');
-               }
+
+               /*
+                * 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
                $listenerInstance->initListener();
@@ -599,7 +569,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                $this->getListenerPoolInstance()->addListener($decoratorInstance);
 
                // Get a decorator class
-               $decoratorInstance = ObjectFactory::createObjectByConfiguredName('peer_udp_listener_class', array($listenerInstance));
+               $decoratorInstance = ObjectFactory::createObjectByConfiguredName('client_udp_listener_class', array($listenerInstance));
 
                // Add this listener to the pool
                $this->getListenerPoolInstance()->addListener($decoratorInstance);
@@ -649,7 +619,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
        /**
         * Getter for isActive attribute
         *
-        * @return      $isActive       Wether the hub is active
+        * @return      $isActive       Whether the hub is active
         */
        public final function getIsActive () {
                return $this->isActive;
@@ -658,7 +628,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
        /**
         * Enables (default) or disables isActive flag
         *
-        * @param       $isActive       Wether the hub is active
+        * @param       $isActive       Whether the hub is active
         * @return      void
         */
        public final function enableIsActive ($isActive = true) {