* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class BaseProtocolHandler extends BaseHandler { /** * Whole UNL data array */ private $universalNodeLocatorData = array(); /** * Name of used protocol */ private $protocolName = 'invalid'; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); } /** * Getter for protocol name * * @return $protocolName Name of used protocol */ public final function getProtocolName () { return $this->protocolName; } /** * Setter for protocol name * * @param $protocolName Name of used protocol * @return void */ protected final function setProtocolName ($protocolName) { $this->protocolName = $protocolName; } /** * Setter for UNL data array to satify HandleableProtocol * * @para $unlData The UNL data array * @return void */ protected final function setUniversalNodeLocatorData (array $unlData) { // Set new UNL data array $this->universalNodeLocatorData = $unlData; } /** * Getter for UNL data array to satify HandleableProtocol * * @return $unlData The UNL data array */ public final function getUniversalNodeLocatorDataArray () { // Return UNL data array return $this->universalNodeLocatorData; } /** * Validates given UNL very basicly by given regular expression. You * normally don't need/want to overwrite this method as this is a very basic * validation only based on a regex. * * @param $unl Universal Node Locator to validate * @return $isValid Whether the UNL is valid */ protected final function isValidUniversalNodeLocator ($unl) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',regex=' . $regex . ' - CALLED!'); // Very basic regex check $isValid = (preg_match($this->getRegularExpression(), $unl) === 1); // Return result //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: isValid=' . intval($isValid) . ' - EXIT!'); return $isValid; } /** * Parses the given UNL by splitting it up in its components. The UNL ... * * protocol://address[:port] * * ... becomes: * * array( * 'protocol' => 'value', * 'address' => 'value', * 'extra' => 'port' * ) * * The value for 'extra' then must be handled by parseUniversalNodeLocator() * of the individual protocol handler as this is protocol-specific. * * @param $unl Universal Node Locator (UNL) to "parse" * @return $unlData Array with all components of the UNL */ protected function parseGenericUniversalNodeLocator ($unl) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ' - CALLED!'); // Make sure the UNL is valid assert($this->isValidUniversalNodeLocator($unl)); /* * "Parse" the UNL "generically", sadly this cannot be done by using preg_match() :-( * @TODO If you know why, please fix and explain it to me. */ $unlParts = explode('://', $unl); // Split again the last part as: address:port $unlParts[1] = explode(':', $unlParts[1]); // Now there is an almost useable array which then can be copied to the "real" array. $unlData = array( UniversalNodeLocator::UNL_PART_PROTOCOL => $unlParts[0], UniversalNodeLocator::UNL_PART_ADDRESS => $unlParts[1][0], UniversalNodeLocator::UNL_PART_EXTRA => $unlParts[1][1] ); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unlData=' . print_r($unlData, TRUE) . ' - EXIT!'); // Return the generic array return $unlData; } /** * Gets an element from universalNodeLocatorData array * * @param $element Element in universalNodeLocatorData array * @return $value Found value */ protected final function getUniversalNodeLocatorDataElement ($element) { // Is the element there? assert(isset($this->universalNodeLocatorData[$element])); // Return it return $this->universalNodeLocatorData[$element]; } /** * "Getter" for currently saved UNL * * @return $unl Currently saved Universal Node Locator */ public final function getCurrentUniversalNodeLocator () { // Construct generic UNL $unl = sprintf('%s://%s', $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PROTOCOL), $this->getAddressPart() ); // Return it return $unl; } /** * Default implementation for returning address part, may not be suitable * for IPv4/IPv6 protocol handlers. So you have to overwrite (NOT CHANGE!) this method. * * @return $address Address part for the final UNL */ public function getAddressPart () { // Return it return $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_ADDRESS); } /** * If the found UNL (address) matches own external or internal address * * @param $unl UNL to test * @return $ifMatches Whether the found UNL matches own addresss */ public function isOwnAddress ($unl) { // Get own external UNL $externalUnl = HubTools::determineOwnExternalAddress(); // Get internal UNL $internalUnl = HubTools::determineOwnInternalAddress(); // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: unl=' . $unl . ',externalUnl=' . $externalUnl . ',internalUnl=' . $internalUnl); //* DIE-DEBUG: */ die(__METHOD__.':unl=' . $unl . ',this='.print_r($this, TRUE)); // Is it the same? $ifMatches = (($unl === $externalUnl) || ($unl === $internalUnl)); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: ifMatches=' . intval($ifMatches)); // Return result return $ifMatches; } } // [EOF] ?>