]> git.mxchange.org Git - hub.git/blob - application/hub/main/resolver/protocol/tcp/class_TcpProtocolResolver.php
Updated 'core'.
[hub.git] / application / hub / main / resolver / protocol / tcp / class_TcpProtocolResolver.php
1 <?php
2 /**
3  * A TCP protocol resolver class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
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
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 TcpProtocolResolver extends BaseProtocolResolver implements ProtocolResolver, Registerable {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33
34                 // Set protocol type
35                 $this->setProtocolName('tcp');
36         }
37
38         /**
39          * Creates an instance of a TCP protocol resolver
40          *
41          * @return      $resolverInstance       The prepared command resolver instance
42          */
43         public static final function createTcpProtocolResolver () {
44                 // Create the new instance
45                 $resolverInstance = new TcpProtocolResolver();
46
47                 // Return the prepared instance
48                 return $resolverInstance;
49         }
50
51         /**
52          * Returns an instance of a LocateableNode class for a given NodeHelper
53          * instance or null if it was not found.
54          *
55          * @param       $nodeInstance   An instance of a NodeHelper class
56          * @return      $unlInstance    An instance of a LocateableNode class
57          * @todo        0% done
58          */
59         public function resolveUniversalNodeLocatorFromNodeHelper (NodeHelper $nodeInstance) {
60                 // Get search instance (to lookup database result)
61                 $searchInstance = $nodeInstance->getSearchInstance();
62
63                 // Make sure the in stance is valid
64                 assert($searchInstance instanceof SearchCriteria);
65
66                 // Refetch and rewind iterator
67                 $resultInstance = $nodeInstance->getWrapperInstance()->doSelectByCriteria($searchInstance);
68
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.
72                         return NULL;
73                 } // END - if
74
75                 // Get current entry
76                 $current = $resultInstance->current();
77
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]);
80
81                 // Get UNL instance and handle over all data
82                 $unlInstance = ObjectFactory::createObjectByConfiguredName('universal_node_locator_class', array($current));
83
84                 // Return resolved instance
85                 return $unlInstance;
86         }
87
88         /**
89          * Returns the UNL as string from given configuration key.
90          *
91          * @param       $configKey      Configuration key for UNL address (valid: internal,external)
92          * @return      $unl            Universal node locator
93          */
94         public function resolveUniversalNodeLocatorFromConfigKey ($configKey) {
95                 // Get address
96                 $address = $this->getConfigInstance()->getConfigEntry($configKey . '_address');
97
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();
104                                         break;
105
106                                 case 'internal': // Internal IP
107                                         $address = ConsoleTools::acquireSelfIPAddress();
108                                         break;
109                         } // END - switch
110                 } // END - if
111
112                 // Put all together
113                 $unl = sprintf('%s://%s:%s',
114                         $this->getProtocolName(),
115                         $address,
116                         $this->getConfigInstance()->getConfigEntry('node_listen_port')
117                 );
118
119                 /*
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.
123                  */
124                 return $unl;
125         }
126 }
127
128 // [EOF]
129 ?>