3 * A TCP protocol resolver class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 TcpProtocolResolver extends BaseProtocolResolver implements ProtocolResolver, Registerable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
35 $this->setProtocolName('tcp');
39 * Creates an instance of a TCP protocol resolver
41 * @return $resolverInstance The prepared command resolver instance
43 public static final function createTcpProtocolResolver () {
44 // Create the new instance
45 $resolverInstance = new TcpProtocolResolver();
47 // Return the prepared instance
48 return $resolverInstance;
52 * Returns an instance of a LocateableNode class for a given NodeHelper
53 * instance or null if it was not found.
55 * @param $nodeInstance An instance of a NodeHelper class
56 * @return $unlInstance An instance of a LocateableNode class
59 public function resolveUniversalNodeLocatorFromNodeHelper (NodeHelper $nodeInstance) {
60 // Get search instance (to lookup database result)
61 $searchInstance = $nodeInstance->getSearchInstance();
63 // Make sure the in stance is valid
64 assert($searchInstance instanceof SearchCriteria);
66 // Refetch and rewind iterator
67 $resultInstance = $nodeInstance->getWrapperInstance()->doSelectByCriteria($searchInstance);
69 // Is the result valid?
70 if ((!$resultInstance->valid()) || (!$resultInstance->next())) {
71 // Node not found in database, this could mean that your database file is damaged.
76 $current = $resultInstance->current();
78 // This should always be the case, if not your database file might be damaged.
79 assert($nodeInstance->getNodeId() == $current[NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID]);
81 // Get UNL instance and handle over all data
82 $unlInstance = ObjectFactory::createObjectByConfiguredName('universal_node_locator_class', array($current));
84 // Return resolved instance
89 * Returns the UNL as string from given configuration key.
91 * @param $configKey Configuration key for UNL address (valid: internal,external)
92 * @return $unl Universal node locator
94 public function resolveUniversalNodeLocatorFromConfigKey ($configKey) {
96 $address = $this->getConfigInstance()->getConfigEntry($configKey . '_address');
98 // Is the address empty?
99 if (empty($address)) {
100 // Okay, then find it
101 switch ($configKey) {
102 case 'external': // External IP
103 $address = ConsoleTools::determineExternalAddress();
106 case 'internal': // Internal IP
107 $address = ConsoleTools::acquireSelfIPAddress();
113 $unl = sprintf('%s://%s:%s',
114 $this->getProtocolName(),
116 $this->getConfigInstance()->getConfigEntry('node_listen_port')
120 * And return it. Please note that e.g. a FaxProtocolResolver will
121 * return a different UNL and therefore all protocol resolvers must do
122 * it on their own way.