From 25ae7ed734ce07103f28c97600f5c594133bd201 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 29 May 2017 22:52:35 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20implemented=20translateLastSock?= =?utf8?q?etErrorCodeToName()=20-=20implemented=20getSocketRecipient()=20-=20no=20more=20socketResource=20here=20(and=20else?= =?utf8?q?where=20will=20follow)=20-=20updated=20core=20framework=20-=20mo?= =?utf8?q?re=20debug=20messages=20added=20-=20registering=20helper/listene?= =?utf8?q?r=20instances=20with=20socket=20instance=20must=20be=20done=20?= =?utf8?q?=20=20outaside=20constructor,=20much=20easier=20code=20-=20for?= =?utf8?q?=20this,=20registerInfoInstance()=20is=20now=20implemented=20Sig?= =?utf8?q?ned-off-by:=20Roland=20H=C3=A4der=20?= 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 | 2 +- .../container/class_BaseHubContainer.php | 253 ++++++++++++++++++ .../socket/class_SocketContainer.php | 145 +++++++--- .../frontend/class_BaseHubDatabaseWrapper.php | 25 +- .../class_PeerStateLookupDatabaseWrapper.php | 23 +- .../states/peer/class_PeerStateFactory.php | 9 +- .../classes/handler/class_BaseHubHandler.php | 25 ++ .../info/connection/class_ConnectionInfo.php | 4 +- .../listener/class_BaseListenerDecorator.php | 6 +- .../class_SocketFileListenerDecorator.php | 3 + .../class_ClientTcpListenerDecorator.php | 3 + .../class_HubTcpListenerDecorator.php | 3 + .../class_ClientUdpListenerDecorator.php | 3 + .../class_HubUdpListenerDecorator.php | 3 + .../pools/peer/class_DefaultPeerPool.php | 3 + .../registry/socket/class_SocketRegistry.php | 21 +- .../state/peer/class_PeerStateResolver.php | 2 +- .../hub/interfaces/class_HubInterface.php | 7 + .../container/socket/class_StorableSocket.php | 36 +++ .../interfaces/helper/hub/class_HubHelper.php | 5 +- .../peer_states/class_LookupablePeerState.php | 2 +- core | 2 +- 22 files changed, 517 insertions(+), 68 deletions(-) create mode 100644 application/hub/classes/container/class_BaseHubContainer.php diff --git a/application/hub/classes/class_BaseHubSystem.php b/application/hub/classes/class_BaseHubSystem.php index dff2ac434..10c9710ec 100644 --- a/application/hub/classes/class_BaseHubSystem.php +++ b/application/hub/classes/class_BaseHubSystem.php @@ -361,7 +361,7 @@ class BaseHubSystem extends BaseFrameworkSystem implements HubInterface { * * @return $listenerInstance A Listenable instance */ - protected final function getListenerInstance () { + public final function getListenerInstance () { return $this->listenerInstance; } diff --git a/application/hub/classes/container/class_BaseHubContainer.php b/application/hub/classes/container/class_BaseHubContainer.php new file mode 100644 index 000000000..85489ecd2 --- /dev/null +++ b/application/hub/classes/container/class_BaseHubContainer.php @@ -0,0 +1,253 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2011 - 2014 Cruncher 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 BaseHubContainer extends BaseContainer implements HubInterface { + /** + * Listener instance + */ + private $listenerInstance = NULL; + + /** + * Listener pool instance + */ + private $listenerPoolInstance = NULL; + + /** + * Info instance + */ + private $infoInstance = NULL; + + /** + * A StorableSocket instance + */ + private $socketInstance = NULL; + + /** + * An instance of a LocateableNode class + */ + private $universalNodeLocatorInstance = NULL; + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } + + /** + * Checks whether start/end marker are set + * + * @param $data Data to be checked + * @return $isset Whether start/end marker are set + */ + public final function ifStartEndMarkersSet ($data) { + // Determine it + $isset = ((substr($data, 0, strlen(BaseRawDataHandler::STREAM_START_MARKER)) == BaseRawDataHandler::STREAM_START_MARKER) && (substr($data, -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER), strlen(BaseRawDataHandler::STREAM_END_MARKER)) == BaseRawDataHandler::STREAM_END_MARKER)); + + // ... and return it + return $isset; + } + + /** + * Setter for listener pool instance + * + * @param $listenerPoolInstance The new listener pool instance + * @return void + */ + protected final function setListenerPoolInstance (Poolable $listenerPoolInstance) { + $this->listenerPoolInstance = $listenerPoolInstance; + } + + /** + * Getter for listener pool instance + * + * @return $listenerPoolInstance Our current listener pool instance + */ + public final function getListenerPoolInstance () { + return $this->listenerPoolInstance; + } + + /** + * Setter for info instance + * + * @param $infoInstance A ShareableInfo instance + * @return void + */ + protected final function setInfoInstance (ShareableInfo $infoInstance) { + $this->infoInstance = $infoInstance; + } + + /** + * Getter for info instance + * + * @return $infoInstance An instance of a ShareableInfo class + */ + public final function getInfoInstance () { + return $this->infoInstance; + } + + /** + * Setter for socket instance + * + * @param $socketInstance A StorableSocket instance + * @return void + */ + public final function setSocketInstance (StorableSocket $socketInstance) { + $this->socketInstance = $socketInstance; + } + + /** + * Getter for socket instance + * + * @return $socketInstance An instance of a StorableSocket class + */ + public final function getSocketInstance () { + return $this->socketInstance; + } + + /** + * Setter for UNL instance + * + * @para $unlInstance An instance of a LocateableNode class + * @return void + */ + protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + // Set new UNL data array + $this->universalNodeLocatorInstance = $unlInstance; + } + + /** + * Getter for UNL instance + * + * @return $unlData An instance of a LocateableNode class + */ + public final function getUniversalNodeLocatorInstance () { + // Return UNL data array + return $this->universalNodeLocatorInstance; + } + + /** + * Setter for node id + * + * @param $nodeId The new node id + * @return void + */ + protected final function setNodeId ($nodeId) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for node id + * + * @return $nodeId Current node id + */ + public final function getNodeId () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Setter for private key + * + * @param $privateKey The new private key + * @return void + */ + protected final function setPrivateKey ($privateKey) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for private key + * + * @return $privateKey Current private key + */ + public final function getPrivateKey () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Setter for private key hash + * + * @param $privateKeyHash The new private key hash + * @return void + */ + protected final function setPrivateKeyHash ($privateKeyHash) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for private key hash + * + * @return $privateKeyHash Current private key hash + */ + public final function getPrivateKeyHash () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for session id + * + * @return $sessionId Current session id + */ + public final function getSessionId () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Setter for listener instance + * + * @param $listenerInstance A Listenable instance + * @return void + */ + protected final function setListenerInstance (Listenable $listenerInstance) { + $this->listenerInstance = $listenerInstance; + } + + /** + * Getter for listener instance + * + * @return $listenerInstance A Listenable instance + */ + public final function getListenerInstance () { + return $this->listenerInstance; + } + +} diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 52ef686dc..7a2d594d3 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -3,17 +3,18 @@ namespace Hub\Container\Socket; // Import application-specific stuff +use Hub\Container\BaseHubContainer; use Hub\Handler\Network\RawData\BaseRawDataHandler; use Hub\Factory\Network\Locator\UniversalNodeLocatorFactory; use Hub\Factory\Socket\SocketFactory; use Hub\Helper\Connection\BaseConnectionHelper; +use Hub\Helper\Connection\ConnectionHelper; use Hub\Information\ShareableInfo; use Hub\Listener\BaseListener; use Hub\Listener\Listenable; use Hub\Network\Package\NetworkPackage; // Import framework stuff -use CoreFramework\Container\BaseContainer; use CoreFramework\Factory\ObjectFactory; use CoreFramework\Registry\Registerable; use CoreFramework\Socket\InvalidSocketException; @@ -51,7 +52,7 @@ use \LogicException; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class SocketContainer extends BaseContainer implements StorableSocket, Visitable, Registerable { +class SocketContainer extends BaseHubContainer implements StorableSocket, Visitable, Registerable { /** * Socket protocol: * - 'tcp' for TCP/IPv4 connections @@ -95,12 +96,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * @param $socketResource A valid socket resource * @param $socketProtocol Socket protocol * @param $packageData Raw package data - * @param $infoInstance An instance of a ShareableInfo class * @return $socketInstance An instance of this Container class */ - public static final function createSocketContainer ($socketResource, $socketProtocol, array $packageData, ShareableInfo $infoInstance = NULL) { + public static final function createSocketContainer ($socketResource, $socketProtocol, array $packageData) { // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET: socketResource=%s,socketProtocol=%s,packageData()=%d,infoInstance[]=%s - CALLED!', $socketResource, $socketProtocol, count($packageData), gettype($infoInstance))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET: socketResource=%s,socketProtocol=%s,packageData()=%d - CALLED!', $socketResource, $socketProtocol, count($packageData))); /* DEBUG-PRINT: */ printf('[%s:%d]: packageData=%s', __METHOD__, __LINE__, print_r($packageData, TRUE)); // Get a new instance @@ -113,31 +113,6 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]); unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]); - // Is the info instance set? - if ($infoInstance instanceof ShareableInfo) { - // Get listener/helper from info class - $listenerInstance = $infoInstance->getListenerInstance(); - $helperInstance = $infoInstance->getHelperInstance(); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: listenerInstance[]=' . gettype($listenerInstance)); - - // Is there a listener instance set? - if ($listenerInstance instanceof Listenable) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...'); - - // Set it here for later usage - $socketInstance->setListenerInstance($listenerInstance); - } elseif ($helperInstance instanceof ConnectionHelper) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting helperInstance=' . $helperInstance->__toString() . ' ...'); - - // Set it here for later usage - $socketInstance->setHelperInstance($helperInstance); - } - } // END - if - // Set the resource ... $socketInstance->setSocketResource($socketResource); @@ -298,8 +273,15 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * * @return $recipient Recipient array element * @throws LogicException If 'recipient' array element is not found + * @throws InvalidSocketException If socket is invalid */ public function getSocketRecipient () { + // Should be valid socket + if (!$this->isValidSocket()) { + // Throw exception + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + } // END - if + // Get package data $packageData = $this->getPackageData(); @@ -313,6 +295,59 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable return $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]; } + /** + * Getter for socket recipient's address part + * + * @return $recipientAddress Recipient's address part + * @throws InvalidSocketException If socket is invalid + */ + public function getSocketRecipientAddress () { + // Should be valid socket + if (!$this->isValidSocket()) { + // Throw exception + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + } // END - if + + // Get recipient + $recipient = $this->getSocketRecipient(); + + // Generate UNL instance for it + $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient); + + // Get address part + $recipientAddress = $unlInstance->getUnlAddress(); + + // Return it + return $recipientAddress; + } + + /** + * Getter for socket recipient's port part + * + * @return $recipientPort Recipient's port part + * @throws InvalidSocketException If socket is invalid + */ + public function getSocketRecipientPort () { + // Should be valid socket + if (!$this->isValidSocket()) { + // Throw exception + throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); + } // END - if + + // Get recipient + $recipient = $this->getSocketRecipient(); + + // Generate UNL instance for it + $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient); + + // Get port part + $recipientPort = $unlInstance->getUnlPort(); + + // Return it + return $recipientPort; + + } + /** * Validates stored stocket * @@ -376,6 +411,22 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable return $errorMessage; } + /** + * Translates last socket error code into a human-readable form + * + * @return $codeName Code name to used e.g. with some_$codeName_socket_error_class or so + */ + public function translateLastSocketErrorCodeToName () { + // Get last error code + $errorCode = $this->getLastSocketErrorCode(); + + // Call "translate" method + $codeName = $this->translateSocketErrorCodeToName($errorCode); + + // Return it + return $codeName; + } + /** * Tries to bind the socket. * @@ -702,6 +753,40 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!'); } + /** + * Registers whether helper or listener instance found in given info + * instance with this socket container. + * + * @param $infoInstance An instance of a ShareableInfo class + * @return void + */ + public function registerInfoInstance (ShareableInfo $infoInstance) { + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling infoInstance->getListenerInstance() ...', strtoupper($this->getSocketProtocol()))); + + // Get listener/helper from info class + $listenerInstance = $infoInstance->getListenerInstance(); + $helperInstance = $infoInstance->getHelperInstance(); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance)); + + // Is there a listener instance set? + if ($listenerInstance instanceof Listenable) { + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...'); + + // Set it here for later usage + $this->setListenerInstance($listenerInstance); + } elseif ($helperInstance instanceof ConnectionHelper) { + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting helperInstance=' . $helperInstance->__toString() . ' ...'); + + // Set it here for later usage + $this->setHelperInstance($helperInstance); + } + } + /** * Handles socket error for given socket resource and peer data. This method * validates socket resource stored in given container if it is a valid diff --git a/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php b/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php index de83717bf..0afec33b0 100644 --- a/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php @@ -14,7 +14,7 @@ use Hub\Pool\Poolable; use CoreFramework\Database\Frontend\BaseDatabaseWrapper; /** - * A database wrapper for cruncher work/test units + * A general hub database wrapper * * @author Roland Haeder * @version 0.0.0 @@ -36,6 +36,10 @@ use CoreFramework\Database\Frontend\BaseDatabaseWrapper; * along with this program. If not, see . */ class BaseHubDatabaseWrapper extends BaseDatabaseWrapper implements HubInterface { + /** + * Listener instance + */ + private $listenerInstance = NULL; /** * Listener pool instance @@ -226,4 +230,23 @@ class BaseHubDatabaseWrapper extends BaseDatabaseWrapper implements HubInterface throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } + /** + * Setter for listener instance + * + * @param $listenerInstance A Listenable instance + * @return void + */ + protected final function setListenerInstance (Listenable $listenerInstance) { + $this->listenerInstance = $listenerInstance; + } + + /** + * Getter for listener instance + * + * @return $listenerInstance A Listenable instance + */ + public final function getListenerInstance () { + return $this->listenerInstance; + } + } diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php index 1f305f780..00db969f7 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php @@ -166,9 +166,9 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L $peerPort = '0'; // Get peer name - if (!@socket_getpeername($socketInstance, $peerAddress, $peerPort)) { + if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) { // Get last error - $lastError = socket_last_error($socketInstance); + $lastError = $socketInstance->getLastSocketErrorCode(); // ... and cleartext message from it and put both into criteria $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $lastError); @@ -234,21 +234,10 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L * * @param $socketInstance An instance of a StorableSocket class * @return void - * @throws InvalidSocketException If the socket resource was invalid * @todo Unfinished area, please rewrite! */ - public function purgeOldEntriesBysocketInstance (StorableSocket $socketInstance) { - // Get peer name - if (!@socket_getpeername($socketInstance, $peerAddress, $peerPort)) { - // Get last error - $lastError = socket_last_error($socketInstance); - - // Doesn't work! - throw new InvalidSocketException(array($this, $socketInstance, $lastError, socket_strerror($lastError)), self::EXCEPTION_INVALID_SOCKET); - } // END - if - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: peerAddress=' . $peerAddress . ',peerPort=' . $peerPort . ' - UNFINISHED!'); + public function purgeOldEntriesBySocketInstance (StorableSocket $socketInstance) { + $this->partialStub('Please finish this method.'); } /** @@ -290,7 +279,5 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L // Return it return $isSamePeerState; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php index 6e414948f..746ccf24b 100644 --- a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php +++ b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php @@ -3,6 +3,7 @@ namespace Hub\Factory\State\Peer; // Import application-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Helper\Connection\ConnectionHelper; // Import framework stuff @@ -71,11 +72,11 @@ class PeerStateFactory extends ObjectFactory { * * @param $helperInstance An instance of a ConnectionHelper class * @param $packageData Raw package data - * @param $socketResource A valid socket resource + * @param $socketInstance A valid socket resource * @param $errorCode The last error code * @return $stateInstance A Stateable class instance */ - public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, $socketResource, $errorCode) { + public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, StorableSocket $socketInstance, $errorCode) { // Init state instance, this is better coding practice $stateInstance = NULL; @@ -88,7 +89,7 @@ class PeerStateFactory extends ObjectFactory { */ try { // Purge old entries - $tableInstance->purgeOldEntriesBySocketResource($socketResource); + $tableInstance->purgeOldEntriesBySocketInstance($socketInstance); } catch (InvalidSocketException $e) { // Just log all errors //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Purging of old entries failed. Message from exception: ' . $e->getMessage()); @@ -100,7 +101,7 @@ class PeerStateFactory extends ObjectFactory { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] errorCode=' . $errorCode); // Register the new peer with its session id - $tableInstance->registerPeerByPackageData($packageData, $socketResource); + $tableInstance->registerPeerByPackageData($packageData, $socketInstance); /* * It is a new peer so create the state instance based on error diff --git a/application/hub/classes/handler/class_BaseHubHandler.php b/application/hub/classes/handler/class_BaseHubHandler.php index a0d486109..675eeb085 100644 --- a/application/hub/classes/handler/class_BaseHubHandler.php +++ b/application/hub/classes/handler/class_BaseHubHandler.php @@ -7,6 +7,7 @@ use Hub\Container\Socket\StorableSocket; use Hub\Handler\Network\RawData\BaseRawDataHandler; use Hub\Generic\HubInterface; use Hub\Information\ShareableInfo; +use Hub\Listener\Listenable; use Hub\Locator\Node\LocateableNode; use Hub\Pool\Poolable; @@ -37,6 +38,11 @@ use CoreFramework\Handler\Handleable; * along with this program. If not, see . */ class BaseHubHandler extends BaseHandler implements Handleable, HubInterface { + /** + * Listener instance + */ + private $listenerInstance = NULL; + /** * Listener pool instance */ @@ -226,4 +232,23 @@ class BaseHubHandler extends BaseHandler implements Handleable, HubInterface { throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } + /** + * Setter for listener instance + * + * @param $listenerInstance A Listenable instance + * @return void + */ + protected final function setListenerInstance (Listenable $listenerInstance) { + $this->listenerInstance = $listenerInstance; + } + + /** + * Getter for listener instance + * + * @return $listenerInstance A Listenable instance + */ + public function getListenerInstance () { + return $this->listenerInstance; + } + } diff --git a/application/hub/classes/info/connection/class_ConnectionInfo.php b/application/hub/classes/info/connection/class_ConnectionInfo.php index 1d6c48b10..50319ec8f 100644 --- a/application/hub/classes/info/connection/class_ConnectionInfo.php +++ b/application/hub/classes/info/connection/class_ConnectionInfo.php @@ -92,8 +92,8 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { // Fill the generic array with several data from the listener: $this->setProtocolName($helperInstance->getSocketInstance()->getSocketProtocol()); - $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getSocketInstance()->getRecipientAddress()); - $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $helperInstance->getSocketInstance()->getRecipientPort()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getSocketInstance()->getSocketRecipientAddress()); + $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $helperInstance->getSocketInstance()->getSocketRecipientPort()); // Set helper here $this->setHelperInstance($helperInstance); diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index 46fecd8ce..b94ee0180 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -70,6 +70,8 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable { * @return $listenAddress The address this listener should listen on */ public final function getListenAddress () { + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('LISTENER-DECORATOR: Calling this->getListenerInstance()->getListenAddress() ...'); return $this->getListenerInstance()->getListenAddress(); } @@ -79,6 +81,8 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable { * @return $listenPort The port this listener should listen on */ public final function getListenPort () { + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('LISTENER-DECORATOR: Calling this->getListenerInstance()->getListenPort() ...'); return $this->getListenerInstance()->getListenPort(); } @@ -263,7 +267,7 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable { * * @return $listenerInstance A Listenable instance */ - protected final function getListenerInstance () { + public final function getListenerInstance () { return $this->listenerInstance; } diff --git a/application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php b/application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php index 6d54616a7..a56d29c14 100644 --- a/application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php +++ b/application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php @@ -78,6 +78,9 @@ class SocketFileListenerDecorator extends BaseListenerDecorator implements Liste * @return void */ public function doListen () { + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Calling this->getListenerInstance() ...'); + // Handle generic TCP package $this->getListenerInstance()->doListen(); diff --git a/application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php b/application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php index a38d4d2fd..82d61a4fc 100644 --- a/application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php +++ b/application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php @@ -78,6 +78,9 @@ class ClientTcpListenerDecorator extends BaseListenerDecorator implements Listen * @return void */ public function doListen () { + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CLIENT-TCP-LISTENER: Calling this->getListenerInstance() ...'); + // Handle generic TCP package $this->getListenerInstance()->doListen(); diff --git a/application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php b/application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php index f5f2c08b0..748a5cf8e 100644 --- a/application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php +++ b/application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php @@ -78,6 +78,9 @@ class HubTcpListenerDecorator extends BaseListenerDecorator implements Listenabl * @return void */ public function doListen () { + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TCP-LISTENER: Calling this->getListenerInstance() ...'); + // Handle generic TCP package $this->getListenerInstance()->doListen(); diff --git a/application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php b/application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php index 15008105e..f43a3b96c 100644 --- a/application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php +++ b/application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php @@ -78,6 +78,9 @@ class ClientUdpListenerDecorator extends BaseListenerDecorator implements Listen * @return void */ public function doListen () { + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CLIENT-UDP-LISTENER: Calling this->getListenerInstance() ...'); + // Handle generic UDP packages first $this->getListenerInstance()->doListen(); diff --git a/application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php b/application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php index 15cd60583..a47f0e02e 100644 --- a/application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php +++ b/application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php @@ -78,6 +78,9 @@ class HubUdpListenerDecorator extends BaseListenerDecorator implements Listenabl * @return void */ public function doListen () { + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-UDP-LISTENER: Calling this->getListenerInstance() ...'); + // Handle generic UDP package first $this->getListenerInstance()->doListen(); diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index e646b7138..5312daa98 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -121,6 +121,9 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $peerAddress = '0.0.0.0'; $peerPort = '0'; + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Calling this->getListenerInstance() ...'); + // The socket resource should not match server socket if (!$this->getListenerInstance()->getSocketInstance()->equals($socketInstance)) { // Try to determine the peer's IP number diff --git a/application/hub/classes/registry/socket/class_SocketRegistry.php b/application/hub/classes/registry/socket/class_SocketRegistry.php index d191433c7..3c526d863 100644 --- a/application/hub/classes/registry/socket/class_SocketRegistry.php +++ b/application/hub/classes/registry/socket/class_SocketRegistry.php @@ -5,6 +5,7 @@ namespace Hub\Registry\Socket; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; use Hub\Factory\Information\Connection\ConnectionInfoFactory; +use Hub\Helper\Connection\ConnectionHelper; use Hub\Information\ShareableInfo; use Hub\Listener\BaseListener; use Hub\Listener\Listenable; @@ -242,6 +243,9 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke // We have a sub-registry, the socket key and the socket, now we need to put all together //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: socketKey=' . $socketKey . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - adding socket container instance ...'); $registryInstance->addInstance($socketKey, $socketInstance); + + // Also register all instances from info instance in socket + $socketInstance->registerInfoInstance($infoInstance); } /** @@ -297,18 +301,21 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',registryInstance=' . $registryInstance->__toString()); // This is always a SubRegistry instance - foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) { + foreach ($registryInstance->getInstanceRegistry() as $subKey => $socketInstance) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',socketInstance=' . $socketInstance->__toString()); // Is this a StorableSocket instance and is the address the same? - if (($containerInstance instanceof StorableSocket) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) { + if (($socketInstance instanceof StorableSocket) && ($socketInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) { // Debug die - //* DEBUG-DIE: */ die(__METHOD__ . ': containerInstance=' . print_r($containerInstance, TRUE)); + //* DEBUG-DIE: */ die(__METHOD__ . ': socketInstance=' . print_r($socketInstance, TRUE)); + + // Trace message + /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: Calling socketInstance->getListenerInstance() ...'); // Get listener and helper instances - $listenerInstance = $containerInstance->getListenerInstance(); - $helperInstance = $containerInstance->getHelperInstance(); + $listenerInstance = $socketInstance->getListenerInstance(); + $helperInstance = $socketInstance->getHelperInstance(); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance)); @@ -328,7 +335,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke $infoInstance->fillWithConnectionHelperInformation($helperInstance); } else { // Not supported state! - $this->debugInstance(sprintf('[%s:%d]: Invalid state found, please report this to the developers with full debug infos.', __METHOD__, __LINE__)); + $this->debugInstance(sprintf('[%s:%d]: Invalid script-state found, please report this to the developers with full debug infos. socketInstance=%s', __METHOD__, __LINE__, print_r($socketInstance, TRUE))); } // Debug message diff --git a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php index 8fec88c5c..97147445e 100644 --- a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php +++ b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php @@ -92,7 +92,7 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver { } // END - if // Translate the error code to an own name - $errorName = $socketInstance->translateSocketLastErrorCodeToName(); + $errorName = $socketInstance->translateLastSocketErrorCodeToName(); // Create a state instance based on $errorCode. This factory does the hard work for us $stateInstance = PeerStateFactory::createPeerStateInstanceBySocketStatusCode($helperInstance, $packageData, $socketInstance, $errorName); diff --git a/application/hub/interfaces/class_HubInterface.php b/application/hub/interfaces/class_HubInterface.php index 2abd9c85c..1e08ea121 100644 --- a/application/hub/interfaces/class_HubInterface.php +++ b/application/hub/interfaces/class_HubInterface.php @@ -105,4 +105,11 @@ interface HubInterface extends FrameworkInterface { */ function getUniversalNodeLocatorInstance (); + /** + * Getter for listener instance + * + * @return $listenerInstance A Listenable instance + */ + function getListenerInstance (); + } diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index b217b0799..68348ef4b 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -2,6 +2,9 @@ // Own namespace namespace Hub\Container\Socket; +// Import application-related stuff +use Hub\Information\ShareableInfo; + // Inport frameworks stuff use CoreFramework\Generic\FrameworkInterface; @@ -165,9 +168,26 @@ interface StorableSocket extends FrameworkInterface { * * @return $recipient Recipient array element * @throws LogicException If 'recipient' array element is not found + * @throws InvalidSocketException If stored socket is invalid */ function getSocketRecipient (); + /** + * Getter for socket recipient's address part + * + * @return $recipientAddress Recipient's address part + * @throws InvalidSocketException If stored socket is invalid + */ + function getSocketRecipientAddress (); + + /** + * Getter for socket recipient's port part + * + * @return $recipientPort Recipient's port part + * @throws InvalidSocketException If stored socket is invalid + */ + function getSocketRecipientPort (); + /** * Checks whether the given Universal Node Locator matches with the one from package data * @@ -223,6 +243,13 @@ interface StorableSocket extends FrameworkInterface { */ function getLastSocketErrorMessage (); + /** + * Translates last socket error code into a human-readable form + * + * @return $codeName Code name to used e.g. with some_$codeName_socket_error_class or so + */ + function translateLastSocketErrorCodeToName (); + /** * Handles socket error for given socket resource and peer data. This method * validates socket resource stored in given container if it is a valid @@ -264,4 +291,13 @@ interface StorableSocket extends FrameworkInterface { */ function clearLastSocketError (); + /** + * Registers whether helper or listener instance found in given info + * instance with this socket container. + * + * @param $infoInstance An instance of a ShareableInfo class + * @return void + */ + function registerInfoInstance (ShareableInfo $infoInstance); + } diff --git a/application/hub/interfaces/helper/hub/class_HubHelper.php b/application/hub/interfaces/helper/hub/class_HubHelper.php index f036ed444..ae2f8608c 100644 --- a/application/hub/interfaces/helper/hub/class_HubHelper.php +++ b/application/hub/interfaces/helper/hub/class_HubHelper.php @@ -2,6 +2,9 @@ // Own namespace namespace Hub\Helper; +// Import application-specific stuff +use Hub\Generic\HubInterface; + // Import framework stuff use CoreFramework\Helper\Helper; @@ -27,7 +30,7 @@ use CoreFramework\Helper\Helper; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface HubHelper extends Helper { +interface HubHelper extends Helper, HubInterface { /** * Tries to determine the used protocol for this package (this helper is helping to send out) * diff --git a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php index 01b8a3bcc..62a644470 100644 --- a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php +++ b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php @@ -66,7 +66,7 @@ interface LookupablePeerState extends Lookupable { * @param $socketInstance An instance of a StorableSocket class * @return void */ - function purgeOldEntriesBysocketInstance (StorableSocket $socketInstance); + function purgeOldEntriesBySocketInstance (StorableSocket $socketInstance); /** * Checks whether a given peer state (in helper instance) is same as stored diff --git a/core b/core index e9e524279..dc46df7fb 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit e9e5242797adec674d25da4e641965f8d6dcbecd +Subproject commit dc46df7fb4a0358cb7554c33b6f08d9ff8418c66 -- 2.39.5