3 * A general handler for protocols such as TCP, UDP and others.
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 BaseProtocolHandler extends BaseHandler {
26 * Whole UNL data array
28 private $universalNodeLocatorData = array();
31 * Name of used protocol
33 private $protocolName = 'invalid';
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * Getter for protocol name
49 * @return $protocolName Name of used protocol
51 public final function getProtocolName () {
52 return $this->protocolName;
56 * Setter for protocol name
58 * @param $protocolName Name of used protocol
61 protected final function setProtocolName ($protocolName) {
62 $this->protocolName = $protocolName;
66 * Setter for UNL data array to satify HandleableProtocol
68 * @para $unlData The UNL data array
71 protected final function setUniversalNodeLocatorData (array $unlData) {
72 // Set new UNL data array
73 $this->universalNodeLocatorData = $unlData;
77 * Getter for UNL data array to satify HandleableProtocol
79 * @return $unlData The UNL data array
81 public final function getUniversalNodeLocatorDataArray () {
82 // Return UNL data array
83 return $this->universalNodeLocatorData;
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.
91 * @param $unl Universal Node Locator to validate
92 * @return $isValid Whether the UNL is valid
94 protected final function isValidUniversalNodeLocator ($unl) {
96 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',regex=' . $regex . ' - CALLED!');
98 // Very basic regex check
99 $isValid = (preg_match($this->getRegularExpression(), $unl) === 1);
102 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
107 * Parses the given UNL by splitting it up in its components. The UNL ...
109 * protocol://address[:port]
114 * 'protocol' => 'value',
115 * 'address' => 'value',
119 * The value for 'extra' then must be handled by parseUniversalNodeLocator()
120 * of the individual protocol handler as this is protocol-specific.
122 * @param $unl Universal Node Locator (UNL) to "parse"
123 * @return $unlData Array with all components of the UNL
125 protected function parseGenericUniversalNodeLocator ($unl) {
127 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - CALLED!');
129 // Make sure the UNL is valid
130 assert($this->isValidUniversalNodeLocator($unl));
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.
136 $unlParts = explode('://', $unl);
138 // Split again the last part as: address:port
139 $unlParts[1] = explode(':', $unlParts[1]);
141 // Now there is an almost useable array which then can be copied to the "real" array.
143 UniversalNodeLocator::UNL_PART_PROTOCOL => $unlParts[0],
144 UniversalNodeLocator::UNL_PART_ADDRESS => $unlParts[1][0],
145 UniversalNodeLocator::UNL_PART_EXTRA => $unlParts[1][1]
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
151 // Return the generic array
156 * Gets an element from universalNodeLocatorData array
158 * @param $element Element in universalNodeLocatorData array
159 * @return $value Found value
161 protected final function getUniversalNodeLocatorDataElement ($element) {
162 // Is the element there?
163 assert(isset($this->universalNodeLocatorData[$element]));
166 return $this->universalNodeLocatorData[$element];
170 * "Getter" for currently saved UNL
172 * @return $unl Currently saved Universal Node Locator
174 public final function getCurrentUniversalNodeLocator () {
175 // Construct generic UNL
176 $unl = sprintf('%s://%s',
177 $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PROTOCOL),
178 $this->getAddressPart()
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.
189 * @return $address Address part for the final UNL
191 public function getAddressPart () {
193 return $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_ADDRESS);
197 * If the found UNL (address) matches own external or internal address
199 * @return $ifMatches Whether the found UNL matches own addresss
201 public function isOwnAddress () {
202 // Get current UNL (from universalNodeLocatorData array)
203 $currentUnl = $this->getCurrentUniversalNodeLocator();
205 // Get own external UNL
206 $externalUnl = HubTools::determineOwnExternalAddress();
209 $internalUnl = HubTools::determineOwnInternalAddress();
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));
216 $ifMatches = (($currentUnl === $externalUnl) || ($currentUnl === $internalUnl));
219 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: ifMatches=' . intval($ifMatches));