]> git.mxchange.org Git - hub.git/blob - application/hub/main/handler/protocol/class_BaseProtocolHandler.php
Merge branch 'master' of /var/cache/git/repos/hub into refacuring/protocol_handler
[hub.git] / application / hub / main / handler / protocol / class_BaseProtocolHandler.php
1 <?php
2 /**
3  * A general handler for protocols such as TCP, UDP and others.
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 BaseProtocolHandler extends BaseHandler {
25         /**
26          * Whole UNL data array
27          */
28         private $universalNodeLocatorData = array();
29
30         /**
31          * Name of used protocol
32          */
33         private $protocolName = 'invalid';
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44         }
45
46         /**
47          * Getter for protocol name
48          *
49          * @return      $protocolName   Name of used protocol
50          */
51         public final function getProtocolName () {
52                 return $this->protocolName;
53         }
54
55         /**
56          * Setter for protocol name
57          *
58          * @param       $protocolName   Name of used protocol
59          * @return      void
60          */
61         protected final function setProtocolName ($protocolName) {
62                 $this->protocolName = $protocolName;
63         }
64
65         /**
66          * Setter for UNL data array to satify HandleableProtocol
67          *
68          * @para        $unlData        The UNL data array
69          * @return      void
70          */
71         protected final function setUniversalNodeLocatorData (array $unlData) {
72                 // Set new UNL data array
73                 $this->universalNodeLocatorData = $unlData;
74         }
75
76         /**
77          * Getter for UNL data array to satify HandleableProtocol
78          *
79          * @return      $unlData        The UNL data array
80          */
81         public final function getUniversalNodeLocatorDataArray () {
82                 // Return UNL data array
83                 return $this->universalNodeLocatorData;
84         }
85
86         /**
87          * Validates given UNL very basicly by given regular expression. You
88          * normally don't need/want to overwrite this method as this is a very basic
89          * validation only based on a regex.
90          *
91          * @param       $unl            Universal Node Locator to validate
92          * @return      $isValid        Whether the UNL is valid
93          */
94         protected final function isValidUniversalNodeLocator ($unl) {
95                 // Debug message
96                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',regex=' . $regex . ' - CALLED!');
97
98                 // Very basic regex check
99                 $isValid = (preg_match($this->getRegularExpression(), $unl) === 1);
100
101                 // Return result
102                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
103                 return $isValid;
104         }
105
106         /**
107          * Parses the given UNL by splitting it up in its components. The UNL ...
108          *
109          * protocol://address[:port]
110          *
111          * ... becomes:
112          *
113          * array(
114          *     'protocol' => 'value',
115          *     'address'  => 'value',
116          *     'extra'    => 'port'
117          * )
118          *
119          * The value for 'extra' then must be handled by parseUniversalNodeLocator()
120          * of the individual protocol handler as this is protocol-specific.
121          *
122          * @param       $unl            Universal Node Locator (UNL) to "parse"
123          * @return      $unlData        Array with all components of the UNL
124          */
125         protected function parseGenericUniversalNodeLocator ($unl) {
126                 // Debug message
127                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - CALLED!');
128
129                 // Make sure the UNL is valid
130                 assert($this->isValidUniversalNodeLocator($unl));
131
132                 /*
133                  * "Parse" the UNL "generically", sadly this cannot be done by using preg_match() :-(
134                  * @TODO If you know why, please fix and explain it to me.
135                  */
136                 $unlParts = explode('://', $unl);
137
138                 // Split again the last part as: address:port
139                 $unlParts[1] = explode(':', $unlParts[1]);
140
141                 // Now there is an almost useable array which then can be copied to the "real" array.
142                 $unlData = array(
143                         UniversalNodeLocator::UNL_PART_PROTOCOL => $unlParts[0],
144                         UniversalNodeLocator::UNL_PART_ADDRESS  => $unlParts[1][0],
145                         UniversalNodeLocator::UNL_PART_EXTRA    => $unlParts[1][1]
146                 );
147
148                 // Debug message
149                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
150
151                 // Return the generic array
152                 return $unlData;
153         }
154
155         /**
156          * Gets an element from universalNodeLocatorData array
157          *
158          * @param       $element        Element in universalNodeLocatorData array
159          * @return      $value          Found value
160          */
161         protected final function getUniversalNodeLocatorDataElement ($element) {
162                 // Is the element there?
163                 assert(isset($this->universalNodeLocatorData[$element]));
164
165                 // Return it
166                 return $this->universalNodeLocatorData[$element];
167         }
168
169         /**
170          * "Getter" for currently saved UNL
171          *
172          * @return      $unl    Currently saved Universal Node Locator
173          */
174         public final function getCurrentUniversalNodeLocator () {
175                 // Construct generic UNL
176                 $unl = sprintf('%s://%s',
177                         $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PROTOCOL),
178                         $this->getAddressPart()
179                 );
180
181                 // Return it
182                 return $unl;
183         }
184
185         /**
186          * Default implementation for returning address part, may not be suitable
187          * for IPv4/IPv6 protocol handlers. So you have to overwrite (NOT CHANGE!) this method.
188          *
189          * @return      $address        Address part for the final UNL
190          */
191         public function getAddressPart () {
192                 // Return it
193                 return $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_ADDRESS);
194         }
195
196         /**
197          * If the found UNL (address) matches own external or internal address
198          *
199          * @return      $ifMatches      Whether the found UNL matches own addresss
200          */
201         public function isOwnAddress () {
202                 // Get current UNL (from universalNodeLocatorData array)
203                 $currentUnl = $this->getCurrentUniversalNodeLocator();
204
205                 // Get own external UNL
206                 $externalUnl = HubTools::determineOwnExternalAddress();
207
208                 // Get internal UNL
209                 $internalUnl = HubTools::determineOwnInternalAddress();
210
211                 // Debug message
212                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: currentUnl=' . $currentUnl . ',externalUnl=' . $externalUnl . ',internalUnl=' . $internalUnl);
213                 //* DIE-DEBUG: */ die(__METHOD__.':currentUnl=' . $currentUnl . ',this='.print_r($this, TRUE));
214
215                 // Is it the same?
216                 $ifMatches = (($currentUnl === $externalUnl) || ($currentUnl === $internalUnl));
217
218                 // Debug message
219                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: ifMatches=' . intval($ifMatches));
220
221                 // Return result
222                 return $ifMatches;
223         }
224 }
225
226 // [EOF]
227 ?>