]> git.mxchange.org Git - hub.git/commitdiff
The package hash needs to be copied to the message array as it needs to be
authorRoland Haeder <roland@mxchange.org>
Sun, 24 May 2015 18:14:53 +0000 (20:14 +0200)
committerRoland Haeder <roland@mxchange.org>
Sun, 24 May 2015 18:14:53 +0000 (20:14 +0200)
handled over to the miner after the message (e.g. initial announcement message)
has been processed and handled.

For example, when the node is booting, the announcement will contain the node
id. The node id is required to assign the "mining reward" to the proper node
again. Because of this, the announcement message must be handled before the
hash is being handled over to the miner.

Signed-off-by: Roland Haeder <roland@mxchange.org>
16 files changed:
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/class_PackageHashToMinerFilter.php
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
core

index 3f6c3e215e74553c5e4a1f53e10499560c7ae2bd..d60f2510ec43de13576d92c03ba6793e8fb1b69c 100644 (file)
@@ -30,6 +30,15 @@ interface FilterablePackage extends Filterable {
         * @return      void
         */
        function processMessage (array $messageData, Receivable $packageInstance);
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        */
+       function postProcessMessage (Receivable $packageInstance);
 }
 
 // [EOF]
index 9fd5226a70bba99336f8b7881977e60a48b38002..5cf792583721b403deb225f1696687eb88453d2c 100644 (file)
@@ -141,10 +141,10 @@ interface Receivable extends FrameworkInterface {
         * 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
+        * @param       $messageData    Array with message data
         * @return      void
         */
-       function feedHashToMiner (array $decodedDataArray);
+       function feedHashToMiner (array $messageData);
 }
 
 // [EOF]
index 28cc63f2b8b1e5d074a79fd4aafd8de7df2689d3..c79b62ef986f0d93c1ffaf3071fd95e73d786666 100644 (file)
@@ -66,6 +66,27 @@ class PackageFilterChain extends FilterChain {
                        }
                } // END - foreach
        }
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Get all filters and "run" them
+               foreach ($this->getPostFilters() as $filterInstance) {
+                       // Try to process it
+                       try {
+                               $filterInstance->postProcessMessage($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());
+                               break;
+                       }
+               } // END - foreach
+       }
 }
 
 // [EOF]
index ba3bc4df3ad4e7ab7f881a563ca03a2a408ee0dd..e07b77126a683eaf717e4eb62d583b55dce42feb 100644 (file)
@@ -67,8 +67,8 @@ class PackageFilterChainFactory extends ObjectFactory {
                        // 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 this post-filter to the chain
+                       $chainInstance->addPostFilter($filterInstance);
 
                        // Add the finished chain to the registry
                        Registry::getRegistry()->addInstance($registryKey, $chainInstance);
index dcfb5a4650708307e3945eadc31fcb5bef2a2fd1..017b81d8306500d434c34fe2237a2699e20f9caa 100644 (file)
@@ -58,6 +58,9 @@ class BaseHubFilter extends BaseFilter {
        protected function genericProcessMessage ($messageType, array $messageData, Receivable $packageInstance) {
                // Make sure the wanted element is there
                assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE]));
+               assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_SENDER]));
+               assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_HASH]));
+               assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_TAGS]));
 
                // Get a template instance from the factory
                $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_' . $messageType . '_template_class');
@@ -103,9 +106,15 @@ class BaseHubFilter extends BaseFilter {
                // Construct an array for pushing it on next stack
                $messageArray = array(
                        // Message data itself
-                       NetworkPackage::MESSAGE_ARRAY_DATA => $this->dataXmlNodes,
+                       NetworkPackage::MESSAGE_ARRAY_DATA   => $this->dataXmlNodes,
                        // Message type (which is $messageType)
-                       NetworkPackage::MESSAGE_ARRAY_TYPE => $messageType
+                       NetworkPackage::MESSAGE_ARRAY_TYPE   => $messageType,
+                       // Message sender
+                       NetworkPackage::MESSAGE_ARRAY_SENDER => $messageData[NetworkPackage::PACKAGE_CONTENT_SENDER],
+                       // Package hash
+                       NetworkPackage::MESSAGE_ARRAY_SENDER => $messageData[NetworkPackage::PACKAGE_CONTENT_HASH],
+                       // Package tags
+                       NetworkPackage::MESSAGE_ARRAY_SENDER => $messageData[NetworkPackage::PACKAGE_CONTENT_TAGS],
                );
 
                // Push the processed message back on stack
index 4fe8168ec6773d628edddf9f4cf376a1e1e5b644..12e724f7e84802d32b9f1d85b2932b7af31391ac 100644 (file)
@@ -60,22 +60,33 @@ class PackageHashToMinerFilter extends BaseHubFilter implements FilterablePackag
        }
 
        /**
-        * 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
+        * Processes the given raw message content.
         *
         * @param       $messageData            Raw message data array
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
+        * @throws      UnsupportedOperationException   If this method is called, please use processMessage() instead!
         */
        public function processMessage (array $messageData, Receivable $packageInstance) {
-               // Process message generic
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
+
+       /**
+        * Post-processes the given raw message content. The method renderXmlContent
+        * may throw (not the method itself) several exceptions:
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Get current entry from stack
+               $messageData = $packageInstance->getStackInstance()->getNamed(NetworkPackage::STACKER_NAME_PROCESSED_MESSAGE);
+
+               /*
+                * Feed hash to miner by handling over the whole array as also the
+                * sender and tags are needed.
+                */
                $packageInstance->feedHashToMiner($messageData);
        }
 }
index 315e384936ef998861f163f94b64d563c38441c9..7daf9ee59f162b2ec3120ea981db24b9436125ae 100644 (file)
@@ -79,6 +79,19 @@ class PackageAnnouncementAnswerTagFilter extends BaseNodeFilter implements Filte
                // Process message in generic way
                $this->genericProcessMessage('announcement_answer', $messageData, $packageInstance);
        }
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        * @throws      UnsupportedOperationException   If this method is called, please use processMessage() instead!
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
 }
 
 // [EOF]
index fc35994fd4db2255ac910ac927c49d7a8e2a3d7a..2b5c4dd759429d69601cfaa7f7df3a8d87eeedb4 100644 (file)
@@ -77,6 +77,19 @@ class PackageDhtBootstrapAnswerTagFilter extends BaseNodeFilter implements Filte
                // Process message in generic way
                $this->genericProcessMessage('dht_bootstrap_answer', $messageData, $packageInstance);
        }
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        * @throws      UnsupportedOperationException   If this method is called, please use processMessage() instead!
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
 }
 
 // [EOF]
index 3f26eadaac30ffaa13c4628662e6a77f635cd1d3..33d643fac4ec6699c44fd5892d32f32651f94124 100644 (file)
@@ -77,6 +77,19 @@ class PackageRequestNodeListAnswerTagFilter extends BaseNodeFilter implements Fi
                // Process message in generic way
                $this->genericProcessMessage('request_node_list_answer', $messageData, $packageInstance);
        }
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        * @throws      UnsupportedOperationException   If this method is called, please use processMessage() instead!
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
 }
 
 // [EOF]
index 11d6aa989e59182c5b40788ed9b37c5f19492597..2c92ba1ccbc809b2358a4e1a1018534411d0c97c 100644 (file)
@@ -87,6 +87,19 @@ class PackageAnnouncementTagFilter extends BaseNodeFilter implements FilterableP
                // Process message generic
                $this->genericProcessMessage('announcement', $messageData, $packageInstance);
        }
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        * @throws      UnsupportedOperationException   If this method is called, please use processMessage() instead!
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
 }
 
 // [EOF]
index 0d8c5aab848c7d2e07a08233878a2070e2102740..c40c2c9e86884a3b8ca861fd4d6e8a784b33e638 100644 (file)
@@ -86,6 +86,19 @@ class PackageDhtBootstrapTagFilter extends BaseNodeFilter implements FilterableP
                // Process message generic
                $this->genericProcessMessage('dht_bootstrap', $messageData, $packageInstance);
        }
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        * @throws      UnsupportedOperationException   If this method is called, please use processMessage() instead!
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
 }
 
 // [EOF]
index d01da6b3690ed4382ae788565f430c41cc1a4f37..71c45c448d9e18206962a8d217d17e38191c8fdb 100644 (file)
@@ -75,6 +75,19 @@ class PackageRequestNodeListTagFilter extends BaseNodeFilter implements Filterab
                // Process messasge generic
                $this->genericProcessMessage('request_node_list', $messageData, $packageInstance);
        }
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        * @throws      UnsupportedOperationException   If this method is called
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
 }
 
 // [EOF]
index 2211d4b290d2dce49948ac7b608f89ba60b8b844..bd75e45058683ea9d61b719e21a86444b4d83b7c 100644 (file)
@@ -83,6 +83,19 @@ class PackageSelfConnectTagFilter extends BaseNodeFilter implements FilterablePa
                // Process generic
                $this->genericProcessMessage('self_connect', $messageData, $packageInstance);
        }
+
+       /**
+        * Post-processes the stacked message. Do not call popNamed() as then no
+        * other class can process the message.
+        *
+        * @param       $packageInstance        An instance of a Receivable class
+        * @return      void
+        * @throws      UnsupportedOperationException   If this method is called, please use processMessage() instead!
+        */
+       public function postProcessMessage (Receivable $packageInstance) {
+               // Please don't call this method
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
 }
 
 // [EOF]
index c1d8f059a17781c9d5d1c8b31b963fbccf46227d..7eb2c4d9467a211111526bafce68bc0c4e2a7ac6 100644 (file)
@@ -117,8 +117,11 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        /**
         * Constants for message data array
         */
-       const MESSAGE_ARRAY_DATA = 'message_data';
-       const MESSAGE_ARRAY_TYPE = 'message_type';
+       const MESSAGE_ARRAY_DATA   = 'message_data';
+       const MESSAGE_ARRAY_TYPE   = 'message_type';
+       const MESSAGE_ARRAY_SENDER = 'message_sender';
+       const MESSAGE_ARRAY_HASH   = 'message_hash';
+       const MESSAGE_ARRAY_TAGS   = 'message_tags';
 
        /**
         * Generic answer status field
@@ -1334,6 +1337,9 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @todo        Implement verification of all sent tags here?
         */
        public function handleNewlyArrivedMessage () {
+               // Make sure there is at least one message
+               assert($this->isNewMessageArrived());
+
                // Get it from the stacker, it is the full array with the decoded message
                $decodedContent = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
 
@@ -1351,6 +1357,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * elements from $decodedContent are no longer needed.
                 */
                $chainInstance->processMessage($decodedContent, $this);
+
+               /*
+                * Post-processing of message data (this won't remote the message from
+                * the stack).
+                */
+               $chainInstance->postProcessMessage();
        }
 
        /**
@@ -1394,21 +1406,24 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * 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
+        * @param       $messageData    Array with message data
         * @return      void
         * @todo        ~10% done?
         */
-       public function feedHashToMiner (array $decodedDataArray) {
+       public function feedHashToMiner (array $messageData) {
                // 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]));
+               assert(isset($messageData[self::MESSAGE_ARRAY_SENDER]));
+               assert(isset($messageData[self::MESSAGE_ARRAY_HASH]));
+               assert(isset($messageData[self::MESSAGE_ARRAY_TAGS]));
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: messageData=' . print_r($messageData, TRUE));
 
                // Resolve session id ('sender' is a session id) into node id
-               $nodeId = HubTools::resolveNodeIdBySessionId($decodedDataArray[self::PACKAGE_CONTENT_SENDER]);
+               $nodeId = HubTools::resolveNodeIdBySessionId($messageData[self::MESSAGE_ARRAY_SENDER]);
 
                // Is 'claim_reward' the message type?
-               if (in_array('claim_reward', $decodedDataArray[self::PACKAGE_CONTENT_TAGS])) {
+               if (in_array('claim_reward', $messageData[self::MESSAGE_ARRAY_TAGS])) {
                        /*
                         * Then don't feed this message to the miner as this causes an
                         * endless loop of mining.
@@ -1416,7 +1431,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        return;
                } // END - if
 
-               $this->partialStub('@TODO nodeId=' . $nodeId . ',decodedDataArray=' . print_r($decodedDataArray, TRUE));
+               $this->partialStub('@TODO nodeId=' . $nodeId . ',messageData=' . print_r($messageData, TRUE));
        }
 }
 
index c5ad36a46d00060c31620d5ee664a264bbedca82..37636f29c74a8c67303b3437039c1ea5246a20ef 100644 (file)
@@ -133,6 +133,7 @@ class HubTools extends BaseHubSystem {
                $nodeData = $selfInstance->getDhtInstance()->findNodeLocalBySessionId($sessionId);
 
                // Make sure the node id is there
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('HUB-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: sessionId=' . $sessionId . ', nodeData[' . gettype($nodeData) . ']=' . print_r($nodeData, TRUE));
                assert(isset($nodeData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID]));
 
                // Return it
diff --git a/core b/core
index df57993ff615192c023c714c46dc5d52d3dffd89..e10ada14e2f86607a9d165dac6f7fb154e0be4eb 160000 (submodule)
--- a/core
+++ b/core
@@ -1 +1 @@
-Subproject commit df57993ff615192c023c714c46dc5d52d3dffd89
+Subproject commit e10ada14e2f86607a9d165dac6f7fb154e0be4eb