]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/message-types/class_BaseMessageHandler.php
Use set DHT instance to shortcut a little
[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                 // Get a DHT instance
37                 $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
38
39                 // Set it here
40                 $this->setDhtInstance($dhtInstance);
41         }
42
43         /**
44          * "Getter" for a translated last exception as a status code
45          *
46          * @return      $statusCode             Translated status code from last exception
47          */
48         protected function getTranslatedStatusFromLastException () {
49                 // Default is all fine
50                 $statusCode = self::MESSAGE_STATUS_CODE_OKAY;
51
52                 // Is the last exception not NULL?
53                 if ($this->getLastException() instanceof FrameworkException) {
54                         // "Determine" the right status code (may differ from exception to exception)
55                         $this->debugInstance('lastException=' . $this->getLastException()->__toString() . ',message=' . $this->getLastException()->getMessage() . ' is not finished!');
56                 } // END - if
57
58                 // Return the status code
59                 return $statusCode;
60         }
61
62         /**
63          * Registers an other node with this node by given message data. The
64          * following data must always be present:
65          *
66          * - session-id  (for finding the node's record together with below data)
67          * - external-ip (hostname or IP number)
68          * - listen-port (TCP/UDP listen port for inbound connections)
69          *
70          * @param       $messageArray   An array with all minimum message data
71          * @return      void
72          * @todo        Rewrite this to use DHT
73          */
74         protected function registerNodeByMessageData (array $messageData) {
75                 // Check if searchData has entries
76                 assert(count($this->searchData) > 0);
77
78                 // Get a wrapper instance
79                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
80
81                 // Get a search criteria class
82                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
83
84                 // Debug message
85                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER: messageData=' . print_r($messageData, true));
86
87                 // Search for the node's session id and external IP/hostname + TCP/UDP listen port
88                 foreach ($this->searchData as $key) {
89                         // Debug message
90                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER: messageData[' . $key . ']=' . $messageData[$key]);
91
92                         // Is it there?
93                         assert(isset($messageData[$key]));
94
95                         // Add criteria
96                         $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]);
97                 } // END - foreach
98
99                 // Only one entry is fine
100                 $searchInstance->setLimit(1);
101
102                 // Run the query
103                 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
104
105                 // Is there already an entry?
106                 if ($resultInstance->next()) {
107                         // Entry found, so update it
108                         $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance);
109                 } else {
110                         // Nothing found, so register it
111                         $wrapperInstance->registerNodeByMessageData($messageData, $this);
112                 }
113
114                 // Save last exception
115                 $this->setLastException($wrapperInstance->getLastException());
116         }
117 }
118
119 // [EOF]
120 ?>