3 namespace Org\Shipsimu\Hub\Handler\Node\Answer\Announcement;
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory;
7 use Org\Shipsimu\Hub\Handler\Message\BaseMessageHandler;
8 use Org\Shipsimu\Hub\Handler\Message\HandleableMessage;
9 use Org\Shipsimu\Hub\Network\Message\DeliverableMessage;
10 use Org\Shipsimu\Hub\Network\Receive\Receivable;
11 use Org\Shipsimu\Hub\Template\Engine\Xml\Announcement\XmlAnnouncementTemplateEngine;
12 use Org\Shipsimu\Hub\Template\Engine\Xml\Answer\Announcement\XmlAnnouncementAnswerTemplateEngine;
13 use Org\Shipsimu\Hub\Template\Engine\Xml\Network\Answer\BaseXmlAnswerTemplateEngine;
15 // Import framework stuff
16 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
17 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
18 use Org\Mxchange\CoreFramework\Registry\Registerable;
21 * A NodeMessageAnnouncementAnswer handler
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class NodeMessageAnnouncementAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
44 * Protected constructor
48 protected function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
53 $this->setHandlerName('message_announcement_answer');
55 // Init message data array
56 $this->messageDataElements = array(
57 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
58 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS,
59 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
60 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID,
61 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
62 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH,
63 BaseXmlAnswerTemplateEngine::ANSWER_STATUS,
66 // Init message-data->configuration translation array
67 $this->messageToConfig = array(
69 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address',
70 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address',
71 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id'
72 XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash'
76 // Init config-copy array
77 $this->configCopy = array(
78 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address',
79 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address',
80 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status',
81 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id',
82 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash',
86 $this->searchData = array(
87 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
88 XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS,
93 * Creates an instance of this class
95 * @return $handlerInstance An instance of a HandleableMessage class
97 public final static function createNodeMessageAnnouncementAnswerHandler () {
99 $handlerInstance = new NodeMessageAnnouncementAnswerHandler();
101 // Return the prepared instance
102 return $handlerInstance;
106 * Handles data array of the message
108 * @param $messageInstance An instance of a DeliverableMessage class
109 * @param $packageInstance An instance of a Receivable class
111 * @throws NoAnnouncementAttemptedException If this node has not attempted to announce itself
113 public function handleMessageData (DeliverableMessage $messageInstance, Receivable $packageInstance) {
115 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-MESSAGE-HANDLER: Creating node instance ...');
116 $nodeInstance = NodeObjectFactory::createNodeInstance();
118 // Has this node attempted to announce itself?
119 if (!$nodeInstance->ifNodeIsAnnouncing()) {
121 * This node has never announced itself, so it doesn't expect
122 * announcement answer messages.
124 throw new NoAnnouncementAttemptedException(array($this, $nodeInstance, $messageInstance), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED);
127 // Register the announcing node with this node
128 $this->registerNodeByMessageInstance($messageInstance);
130 // Handle the answer status element
131 $nodeInstance->handleAnswerStatusByMessageInstance($messageInstance, $packageInstance);
135 * Adds all required elements from given array into data set instance
137 * @param $dataSetInstance An instance of a StoreableCriteria class
138 * @param $messageInstance An instance of a DeliverableMessage class
141 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, DeliverableMessage $messageInstance) {
143 parent::addArrayToDataSet($dataSetInstance, $messageInstance);
146 foreach ($this->messageDataElements as $key) {
148 assert(isset($messageData[$key]));
151 * Add it, but remove any 'my-' prefixes as they are not used in
154 $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
159 * Initializes configuration data from given message data array
161 * @param $messageInstance An instance of a DeliverableMessage class
163 * @throws UnsupportedOperationException If this method is called
165 protected function initMessageConfigurationData (DeliverableMessage $messageInstance) {
166 // Please don't call this method
167 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
171 * Removes configuration data with given message data array from global
174 * @param $messageInstance An instance of a DeliverableMessage class
176 * @throws UnsupportedOperationException If this method is called
178 protected function removeMessageConfigurationData (DeliverableMessage $messageInstance) {
179 // Please don't call this method
180 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);