3 * A database wrapper for node list
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class NodeListDatabaseWrapper extends BaseDatabaseWrapper implements NodeListWrapper, Registerable {
26 const DB_TABLE_NODE_LIST = 'node_list';
29 const INVALID_IP_PORT = 'invalid:invalid';
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates an instance of this database wrapper by a provided user class
44 * @return $wrapperInstance An instance of the created wrapper class
46 public static final function createNodeListDatabaseWrapper () {
48 $wrapperInstance = new NodeListDatabaseWrapper();
50 // Set (primary!) table name
51 $wrapperInstance->setTableName(self::DB_TABLE_NODE_LIST);
53 // Return the instance
54 return $wrapperInstance;
58 * Resolves a session id into an ip:port combination
60 * @param $sessionId A valid session id
61 * @param $protocol Name of the protocol (TCP/UDP)
62 * @return $recipient Recipient as ip:port combination
64 public function resolveIpPortBySessionId ($sessionId, $protocol) {
65 // Set invalid ip:port combination
66 $recipient = self::INVALID_IP_PORT;
68 // Now get a search criteria instance
69 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
71 // Search for the node session id
72 $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, $sessionId);
73 $searchInstance->setLimit(1);
76 $resultInstance = $this->doSelectByCriteria($searchInstance);
79 if ($resultInstance->next()) {
80 // Save the result instance in this class
81 $this->setResultInstance($resultInstance);
83 // Get the node id from result and set it
84 $recipientIp = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP);
88 case 'TCP': // Transmission Control Procol has been used
89 $recipientPort = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT);
92 case 'UDP': // User Datagram Protocol has been used
93 $recipientPort = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_UDP_PORT);
96 default: // Unsupported protocol detected!
97 $this->debugBackTrace('Unsupported protocol ' . $protocol . ' specified!');
101 // Now put both together
102 $recipient = $recipientIp . ':' . $recipientPort;
105 $this->debugOutput('DATABASE-WRAPPER: sessionId[' . $protocol . ']=' . $sessionId . ' resolved as recipient=' . $recipient);
113 * Resolves a ip:port combination into a session id
115 * @param $ipPort Ip:port combination
116 * @param $protocol Name of the used protocol (TCP/UDP)
117 * @return $sessionId A valid session id
119 public function resolveSessionIdByIpPort ($ipPort, $protocol) {
120 // Set invalid session id as default
121 $sessionId = 'invalid';
124 $ipPortArray = explode(':', $ipPort);
126 // Now get a search criteria instance
127 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
129 // Search for the node session id
130 $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP, $ipPortArray[0]);
134 case 'TCP': // Transmission Control Procol has been used
135 $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT, $ipPortArray[1]);
138 case 'UDP': // User Datagram Protocol has been used
139 $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_UDP_PORT, $ipPortArray[1]);
142 default: // Unsupported protocol detected!
143 $this->debugBackTrace('Unsupported protocol ' . $protocol . ' specified!');
148 $searchInstance->setLimit(1);
151 $resultInstance = $this->doSelectByCriteria($searchInstance);
154 if ($resultInstance->next()) {
155 // Save the result instance in this class
156 $this->setResultInstance($resultInstance);
158 // Get the session from result
159 $sessionId = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
167 * Registeres a node by given message data.
169 * @param $messageData An array of all message data
170 * @param $handlerInstance An instance of a HandleableMessage class
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));
177 // Set primary key (session id)
178 $dataSetInstance->setUniqueKey('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
180 // Add all array elements
181 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
183 // Run the "INSERT" query
184 $this->queryInsertDataSet($dataSetInstance);
188 * Updates an existing entry in node list
190 * @param $messageData An array of all message data
191 * @param $handlerInstance An instance of a HandleableMessage class
192 * @param $searchInstance An instance of LocalSearchCriteria class
195 public function updateNodeByMessageData (array $messageData, HandleableMessage $handlerInstance, LocalSearchCriteria $searchInstance) {
196 // Get a data set instance
197 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_LIST));
199 // Add search instance
200 $dataSetInstance->setSearchInstance($searchInstance);
202 // Set primary key (session id)
203 $dataSetInstance->setUniqueKey('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
205 // Add all array elements
206 $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
208 // Run the "UPDATE" query
209 $this->queryUpdateDataSet($dataSetInstance);