* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class BaseProtocolHandler extends BaseHandler {
+ /**
+ * Whole UNL data array
+ */
+ private $universalNodeLocatorData = array();
+
/**
* Protected constructor
*
parent::__construct($className);
}
+ /**
+ * 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
- * @param $regex Regular expression to use for validation without slashes
* @return $isValid Whether the UNL is valid
*/
- protected final function isValidUniversalNodeLocator ($unl, $regex) {
+ 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('/^' . $regex . '$/', $unl) === 1);
+ $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
+ *
+ * @return $ifMatches Whether the found UNL matches own addresss
+ */
+ public function isOwnAddress () {
+ // Get current UNL (from universalNodeLocatorData array)
+ $currentUnl = $this->getCurrentUniversalNodeLocator();
+
+ // Get own external UNL
+ $externalUnl = HubTools::determineOwnExternalAddress();
+
+ // Get internal UNL
+ $internalUnl = HubTools::determineOwnInternalAddress();
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: currentUnl=' . $currentUnl . ',externalUnl=' . $externalUnl . ',internalUnl=' . $internalUnl);
+ //* DIE-DEBUG: */ die(__METHOD__.':currentUnl=' . $currentUnl . ',this='.print_r($this, TRUE));
+
+ // Is it the same?
+ $ifMatches = (($currentUnl === $externalUnl) || ($currentUnl === $internalUnl));
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: ifMatches=' . intval($ifMatches));
+
+ // Return result
+ return $ifMatches;
+ }
}
// [EOF]