]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/message-types/class_BaseMessageHandler.php
Added (maybe right?) array elements
[hub.git] / application / hub / main / handler / message-types / class_BaseMessageHandler.php
index 7b6763eeee2c9b9489855dce300385c0a1b30a60..adf44452169ebf51b0b7788c28b1c8b1592df2a8 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
  * 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 {
-       /**
-        * 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();
-
+abstract class BaseMessageHandler extends BaseDataHandler {
        /**
         * Protected constructor
         *
@@ -41,13 +32,25 @@ class BaseMessageHandler extends BaseHandler {
        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 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->getLastException() instanceof FrameworkException) {
+                       // "Determine" the right status code (may differ from exception to exception)
+                       $this->debugInstance('lastException=' . $this->getLastException()->__toString() . ',message=' . $this->getLastException()->getMessage() . ' is not finished!');
+               } // END - if
+
+               // Return the status code
+               return $statusCode;
        }
 
        /**
@@ -60,8 +63,12 @@ class BaseMessageHandler extends BaseHandler {
         *
         * @param       $messageArray   An array with all minimum message data
         * @return      void
+        * @todo        Add something more, e.g. spreading information over DHT
         */
        protected function registerNodeByMessageData (array $messageData) {
+               // Check if searchData has entries
+               assert(count($this->searchData) > 0);
+
                // Get a wrapper instance
                $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
 
@@ -71,7 +78,7 @@ class BaseMessageHandler extends BaseHandler {
                // 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]);
+                       $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]);
                } // END - foreach
 
                // Only one entry is fine
@@ -82,12 +89,15 @@ class BaseMessageHandler extends BaseHandler {
 
                // Is there already an entry?
                if ($resultInstance->next()) {
-                       // Entry found
-                       $resultInstance->debugBackTrace('Entry found!');
+                       // Entry found, so update it
+                       $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance);
                } else {
                        // Nothing found, so register it
                        $wrapperInstance->registerNodeByMessageData($messageData, $this);
                }
+
+               // Save last exception
+               $this->setLastException($wrapperInstance->getLastException());
        }
 }