]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/message-types/class_BaseMessageHandler.php
Added internal IP handling (unfinished), added package tags
[hub.git] / application / hub / main / handler / message-types / class_BaseMessageHandler.php
index 5b93828568b600de49b132fe88579a7b5f615b70..4763b03cdf7e14f3a54df533047745cd62e0919e 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 /**
- * A general message handler
+ * A general message handler, this class must be abstract to make the template
+ * method pattern working.
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
@@ -21,7 +22,7 @@
  * 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 BaseMessageHandler extends BaseHandler {
+abstract class BaseMessageHandler extends BaseHandler {
        /**
         * Array with search criteria elements
         */
@@ -32,6 +33,17 @@ class BaseMessageHandler extends BaseHandler {
         */
        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;
+
        /**
         * Protected constructor
         *
@@ -50,6 +62,34 @@ class BaseMessageHandler extends BaseHandler {
                );
        }
 
+       /**
+        * Getter for last exception
+        *
+        * @return      $lastException  Last thrown exception
+        */
+       protected final function getLastException () {
+               return $this->lastException;
+       }
+
+       /**
+        * "Getter" for a translated last exception as a status code
+        *
+        * @return      $statusCode             Translated status code from last exception
+        */
+       protected function getTranslatedStatusFromLastException () {
+               // Default is all fine
+               $statusCode = self::MESSAGE_STATUS_CODE_OKAY;
+
+               // Is the last exception not NULL?
+               if ($this->lastException 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!');
+               } // END - if
+
+               // Return the status code
+               return $statusCode;
+       }
+
        /**
         * Registers an other node with this node by given message data. The
         * following data must always be present:
@@ -88,6 +128,9 @@ class BaseMessageHandler extends BaseHandler {
                        // Nothing found, so register it
                        $wrapperInstance->registerNodeByMessageData($messageData, $this);
                }
+
+               // Save last exception
+               $this->lastException = $wrapperInstance->getLastException();
        }
 
        /**
@@ -104,6 +147,12 @@ class BaseMessageHandler extends BaseHandler {
                // 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();
 
@@ -112,7 +161,29 @@ class BaseMessageHandler extends BaseHandler {
 
                // Deliver the package
                $helperInstance->sendPackage($nodeInstance);
+
+               /*
+                * Remove temporary configuration
+                */
+               $this->removeMessageConfigurationData($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]