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 * Protected constructor
33 * @param $className Name of the class
36 protected function __construct ($className) {
37 // Call parent constructor
38 parent::__construct($className);
42 * Setter for UNL data array to satify HandleableProtocol
44 * @para $unlData The UNL data array
47 protected final function setUniversalNodeLocatorData (array $unlData) {
48 // Set new UNL data array
49 $this->universalNodeLocatorData = $unlData;
53 * Getter for UNL data array to satify HandleableProtocol
55 * @return $unlData The UNL data array
57 public final function getUniversalNodeLocatorDataArray () {
58 // Return UNL data array
59 return $this->universalNodeLocatorData;
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.
67 * @param $unl Universal Node Locator to validate
68 * @return $isValid Whether the UNL is valid
70 protected final function isValidUniversalNodeLocator ($unl) {
72 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',regex=' . $regex . ' - CALLED!');
74 // Very basic regex check
75 $isValid = (preg_match($this->getRegularExpression(), $unl) === 1);
78 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!');
83 * Parses the given UNL by splitting it up in its components. The UNL ...
85 * protocol://address[:port]
90 * 'protocol' => 'value',
91 * 'address' => 'value',
95 * The value for 'extra' then must be handled by parseUniversalNodeLocator()
96 * of the individual protocol handler as this is protocol-specific.
98 * @param $unl Universal Node Locator (UNL) to "parse"
99 * @return $unlData Array with all components of the UNL
101 protected function parseGenericUniversalNodeLocator ($unl) {
103 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - CALLED!');
105 // Make sure the UNL is valid
106 assert($this->isValidUniversalNodeLocator($unl));
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.
112 $unlParts = explode('://', $unl);
114 // Split again the last part as: address:port
115 $unlParts[1] = explode(':', $unlParts[1]);
117 // Now there is an almost useable array which then can be copied to the "real" array.
119 UniversalNodeLocator::UNL_PART_PROTOCOL => $unlParts[0],
120 UniversalNodeLocator::UNL_PART_ADDRESS => $unlParts[1][0],
121 UniversalNodeLocator::UNL_PART_EXTRA => $unlParts[1][1]
125 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
127 // Return the generic array
132 * Gets an element from universalNodeLocatorData array
134 * @param $element Element in universalNodeLocatorData array
135 * @return $value Found value
137 protected final function getUniversalNodeLocatorDataElement ($element) {
138 // Is the element there?
139 assert(isset($this->universalNodeLocatorData[$element]));
142 return $this->universalNodeLocatorData[$element];
146 * "Getter" for currently saved UNL
148 * @return $unl Currently saved Universal Node Locator
150 public final function getCurrentUniversalNodeLocator () {
151 // Construct generic UNL
152 $unl = sprintf('%s://%s',
153 $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PROTOCOL),
154 $this->getAddressPart()
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.
165 * @return $address Address part for the final UNL
167 public function getAddressPart () {
169 return $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_ADDRESS);
173 * If the found UNL (address) matches own external or internal address
175 * @param $unl UNL to test
176 * @return $ifMatches Whether the found UNL matches own addresss
178 public function isOwnAddress ($unl) {
179 // Get own external UNL
180 $externalUnl = HubTools::determineOwnExternalAddress();
183 $internalUnl = HubTools::determineOwnInternalAddress();
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));
190 $ifMatches = (($unl === $externalUnl) || ($unl === $internalUnl));
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: ifMatches=' . intval($ifMatches));