]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/nodes/class_BaseHubNode.php
New Exception added, registering of session ids added:
[hub.git] / application / hub / main / nodes / class_BaseHubNode.php
index 5cbe9033932da030ed312af67a24d7526613703f..e3c1dc3bb56cde252a401973e60d272b40b75970 100644 (file)
@@ -51,6 +51,11 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
         */
        private $stateInstance = null;
 
+       /**
+        * Wether this hub is active
+        */
+       private $isActive = false;
+
        /**
         * Protected constructor
         *
@@ -327,7 +332,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
         */
@@ -436,7 +441,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
@@ -451,7 +459,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                } // END - if
 
                // Debug output
-               $this->debugOutput('HUB: Self-announcement: START (taskInstance=' . $taskInstance->__toString(). ')');
+               $this->debugOutput('HUB: Self Announcement: START (taskInstance=' . $taskInstance->__toString(). ')');
 
                // Get a helper instance
                $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_announcement_helper_class', array($this));
@@ -472,7 +480,29 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                $this->getStateInstance()->nodeAnnouncedToUpperHubs();
 
                // Debug output
-               $this->debugOutput('HUB: Self-announcement: FINISHED');
+               $this->debugOutput('HUB: Self 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));
+
+               // And send the package away
+               $helperInstance->doSelfConnect();
+
+               // Debug output
+               $this->debugOutput('HUB: Self Connection: FINISHED');
        }
 
        /**
@@ -486,7 +516,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
        public function activateHub (Requestable $requestInstance, Responseable $responseInstance) {
                // Checks wether a listener is still active and shuts it down if one
                // is still listening.
-               if (($this->determineIfListenerIsActive()) && ($this->determineIfHubIsActive())) {
+               if (($this->determineIfListenerIsActive()) && ($this->getIsActive())) {
                        // Shutdown them down before they can hurt anything
                        $this->shutdownListenerPool();
                } // END - if
@@ -520,7 +550,13 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
 
                // Setup address and port
                $listenerInstance->setListenAddressByConfiguration('node_listen_addr');
-               $listenerInstance->setListenPortByConfiguration('node_tcp_listen_port');
+               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');
+               }
 
                // Initialize the listener
                $listenerInstance->initListener();
@@ -532,7 +568,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                $this->getListenerPoolInstance()->addListener($decoratorInstance);
 
                // Get a decorator class
-               $decoratorInstance = ObjectFactory::createObjectByConfiguredName('client_tcp_listener_class', array($listenerInstance));
+               $decoratorInstance = ObjectFactory::createObjectByConfiguredName('peer_tcp_listener_class', array($listenerInstance));
 
                // Add this listener to the pool
                $this->getListenerPoolInstance()->addListener($decoratorInstance);
@@ -542,7 +578,13 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
 
                // Setup address and port
                $listenerInstance->setListenAddressByConfiguration('node_listen_addr');
-               $listenerInstance->setListenPortByConfiguration('node_udp_listen_port');
+               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');
+               }
 
                // Initialize the listener
                $listenerInstance->initListener();
@@ -554,7 +596,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
                $this->getListenerPoolInstance()->addListener($decoratorInstance);
 
                // Get a decorator class
-               $decoratorInstance = ObjectFactory::createObjectByConfiguredName('client_udp_listener_class', array($listenerInstance));
+               $decoratorInstance = ObjectFactory::createObjectByConfiguredName('peer_udp_listener_class', array($listenerInstance));
 
                // Add this listener to the pool
                $this->getListenerPoolInstance()->addListener($decoratorInstance);
@@ -602,53 +644,39 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
        }
 
        /**
-        * Determines wether the hub is active by checking its current state
+        * Getter for isActive attribute
         *
         * @return      $isActive       Wether the hub is active
         */
-       public function determineIfHubIsActive () {
-               // Check the state
-               // @TODO Add more states e.g. 'firewalled', 'senior'
-               $isActive = ($this->getStateInstance() instanceof NodeActiveState);
+       public final function getIsActive () {
+               return $this->isActive;
+       }
 
-               // Return value
-               return $isActive;
+       /**
+        * Enables (default) or disables isActive flag
+        *
+        * @param       $isActive       Wether the hub is active
+        * @return      void
+        */
+       public final function enableIsActive ($isActive = true) {
+               $this->isActive = (bool) $isActive;
        }
 
        /**
-        * Returns a singleton network package instance. If an instance is found in
-        * the registry it will be returned, else a new instance is created and
-        * stored in the same registry entry.
+        * "Getter for address:port combination
         *
-        * @param       $compressorInstance             A Compressor instance
-        * @return      $packageInstance        A network package instance
-        */
-       public function createPackageInstance (Compressor $compressorInstance) {
-               // Do we have an instance in the registry?
-               if (Registry::getRegistry()->instanceExists('network_package')) {
-                       // Then use this instance
-                       $packageInstance = Registry::getRegistry()->getInstance('network_package');
-               } else {
-                       /**
-                        * Prepare the compressor for our package, GZIP should be fine but we
-                        * keep it open here so you can experiment with the settings and don't
-                        * need to touch any code.
-                        */
-                       $compressorInstance = ObjectFactory::createObjectByConfiguredName('raw_package_compressor_class');
-
-                       // Prepare the decorator compressor (for later flawless and easy updates)
-                       $compressorInstance = ObjectFactory::createObjectByConfiguredName('deco_package_compressor_class', array($compressorInstance));
-
-                       // Now prepare the network package for delivery so only need to do this
-                       // once just before the "big announcement loop".
-                       $packageInstance = ObjectFactory::createObjectByConfiguredName('network_package_class', array($compressorInstance));
-
-                       // Set the instance in registry for further use
-                       Registry::getRegistry()->addInstance('network_package', $packageInstance);
-               }
+        * @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 the instance
-               return $packageInstance;
+               // Return it
+               return $addressPort;
        }
 }