From: Roland Haeder Date: Mon, 11 May 2015 21:16:33 +0000 (+0200) Subject: Rewrote processMessage() to accept whole arrays + added filter for handling X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=79acde36944b08ef27370877a9844e40a0fa26ff;p=hub.git Rewrote processMessage() to accept whole arrays + added filter for handling hash + sender over to the miner for "mining" the hash. Signed-off-by: Roland Haeder --- diff --git a/application/hub/config.php b/application/hub/config.php index 8c9d47e7b..9f26d4909 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -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'); diff --git a/application/hub/interfaces/filter/class_FilterablePackage.php b/application/hub/interfaces/filter/class_FilterablePackage.php index 905113096..3f6c3e215 100644 --- a/application/hub/interfaces/filter/class_FilterablePackage.php +++ b/application/hub/interfaces/filter/class_FilterablePackage.php @@ -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] diff --git a/application/hub/interfaces/package/class_Receivable.php b/application/hub/interfaces/package/class_Receivable.php index 126247bdf..9fd5226a7 100644 --- a/application/hub/interfaces/package/class_Receivable.php +++ b/application/hub/interfaces/package/class_Receivable.php @@ -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] diff --git a/application/hub/main/chains/class_PackageFilterChain.php b/application/hub/main/chains/class_PackageFilterChain.php index dde86c0fe..28cc63f2b 100644 --- a/application/hub/main/chains/class_PackageFilterChain.php +++ b/application/hub/main/chains/class_PackageFilterChain.php @@ -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()); diff --git a/application/hub/main/factories/chain/class_PackageFilterChainFactory.php b/application/hub/main/factories/chain/class_PackageFilterChainFactory.php index 9cd32b50e..ba3bc4df3 100644 --- a/application/hub/main/factories/chain/class_PackageFilterChainFactory.php +++ b/application/hub/main/factories/chain/class_PackageFilterChainFactory.php @@ -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); } diff --git a/application/hub/main/filter/class_BaseHubFilter.php b/application/hub/main/filter/class_BaseHubFilter.php index fbde97be8..dcfb5a465 100644 --- a/application/hub/main/filter/class_BaseHubFilter.php +++ b/application/hub/main/filter/class_BaseHubFilter.php @@ -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 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/filter/package/.htaccess @@ -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 index 000000000..4fe8168ec --- /dev/null +++ b/application/hub/main/filter/package/class_PackageHashToMinerFilter.php @@ -0,0 +1,84 @@ + + * @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 . + */ +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] +?> diff --git a/application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php b/application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php index 9fa200ceb..315e38493 100644 --- a/application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php +++ b/application/hub/main/filter/tags/answer/class_PackageAnnouncementAnswerTagFilter.php @@ -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); } } diff --git a/application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php b/application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php index b3bd4cc18..fc35994fd 100644 --- a/application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php +++ b/application/hub/main/filter/tags/answer/class_PackageDhtBootstrapAnswerTagFilter.php @@ -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); } } diff --git a/application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php b/application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php index abc00a904..3f26eadaa 100644 --- a/application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php +++ b/application/hub/main/filter/tags/answer/class_PackageRequestNodeListAnswerTagFilter.php @@ -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); } } diff --git a/application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php b/application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php index 3d78a25d8..d9519b31e 100644 --- a/application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php +++ b/application/hub/main/filter/tags/class_PackageAnnouncementTagFilter.php @@ -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); } } diff --git a/application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php b/application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php index addd1af7b..0d8c5aab8 100644 --- a/application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php +++ b/application/hub/main/filter/tags/class_PackageDhtBootstrapTagFilter.php @@ -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); } } diff --git a/application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php b/application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php index e8e03736a..d01da6b36 100644 --- a/application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php +++ b/application/hub/main/filter/tags/class_PackageRequestNodeListTagFilter.php @@ -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); } } diff --git a/application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php b/application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php index 2dab1525c..2211d4b29 100644 --- a/application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php +++ b/application/hub/main/filter/tags/class_PackageSelfConnectTagFilter.php @@ -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); } } diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index d0827b789..75df73577 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -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] diff --git a/application/hub/main/tools/class_HubTools.php b/application/hub/main/tools/class_HubTools.php index 5f31cff62..c5ad36a46 100644 --- a/application/hub/main/tools/class_HubTools.php +++ b/application/hub/main/tools/class_HubTools.php @@ -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().