application/hub/main/handler/class_ svneol=native#text/plain
application/hub/main/handler/class_BaseHandler.php svneol=native#text/plain
application/hub/main/handler/message-types/.htaccess -text svneol=unset#text/plain
+application/hub/main/handler/message-types/anouncement/.htaccess -text svneol=unset#text/plain
+application/hub/main/handler/message-types/anouncement/class_NodeMessageAnnouncementHandler.php svneol=native#text/plain
+application/hub/main/handler/message-types/class_BaseMessageHandler.php svneol=native#text/plain
application/hub/main/handler/message-types/class_NodeMessageAnnouncementHandler.php svneol=native#text/plain
application/hub/main/handler/network/.htaccess -text svneol=unset#text/plain
application/hub/main/handler/network/class_ svneol=native#text/plain
* @return void
*/
function handleMessageData (array $messageData, Receivable $packageInstance);
+
+ /**
+ * Adds all required elements from given array into data set instance
+ *
+ * @param $dataSetInstance An instance of a StoreableCriteria class
+ * @param $messageData An array with all message data
+ * @return void
+ */
+ function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData);
}
// [EOF]
function loadDescriptorXml ();
/**
- * Do the helped attempt by delivering a package to ourselfs
+ * Send a package out
*
* @param $nodeInstance An instance of a NodeHelper class
* @return void
* stack.
*
* @param $helperInstance An instance of a HelpableHub class
+ * @param $protocol Name of used protocol (TCP/UDP)
* @return void
*/
- function enqueueRawDataFromTemplate (HelpableHub $helperInstance);
+ function enqueueRawDataFromTemplate (HelpableHub $helperInstance, $protocol);
/**
* Checks whether a package has been enqueued for delivery.
// Table names
const DB_TABLE_NODE_LIST = 'node_list';
- // Constants for column name
- const DB_COLUMN_NODE_SESSION_ID = 'node_session_id';
- const DB_COLUMN_NODE_IP_PORT = 'node_ipport';
-
// Other constants
const INVALID_IP_PORT = 'invalid:invalid';
* 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) {
+ public function resolveIpPortBySessionId ($sessionId, $protocol) {
// Set invalid ip:port combination
$recipient = self::INVALID_IP_PORT;
$searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
// Search for the node session id
- $searchInstance->addCriteria(NodeListDatabaseWrapper::DB_COLUMN_NODE_SESSION_ID, $sessionId);
+ $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, $sessionId);
$searchInstance->setLimit(1);
// Get a result back
$this->setResultInstance($resultInstance);
// Get the node id from result and set it
- $recipient = $this->getField(NodeListDatabaseWrapper::DB_COLUMN_NODE_IP_PORT);
+ $recipientIp = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP);
+
+ // Which protocol?
+ switch ($protocol) {
+ case 'TCP': // Transmission Control Procol has been used
+ $recipientPort = $this->getField('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT);
+ break;
+
+ case '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
} // END - if
// Return result
* 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) {
+ 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(NodeListDatabaseWrapper::DB_COLUMN_NODE_IP_PORT, $ipPort);
+ $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP, $ipPortArray[0]);
+
+ // Which protocol?
+ switch ($protocol) {
+ case 'TCP': // Transmission Control Procol has been used
+ $searchInstance->addCriteria('node_' . XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT, $ipPortArray[1]);
+ break;
+
+ case '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
$this->setResultInstance($resultInstance);
// Get the session from result
- $sessionId = $this->getField(NodeListDatabaseWrapper::DB_COLUMN_NODE_SESSION_ID);
+ $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, HandleableMessage $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);
+ }
}
// [EOF]
$dataSetInstance->addCriteria(self::DB_COLUMN_PEER_STATE , $stateInstance->getStateName());
// Try to resolve sender's session id
- $senderData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
+ $senderData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_SENDER], $packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]));
// Just make sure that 'invalid:invalid' is not being processed
assert(($senderData[0] != 'invalid') && ($senderData[1] != 'invalid'));
// Determine protocol name
$protocolName = $this->determineProtocolByPackageData($packageData);
+ // Is it the same?
+ assert(strtoupper($protocolName) == $packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]);
+
// Get the listener instance
$listenerInstance = $this->discoverListenerInstance($protocolName, $packageData);
$socketResource = $listenerInstance->getPoolInstance()->getSocketFromPackageData($packageData);
// Debug message
- //* NOISY-DEBUG: */ $this->debugOutput('socketResource=' . $socketResource . ',packageData='.print_r($packageData,true));
+ //* NOISY-DEBUG: */ $this->debugOutput('socketResource=' . $socketResource . ',packageData=' . print_r($packageData, true));
// Is it false, the recipient isn't known to us and we have no connection to it
if (($socketResource === false) || (!is_resource($socketResource)) || (socket_last_error($socketResource) > 0)) {
--- /dev/null
+Deny from all
--- /dev/null
+<?php
+/**
+ * A NodeMessageAnnouncement handler
+ *
+ * @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 NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable {
+ /**
+ * Array with all data XML nodes (which hold the actual data) and their values
+ */
+ private $messageDataElements = array();
+
+ /**
+ * Protected constructor
+ *
+ * @return void
+ */
+ protected function __construct () {
+ // Call parent constructor
+ parent::__construct(__CLASS__);
+
+ // Set handler name
+ $this->setHandlerName('message_type_announcement');
+
+ // Init message data array
+ $this->messageDataElements = array(
+ XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP,
+ XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS,
+ XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
+ XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT,
+ XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_UDP_PORT
+ );
+ }
+
+ /**
+ * Creates an instance of this class
+ *
+ * @return $handlerInstance An instance of a HandleableMessage class
+ */
+ public final static function createNodeMessageAnnouncementHandler () {
+ // Get new instance
+ $handlerInstance = new NodeMessageAnnouncementHandler();
+
+ // Return the prepared instance
+ return $handlerInstance;
+ }
+
+ /**
+ * Handles data array of the message
+ *
+ * @param $messageData An array with message data to handle
+ * @param $packageInstance An instance of a Receivable class
+ * @return void
+ */
+ public function handleMessageData (array $messageData, Receivable $packageInstance) {
+ // Register the announcing node with this node
+ $this->registerNodeByMessageData($messageData);
+
+ // Prepare answer message to be delivered back to the other node
+ $this->prepareAnswerMessage($messageData, $packageInstance);
+ }
+
+ /**
+ * Adds all required elements from given array into data set instance
+ *
+ * @param $dataSetInstance An instance of a StoreableCriteria class
+ * @param $messageData An array with all message data
+ * @return void
+ */
+ public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) {
+ // Add all ements
+ foreach ($this->messageDataElements as $key) {
+ // Is it there?
+ assert(isset($messageData[$key]));
+
+ // Add it
+ $dataSetInstance->addCriteria('node_' . $key, $messageData[$key]);
+ } // END - foreach
+ }
+}
+
+// [EOF]
+?>
--- /dev/null
+<?php
+/**
+ * A general message handler
+ *
+ * @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 BaseMessageHandler extends BaseHandler {
+ /**
+ * Array with search criteria elements
+ */
+ private $searchData = array();
+
+ /**
+ * Protected constructor
+ *
+ * @param $className Name of the class
+ * @return void
+ */
+ protected function __construct ($className) {
+ // Call parent constructor
+ parent::__construct($className);
+
+ // Init array
+ $this->searchData = array(
+ XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID,
+ XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_IP,
+ XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_TCP_PORT
+ );
+ }
+
+ /**
+ * Registers an other node with this node by given message data. The
+ * following data must always be present:
+ *
+ * - session-id (for finding the node's record together with below data)
+ * - external-ip (hostname or IP number)
+ * - tcp-port (TCP port for inbound connections)
+ *
+ * @param $messageArray An array with all minimum message data
+ * @return void
+ */
+ protected function registerNodeByMessageData (array $messageData) {
+ // Get a wrapper instance
+ $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
+
+ // Get a search criteria class
+ $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+ // Search for the node's session id and external IP/hostname + TCP port
+ foreach ($this->searchData as $key) {
+ // Add criteria
+ $searchInstance->addCriteria('node_' . $key, $messageData[$key]);
+ } // END - foreach
+
+ // Only one entry is fine
+ $searchInstance->setLimit(1);
+
+ // Run the query
+ $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
+
+ // Is there already an entry?
+ if ($resultInstance->next()) {
+ // Entry found
+ $resultInstance->debugBackTrace('Entry found!');
+ } else {
+ // Nothing found, so register it
+ $wrapperInstance->registerNodeByMessageData($messageData, $this);
+ }
+ }
+}
+
+// [EOF]
+?>
<?php
-/**
- * A NodeMessageAnnouncement handler
- *
- * @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 NodeMessageAnnouncementHandler extends BaseHandler implements HandleableMessage, Registerable {
- /**
- * Protected constructor
- *
- * @return void
- */
- protected function __construct () {
- // Call parent constructor
- parent::__construct(__CLASS__);
-
- // Set handler name
- $this->setHandlerName('message_type_announcement');
- }
-
- /**
- * Creates an instance of this class
- *
- * @return $handlerInstance An instance of a HandleableMessage class
- */
- public final static function createNodeMessageAnnouncementHandler () {
- // Get new instance
- $handlerInstance = new NodeMessageAnnouncementHandler();
-
- // Return the prepared instance
- return $handlerInstance;
- }
-
- /**
- * Handles data array of the message
- *
- * @param $messageData An array with message data to handle
- * @param $packageInstance An instance of a Receivable class
- * @return void
- */
- public function handleMessageData (array $messageData, Receivable $packageInstance) {
- }
-}
-
-// [EOF]
+// @DEPRECATED
?>
// Try to solve the recipient
try {
// Resolve any session ids; 0 = IP, 1 = Port
- $recipientData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
+ $recipientData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 'TCP'));
} catch (NoValidHostnameException $e) {
// Debug message
$helperInstance->debugOutput('CONNECTION: Failed to resolve ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ':' . $e->getMessage());
$packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
// Next, feed the content in. The network package class is a pipe-through class.
- $packageInstance->enqueueRawDataFromTemplate($this);
+ $packageInstance->enqueueRawDataFromTemplate($this, 'TCP');
}
/**
$packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
// Next, feed the content in. The network package class is a pipe-through class.
- $packageInstance->enqueueRawDataFromTemplate($this);
+ $packageInstance->enqueueRawDataFromTemplate($this, 'TCP');
}
/**
*/
const PACKAGE_DATA_SENDER = 'sender';
const PACKAGE_DATA_RECIPIENT = 'recipient';
+ const PACKAGE_DATA_PROTOCOL = 'protocol';
const PACKAGE_DATA_CONTENT = 'content';
const PACKAGE_DATA_STATUS = 'status';
const PACKAGE_DATA_SIGNATURE = 'signature';
* stack.
*
* @param $helperInstance An instance of a HelpableHub class
+ * @param $protocol Name of used protocol (TCP/UDP)
* @return void
*/
- public function enqueueRawDataFromTemplate (HelpableHub $helperInstance) {
+ public function enqueueRawDataFromTemplate (HelpableHub $helperInstance, $protocolName) {
// Get the raw content ...
$content = $helperInstance->getTemplateInstance()->getRawTemplateData();
$this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
self::PACKAGE_DATA_SENDER => $this->getSessionId(),
self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(),
+ self::PACKAGE_DATA_PROTOCOL => $protocolName,
self::PACKAGE_DATA_CONTENT => $content,
self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_NEW,
self::PACKAGE_DATA_SIGNATURE => $this->generatePackageSignature($content, $this->getSessionId())
public function fragmentPackageArray (array $packageData, ConnectionHelper $helperInstance) {
// Is this package already fragmented?
if (!$this->isPackageProcessed($packageData)) {
- // Remove package status, the recipient doesn't need this
+ // Remove package status and protocol, the recipient doesn't need this
unset($packageData[NetworkPackage::PACKAGE_DATA_STATUS]);
+ unset($packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]);
// First we need to "implode" the array
$rawData = implode(NetworkPackage::PACKAGE_DATA_SEPARATOR, $packageData);
$socketResource = false;
// Temporary resolve recipient field
- $recipientIpArray = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
+ $recipientIpArray = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]));
// Make sure it is a valid ip:port array (2 elements)
assert(count($recipientIpArray) == 2);
/**
* Some XML nodes must be available for later data extraction
*/
- const ANNOUNCEMENT_DATA_EXTERNAL_IP = 'external-ip';
- const ANNOUNCEMENT_DATA_NODE_STATUS = 'node-status';
const ANNOUNCEMENT_DATA_SESSION_ID = 'session-id';
+ const ANNOUNCEMENT_DATA_NODE_STATUS = 'node-status';
+ const ANNOUNCEMENT_DATA_EXTERNAL_IP = 'external-ip';
const ANNOUNCEMENT_DATA_TCP_PORT = 'tcp-port';
const ANNOUNCEMENT_DATA_UDP_PORT = 'udp-port';
* Resolves a session id into an ip:port combination
*
* @param $sessionId A valid session id
+ * @param $protocol Name of the used protocol: TCP/UDP
* @return $recipient Recipient as ip:port combination
*/
- protected function resolveIpPortBySessionId ($sessionId) {
+ protected function resolveIpPortBySessionId ($sessionId, $protocol) {
// Get a wrapper instance
$wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
// And ask it for the session id
- $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId);
+ $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId, $protocol);
// Is the recipient invalid?
if ($recipient == 'invalid:invalid') {
// Is the session id the same?
if ($nodeInstance->getSessionId() == $sessionId) {
// Then get the ip:port from it, assume TCP by default
- $recipient = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_tcp_listen_port');
+ $recipient = self::determineOwnExternalIp() . ':' . $nodeInstance->getConfigInstance()->getConfigEntry('node_' . strtolower($protocol) . '_listen_port');
} // END - if
} // END - if
* Resolves a ip:port combination into a session id
*
* @param $ipPort Ip:port combination
+ * @param $protocol Name of used protocol (TCP/UDP)
* @return $sessionId Valid session id
*/
- public static function resolveSessionIdByIpPort ($ipPort) {
+ public static function resolveSessionIdByIpPort ($ipPort, $protocol) {
// Get a wrapper instance
$wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
// And ask it for the session id
- $sessionId = $wrapperInstance->resolveSessionIdByIpPort($ipPort);
+ $sessionId = $wrapperInstance->resolveSessionIdByIpPort($ipPort, $protocol);
// Return result
return $sessionId;
* Resolves given session id into an ip:port combination, if ip:port is set, it won't be translated
*
* @param $sessionId Session id or ip:port combination
+ * @param $protocol Name of the used protocol (TCP/UDP)
* @return $recipient Recipient as ip:port combination
* @throws InvalidSessionIdException If the provided session id is invalid (and no ip:port combination)
* @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address
*/
- public static function resolveSessionId ($sessionId) {
+ public static function resolveSessionId ($sessionId, $protocol) {
// Get an own instance
$selfInstance = self::getSelfInstance();
$selfInstance->debugOutput('HUB-TOOLS: Using internal session id resolver.');
// Resolve session id into a ip:port combination
- $recipient = $selfInstance->resolveIpPortBySessionId($sessionId);
+ $recipient = $selfInstance->resolveIpPortBySessionId($sessionId, $protocol);
// Debug message
$selfInstance->debugOutput('HUB-TOOLS: session id ' . $sessionId . ' resolved to ' . $recipient);