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