From 7db1f31a4de10c2192e847db73060aed8f7a3f70 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 20 May 2017 21:47:20 +0200 Subject: [PATCH] Continued: - imported HandleableProtocol - imported Registerable (only templates) - created some namespaces - the "new" exception is coming back from 'core' as it is not generic - updated core MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../hub/classes/class_BaseHubSystem.php | 49 +++++++++++++++++++ .../protocol/class_ProtocolDiscovery.php | 4 +- .../socket/class_PackageSocketDiscovery.php | 1 + .../factories/client/class_ClientFactory.php | 1 + .../factories/socket/class_SocketFactory.php | 1 + .../hub/classes/handler/protocol/class_ | 13 +++-- .../protocol/class_BaseProtocolHandler.php | 45 ++++++----------- .../hub/classes/handler/protocol/ipv4/class_ | 13 +++-- .../ipv4/class_BaseIpV4ProtocolHandler.php | 23 +++------ .../ipv4/tcp/class_TcpProtocolHandler.php | 3 ++ .../ipv4/class_BaseIpV4ConnectionHelper.php | 4 +- .../ipv4/tcp/class_TcpConnectionHelper.php | 15 ++---- .../tags/package/class_PackageTags.php | 4 +- application/hub/exceptions/socket/.htaccess | 1 + .../class_NoSocketRegisteredException.php | 48 ++++++++++++++++++ .../socket/class_DiscoverableSocket.php | 6 +++ .../protocol/class_HandleableProtocol.php | 15 ++++++ .../connections/class_ConnectionHelper.php | 2 +- .../hub/interfaces/tags/class_Tagable.php | 3 -- core | 2 +- 20 files changed, 176 insertions(+), 77 deletions(-) create mode 100644 application/hub/exceptions/socket/.htaccess create mode 100644 application/hub/exceptions/socket/class_NoSocketRegisteredException.php diff --git a/application/hub/classes/class_BaseHubSystem.php b/application/hub/classes/class_BaseHubSystem.php index e81113d92..1f100e258 100644 --- a/application/hub/classes/class_BaseHubSystem.php +++ b/application/hub/classes/class_BaseHubSystem.php @@ -4,6 +4,7 @@ namespace Hub\Generic; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\Handler\Protocol\HandleableProtocol; use Hub\Handler\RawData\BaseRawDataHandler; use Hub\Information\ShareableInfo; use Hub\Listener\BaseListener; @@ -102,6 +103,16 @@ class BaseHubSystem extends BaseFrameworkSystem implements HubInterface { */ private $listenerInstance = NULL; + /** + * A HandleableProtocol instance + */ + private $protocolInstance = NULL; + + /** + * Name of used protocol + */ + private $protocolName = 'invalid'; + /** * Protected constructor * @@ -391,4 +402,42 @@ class BaseHubSystem extends BaseFrameworkSystem implements HubInterface { return $this->listenerInstance; } + /** + * Setter for HandleableProtocol instance + * + * @param $protocolInstance An instance of an HandleableProtocol + * @return void + */ + public final function setProtocolInstance (HandleableProtocol $protocolInstance) { + $this->protocolInstance = $protocolInstance; + } + + /** + * Getter for HandleableProtocol instance + * + * @return $protocolInstance An instance of an HandleableProtocol + */ + public final function getProtocolInstance () { + return $this->protocolInstance; + } + + /** + * 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; + } + } diff --git a/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php b/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php index acae4da49..f49bdc812 100644 --- a/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php +++ b/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php @@ -104,7 +104,5 @@ class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtoco // @TODO Add some validation here??? return $protocolType; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php index c08c97f1a..4a57ec5e4 100644 --- a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php +++ b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php @@ -7,6 +7,7 @@ use Hub\Container\Socket\StorableSocket; use Hub\Factory\Node\NodeObjectFactory; use Hub\Factory\Socket\SocketFactory; use Hub\Generic\BaseHubSystem; +use Hub\Handler\Protocol\HandleableProtocol; use Hub\Helper\Connection\BaseConnectionHelper; use Hub\Network\Package\NetworkPackage; diff --git a/application/hub/classes/factories/client/class_ClientFactory.php b/application/hub/classes/factories/client/class_ClientFactory.php index b40974d2a..d5e9c457b 100644 --- a/application/hub/classes/factories/client/class_ClientFactory.php +++ b/application/hub/classes/factories/client/class_ClientFactory.php @@ -4,6 +4,7 @@ namespace Hub\Factory\Client; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\Handler\Protocol\HandleableProtocol; // Import framework stuff use CoreFramework\Factory\ObjectFactory; diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 297342a5b..dfacab06b 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -3,6 +3,7 @@ namespace Hub\Factory\Socket; // Import application-specific stuff +use Hub\Handler\Protocol\HandleableProtocol; use Hub\Listener\BaseListener; use Hub\Network\Package\NetworkPackage; diff --git a/application/hub/classes/handler/protocol/class_ b/application/hub/classes/handler/protocol/class_ index 665316593..beada8cf3 100644 --- a/application/hub/classes/handler/protocol/class_ +++ b/application/hub/classes/handler/protocol/class_ @@ -1,4 +1,13 @@ +} diff --git a/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php b/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php index 222b467cf..079ceea80 100644 --- a/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php +++ b/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php @@ -3,6 +3,7 @@ namespace Hub\Handler\Protocol; // Import application-specific sutff +use Hub\Handler\Protocol\HandleableProtocol; use Hub\Locator\Node\UniversalNodeLocator; use Hub\Tools\HubTools; @@ -28,11 +29,11 @@ use Hub\Tools\HubTools; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BaseProtocolHandler extends BaseHandler { +class BaseProtocolHandler extends BaseHandler implements HandleableProtocol { /** - * Whole UNL data array + * Whole UNL instance */ - private $universalNodeLocatorData = array(); + private $universalNodeLocatorInstance = NULL; /** * Protected constructor @@ -46,24 +47,14 @@ class BaseProtocolHandler extends BaseHandler { } /** - * Setter for UNL data array to satify HandleableProtocol + * Setter for UNL instance * - * @para $unlData The UNL data array + * @para $unlInstance An instance of a LocateableNode class * @return void */ - protected final function setUniversalNodeLocatorData (array $unlData) { + protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { // 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; + $this->universalNodeLocatorInstance = $unlInstance; } /** @@ -150,19 +141,13 @@ class BaseProtocolHandler extends BaseHandler { } /** - * "Getter" for currently saved UNL + * Getter for UNL instance * - * @return $unl Currently saved Universal Node Locator + * @return $unlData An instance of a LocateableNode class */ - public final function getCurrentUniversalNodeLocator () { - // Construct generic UNL - $unl = sprintf('%s://%s', - $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PROTOCOL), - $this->getAddressPart() - ); - - // Return it - return $unl; + public final function getUniversalNodeLocatorInstance () { + // Return UNL data array + return $this->universalNodeLocatorInstance; } /** @@ -202,7 +187,5 @@ class BaseProtocolHandler extends BaseHandler { // Return result return $ifMatches; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/handler/protocol/ipv4/class_ b/application/hub/classes/handler/protocol/ipv4/class_ index 665316593..88954cebe 100644 --- a/application/hub/classes/handler/protocol/ipv4/class_ +++ b/application/hub/classes/handler/protocol/ipv4/class_ @@ -1,4 +1,11 @@ . */ -class ???ProtocolHandler extends BaseProtocolHandler implements HandleableProtocol, Registerable { +class ???ProtocolHandler extends BaseIpV4Handler { /** * Protected constructor * @@ -47,7 +54,5 @@ class ???ProtocolHandler extends BaseProtocolHandler implements HandleableProtoc // Return the prepared instance return $handlerInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php index 2ab056d74..3a1ff1f48 100644 --- a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php +++ b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php @@ -52,7 +52,7 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { } /** - * Setter for port number to satify HandleableProtocol + * Setter for port number * * @para $port The port number * @return void @@ -63,7 +63,7 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { } /** - * Getter for port number to satify HandleableProtocol + * Getter for port number * * @return $port The port number */ @@ -140,20 +140,13 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { // If this doesn't fail, continue validating the IP:port combination if ($isValid === TRUE) { // ... and validate IP:port, first "parse" the UNL - $unlData = $this->parseUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); - - /* - * Make sure the extra field 'port' is there. This may look - * superflious but in case of a rewrite this assert will stop at - * badly formated arrays. - */ - assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT])); + $unlInstance = $this->parseUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); // Set whole UNL data array - $this->setUniversalNodeLocatorData($unlData); + $this->setUniversalNodeLocatorInstance($unlInstance); // Set port - $this->setPort($unlData[UniversalNodeLocator::UNL_PART_PORT]); + $this->setPort($unlInstance->getUnlPort()); } // END - if // Return result @@ -171,13 +164,11 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { // Construct address $address = sprintf('%s:%s', parent::getAddressPart(), - $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_PORT) + $this->getUniversalNodeLocatorInstance()->getUnlPort() ); // Return it return $address; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/handler/protocol/ipv4/tcp/class_TcpProtocolHandler.php b/application/hub/classes/handler/protocol/ipv4/tcp/class_TcpProtocolHandler.php index cc4469f12..332a995a9 100644 --- a/application/hub/classes/handler/protocol/ipv4/tcp/class_TcpProtocolHandler.php +++ b/application/hub/classes/handler/protocol/ipv4/tcp/class_TcpProtocolHandler.php @@ -2,6 +2,9 @@ // Own namespace namespace Hub\Handler\Protocol\Tcp; +// Import application-specific stuff +use Hub\Handler\Protocol\HandleableProtocol; + // Import framework stuff use CoreFramework\Registry\Registerable; diff --git a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php index 8445a5696..3bd764482 100644 --- a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php @@ -48,7 +48,7 @@ class BaseIpV4ConnectionHelper extends BaseConnectionHelper { } /** - * Getter for port number to satify HandleableProtocol + * Getter for port number to * * @return $connectionPort The port number */ @@ -57,7 +57,7 @@ class BaseIpV4ConnectionHelper extends BaseConnectionHelper { } /** - * Setter for port number to satify HandleableProtocol + * Setter for port number * * @param $connectionPort The port number * @return void diff --git a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php index 00229855e..6859446f1 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -97,20 +97,15 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // @TODO The whole resolving part should be moved out and made more configurable // Init recipient data - $unlData = array(); + $unlInstance = NULL; // Try to solve the recipient try { // Get protocol handler back from package data $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData); - // Get UNL data - $unlData = $handlerInstance->getUniversalNodeLocatorDataArray(); - - // Make sure it is a valid Universal Node Locator array (3 elements) - assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL])); - assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS])); - assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT])); + // Get UNL instance + $unlInstance = $handlerInstance->getUniversalNodeLocatorInstance(); // Set handler instance $helperInstance->setHandlerInstance($handlerInstance); @@ -143,8 +138,8 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection } // Set address and maybe port - $helperInstance->setAddress($unlData[UniversalNodeLocator::UNL_PART_ADDRESS]); - $helperInstance->setConnectionPort($unlData[UniversalNodeLocator::UNL_PART_PORT]); + $helperInstance->setAddress($unlInstance->getUnlAddress()); + $helperInstance->setConnectionPort($unlInstance->getUnlPort()); // Now connect to it if (!$helperInstance->connectToPeerByUnlData($unlData)) { diff --git a/application/hub/classes/tags/package/class_PackageTags.php b/application/hub/classes/tags/package/class_PackageTags.php index 1bb2d03bb..f1e67b1fe 100644 --- a/application/hub/classes/tags/package/class_PackageTags.php +++ b/application/hub/classes/tags/package/class_PackageTags.php @@ -194,7 +194,5 @@ class PackageTags extends BaseTag implements Tagable { // And return the result return $accepts; } -} -// [EOF] -?> +} diff --git a/application/hub/exceptions/socket/.htaccess b/application/hub/exceptions/socket/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/exceptions/socket/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/exceptions/socket/class_NoSocketRegisteredException.php b/application/hub/exceptions/socket/class_NoSocketRegisteredException.php new file mode 100644 index 000000000..cbce9f062 --- /dev/null +++ b/application/hub/exceptions/socket/class_NoSocketRegisteredException.php @@ -0,0 +1,48 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 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 NoSocketRegisteredException extends AbstractSocketException { + /** + * A Constructor for this exception + * + * @param $protocolInstance An instance of a HandleableProtocol class + * @param $code Error code + * @return void + */ + public function __construct (HandleableProtocol $protocolInstance, $code) { + // Construct the message + $message = sprintf('[%s:] Requested socket is not yet registered.', + $protocolInstance->__toString() + ); + + // Call parent exception constructor + parent::__construct($message, $code); + } + +} diff --git a/application/hub/interfaces/discovery/recipient/socket/class_DiscoverableSocket.php b/application/hub/interfaces/discovery/recipient/socket/class_DiscoverableSocket.php index c88c73d3b..95dd2de72 100644 --- a/application/hub/interfaces/discovery/recipient/socket/class_DiscoverableSocket.php +++ b/application/hub/interfaces/discovery/recipient/socket/class_DiscoverableSocket.php @@ -1,4 +1,10 @@ . */ interface HandleableProtocol extends Handleable { + /** + * Getter for UNL instance + * + * @return $unlData An instance of a LocateableNode class + */ + function getUniversalNodeLocatorInstance (); + /** * Getter for port number to satify HandleableProtocol * @@ -42,6 +49,14 @@ interface HandleableProtocol extends Handleable { */ function getProtocolName (); + /** + * 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 UNL + */ + function getAddressPart (); + /** * Validates given 'recipient' if it is a valid UNL. This means that the UNL * can be parsed by the protocol handler. diff --git a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php index fbbc630ca..a053edcea 100644 --- a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php +++ b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php @@ -48,7 +48,7 @@ interface ConnectionHelper extends HubHelper { function doShutdown (); /** - * Getter for port number to satify HandleableProtocol + * Getter for port number * * @return $port The port number */ diff --git a/application/hub/interfaces/tags/class_Tagable.php b/application/hub/interfaces/tags/class_Tagable.php index 5ece046b6..0a683eb52 100644 --- a/application/hub/interfaces/tags/class_Tagable.php +++ b/application/hub/interfaces/tags/class_Tagable.php @@ -49,6 +49,3 @@ interface Tagable extends HubInterface { function ifPackageDataIsAcceptedByListener (array $packageData, Listenable $listenerInstance); } - -// [EOF] -?> diff --git a/core b/core index aa76ddf7f..8d7422139 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit aa76ddf7f1d5d302f1d752efcd113515ecf152a2 +Subproject commit 8d7422139a55477388fcd6a30a1ce41d83780e5e -- 2.39.5