* A general message handler, this class must be abstract to make the template
* method pattern working.
*
- * @author Roland Haeder <webmaster@ship-simu.org>
+ * @author Roland Haeder <webmaster@shipsimu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
+ * @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
+ * @link http://www.shipsimu.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
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-abstract class BaseMessageHandler extends BaseHandler {
- /**
- * Array with search criteria elements
- */
- private $searchData = array();
-
- /**
- * Array with all data XML nodes (which hold the actual data) and their values
- */
- protected $messageDataElements = array();
-
- /**
- * Array for translating message data elements (other node's data mostly)
- * into configuration elements.
- */
- protected $messageToConfig = array();
-
- /**
- * Last exception instance from database layer or NULL (default)
- */
- private $lastException = NULL;
-
+abstract class BaseMessageHandler extends BaseDataHandler {
/**
* Protected constructor
*
protected function __construct ($className) {
// Call parent constructor
parent::__construct($className);
-
- // Init array
- $this->searchData = array(
- XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
- XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP,
- XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT
- );
- }
-
- /**
- * Getter for last exception
- *
- * @return $lastException Last thrown exception
- */
- protected final function getLastException () {
- return $this->lastException;
}
/**
*/
protected function getTranslatedStatusFromLastException () {
// Default is all fine
- $statusCode = self::MESSAGE_STATUS_CODE_OKAY;
+ $statusCode = BaseHubSystem::MESSAGE_STATUS_CODE_OKAY;
// Is the last exception not NULL?
- if ($this->lastException instanceof FrameworkException) {
+ if ($this->getLastException() instanceof FrameworkException) {
// "Determine" the right status code (may differ from exception to exception)
- $this->debugInstance('lastException=' . $this->lastException->__toString() . ',message=' . $this->lastException->getMessage() . ' is not finished!');
+ $this->debugInstance('lastException=' . $this->getLastException()->__toString() . ',message=' . $this->getLastException()->getMessage() . ' is not finished!');
} // END - if
// Return the status code
* following data must always be present:
*
* - session-id (for finding the node's record together with below data)
- * - external-ip (hostname or IP number)
- * - tcp-port (TCP port for inbound connections)
+ * - external-address (hostname or IP number)
+ * - listen-port (TCP/UDP listen port for inbound connections)
*
* @param $messageArray An array with all minimum message data
* @return void
*/
protected function registerNodeByMessageData (array $messageData) {
- // Get a wrapper instance
- $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
-
- // Get a search criteria class
- $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
- // Search for the node's session id and external IP/hostname + TCP port
- foreach ($this->searchData as $key) {
- // Add criteria
- $searchInstance->addCriteria('node_' . $key, $messageData[$key]);
- } // END - foreach
-
- // Only one entry is fine
- $searchInstance->setLimit(1);
+ // Check if searchData has entries
+ assert(count($this->getSearchData()) > 0);
- // Run the query
- $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
-
- // Is there already an entry?
- if ($resultInstance->next()) {
- // Entry found
- $resultInstance->debugBackTrace('Entry found!');
- } else {
- // Nothing found, so register it
- $wrapperInstance->registerNodeByMessageData($messageData, $this);
- }
-
- // Save last exception
- $this->lastException = $wrapperInstance->getLastException();
+ // Let the DHT facade do the work
+ $this->getDhtInstance()->registerNodeByMessageData($messageData, $this);
}
/**
- * Prepares a message as answer for given message data for delivery.
+ * Posty-handles data array of the message
*
- * @param $messageData An array with all message data
- * @param $packageInstance An instance of a Deliverable instance
+ * @param $messageData An array with message data to handle
+ * @param $packageInstance An instance of a Receivable class
* @return void
*/
- protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
- // Get a helper instance based on this handler's name
- $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
-
- // Load descriptor XML
- $helperInstance->loadDescriptorXml();
-
- /*
- * Set missing (temporary) configuration data, mostly it needs to be
- * copied from message data array.
- */
- $this->initMessageConfigurationData($messageData);
-
- // Compile any configuration variables
- $helperInstance->getTemplateInstance()->compileConfigInVariables();
-
- // Get node instance
- $nodeInstance = Registry::getRegistry()->getInstance('node');
-
- // Deliver the package
- $helperInstance->sendPackage($nodeInstance);
-
+ public function postHandleMessageData (array $messageData, Receivable $packageInstance) {
/*
- * Remove temporary configuration
+ * Feed hash to miner by handling over the whole array as also the
+ * sender and tags are needed.
*/
- $this->removeMessageConfigurationData($messageData);
+ $packageInstance->feedHashToMiner($messageData);
}
-
- /**
- * Initializes configuration data from given message data array
- *
- * @param $messageData An array with all message data
- * @return void
- */
- abstract protected function initMessageConfigurationData (array $messageData);
-
- /**
- * Removes configuration data with given message data array from global
- * configuration
- *
- * @param $messageData An array with all message data
- * @return void
- */
- abstract protected function removeMessageConfigurationData (array $messageData);
}
// [EOF]