]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/message-types/class_BaseMessageHandler.php
Rewrites, some more methods:
[hub.git] / application / hub / main / handler / message-types / class_BaseMessageHandler.php
1 <?php
2 /**
3  * A general message handler, this class must be abstract to make the template
4  * method pattern working.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 abstract class BaseMessageHandler extends BaseDataHandler {
26         /**
27          * Protected constructor
28          *
29          * @param       $className      Name of the class
30          * @return      void
31          */
32         protected function __construct ($className) {
33                 // Call parent constructor
34                 parent::__construct($className);
35         }
36
37         /**
38          * "Getter" for a translated last exception as a status code
39          *
40          * @return      $statusCode             Translated status code from last exception
41          */
42         protected function getTranslatedStatusFromLastException () {
43                 // Default is all fine
44                 $statusCode = self::MESSAGE_STATUS_CODE_OKAY;
45
46                 // Is the last exception not NULL?
47                 if ($this->getLastException() instanceof FrameworkException) {
48                         // "Determine" the right status code (may differ from exception to exception)
49                         $this->debugInstance('lastException=' . $this->getLastException()->__toString() . ',message=' . $this->getLastException()->getMessage() . ' is not finished!');
50                 } // END - if
51
52                 // Return the status code
53                 return $statusCode;
54         }
55
56         /**
57          * Registers an other node with this node by given message data. The
58          * following data must always be present:
59          *
60          * - session-id  (for finding the node's record together with below data)
61          * - external-ip (hostname or IP number)
62          * - listen-port (TCP/UDP listen port for inbound connections)
63          *
64          * @param       $messageArray   An array with all minimum message data
65          * @return      void
66          * @todo        Rewrite this to use DHT
67          */
68         protected function registerNodeByMessageData (array $messageData) {
69                 // Check if searchData has entries
70                 assert(count($this->searchData) > 0);
71
72                 // Get a wrapper instance
73                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
74
75                 // Get a search criteria class
76                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
77
78                 // Debug message
79                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER: messageData=' . print_r($messageData, true));
80
81                 // Search for the node's session id and external IP/hostname + TCP/UDP listen port
82                 foreach ($this->searchData as $key) {
83                         // Debug message
84                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER: messageData[' . $key . ']=' . $messageData[$key]);
85
86                         // Is it there?
87                         assert(isset($messageData[$key]));
88
89                         // Add criteria
90                         $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]);
91                 } // END - foreach
92
93                 // Only one entry is fine
94                 $searchInstance->setLimit(1);
95
96                 // Run the query
97                 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
98
99                 // Is there already an entry?
100                 if ($resultInstance->next()) {
101                         // Entry found, so update it
102                         $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance);
103                 } else {
104                         // Nothing found, so register it
105                         $wrapperInstance->registerNodeByMessageData($messageData, $this);
106                 }
107
108                 // Save last exception
109                 $this->setLastException($wrapperInstance->getLastException());
110         }
111 }
112
113 // [EOF]
114 ?>