From: Roland Häder Date: Sun, 21 May 2017 11:37:33 +0000 (+0200) Subject: WIP: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=776bb92a800bdac5107761be0ba88f5d4b1949a7;p=hub.git WIP: - rewrite from unlData -> unlInstance (still open things) - moved constants from class to interface LocateableNode - moved UNL_REGEX to same interface for rewrite of those parse methods. They should be moved to a factory instead. Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 95b79fc95..d23b46f6e 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -646,11 +646,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Handles socket error 'connection timed out', but does not clear it for * later debugging purposes. * - * @param $unlData A valid UNL data array + * @param $socketData Valid socket data array * @return void * @throws SocketConnectionException The connection attempts fails with a time-out */ - protected function handleSocketErrorConnectionTimedOut (array $unlData) { + protected function handleSocketErrorConnectionTimedOut (array $socketData) { // Get socket error code for verification $socketError = $this->getLastSocketError(); @@ -668,11 +668,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Handles socket error 'resource temporary unavailable', but does not * clear it for later debugging purposes. * - * @param $unlData A valid UNL data array + * @param $socketData Valid socket data array * @return void * @throws SocketConnectionException The connection attempts fails with a time-out */ - protected function handleSocketErrorResourceUnavailable (array $unlData) { + protected function handleSocketErrorResourceUnavailable (array $socketData) { // Get socket error code for verification $socketError = $this->getLastSocketError(); @@ -690,11 +690,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Handles socket error 'connection refused', but does not clear it for * later debugging purposes. * - * @param $unlData A valid UNL data array + * @param $socketData Valid socket data array * @return void * @throws SocketConnectionException The connection attempts fails with a time-out */ - protected function handleSocketErrorConnectionRefused (array $unlData) { + protected function handleSocketErrorConnectionRefused (array $socketData) { // Get socket error code for verification $socketError = $this->getLastSocketError(); @@ -712,11 +712,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Handles socket error 'no route to host', but does not clear it for later * debugging purposes. * - * @param $unlData A valid UNL data array + * @param $socketData Valid socket data array * @return void * @throws SocketConnectionException The connection attempts fails with a time-out */ - protected function handleSocketErrorNoRouteToHost (array $unlData) { + protected function handleSocketErrorNoRouteToHost (array $socketData) { // Get socket error code for verification $socketError = $this->getLastSocketError(); @@ -732,14 +732,14 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable /** * Handles socket error 'operation already in progress' which happens in - * method connectToPeerByUnlData() on timed out connection + * method connectToPeerByUnlInstance() on timed out connection * attempts. * - * @param $unlData A valid UNL data array + * @param $socketData Valid socket data array * @return void * @throws SocketConnectionException The connection attempts fails with a time-out */ - protected function handleSocketErrorOperationAlreadyProgress (array $unlData) { + protected function handleSocketErrorOperationAlreadyProgress (array $socketData) { // Get socket error code for verification $socketError = $this->getLastSocketError(); @@ -757,11 +757,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Handles socket error 'connection reset by peer', but does not clear it for * later debugging purposes. * - * @param $unlData A valid UNL data array + * @param $socketData Valid socket data array * @return void * @throws SocketConnectionException The connection attempts fails with a time-out */ - protected function handleSocketErrorConnectionResetByPeer (array $unlData) { + protected function handleSocketErrorConnectionResetByPeer (array $socketData) { // Get socket error code for verification $socketError = $this->getLastSocketError(); @@ -779,11 +779,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Handles socket error 'operation not supported', but does not clear it for * later debugging purposes. * - * @param $unlData A valid UNL data array + * @param $socketData Valid socket data array * @return void * @throws SocketOperationException The connection attempts fails with a time-out */ - protected function handleSocketErrorOperationNotSupported (array $unlData) { + protected function handleSocketErrorOperationNotSupported (array $socketData) { // Get socket error code for verification $socketError = $this->getLastSocketError(); @@ -801,10 +801,10 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * Handles socket "error" 'operation now in progress' which can be safely * passed on with non-blocking connections. * - * @param $unlData A valid UNL data array + * @param $socketData Valid socket data array * @return void */ - protected function handleSocketErrorOperationInProgress (array $unlData) { + protected function handleSocketErrorOperationInProgress (array $socketData) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.'); } diff --git a/application/hub/classes/discovery/node/class_UniversalNodeLocatorDiscovery.php b/application/hub/classes/discovery/node/class_UniversalNodeLocatorDiscovery.php index 48495b276..e0b4d4856 100644 --- a/application/hub/classes/discovery/node/class_UniversalNodeLocatorDiscovery.php +++ b/application/hub/classes/discovery/node/class_UniversalNodeLocatorDiscovery.php @@ -110,7 +110,5 @@ class UniversalNodeLocatorDiscovery extends BaseNodeDiscovery implements Discove //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNL-DISCOVERY:configKey=' . $configKey . ',unl=' . $GLOBALS[__METHOD__][$configKey] . ' - EXIT!'); return $GLOBALS[__METHOD__][$configKey]; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index d62d7a55a..54dd00e32 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -60,9 +60,6 @@ class SocketFactory extends ObjectFactory { // Init instance $socketInstance = NULL; - // Get an instance - $factoryInstance = new SocketFactory(); - // Construct registry key $registryKey = 'socket_' . $protocolInstance->getProtocolName() . '_' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]; @@ -84,13 +81,7 @@ class SocketFactory extends ObjectFactory { //*NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Going to use class ' . $className . ' for creating a socket resource ...'); // And call the static method - $socketResource = call_user_func($className . '::createConnectionFromPackageData', $packageData); - - // Debug message - //*NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Created socket ' . $socketResource . '(' . gettype($socketResource) . ') from class ' . $className . '.'); - - // Construct container class, this won't be reached if an exception is thrown - $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, NULL, $packageData)); + $socketInstance = call_user_func($className . '::createConnectionFromPackageData', $packageData); // Register it with the registry Registry::getRegistry()->addInstance($registryKey, $socketInstance); diff --git a/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php b/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php index 079ceea80..8e59424f0 100644 --- a/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php +++ b/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php @@ -4,7 +4,7 @@ namespace Hub\Handler\Protocol; // Import application-specific sutff use Hub\Handler\Protocol\HandleableProtocol; -use Hub\Locator\Node\UniversalNodeLocator; +use Hub\Locator\Node\LocateableNode; use Hub\Tools\HubTools; /** @@ -90,13 +90,14 @@ class BaseProtocolHandler extends BaseHandler implements HandleableProtocol { * 'extra' => 'port' * ) * - * The value for 'extra' then must be handled by parseUniversalNodeLocator() - * of the individual protocol handler as this is protocol-specific. + * The value for 'extra' then must be handled by the overwriting + * parseUniversalNodeLocator() method 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 + * @return $unlInstance An instance of a LocateableNode class */ - protected function parseGenericUniversalNodeLocator ($unl) { + protected function parseUniversalNodeLocator ($unl) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(': unl=' . $unl . ' - CALLED!'); @@ -114,16 +115,20 @@ class BaseProtocolHandler extends BaseHandler implements HandleableProtocol { // 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] + LocateableNode::UNL_PART_PROTOCOL => $unlParts[0], + LocateableNode::UNL_PART_ADDRESS => $unlParts[1][0], + LocateableNode::UNL_PART_EXTRA => $unlParts[1][1] ); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(': unlData=' . print_r($unlData, TRUE) . ' - EXIT!'); - // Return the generic array - return $unlData; + // Create instance from it + //$unlInstance = ; + $this->partialStub('Please rewrite this part.'); + + // Return instance + return $unlInstance; } /** @@ -158,7 +163,7 @@ class BaseProtocolHandler extends BaseHandler implements HandleableProtocol { */ public function getAddressPart () { // Return it - return $this->getUniversalNodeLocatorDataElement(UniversalNodeLocator::UNL_PART_ADDRESS); + return $this->getUniversalNodeLocatorDataElement(LocateableNode::UNL_PART_ADDRESS); } /** diff --git a/application/hub/classes/handler/protocol/ipv4/class_ b/application/hub/classes/handler/protocol/ipv4/class_ index 88954cebe..49a462c24 100644 --- a/application/hub/classes/handler/protocol/ipv4/class_ +++ b/application/hub/classes/handler/protocol/ipv4/class_ @@ -3,6 +3,7 @@ namespace Hub\Handler\Protocol\; // Import application-specific stuff +use Hub\Handler\Protocol\Network\IpV4\BaseIpV4Handler; // Import framework stuff diff --git a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php index 3a1ff1f48..70056e263 100644 --- a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php +++ b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php @@ -3,7 +3,7 @@ namespace Hub\Handler\Protocol\Network\IpV4; // Import application-specific stuff -use Hub\Locator\Node\UniversalNodeLocator; +use Hub\Locator\Node\LocateableNode; use Hub\Network\Package\NetworkPackage; /** @@ -29,9 +29,6 @@ use Hub\Network\Package\NetworkPackage; * along with this program. If not, see . */ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { - // Regular expression for validating IP:port UNLs - const UNL_REGEX = '/^([a-z]{1,}):\/\/\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b:(6553[0-5]|655[0-2][0-9]\d|65[0-4](\d){2}|6[0-4](\d){3}|[1-5](\d){4}|[1-9](\d){0,3})$/'; - /** * Port number */ @@ -48,7 +45,7 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { parent::__construct($className); // Set regex - $this->setRegularExpression(self::UNL_REGEX); + $this->setRegularExpression(LocateableNode::UNL_REGEX); } /** @@ -72,48 +69,6 @@ class BaseIpV4ProtocolHandler extends BaseProtocolHandler { return $this->port; } - /** - * Parses the given UNL by splitting it up in its components. The UNL ... - * - * protocol://address[:port] - * - * ... becomes: - * - * array( - * 'protocol' => 'value', - * 'address' => 'value', - * 'port' => 123 - * ) - * - * @param $unl Universal Node Locator (UNL) to "parse" - * @return $unlData Array with all components of the UNL - */ - protected function parseUniversalNodeLocator ($unl) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(': unl=' . $unl . ' - CALLED!'); - - // First generic parse - $unlData = parent::parseGenericUniversalNodeLocator($unl); - - /* - * Make sure the generic parts are all there. In case of a rewrite, - * these assertitions will bail out on badly formed arrays. - */ - assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL])); - assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS])); - assert(isset($unlData[UniversalNodeLocator::UNL_PART_EXTRA])); - - // Copy 'extra' -> 'port' ... - $unlData[UniversalNodeLocator::UNL_PART_PORT] = $unlData[UniversalNodeLocator::UNL_PART_EXTRA]; - - // ... and drop 'extra' - unset($unlData[UniversalNodeLocator::UNL_PART_EXTRA]); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(': unlData=' . print_r($unlData, TRUE) . ' - EXIT!'); - return $unlData; - } - /** * Validates given 'recipient' if it is a valid UNL. This means that the UNL * can be parsed by the protocol handler. @@ -140,9 +95,10 @@ 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 - $unlInstance = $this->parseUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); - // Set whole UNL data array + // Set whole UNL instance + // @TODO don't do this in a isValidFoo() method $this->setUniversalNodeLocatorInstance($unlInstance); // Set port diff --git a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php index b9799d4dc..f906434f5 100644 --- a/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/class_BaseIpV4ConnectionHelper.php @@ -5,7 +5,7 @@ namespace Hub\Helper\Connection\IpV4; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; use Hub\Helper\Connection\BaseConnectionHelper; -use Hub\Locator\Node\UniversalNodeLocator; +use Hub\Locator\Node\LocateableNode; /** * A ??? connection helper class @@ -93,35 +93,35 @@ class BaseIpV4ConnectionHelper extends BaseConnectionHelper { } /** - * Attempts to connect to a peer by given IP number and port from a valid - * unlData array with currently configured timeout. + * Attempts to connect to a peer by given IP number and port from an UNL + * instance with currently configured timeout. * - * @param $unlData Valid UNL data array + * @param $unlInstance An instance of a LocateableNode class * @return $isConnected Whether the connection went fine * @see Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code * @todo Rewrite the while() loop to a iterator to not let the software stay very long here */ - protected function connectToPeerByUnlData (array $unlData) { + protected function connectToPeerByUnlInstance (LocateableNode $unlInstance) { // Only call this if the connection is initialized by initConnection() assert($this->isInitialized()); - // Is unlData complete? - assert(isset($unlData[UniversalNodeLocator::UNL_PART_PROTOCOL])); - assert(isset($unlData[UniversalNodeLocator::UNL_PART_ADDRESS])); - assert(isset($unlData[UniversalNodeLocator::UNL_PART_PORT])); + // Is the UNL data complete? + assert($unlInstance->isValidUnl()); // "Cache" socket resource and timeout config $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds'); // Debug output - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $unlData[UniversalNodeLocator::UNL_PART_ADDRESS] . ':' . $unlData[UniversalNodeLocator::UNL_PART_PORT] . ' with socketResource[' . gettype($this->getSocketInstance()->getSocketResource() . ']=' . $this->getSocketInstance()->getSocketResource() . ' ...'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $unlInstance->getUnlAddress() . ':' . $unlInstance->getUnlPort() . ' with socketResource[' . gettype($this->getSocketInstance()->getSocketResource() . ']=' . $this->getSocketInstance()->getSocketResource() . ' ...'); // Get current time $hasTimedOut = FALSE; $time = time(); + $this->partialStub('Please rewrite this part.'); + return; // Try to connect until it is connected - while ($isConnected = !@socket_connect($this->getSocketInstance(), $unlData[UniversalNodeLocator::UNL_PART_ADDRESS], $unlData[UniversalNodeLocator::UNL_PART_PORT])) { + while ($isConnected = !@socket_connect($this->getSocketInstance(), $unlData[LocateableNode::UNL_PART_ADDRESS], $unlData[LocateableNode::UNL_PART_PORT])) { // Get last socket error $socketError = socket_last_error($this->getSocketInstance()); 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 d89f0f235..212d97205 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -4,10 +4,13 @@ namespace Hub\Helper\Connection\Tcp; // Import application-specific stuff use Hub\Helper\Connection\ConnectionHelper; -use Hub\Locator\Node\UniversalNodeLocator; +use Hub\Locator\Node\LocateableNode; use Hub\Network\Package\NetworkPackage; use Hub\Tools\HubTools; +// Import framework stuff +use CoreFramework\Factory\ObjectFactory; + /** * A TCP connection helper class * @@ -63,8 +66,14 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Create a socket instance $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + // Debug message + //*NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Created socket ' . $socketResource . '(' . gettype($socketResource) . ') from class ' . $className . '.'); + + // Construct container class, this won't be reached if an exception is thrown + $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', array($socketResource, NULL, $packageData)); + // Is the socket resource valid? - if (!is_resource($socketResource)) { + if (!$socketInstance->isValidSocket()) { /* * Something bad happened, calling handleSocketError() is not * possible here because that method would throw an @@ -74,22 +83,22 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection } // END - if // Get socket error code for verification - $socketError = socket_last_error($socketResource); + $socketError = $socketInstance->getLastSocketError(); // Check if there was an error else if ($socketError > 0) { // Handle this socket error with a faked recipientData array - $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); + $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketInstance, array('0.0.0.0', '0')); // Then throw again throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), StorableSocket::EXCEPTION_SOCKET_CREATION_FAILED); } // END - if // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HELPER: Setting socket resource ... (' . gettype($socketResource) . ')'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HELPER: Setting socket instance ... (' . $socketInstance->__toString() . ')'); // Set the resource - $helperInstance->setSocketResource($socketResource); + $helperInstance->setSocketInstance($socketInstance); // Init connection $helperInstance->initConnection(); @@ -115,7 +124,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Is the recipient equal as configured IP if (substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($helperInstance->getConfigInstance()->getConfigEntry('external_address'))) == $helperInstance->getConfigInstance()->getConfigEntry('external_address')) { // This may connect to shipsimu.org and requests 'ip.php' which will return our external IP address - $unlData[UniversalNodeLocator::UNL_PART_ADDRESS] = HubTools::determineExternalAddress(); + $unlAddress = HubTools::determineExternalAddress(); // Do we have ip:port match? // @TODO Rewrite this test for UNLs @@ -129,7 +138,11 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection $recipientArray = explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); // Add the port - $unlData[UniversalNodeLocator::UNL_PART_PORT] = $recipientArray[UniversalNodeLocator::UNL_PART_PORT]; + $unlPort = $recipientArray[LocateableNode::UNL_PART_PORT]; + + // Create UNL instance + $this->partialStub('Please rewrite this part.'); + //$unlInstance =; } else { // It doesn't match, we need to take care of this later $helperInstance->debugInstance($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . '!=' . $helperInstance->getConfigInstance()->getConfigEntry('external_address')); @@ -141,7 +154,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection $helperInstance->setConnectionPort($unlInstance->getUnlPort()); // Now connect to it - if (!$helperInstance->connectToPeerByUnlData($unlData)) { + if (!$helperInstance->connectToPeerByUnlInstance($unlData)) { // Debug message self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HELPER: helperInstance=' . $helperInstance->__toString() . ',unlData=' . print_r($unlData, TRUE)); diff --git a/application/hub/classes/info/connection/class_ConnectionInfo.php b/application/hub/classes/info/connection/class_ConnectionInfo.php index 0eb2b484f..3da29129c 100644 --- a/application/hub/classes/info/connection/class_ConnectionInfo.php +++ b/application/hub/classes/info/connection/class_ConnectionInfo.php @@ -6,7 +6,7 @@ namespace Hub\Information\Connection; use Hub\Helper\Connection\ConnectionHelper; use Hub\Information\BaseInfo; use Hub\Information\ShareableInfo; -use Hub\Locator\Node\UniversalNodeLocator; +use Hub\Locator\Node\LocateableNode; // Import framework stuff use CoreFramework\Listener\Listenable; @@ -70,8 +70,8 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { // Fill the generic array with several data from the listener: $this->setProtocolName($listenerInstance->getProtocolName()); - $this->setGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_ADDRESS , $listenerInstance->getListenAddress()); - $this->setGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_PORT , $listenerInstance->getListenPort()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $listenerInstance->getListenAddress()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $listenerInstance->getListenPort()); // Set listener here $this->setListenerInstance($listenerInstance); @@ -92,8 +92,8 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { // Fill the generic array with several data from the listener: $this->setProtocolName($helperInstance->getProtocolName()); - $this->setGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_ADDRESS , $helperInstance->getAddress()); - $this->setGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_PORT , $helperInstance->getConnectionPort()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getAddress()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $helperInstance->getConnectionPort()); // Set helper here $this->setHelperInstance($helperInstance); @@ -109,7 +109,7 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { */ public final function getAddress () { // Return it from generic array - return $this->getGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_ADDRESS); + return $this->getGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS); } /** @@ -119,7 +119,7 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { */ public final function getPort () { // Return it from generic array - return $this->getGenericArrayElement('connection', 'dummy', 'dummy', UniversalNodeLocator::UNL_PART_PORT); + return $this->getGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT); } } diff --git a/application/hub/classes/locator/class_UniversalNodeLocator.php b/application/hub/classes/locator/class_UniversalNodeLocator.php index 999d95063..25d92a783 100644 --- a/application/hub/classes/locator/class_UniversalNodeLocator.php +++ b/application/hub/classes/locator/class_UniversalNodeLocator.php @@ -29,16 +29,6 @@ use Hub\Generic\BaseHubSystem; * along with this program. If not, see . */ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode { - //------- UNL parts ------- - // Protocol - const UNL_PART_PROTOCOL = 'protocol'; - // Address - const UNL_PART_ADDRESS = 'address'; - // Extra part - const UNL_PART_EXTRA = 'extra'; - // Port (if any) - const UNL_PART_PORT = 'port'; - /** * UNL data array */ diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index d8272757f..b5ee6eb7b 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -4,7 +4,7 @@ namespace Hub\Pool\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; -use Hub\Locator\Node\UniversalNodeLocator; +use Hub\Locator\Node\LocateableNode; use Hub\Network\Package\NetworkPackage; use Hub\Pool\BasePool; use Hub\Pool\Peer\PoolablePeer; @@ -227,12 +227,12 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $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])); + assert(isset($unlData[LocateableNode::UNL_PART_PROTOCOL])); + assert(isset($unlData[LocateableNode::UNL_PART_ADDRESS])); + assert(isset($unlData[LocateableNode::UNL_PART_PORT])); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),unlData[' . UniversalNodeLocator::UNL_PART_ADDRESS . ']=' . $unlData[UniversalNodeLocator::UNL_PART_ADDRESS] . ',unlData[' . UniversalNodeLocator::UNL_PART_PORT . ']=' . $unlData[UniversalNodeLocator::UNL_PART_PORT] . ' ...'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),unlData[' . LocateableNode::UNL_PART_ADDRESS . ']=' . $unlData[LocateableNode::UNL_PART_ADDRESS] . ',unlData[' . LocateableNode::UNL_PART_PORT . ']=' . $unlData[LocateableNode::UNL_PART_PORT] . ' ...'); // Default is all sockets $sockets = $this->getAllSockets(); @@ -267,7 +267,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Get // If the "peer" IP and recipient is same, use it - if ($peerAddress == $unlData[UniversalNodeLocator::UNL_PART_ADDRESS]) { + if ($peerAddress == $unlData[LocateableNode::UNL_PART_ADDRESS]) { // IPs match, so take the socket and quit this loop $socketInstance = $socketArray[self::SOCKET_ARRAY_INSTANCE]; diff --git a/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php b/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php index 4e4ba6b2c..79575b8b8 100644 --- a/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php +++ b/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php @@ -31,7 +31,7 @@ interface HandleableProtocol extends Handleable { /** * Getter for UNL instance * - * @return $unlData An instance of a LocateableNode class + * @return $unlInstance An instance of a LocateableNode class */ function getUniversalNodeLocatorInstance (); diff --git a/application/hub/interfaces/locator/class_LocateableNode.php b/application/hub/interfaces/locator/class_LocateableNode.php index c9c98454a..6ae4a6ad5 100644 --- a/application/hub/interfaces/locator/class_LocateableNode.php +++ b/application/hub/interfaces/locator/class_LocateableNode.php @@ -28,6 +28,21 @@ use Hub\Generic\HubInterface; * along with this program. If not, see . */ interface LocateableNode extends HubInterface { + //------- UNL parts ------- + // Protocol + const UNL_PART_PROTOCOL = 'protocol'; + // Address + const UNL_PART_ADDRESS = 'address'; + // Extra part + const UNL_PART_EXTRA = 'extra'; + // Port (if any) + const UNL_PART_PORT = 'port'; + + /** + * Regular expression for validating IP:port UNLs + */ + const UNL_REGEX = '/^([a-z]{1,}):\/\/\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b:(6553[0-5]|655[0-2][0-9]\d|65[0-4](\d){2}|6[0-4](\d){3}|[1-5](\d){4}|[1-9](\d){0,3})$/'; + /** * Getter for external UNL * diff --git a/application/hub/interfaces/resolver/class_ProtocolResolver.php b/application/hub/interfaces/resolver/class_ProtocolResolver.php index 624362f75..0bea55e04 100644 --- a/application/hub/interfaces/resolver/class_ProtocolResolver.php +++ b/application/hub/interfaces/resolver/class_ProtocolResolver.php @@ -46,7 +46,5 @@ interface ProtocolResolver extends HubInterface { * @return $unl Universal node locator */ function resolveUniversalNodeLocatorFromConfigKey ($configKey); -} -// [EOF] -?> +}