]> git.mxchange.org Git - hub.git/commitdiff
Rewrote processMessage() to accept whole arrays + added filter for handling
authorRoland Haeder <roland@mxchange.org>
Mon, 11 May 2015 21:16:33 +0000 (23:16 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 11 May 2015 21:16:33 +0000 (23:16 +0200)
hash + sender over to the miner for "mining" the hash.

Signed-off-by: Roland Haeder <roland@mxchange.org>
17 files changed:
application/hub/config.php
application/hub/interfaces/filter/class_FilterablePackage.php
application/hub/interfaces/package/class_Receivable.php
application/hub/main/chains/class_PackageFilterChain.php
application/hub/main/factories/chain/class_PackageFilterChainFactory.php
application/hub/main/filter/class_BaseHubFilter.php
application/hub/main/filter/package/.htaccess [new file with mode: 0644]
application/hub/main/filter/package/class_PackageHashToMinerFilter.php [new file with mode: 0644]
application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php
application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php
application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php
application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php
application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php
application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php
application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php
application/hub/main/package/class_NetworkPackage.php
application/hub/main/tools/class_HubTools.php

index 8c9d47e7b7247c340746aa7ed6a909304ac157ba..9f26d4909b6de7e18496db3492f6f489801bf97c 100644 (file)
@@ -204,6 +204,9 @@ $cfg->setConfigEntry('package_tag_request_node_list_answer_filter', 'PackageRequ
 // CFG: PACKAGE-TAG-DHT-BOOTSTRAP-FILTER
 $cfg->setConfigEntry('package_tag_dht_bootstrap_filter', 'PackageDhtBootstrapTagFilter');
 
+// CFG: PACKAGE-HASH-TO-MINER-FILTER
+$cfg->setConfigEntry('package_hash_to_miner_filter', 'PackageHashToMinerFilter');
+
 // CFG: MESSAGE-TYPE-ANNOUNCEMENT-HANDLER-CLASS
 $cfg->setConfigEntry('message_type_announcement_handler_class', 'NodeMessageAnnouncementHandler');
 
index 9051130960f227e5ac413da17bc4796a090ee5e4..3f6c3e215e74553c5e4a1f53e10499560c7ae2bd 100644 (file)
@@ -25,11 +25,11 @@ interface FilterablePackage extends Filterable {
        /**
         * Processes the given raw message content.
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         */
-       function processMessage ($messageContent, Receivable $packageInstance);
+       function processMessage (array $messageData, Receivable $packageInstance);
 }
 
 // [EOF]
index 126247bdfca788190e0dbea614096152d71ae0f2..9fd5226a70bba99336f8b7881977e60a48b38002 100644 (file)
@@ -134,6 +134,17 @@ interface Receivable extends FrameworkInterface {
         * @return      void
         */
        function handleMultipleMessages ();
+
+       /**
+        * Feeds the hash and sender (as recipient for the 'sender' reward) to the
+        * miner's queue, unless the message is not a "reward claim" message as this
+        * leads to an endless loop. You may wish to run the miner to get some
+        * reward ("HubCoins") for "mining" this hash.
+        *
+        * @param       $decodedDataArray       Array with decoded message
+        * @return      void
+        */
+       function feedHashToMiner (array $decodedDataArray);
 }
 
 // [EOF]
index dde86c0fe2af86ff7cbfab122d018d1d3492dea1..28cc63f2b8b1e5d074a79fd4aafd8de7df2689d3 100644 (file)
@@ -48,17 +48,17 @@ class PackageFilterChain extends FilterChain {
        /**
         * Processes the given raw message content through all filters
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         * @todo        This may be slow if a message with a lot tags arrived
         */
-       public function processMessage ($messageContent, Receivable $packageInstance) {
+       public function processMessage (array $messageData, Receivable $packageInstance) {
                // Get all filters and "run" them
                foreach ($this->getFilters() as $filterInstance) {
                        // Try to process it
                        try {
-                               $filterInstance->processMessage($messageContent, $packageInstance);
+                               $filterInstance->processMessage($messageData, $packageInstance);
                        } catch (FilterChainException $e) {
                                // This exception can be thrown to just skip any further processing
                                self::createDebugInstance(__CLASS__)->debugOutput('Failed to execute lase filter ' . $filterInstance->__toString() . ': ' . $e->getMessage());
index 9cd32b50e0eae5ad12d396d8cdc8abd02192431e..ba3bc4df3ad4e7ab7f881a563ca03a2a408ee0dd 100644 (file)
@@ -64,6 +64,12 @@ class PackageFilterChainFactory extends ObjectFactory {
                                $chainInstance->addFilter($filterInstance);
                        } // END - foreach
 
+                       // Add filter for handling hash to miner queue
+                       $filterInstance = self::createObjectByConfiguredName('package_hash_to_miner_filter');
+
+                       // Add this filter to the chain
+                       $chainInstance->addFilter($filterInstance);
+
                        // Add the finished chain to the registry
                        Registry::getRegistry()->addInstance($registryKey, $chainInstance);
                }
index fbde97be8283d615814e84a3e64e375df68eee00..dcfb5a4650708307e3945eadc31fcb5bef2a2fd1 100644 (file)
@@ -50,15 +50,21 @@ class BaseHubFilter extends BaseFilter {
         *                            well-formed
         *
         * @param       $messageType            Type of message
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         * @todo        Exceptions from renderXmlContent() are currently unhandled
         */
-       protected function genericProcessMessage ($messageType, $messageContent, Receivable $packageInstance) {
+       protected function genericProcessMessage ($messageType, array $messageData, Receivable $packageInstance) {
+               // Make sure the wanted element is there
+               assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE]));
+
                // Get a template instance from the factory
                $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_' . $messageType . '_template_class');
 
+               // Get message content
+               $messageContent = $messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE];
+
                // And render the XML content (aka message)
                $templateInstance->renderXmlContent($messageContent);
 
diff --git a/application/hub/main/filter/package/.htaccess b/application/hub/main/filter/package/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/application/hub/main/filter/package/class_PackageHashToMinerFilter.php b/application/hub/main/filter/package/class_PackageHashToMinerFilter.php
new file mode 100644 (file)
index 0000000..4fe8168
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * A package filter for passing over a message hash and sender to the miner's
+ * queue.
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class PackageHashToMinerFilter extends BaseHubFilter implements FilterablePackage {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this filter class
+        *
+        * @return      $filterInstance         An instance of this filter class
+        */
+       public final static function createPackageHashToMinerFilter () {
+               // Get a new instance
+               $filterInstance = new PackageHashToMinerFilter();
+
+               // Return the instance
+               return $filterInstance;
+       }
+
+       /**
+        * Executes the filter with given request and response objects
+        *
+        * @param       $requestInstance        An instance of a class with an Requestable interface
+        * @param       $responseInstance       An instance of a class with an Responseable interface
+        * @return      void
+        * @throws      UnsupportedOperationException   If this method is called, please use processMessage() instead!
+        */
+       public function execute (Requestable $requestInstance, Responseable $responseInstance) {
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
+
+       /**
+        * Processes the given raw message content. The method renderXmlContent
+        * may throw (not the method itself) several exceptions:
+        *
+        * InvalidXmlNodeException  - If an invalid XML node has been found (e.g.
+        *                            wrong/out-dated template used)
+        * XmlNodeMismatchException - Again might be caused by invalid XML node
+        *                            usage
+        * XmlParserException       - If the XML message is damaged or not
+        *                            well-formed
+        *
+        * @param       $messageData            Raw message data array
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        */
+       public function processMessage (array $messageData, Receivable $packageInstance) {
+               // Process message generic
+               $packageInstance->feedHashToMiner($messageData);
+       }
+}
+
+// [EOF]
+?>
index 9fa200ceb4e60c00e617a9c6dc4a90733ba89e3c..315e384936ef998861f163f94b64d563c38441c9 100644 (file)
@@ -71,13 +71,13 @@ class PackageAnnouncementAnswerTagFilter extends BaseNodeFilter implements Filte
         * Processes the given raw message content. The method renderXmlContent
         * may throw (not the method itself) several exceptions:
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         */
-       public function processMessage ($messageContent, Receivable $packageInstance) {
+       public function processMessage (array $messageData, Receivable $packageInstance) {
                // Process message in generic way
-               $this->genericProcessMessage('announcement_answer', $messageContent, $packageInstance);
+               $this->genericProcessMessage('announcement_answer', $messageData, $packageInstance);
        }
 }
 
index b3bd4cc189130394989a63d069e79e33363a4ea4..fc35994fd4db2255ac910ac927c49d7a8e2a3d7a 100644 (file)
@@ -69,13 +69,13 @@ class PackageDhtBootstrapAnswerTagFilter extends BaseNodeFilter implements Filte
         * Processes the given raw message content. The method renderXmlContent
         * may throw (not the method itself) several exceptions:
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         */
-       public function processMessage ($messageContent, Receivable $packageInstance) {
+       public function processMessage (array $messageData, Receivable $packageInstance) {
                // Process message in generic way
-               $this->genericProcessMessage('dht_bootstrap_answer', $messageContent, $packageInstance);
+               $this->genericProcessMessage('dht_bootstrap_answer', $messageData, $packageInstance);
        }
 }
 
index abc00a904ddaead7a990833d753dcefe33a947b6..3f26eadaac30ffaa13c4628662e6a77f635cd1d3 100644 (file)
@@ -69,13 +69,13 @@ class PackageRequestNodeListAnswerTagFilter extends BaseNodeFilter implements Fi
         * Processes the given raw message content. The method renderXmlContent
         * may throw (not the method itself) several exceptions:
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         */
-       public function processMessage ($messageContent, Receivable $packageInstance) {
+       public function processMessage (array $messageData, Receivable $packageInstance) {
                // Process message in generic way
-               $this->genericProcessMessage('request_node_list_answer', $messageContent, $packageInstance);
+               $this->genericProcessMessage('request_node_list_answer', $messageData, $packageInstance);
        }
 }
 
index 3d78a25d87c187aa292d0806c0156e1521c15b9b..d9519b31ebd50eb682e646ade625324919db083b 100644 (file)
@@ -78,13 +78,13 @@ class PackageAnnouncementTagFilter extends BaseNodeFilter implements FilterableP
         * XmlParserException       - If the XML message is damaged or not
         *                            well-formed
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         */
-       public function processMessage ($messageContent, Receivable $packageInstance) {
+       public function processMessage (array $messageData, Receivable $packageInstance) {
                // Process message generic
-               $this->genericProcessMessage('announcement', $messageContent, $packageInstance);
+               $this->genericProcessMessage('announcement', $messageData, $packageInstance);
        }
 }
 
index addd1af7bb5c4daf5cb914b8a11e917e56b37861..0d8c5aab848c7d2e07a08233878a2070e2102740 100644 (file)
@@ -78,13 +78,13 @@ class PackageDhtBootstrapTagFilter extends BaseNodeFilter implements FilterableP
         * XmlParserException       - If the XML message is damaged or not
         *                            well-formed
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         */
-       public function processMessage ($messageContent, Receivable $packageInstance) {
+       public function processMessage (array $messageData, Receivable $packageInstance) {
                // Process message generic
-               $this->genericProcessMessage('dht_bootstrap', $messageContent, $packageInstance);
+               $this->genericProcessMessage('dht_bootstrap', $messageData, $packageInstance);
        }
 }
 
index e8e03736aab04ec808955ed7562cefb144978e2b..d01da6b3690ed4382ae788565f430c41cc1a4f37 100644 (file)
@@ -67,13 +67,13 @@ class PackageRequestNodeListTagFilter extends BaseNodeFilter implements Filterab
        /**
         * Processes the given raw message content.
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         */
-       public function processMessage ($messageContent, Receivable $packageInstance) {
+       public function processMessage (array $messageData, Receivable $packageInstance) {
                // Process messasge generic
-               $this->genericProcessMessage('request_node_list', $messageContent, $packageInstance);
+               $this->genericProcessMessage('request_node_list', $messageData, $packageInstance);
        }
 }
 
index 2dab1525cb47ecd58c62137f8aabf76d4dc41215..2211d4b290d2dce49948ac7b608f89ba60b8b844 100644 (file)
@@ -75,13 +75,13 @@ class PackageSelfConnectTagFilter extends BaseNodeFilter implements FilterablePa
         * XmlParserException       - If the XML message is damaged or not
         *                            well-formed
         *
-        * @param       $messageContent         Raw message content
+        * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
         */
-       public function processMessage ($messageContent, Receivable $packageInstance) {
+       public function processMessage (array $messageData, Receivable $packageInstance) {
                // Process generic
-               $this->genericProcessMessage('self_connect', $messageContent, $packageInstance);
+               $this->genericProcessMessage('self_connect', $messageData, $packageInstance);
        }
 }
 
index d0827b789976686d79b8cda139af2f83fd3f6fd8..75df73577e979f9c17b16a1ba03be8ac571b3697 100644 (file)
@@ -91,6 +91,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        const PACKAGE_CONTENT_MESSAGE   = 'message';
        const PACKAGE_CONTENT_TAGS      = 'tags';
        const PACKAGE_CONTENT_CHECKSUM  = 'checksum';
+       const PACKAGE_CONTENT_SENDER    = 'sender';
+       const PACKAGE_CONTENT_HASH      = 'hash';
 
        /**
         * Named array elements for package data
@@ -1247,7 +1249,11 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        // Tags as an indexed array for "tagging" the message
                        self::PACKAGE_CONTENT_TAGS      => explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]),
                        // Checksum of the _decoded_ data
-                       self::PACKAGE_CONTENT_CHECKSUM  => $decodedContent[self::INDEX_CHECKSUM]
+                       self::PACKAGE_CONTENT_CHECKSUM  => $decodedContent[self::INDEX_CHECKSUM],
+                       // Sender's id
+                       self::PACKAGE_CONTENT_SENDER    => $decodedData[self::PACKAGE_DATA_SENDER],
+                       // Hash from decoded raw data
+                       self::PACKAGE_CONTENT_HASH      => $decodedData[self::PACKAGE_DATA_HASH]
                );
 
                // Is the checksum valid?
@@ -1296,7 +1302,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * Process the message through all filters, note that all other
                 * elements from $decodedContent are no longer needed.
                 */
-               $chainInstance->processMessage($decodedContent[self::PACKAGE_CONTENT_MESSAGE], $this);
+               $chainInstance->processMessage($decodedContent, $this);
        }
 
        /**
@@ -1333,6 +1339,37 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                // Handle message data
                $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this);
        }
+
+       /**
+        * Feeds the hash and sender (as recipient for the 'sender' reward) to the
+        * miner's queue, unless the message is not a "reward claim" message as this
+        * leads to an endless loop. You may wish to run the miner to get some
+        * reward ("HubCoins") for "mining" this hash.
+        *
+        * @param       $decodedDataArray       Array with decoded message
+        * @return      void
+        * @todo        ~10% done?
+        */
+       public function feedHashToMiner (array $decodedDataArray) {
+               // Make sure the required elements are there
+               assert(isset($decodedDataArray[self::PACKAGE_CONTENT_SENDER]));
+               assert(isset($decodedDataArray[self::PACKAGE_CONTENT_HASH]));
+               assert(isset($decodedDataArray[self::PACKAGE_CONTENT_TAGS]));
+
+               // Resolve session id ('sender' is a session id) into node id
+               $nodeId = HubTools::resolveNodeIdBySessionId($decodedDataArray[self::PACKAGE_CONTENT_SENDER]);
+
+               // Is 'claim_reward' the message type?
+               if (in_array('claim_reward', $decodedDataArray[self::PACKAGE_CONTENT_TAGS])) {
+                       /*
+                        * Then don't feed this message to the miner as this causes and
+                        * endless loop of mining.
+                        */
+                       return;
+               } // END - if
+
+               $this->partialStub('@TODO nodeId=' . $nodeId . ',decodedDataArray=' . print_r($decodedDataArray, TRUE));
+       }
 }
 
 // [EOF]
index 5f31cff62b6e8d23ff1cd4828602af30ec798558..c5ad36a46d00060c31620d5ee664a264bbedca82 100644 (file)
@@ -119,6 +119,26 @@ class HubTools extends BaseHubSystem {
                return $recipientUniversalNodeLocator;
        }
 
+       /**
+        * Resolves a session id into a node id by asking local DHT.
+        *
+        * @param       $sessionId      Session id
+        * @return      $nodeId         Node id
+        */
+       public static function resolveNodeIdBySessionId ($sessionId) {
+               // Get an own instance
+               $selfInstance = self::getSelfInstance();
+
+               // And ask it for session id by given Universal Node Locator
+               $nodeData = $selfInstance->getDhtInstance()->findNodeLocalBySessionId($sessionId);
+
+               // Make sure the node id is there
+               assert(isset($nodeData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID]));
+
+               // Return it
+               return $nodeData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID];
+       }
+
        /**
         * Resolves a Universal Node Locator into a session id. The "opposite" method
         * is resolveUniversalNodeLocatorBySessionId().