From: Roland Haeder Date: Sun, 24 May 2015 18:14:53 +0000 (+0200) Subject: The package hash needs to be copied to the message array as it needs to be X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=edd299429d84e0956d14ade73a26e894e3e5c313;p=hub.git The package hash needs to be copied to the message array as it needs to be 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 --- diff --git a/application/hub/interfaces/filter/class_FilterablePackage.php b/application/hub/interfaces/filter/class_FilterablePackage.php index 3f6c3e215..d60f2510e 100644 --- a/application/hub/interfaces/filter/class_FilterablePackage.php +++ b/application/hub/interfaces/filter/class_FilterablePackage.php @@ -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] diff --git a/application/hub/interfaces/package/class_Receivable.php b/application/hub/interfaces/package/class_Receivable.php index 9fd5226a7..5cf792583 100644 --- a/application/hub/interfaces/package/class_Receivable.php +++ b/application/hub/interfaces/package/class_Receivable.php @@ -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] diff --git a/application/hub/main/chains/class_PackageFilterChain.php b/application/hub/main/chains/class_PackageFilterChain.php index 28cc63f2b..c79b62ef9 100644 --- a/application/hub/main/chains/class_PackageFilterChain.php +++ b/application/hub/main/chains/class_PackageFilterChain.php @@ -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] diff --git a/application/hub/main/factories/chain/class_PackageFilterChainFactory.php b/application/hub/main/factories/chain/class_PackageFilterChainFactory.php index ba3bc4df3..e07b77126 100644 --- a/application/hub/main/factories/chain/class_PackageFilterChainFactory.php +++ b/application/hub/main/factories/chain/class_PackageFilterChainFactory.php @@ -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); diff --git a/application/hub/main/filter/class_BaseHubFilter.php b/application/hub/main/filter/class_BaseHubFilter.php index dcfb5a465..017b81d83 100644 --- a/application/hub/main/filter/class_BaseHubFilter.php +++ b/application/hub/main/filter/class_BaseHubFilter.php @@ -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 diff --git a/application/hub/main/filter/package/class_PackageHashToMinerFilter.php b/application/hub/main/filter/package/class_PackageHashToMinerFilter.php index 4fe8168ec..12e724f7e 100644 --- a/application/hub/main/filter/package/class_PackageHashToMinerFilter.php +++ b/application/hub/main/filter/package/class_PackageHashToMinerFilter.php @@ -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); } } diff --git a/application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php b/application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php index 315e38493..7daf9ee59 100644 --- a/application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php +++ b/application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php @@ -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] diff --git a/application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php b/application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php index fc35994fd..2b5c4dd75 100644 --- a/application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php +++ b/application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php @@ -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] diff --git a/application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php b/application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php index 3f26eadaa..33d643fac 100644 --- a/application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php +++ b/application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php @@ -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] diff --git a/application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php b/application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php index 11d6aa989..2c92ba1cc 100644 --- a/application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php +++ b/application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php @@ -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] diff --git a/application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php b/application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php index 0d8c5aab8..c40c2c9e8 100644 --- a/application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php +++ b/application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php @@ -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] diff --git a/application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php b/application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php index d01da6b36..71c45c448 100644 --- a/application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php +++ b/application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php @@ -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] diff --git a/application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php b/application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php index 2211d4b29..bd75e4505 100644 --- a/application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php +++ b/application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php @@ -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] diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index c1d8f059a..7eb2c4d94 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -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)); } } diff --git a/application/hub/main/tools/class_HubTools.php b/application/hub/main/tools/class_HubTools.php index c5ad36a46..37636f29c 100644 --- a/application/hub/main/tools/class_HubTools.php +++ b/application/hub/main/tools/class_HubTools.php @@ -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 df57993ff..e10ada14e 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit df57993ff615192c023c714c46dc5d52d3dffd89 +Subproject commit e10ada14e2f86607a9d165dac6f7fb154e0be4eb