]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/nodes/class_BaseHubNode.php
Mode-validation and IP detection added, mode classes added
[hub.git] / application / hub / main / nodes / class_BaseHubNode.php
index c000f64d752abf8c0fa59a8ecfd4808a4c0e539a..61028b3287abc1aa3ad388df42ec403ce669d56c 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 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -25,7 +25,12 @@ class BaseHubNode extends BaseFrameworkSystem implements Updateable {
        /**
         * Node id
         */
-       private $nodeId = "";
+       private $nodeId = '';
+
+       /**
+        * IP/port number of bootstrapping node
+        */
+       private $bootIpPort = '';
 
        /**
         * Protected constructor
@@ -62,7 +67,106 @@ class BaseHubNode extends BaseFrameworkSystem implements Updateable {
        }
 
        /**
-        * Method to aquire a hub-id. On first run this generates a new one
+        * Checks wether 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
+        */
+       private function ifAddressMatchesBootstrappingNodes ($remoteAddr) {
+               // By default nothing is found
+               $isFound = false;
+
+               // Run through all configured IPs
+               foreach (explode(',', $this->getConfigInstance()->readConfig('hub_bootstrap_nodes')) as $ipPort) {
+                       // Does it match?
+                       if (substr($ipPort, 0, strlen($remoteAddr)) == $remoteAddr) {
+                               // Found it!
+                               $isFound = true;
+
+                               // Remember the port number
+                               $this->bootIpPort = $ipPort;
+
+                               // Output message
+                               $this->getDebugInstance()->output(__FUNCTION__.': IP matches remote address ' . $ipPort . '.');
+
+                               // Stop further searching
+                               break;
+                       } elseif (substr($ipPort, 0, strlen($this->getConfigInstance()->readConfig('node_listen_addr'))) == $this->getConfigInstance()->readConfig('node_listen_addr')) {
+                               // IP matches listen address. At this point we really don't care
+                               // if we can also listen on that address!
+                               $isFound = true;
+
+                               // Remember the port number
+                               $this->bootIpPort = $ipPort;
+
+                               // Output message
+                               $this->getDebugInstance()->output(__FUNCTION__.': IP matches listen address ' . $ipPort . '.');
+
+                               // Stop further searching
+                               break;
+                       }
+               } // END - foreach
+
+               // Return the result
+               return $isFound;
+       }
+
+       /**
+        * Outputs the console teaser. This should only be executed on startup or
+        * full restarts. This method generates some space around the teaser.
+        *
+        * @return      void
+        */
+       private function outputConsoleTeaser () {
+               // Get the app instance (for shortening our code)
+               $app = $this->getApplicationInstance();
+
+               // Output all lines
+               $this->getDebugInstance()->output(' ');
+               $this->getDebugInstance()->output($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' mode active');
+               $this->getDebugInstance()->output('Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team');
+               $this->getDebugInstance()->output(' ');
+               $this->getDebugInstance()->output('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
+               $this->getDebugInstance()->output('This is free software, and you are welcome to redistribute it under certain');
+               $this->getDebugInstance()->output('conditions; see docs/COPYING for details.');
+               $this->getDebugInstance()->output(' ');
+       }
+
+       /**
+        * Do generic things for bootup phase. This can be e.g. checking if the
+        * right node mode is selected for this hub's IP number.
+        *
+        * @todo        This method is maybe not yet finished.
+        * @return      void
+        */
+       protected function doGenericBootstrapping () {
+               // Now check if the IP address matches one of the bootstrap nodes
+               if ($this->ifAddressMatchesBootstrappingNodes($_SERVER['REMOTE_ADDR'])) {
+                       // Get our port
+                       $ourPort = $this->getConfigInstance()->readConfig('node_listen_port');
+
+                       // Is the port the same?
+                       if (substr($this->bootIpPort, -strlen($ourPort), strlen($ourPort)) == $ourPort) {
+                               // It is the same!
+                               $this->getDebugInstance()->output(__FUNCTION__.': IP/port matches bootstrapping node ' . $this->bootIpPort . '.');
+
+                               // Now, does the mode match (should be 'boot'!)
+                               if ($this->getRequestInstance()->getRequestElement('mode') == 'boot') {
+                                       // Output debug message
+                                       $this->getDebugInstance()->output(__FUNCTION__.': Our node is a valid boot-strapping node.');
+                               } else {
+                                       // Output warning
+                                       $this->getDebugInstance()->output(__FUNCTION__.': WARNING! Mismatching mode ' . $this->getRequestInstance()->getRequestElement('mode') . '!=boot detected.');
+                               }
+                       } // END - if
+               } // END - if
+
+               // Finally output our teaser. This should be the last line!
+               $this->outputConsoleTeaser();
+       }
+
+       /**
+        * Generic method to aquire a hub-id. On first run this generates a new one
         * based on many pseudo-random data. On any later run, unless the id
         * got not removed from database, it will be restored from the database.
         *
@@ -116,7 +220,7 @@ class BaseHubNode extends BaseFrameworkSystem implements Updateable {
        }
 
        /**
-        * Adds registration elements to a given dataset instance
+        * Adds hub data elements to a given dataset instance
         *
         * @param       $criteriaInstance       An instance of a storeable criteria
         * @param       $requestInstance        An instance of a Requestable class