hash + sender over to the miner for "mining" the hash.
Signed-off-by: Roland Haeder <roland@mxchange.org>
// 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');
/**
* 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]
* @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]
/**
* 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());
$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);
}
* 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);
--- /dev/null
+Deny from all
--- /dev/null
+<?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]
+?>
* 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);
}
}
* 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);
}
}
* 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);
}
}
* 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);
}
}
* 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);
}
}
/**
* 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);
}
}
* 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);
}
}
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
// 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?
* 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);
}
/**
// 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]
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().