<?php
-/**
- * A database wrapper for node list
- *
- * @author Roland Haeder <webmaster@ship-simu.org>
- * @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
- * @license GNU GPL 3.0 or any newer version
- * @link http://www.ship-simu.org
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * 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 NodeListDatabaseWrapper extends BaseDatabaseWrapper implements NodeListWrapper, Registerable {
- // Table names
- const DB_TABLE_NODE_LIST = 'node_list';
-
- // Table columns
- const DB_COLUMN_ANSWER_STATUS = 'last_answer_status';
- const DB_COLUMN_MESSAGE_TYPE = 'last_message_type';
-
- // Other constants
- const INVALID_IP_PORT = 'invalid:invalid';
-
- /**
- * Protected constructor
- *
- * @return void
- */
- protected function __construct () {
- // Call parent constructor
- parent::__construct(__CLASS__);
- }
-
- /**
- * Creates an instance of this database wrapper by a provided user class
- *
- * @return $wrapperInstance An instance of the created wrapper class
- */
- public static final function createNodeListDatabaseWrapper () {
- // Get a new instance
- $wrapperInstance = new NodeListDatabaseWrapper();
-
- // Set (primary!) table name
- $wrapperInstance->setTableName(self::DB_TABLE_NODE_LIST);
-
- // Return the instance
- return $wrapperInstance;
- }
-
- /**
- * Resolves a session id into an ip:port combination
- *
- * @param $sessionId A valid session id
- * @param $protocol Name of the protocol (TCP/UDP)
- * @return $recipient Recipient as ip:port combination
- */
- public function resolveIpPortBySessionId ($sessionId, $protocol) {
- // Set invalid ip:port combination
- $recipient = self::INVALID_IP_PORT;
-
- // Now get a search criteria instance
- $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
- // Search for the node session id
- $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, $sessionId);
- $searchInstance->setLimit(1);
-
- // Get a result back
- $resultInstance = $this->doSelectByCriteria($searchInstance);
-
- // Is it valid?
- if ($resultInstance->next()) {
- // Save the result instance in this class
- $this->setResultInstance($resultInstance);
-
- // Get the node id from result and set it
- $recipientIp = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP);
-
- // Which protocol?
- switch ($protocol) {
- case NetworkPackage::PROTOCOL_TCP: // Transmission Control Protocol has been used
- $recipientPort = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT);
- break;
-
- case NetworkPackage::PROTOCOL_UDP: // User Datagram Protocol has been used
- $recipientPort = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_UDP_PORT);
- break;
-
- default: // Unsupported protocol detected!
- $this->debugBackTrace('Unsupported protocol ' . $protocol . ' specified!');
- break;
- } // END - switch
-
- // Now put both together
- $recipient = $recipientIp . ':' . $recipientPort;
-
- // Debug message
- self::createDebugInstance(__CLASS__)->debugOutput('DATABASE-WRAPPER: sessionId[' . $protocol . ']=' . $sessionId . ' resolved as recipient=' . $recipient);
- } // END - if
-
- // Return result
- return $recipient;
- }
-
- /**
- * Resolves a ip:port combination into a session id
- *
- * @param $ipPort Ip:port combination
- * @param $protocol Name of the used protocol (TCP/UDP)
- * @return $sessionId A valid session id
- */
- public function resolveSessionIdByIpPort ($ipPort, $protocol) {
- // Set invalid session id as default
- $sessionId = 'invalid';
-
- // Split ip:port
- $ipPortArray = explode(':', $ipPort);
-
- // Now get a search criteria instance
- $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
-
- // Search for the node session id
- $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP, $ipPortArray[0]);
-
- // Which protocol?
- switch ($protocol) {
- case NetworkPackage::PROTOCOL_TCP: // Transmission Control Procol has been used
- $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT, $ipPortArray[1]);
- break;
-
- case NetworkPackage::PROTOCOL_UDP: // User Datagram Protocol has been used
- $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_UDP_PORT, $ipPortArray[1]);
- break;
-
- default: // Unsupported protocol detected!
- $this->debugBackTrace('Unsupported protocol ' . $protocol . ' specified!');
- break;
- } // END - switch
-
- // Only one record
- $searchInstance->setLimit(1);
-
- // Get a result back
- $resultInstance = $this->doSelectByCriteria($searchInstance);
-
- // Is it valid?
- if ($resultInstance->next()) {
- // Save the result instance in this class
- $this->setResultInstance($resultInstance);
-
- // Get the session from result
- $sessionId = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
- } // END - if
-
- // Return result
- return $sessionId;
- }
-
- /**
- * Registeres a node by given message data.
- *
- * @param $messageData An array of all message data
- * @param $handlerInstance An instance of a HandleableMessage class
- * @return void
- */
- public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance) {
- // Get a data set instance
- $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_LIST));
-
- // Set primary key (session id)
- $dataSetInstance->setUniqueKey('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
-
- // Add all array elements
- $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
-
- // Run the "INSERT" query
- $this->queryInsertDataSet($dataSetInstance);
- }
-
- /**
- * Updates an existing entry in node list
- *
- * @param $messageData An array of all message data
- * @param $handlerInstance An instance of a HandleableMessage class
- * @param $searchInstance An instance of LocalSearchCriteria class
- * @return void
- */
- public function updateNodeByMessageData (array $messageData, Handleable $handlerInstance, LocalSearchCriteria $searchInstance) {
- // Get a data set instance
- $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_LIST));
-
- // Add search instance
- $dataSetInstance->setSearchInstance($searchInstance);
-
- // Set primary key (session id)
- $dataSetInstance->setUniqueKey('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID);
-
- // Add all array elements
- $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData);
-
- // Run the "UPDATE" query
- $this->queryUpdateDataSet($dataSetInstance);
- }
-}
-
-// [EOF]
+// @DEPRECATED
?>