From: Roland Häder Date: Thu, 17 May 2012 15:23:05 +0000 (+0000) Subject: Added handler for announcement messages (not yet functional) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4cddd33772b84ec7cdd3229169e545f1ec122b89;p=hub.git Added handler for announcement messages (not yet functional) --- diff --git a/.gitattributes b/.gitattributes index 0572547f5..eaca3b08a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -54,6 +54,8 @@ application/hub/interfaces/handler/.htaccess -text svneol=unset#text/plain application/hub/interfaces/handler/chunks/.htaccess -text svneol=unset#text/plain application/hub/interfaces/handler/chunks/class_HandleableChunks.php svneol=native#text/plain application/hub/interfaces/handler/class_Handleable.php svneol=native#text/plain +application/hub/interfaces/handler/message-types/.htaccess -text svneol=unset#text/plain +application/hub/interfaces/handler/message-types/class_HandleableMessage.php svneol=native#text/plain application/hub/interfaces/handler/network/.htaccess -text svneol=unset#text/plain application/hub/interfaces/handler/network/class_Networkable.php svneol=native#text/plain application/hub/interfaces/handler/task/.htaccess -text svneol=unset#text/plain @@ -207,6 +209,8 @@ application/hub/main/factories/discovery/.htaccess -text svneol=unset#text/plain application/hub/main/factories/discovery/class_PackageDiscoveryFactory.php svneol=native#text/plain application/hub/main/factories/discovery/class_SocketDiscoveryFactory.php svneol=native#text/plain application/hub/main/factories/fragmenter/.htaccess -text +application/hub/main/factories/handler/.htaccess -text svneol=unset#text/plain +application/hub/main/factories/handler/class_MessageTypeHandlerFactory.php svneol=native#text/plain application/hub/main/factories/lists/.htaccess -text svneol=unset#text/plain application/hub/main/factories/lists/class_RecipientListFactory.php svneol=native#text/plain application/hub/main/factories/package/.htaccess -text svneol=unset#text/plain @@ -290,6 +294,8 @@ application/hub/main/handler/chunks/.htaccess -text svneol=unset#text/plain application/hub/main/handler/chunks/class_ChunkHandler.php svneol=native#text/plain application/hub/main/handler/class_ svneol=native#text/plain application/hub/main/handler/class_BaseHandler.php svneol=native#text/plain +application/hub/main/handler/message-types/.htaccess -text svneol=unset#text/plain +application/hub/main/handler/message-types/class_NodeMessageAnnouncementHandler.php svneol=native#text/plain application/hub/main/handler/network/.htaccess -text svneol=unset#text/plain application/hub/main/handler/network/class_ svneol=native#text/plain application/hub/main/handler/network/class_BaseRawDataHandler.php svneol=native#text/plain diff --git a/application/hub/config.php b/application/hub/config.php index 9a86fcda7..0b9c156bc 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -204,6 +204,9 @@ $cfg->setConfigEntry('package_tag_announcement_filter', 'PackageAnnouncementTagF // CFG: PACKAGE-TAG-SELF-CONNECT-FILTER $cfg->setConfigEntry('package_tag_self_connect_filter', 'PackageSelfConnectTagFilter'); +// CFG: MESSAGE-TYPE-ANNOUNCEMENT-HANDLER-CLASS +$cfg->setConfigEntry('message_type_announcement_handler_class', 'NodeMessageAnnouncementHandler'); + // CFG: NEWS-READER-CLASS $cfg->setConfigEntry('news_reader_class', 'ConsoleNewsReader'); diff --git a/application/hub/interfaces/handler/message-types/.htaccess b/application/hub/interfaces/handler/message-types/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/interfaces/handler/message-types/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/interfaces/handler/message-types/class_HandleableMessage.php b/application/hub/interfaces/handler/message-types/class_HandleableMessage.php new file mode 100644 index 000000000..9f55c5963 --- /dev/null +++ b/application/hub/interfaces/handler/message-types/class_HandleableMessage.php @@ -0,0 +1,36 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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 . + */ +interface HandleableMessage extends Handleable { + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + */ + function handleMessageData (array $messageData, Receivable $packageInstance); +} + +// [EOF] +?> diff --git a/application/hub/interfaces/package/class_Receivable.php b/application/hub/interfaces/package/class_Receivable.php index 09b74585f..0a6dcb384 100644 --- a/application/hub/interfaces/package/class_Receivable.php +++ b/application/hub/interfaces/package/class_Receivable.php @@ -68,6 +68,20 @@ interface Receivable extends FrameworkInterface { */ function handleNewlyArrivedMessage (); + /** + * Checks whether a processed message is pending for "interpretation" + * + * @return $isPending Whether a processed message is pending + */ + function isProcessedMessagePending (); + + /** + * Handle processed messages by "interpreting" the 'message_type' element + * + * @return void + */ + function handleProcessedMessage (); + /** * Adds raw decoded data from the given handler instance to this receiver * diff --git a/application/hub/main/factories/handler/.htaccess b/application/hub/main/factories/handler/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/factories/handler/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/factories/handler/class_MessageTypeHandlerFactory.php b/application/hub/main/factories/handler/class_MessageTypeHandlerFactory.php new file mode 100644 index 000000000..4a7591cb2 --- /dev/null +++ b/application/hub/main/factories/handler/class_MessageTypeHandlerFactory.php @@ -0,0 +1,62 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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 MessageTypeHandlerFactory extends ObjectFactory { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Returns a singleton message type handler instance. If an instance is + * found in registry, it will be returned, else a new instance is created + * and stored in the same registry entry. + * + * @param $messageType Message type to create an object from + * @return $handlerInstance A message type handler instance + */ + public static final function createMessageTypeHandlerInstance ($messageType) { + // Do we have an instance in the registry? + if (Registry::getRegistry()->instanceExists('message_type_' . $messageType . '_handler')) { + // Then use this instance + $handlerInstance = Registry::getRegistry()->getInstance('message_type_' . $messageType . '_handler'); + } else { + // Now prepare the tags instance + $handlerInstance = self::createObjectByConfiguredName('message_type_' . $messageType . '_handler_class'); + + // Set the instance in registry for further use + Registry::getRegistry()->addInstance('message_type_' . $messageType . '_handler', $handlerInstance); + } + + // Return the instance + return $handlerInstance; + } +} + +// [EOF] +?> diff --git a/application/hub/main/handler/class_ b/application/hub/main/handler/class_ index 630abf77e..09fb9fa3c 100644 --- a/application/hub/main/handler/class_ +++ b/application/hub/main/handler/class_ @@ -38,7 +38,7 @@ class ???Handler extends BaseHandler implements Handleable { /** * Creates an instance of this class * - * @return $handlerInstance An instance of a !!! class + * @return $handlerInstance An instance of a !!! class */ public final static function create???Handler () { // Get new instance diff --git a/application/hub/main/handler/message-types/.htaccess b/application/hub/main/handler/message-types/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/handler/message-types/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/handler/message-types/class_NodeMessageAnnouncementHandler.php b/application/hub/main/handler/message-types/class_NodeMessageAnnouncementHandler.php new file mode 100644 index 000000000..ef974da04 --- /dev/null +++ b/application/hub/main/handler/message-types/class_NodeMessageAnnouncementHandler.php @@ -0,0 +1,63 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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 NodeMessageAnnouncementHandler extends BaseHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_type_announcement'); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessageAnnouncementHandler () { + // Get new instance + $handlerInstance = new NodeMessageAnnouncementHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + } +} + +// [EOF] +?> diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index b209bd0aa..a6e0e8a67 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -1078,6 +1078,37 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R */ $chainInstance->processMessage($decodedContent[self::PACKAGE_CONTENT_MESSAGE], $this); } + + /** + * Checks whether a processed message is pending for "interpretation" + * + * @return $isPending Whether a processed message is pending + */ + public function isProcessedMessagePending () { + // Check it + $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE)); + + // Return it + return $isPending; + } + + /** + * Handle processed messages by "interpreting" the 'message_type' element + * + * @return void + */ + public function handleProcessedMessage () { + // Get it from the stacker, it is the full array with the processed message + $messageArray = $this->getStackerInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE); + + // Create a handler instance from given message type + $handlerInstance = MessageTypeHandlerFactory::createMessageTypeHandlerInstance($messageArray[self::MESSAGE_ARRAY_TYPE]); + + // Handle message data + $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this); + + die('messageArray='.print_r($messageArray,true)); + } } // [EOF] diff --git a/application/hub/main/tasks/network/class_NetworkPackageReaderTask.php b/application/hub/main/tasks/network/class_NetworkPackageReaderTask.php index 23410b6a9..9b08be432 100644 --- a/application/hub/main/tasks/network/class_NetworkPackageReaderTask.php +++ b/application/hub/main/tasks/network/class_NetworkPackageReaderTask.php @@ -74,7 +74,14 @@ class NetworkPackageReaderTask extends BaseTask implements Taskable, Visitable { */ public function executeTask () { // Do we have something to handle? - if ($this->getPackageInstance()->isNewMessageArrived()) { + if ($this->getPackageInstance()->isProcessedMessagePending()) { + /* + * A previously proccessed message is waiting for being + * "interpreted". This is done by trying to find a configuration + * entry based on 'message_type' element. + */ + $this->getPackageInstance()->handleProcessedMessage(); + } elseif ($this->getPackageInstance()->isNewMessageArrived()) { /* * A fully "decoded" message has been received and added for being * processed. Processing a message should not take long, so mostly