]> git.mxchange.org Git - hub.git/commitdiff
Added getAddressPort() method to ease comparison with 'recipient' field
authorRoland Häder <roland@mxchange.org>
Sat, 22 May 2010 17:49:17 +0000 (17:49 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 22 May 2010 17:49:17 +0000 (17:49 +0000)
application/hub/interfaces/nodes/class_NodeHelper.php
application/hub/main/handler/class_
application/hub/main/handler/class_BaseHandler.php
application/hub/main/handler/network/class_
application/hub/main/handler/network/class_BaseNetworkPackageHandler.php
application/hub/main/handler/network/tcp/class_TcpNetworkPackageHandler.php
application/hub/main/handler/network/udp/class_UdpNetworkPackageHandler.php
application/hub/main/handler/tasks/class_TaskHandler.php
application/hub/main/nodes/class_BaseHubNode.php

index 6b9f7b913e239df6565aec67ed5859d046a1eaca..f023f6e32ca90b7fd252b948dbd596638a0ede10 100644 (file)
@@ -114,6 +114,14 @@ interface NodeHelper extends FrameworkInterface {
         * @return      void
         */
        function doSelfConnection (Taskable $taskInstance);
+
+       /**
+        * "Getter for address:port combination
+        *
+        * @param       $handlerInstance        A valid Networkable instance
+        * @return      $addressPort            A address:port combination for this node
+        */
+       function getAddressPort (Networkable $handlerInstance);
 }
 
 // [EOF]
index bb33e383a98b07af345be579f9ac7afbfe49ea80..47907fa8b7552d851e04b4524d44d9485380a077 100644 (file)
@@ -30,6 +30,9 @@ class ???Handler extends BaseHandler {
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
+
+               // Set handler name
+               $this->setHandlerName('!!!');
        }
 
        /**
index 7c02b75e59aebeb8735f2724032a3073303fb2ad..053bf6d2a072550b22de4c36ffcb707e1b922884 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseHandler extends BaseHubSystem {
+       /**
+        * Handler name
+        */
+       private $handlerName = 'invalid';
+
        /**
         * Protected constructor
         *
@@ -32,6 +37,25 @@ class BaseHandler extends BaseHubSystem {
                // Call parent constructor
                parent::__construct($className);
        }
+
+       /**
+        * Getter for handler name
+        *
+        * @return      $handlerName    Name of this handler
+        */
+       public final function getHandlerName () {
+               return $this->handlerName;
+       }
+
+       /**
+        * Setter for handler name
+        *
+        * @param       $handlerName    Name of this handler
+        * @return      void
+        */
+       protected final function setHandlerName ($handlerName) {
+               $this->handlerName = $handlerName;
+       }
 }
 
 // [EOF]
index 4f7c3214109c3faef29b2e75580b301cc3087222..07ba117aea0fd8ae9b19ab8533faef78b6b8d341 100644 (file)
@@ -30,6 +30,9 @@ class ???NetworkPackageHandler extends BaseNetworkPackageHandler implements Netw
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
+
+               // Set handler name
+               $this->setHandlerName('!!!');
        }
 
        /**
index 5416504df5edd8e43207635e577a3a6e3cf91639..723fb7ff156d4c3868a7f5eba8949d7f46f11bd5 100644 (file)
  */
 class BaseNetworkPackageHandler extends BaseHandler {
        // Error codes
-       const SOCKET_ERROR_UNKNOWN            = 'unknown_error';
-       const SOCKET_ERROR_TRANSPORT_ENDPOINT = 'transport_endpoint';
-       const SOCKET_ERROR_UNHANDLED          = 'unhandled_package';
-       const SOCKET_ERROR_EMPTY_DATA         = 'empty_data';
-       const PACKAGE_ERROR_INVALID_DATA      = 'invalid_data';
-       const PACKAGE_ERROR_INCOMPLETE_DATA   = 'incomplete_data';
-       const PACKAGE_ERROR_INVALID_CONTENT   = 'invalid_content';
-       const PACKAGE_LEVEL_CHECK_OKAY        = 'checked_package';
+       const SOCKET_ERROR_UNKNOWN             = 'unknown_error';
+       const SOCKET_ERROR_TRANSPORT_ENDPOINT  = 'transport_endpoint';
+       const SOCKET_ERROR_UNHANDLED           = 'unhandled_package';
+       const SOCKET_ERROR_EMPTY_DATA          = 'empty_data';
+       const PACKAGE_ERROR_INVALID_DATA       = 'invalid_data';
+       const PACKAGE_ERROR_INCOMPLETE_DATA    = 'incomplete_data';
+       const PACKAGE_ERROR_INVALID_CONTENT    = 'invalid_content';
+       const PACKAGE_ERROR_RECIPIENT_MISMATCH = 'recipient_error';
+       const PACKAGE_LEVEL_CHECK_OKAY         = 'checked_package';
 
        /**
         * Error code from socket
@@ -48,6 +49,24 @@ class BaseNetworkPackageHandler extends BaseHandler {
                parent::__construct($className);
        }
 
+       /**
+        * Checks wether the 'recipient' field matches our own address:port
+        * combination.
+        *
+        * @param       $packageData    Raw package data
+        * @return      $matches                Wether it matches
+        */
+       protected function ifRecipientMatchesOwnAddress (array $packageData) {
+               // Construct own address first,
+               $ownAddress = Registry::getRegistry()->getInstance('node')->getAddressPort($this);
+
+               // Does it match?
+               $matches = ($ownAddress === $packageData[1]);
+
+               // Return result
+               return $matches;
+       }
+
        /**
         * Setter for error code
         *
index 2d634cc3a5cdc2a0309edc7b8b3abcb1389c3a82..e24fbeb5541165d43fc1b8f49dbd896cda7dbcab 100644 (file)
@@ -30,12 +30,15 @@ class TcpNetworkPackageHandler extends BaseNetworkPackageHandler implements Netw
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
+
+               // Set handler name
+               $this->setHandlerName('tcp');
        }
 
        /**
         * Creates an instance of this class
         *
-        * @return      $handlerInstance                An instance of a Networkable class
+        * @return      $handlerInstance        An instance of a Networkable class
         */
        public final static function createTcpNetworkPackageHandler () {
                // Get new instance
@@ -71,6 +74,9 @@ class TcpNetworkPackageHandler extends BaseNetworkPackageHandler implements Netw
                } elseif (count(explode(NetworkPackage::PACKAGE_MASK_SEPERATOR, $packageData[2])) < 2) {
                        // Not entougth fields in content
                        $this->setErrorCode(self::PACKAGE_ERROR_INVALID_CONTENT);
+               } elseif (!$this->ifRecipientMatchesOwnAddress($packageData)) {
+                       // Field 'recipient' doesn't match our address, this must always be the case
+                       $this->setErrorCode(self::PACKAGE_ERROR_RECIPIENT_MISMATCH);
                } else {
                        // This check went fine...
                        $isValid = true;
index edf93218d0484e93fbe98a81eaa32115909c9fd8..477414a098898b92484eca3545106f5e5ba65964 100644 (file)
@@ -30,6 +30,9 @@ class UdpNetworkPackageHandler extends BaseNetworkPackageHandler implements Netw
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
+
+               // Set handler name
+               $this->setHandlerName('udp');
        }
 
        /**
index f96df781e331f5bd361b3606501c53b9a36d65c2..044dc3c916883eb577301b6550d15205c857ba7f 100644 (file)
@@ -39,6 +39,9 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
                // Call parent constructor
                parent::__construct(__CLASS__);
 
+               // Set handler name
+               $this->setHandlerName('task');
+
                // Init the task list
                $this->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
 
index f27af3018a6feac0903708cd8be4a509ee6f29f4..e28e339b08ad2c3a302f4663cf597dd3880d7662 100644 (file)
@@ -649,6 +649,23 @@ class BaseHubNode extends BaseHubSystem implements Updateable {
        public final function enableIsActive ($isActive = true) {
                $this->isActive = (bool) $isActive;
        }
+
+       /**
+        * "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 it
+               return $addressPort;
+       }
 }
 
 // [EOF]