]> git.mxchange.org Git - hub.git/blob - application/hub/main/database/wrapper/node/class_NodeListDatabaseWrapper.php
Introduced (currently not used as type-hints) new interfaces:
[hub.git] / application / hub / main / database / wrapper / node / class_NodeListDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for node list
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class NodeListDatabaseWrapper extends BaseDatabaseWrapper implements NodeListWrapper Registerable {
25         // Table names
26         const DB_TABLE_NODE_LIST = 'node_list';
27
28         // Other constants
29         const INVALID_IP_PORT = 'invalid:invalid';
30
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         protected function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates an instance of this database wrapper by a provided user class
43          *
44          * @return      $wrapperInstance        An instance of the created wrapper class
45          */
46         public static final function createNodeListDatabaseWrapper () {
47                 // Get a new instance
48                 $wrapperInstance = new NodeListDatabaseWrapper();
49
50                 // Set (primary!) table name
51                 $wrapperInstance->setTableName(self::DB_TABLE_NODE_LIST);
52
53                 // Return the instance
54                 return $wrapperInstance;
55         }
56
57         /**
58          * Resolves a session id into an ip:port combination
59          *
60          * @param       $sessionId      A valid session id
61          * @param       $protocol       Name of the protocol (TCP/UDP)
62          * @return      $recipient      Recipient as ip:port combination
63          */
64         public function resolveIpPortBySessionId ($sessionId, $protocol) {
65                 // Set invalid ip:port combination
66                 $recipient = self::INVALID_IP_PORT;
67
68                 // Now get a search criteria instance
69                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
70
71                 // Search for the node session id
72                 $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, $sessionId);
73                 $searchInstance->setLimit(1);
74
75                 // Get a result back
76                 $resultInstance = $this->doSelectByCriteria($searchInstance);
77
78                 // Is it valid?
79                 if ($resultInstance->next()) {
80                         // Save the result instance in this class
81                         $this->setResultInstance($resultInstance);
82
83                         // Get the node id from result and set it
84                         $recipientIp   = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP);
85
86                         // Which protocol?
87                         switch ($protocol) {
88                                 case 'TCP': // Transmission Control Procol has been used
89                                         $recipientPort = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT);
90                                         break;
91
92                                 case 'UDP': // User Datagram Protocol has been used
93                                         $recipientPort = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_UDP_PORT);
94                                         break;
95
96                                 default: // Unsupported protocol detected!
97                                         $this->debugBackTrace('Unsupported protocol ' . $protocol . ' specified!');
98                                         break;
99                         } // END - switch
100
101                         // Now put both together
102                         $recipient = $recipientIp . ':' . $recipientPort;
103
104                         // Debug message
105                         $this->debugOutput('DATABASE-WRAPPER: sessionId[' . $protocol . ']=' . $sessionId . ' resolved as recipient=' . $recipient);
106                 } // END - if
107
108                 // Return result
109                 return $recipient;
110         }
111
112         /**
113          * Resolves a ip:port combination into a session id
114          *
115          * @param       $ipPort         Ip:port combination
116          * @param       $protocol       Name of the used protocol (TCP/UDP)
117          * @return      $sessionId      A valid session id
118          */
119         public function resolveSessionIdByIpPort ($ipPort, $protocol) {
120                 // Set invalid session id as default
121                 $sessionId = 'invalid';
122
123                 // Split ip:port
124                 $ipPortArray = explode(':', $ipPort);
125
126                 // Now get a search criteria instance
127                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
128
129                 // Search for the node session id
130                 $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP, $ipPortArray[0]);
131
132                 // Which protocol?
133                 switch ($protocol) {
134                         case 'TCP': // Transmission Control Procol has been used
135                                 $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT, $ipPortArray[1]);
136                                 break;
137
138                         case 'UDP': // User Datagram Protocol has been used
139                                 $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_UDP_PORT, $ipPortArray[1]);
140                                 break;
141
142                         default: // Unsupported protocol detected!
143                                 $this->debugBackTrace('Unsupported protocol ' . $protocol . ' specified!');
144                                 break;
145                 } // END - switch
146
147                 // Only one record
148                 $searchInstance->setLimit(1);
149
150                 // Get a result back
151                 $resultInstance = $this->doSelectByCriteria($searchInstance);
152
153                 // Is it valid?
154                 if ($resultInstance->next()) {
155                         // Save the result instance in this class
156                         $this->setResultInstance($resultInstance);
157
158                         // Get the session from result
159                         $sessionId = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
160                 } // END - if
161
162                 // Return result
163                 return $sessionId;
164         }
165
166         /**
167          * Registeres a node by given message data.
168          *
169          * @param       $messageData            An array of all message data
170          * @param       $handlerInstance        An instance of a HandleableMessage class
171          * @return      void
172          */
173         public function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance) {
174                 // Get a data set instance
175                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_LIST));
176
177                 // Set primary key (session id)
178                 $dataSetInstance->setUniqueKey('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
179
180                 // Add all array elements
181                 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
182
183                 // Run the "INSERT" query
184                 $this->queryInsertDataSet($dataSetInstance);
185         }
186
187         /**
188          * Updates an existing entry in node list
189          *
190          * @param       $messageData            An array of all message data
191          * @param       $handlerInstance        An instance of a HandleableMessage class
192          * @return      void
193          */
194         public function updateNodeByMessageData (array $messageData, HandleableMessage $handlerInstance) {
195                 // Get a data set instance
196                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_LIST));
197
198                 // Set primary key (session id)
199                 $dataSetInstance->setUniqueKey('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
200
201                 // Add all array elements
202                 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
203
204                 // Run the "UPDATE" query
205                 $this->queryUpdateDataSet($dataSetInstance);
206         }
207 }
208
209 // [EOF]
210 ?>