From: Roland Häder Date: Wed, 2 Dec 2020 11:41:08 +0000 (+0100) Subject: Refacturing: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=862bbf866600a73270fbe99bfd41d08e3a86f106;p=hub.git Refacturing: - introduced traits for $node and $listener instances Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/container/class_BaseHubContainer.php b/application/hub/classes/container/class_BaseHubContainer.php index 1a3a29582..19e9b181b 100644 --- a/application/hub/classes/container/class_BaseHubContainer.php +++ b/application/hub/classes/container/class_BaseHubContainer.php @@ -4,8 +4,8 @@ namespace Org\Shipsimu\Hub\Container; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; -use Org\Shipsimu\Hub\Listener\Listenable; use Org\Shipsimu\Hub\Pool\Poolable; +use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait; // Import framework stuff use Org\Mxchange\CoreFramework\Container\BaseContainer; @@ -33,10 +33,8 @@ use Org\Mxchange\CoreFramework\Container\BaseContainer; * along with this program. If not, see . */ abstract class BaseHubContainer extends BaseContainer implements HubInterface { - /** - * Listener instance - */ - private $listenerInstance = NULL; + // Load traits + use ListenableTrait; /** * Listener pool instance @@ -74,23 +72,4 @@ abstract class BaseHubContainer extends BaseContainer implements HubInterface { return $this->listenerPoolInstance; } - /** - * Setter for listener instance - * - * @param $listenerInstance A Listenable instance - * @return void - */ - public 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/class_BaseHubDatabaseFrontend.php b/application/hub/classes/database/frontend/class_BaseHubDatabaseFrontend.php index 3b678b9b3..c9b90c592 100644 --- a/application/hub/classes/database/frontend/class_BaseHubDatabaseFrontend.php +++ b/application/hub/classes/database/frontend/class_BaseHubDatabaseFrontend.php @@ -4,8 +4,9 @@ namespace Org\Shipsimu\Hub\Database\Frontend; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; -use Org\Shipsimu\Hub\Node\Node; use Org\Shipsimu\Hub\Pool\Poolable; +use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait; +use Org\Shipsimu\Hub\Traits\Node\NodeTrait; // Import framework stuff use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend; @@ -34,21 +35,15 @@ use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException; * along with this program. If not, see . */ abstract class BaseHubDatabaseFrontend extends BaseDatabaseFrontend implements HubInterface { - /** - * Listener instance - */ - private $listenerInstance = NULL; + // Load traits + use ListenableTrait; + use NodeTrait; /** * Listener pool instance */ private $listenerPoolInstance = NULL; - /** - * Node instance - */ - private $nodeInstance = NULL; - /** * Protected constructor * @@ -79,42 +74,4 @@ abstract class BaseHubDatabaseFrontend extends BaseDatabaseFrontend implements H return $this->listenerPoolInstance; } - /** - * Setter for listener instance - * - * @param $listenerInstance A Listenable instance - * @return void - */ - public final function setListenerInstance (Listenable $listenerInstance) { - $this->listenerInstance = $listenerInstance; - } - - /** - * Getter for listener instance - * - * @return $listenerInstance A Listenable instance - */ - public final function getListenerInstance () { - return $this->listenerInstance; - } - - /** - * Setter for node instance - * - * @param $nodeInstance A Node instance - * @return void - */ - public final function setNodeInstance (Node $nodeInstance) { - $this->nodeInstance = $nodeInstance; - } - - /** - * Getter for node instance - * - * @return $nodeInstance A Node instance - */ - public function getNodeInstance () { - return $this->nodeInstance; - } - } diff --git a/application/hub/classes/database/frontend/node_dht/class_NodeDistributedHashTableDatabaseFrontend.php b/application/hub/classes/database/frontend/node_dht/class_NodeDistributedHashTableDatabaseFrontend.php index b4c63f919..2e81da98f 100644 --- a/application/hub/classes/database/frontend/node_dht/class_NodeDistributedHashTableDatabaseFrontend.php +++ b/application/hub/classes/database/frontend/node_dht/class_NodeDistributedHashTableDatabaseFrontend.php @@ -312,7 +312,7 @@ class NodeDistributedHashTableDatabaseFrontend extends BaseHubDatabaseFrontend i * @return $resultInstance An instance of a SearchableResult class * @throws InvalidArgumentException If parameter $sessionId is not valid */ - public function findNodeLocalBySessionId ($sessionId) { + public function findNodeLocalBySessionId (string $sessionId) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: sessionId=%s - CALLED!', $sessionId)); if (empty($sessionId)) { diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php index bd4b101a5..8e8e4f447 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php @@ -17,6 +17,9 @@ use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria; use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; +// Import SPL stuff +use \UnexpectedValueException; + /** * A database frontend for peer state lookups * @@ -238,6 +241,7 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements * @param $helperInstance An instance of a ConnectionHelper class * @param $packageInstance An instance of a DeliverablePackage class * @return $isSamePeerState Whether the peer's state is the same + * @throws UnexpectedValueException If $resultInstance->next() returns FALSE */ public function isSamePeerState (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance) { // Debug message @@ -250,7 +254,10 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements $resultInstance = $this->doSelectByCriteria($searchInstance); // Do we have an entry? This should always the case - assert($resultInstance->next()); + if (!$resultInstance->next()) { + // No next record! + throw new UnexpectedValueException(sprintf('resultInstance=%s,count()=%d has no next entry', $resultInstance->__toString(), $resultInstance->count())); + } // Yes, so get the current (=first) entry from it $rowData = $resultInstance->current(); diff --git a/application/hub/classes/dht/class_BaseDht.php b/application/hub/classes/dht/class_BaseDht.php index ffe9e0d3e..9a6345995 100644 --- a/application/hub/classes/dht/class_BaseDht.php +++ b/application/hub/classes/dht/class_BaseDht.php @@ -15,6 +15,10 @@ use Org\Mxchange\CoreFramework\Result\Search\SearchableResult; use Org\Mxchange\CoreFramework\Traits\Stacker\StackableTrait; use Org\Mxchange\CoreFramework\Traits\State\StateableTrait; +// Import SPL stuff +use \BadMethodCallException; +use \UnexpectedValueException; + /** * A general DHT class * @@ -159,17 +163,24 @@ abstract class BaseDht extends BaseHubSystem implements Distributable { * them by uploading to other (recently appeared) DHT members. * * @return void + * @throws UnexpectedValueException If $resultInstance is not valid */ public function initEntryPublication () { // Call method on database frontend + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!'); $this->getFrontendInstance()->initEntryPublication(); // Get result instance $resultInstance = $this->getFrontendInstance()->getUnpublishedEntriesInstance(); // Make sure the result instance is valid - assert($resultInstance instanceof SearchableResult); - assert($resultInstance->valid()); + if (!($resultInstance instanceof SearchableResult)) { + // Not valid + throw new UnexpectedValueException(sprintf('resultInstance[]=%s does not implement SearchableResult', gettype($resultInstance))); + } elseif (!$resultInstance->valid()) { + // Not valid + throw new UnexpectedValueException(sprintf('resultInstance=%s is not valid', $resultInstance->__toString())); + } // "Walk" through all entries while ($resultInstance->next()) { @@ -181,9 +192,12 @@ abstract class BaseDht extends BaseHubSystem implements Distributable { assert(is_array($current)); // ... and push it to the next stack - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: Pushing entry with ' . count($current) . ' elements to stack ' . self::STACKER_NAME_PENDING_PUBLISHING . ' ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: Pushing entry with ' . count($current) . ' elements to stack ' . self::STACKER_NAME_PENDING_PUBLISHING . ' ...'); $this->getStackInstance()->pushNamed(self::STACKER_NAME_PENDING_PUBLISHING, $current); - } // END - while + } + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: EXIT!'); } /** @@ -193,10 +207,11 @@ abstract class BaseDht extends BaseHubSystem implements Distributable { */ public function hasEntriesPendingPublication () { // Determine it if it is not empty + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!'); $isPending = ($this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PENDING_PUBLISHING) === FALSE); // Return status - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: isPending=' . intval($isPending)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DHT: isPending=%d - EXIT!', intval($isPending))); return $isPending; } @@ -207,10 +222,11 @@ abstract class BaseDht extends BaseHubSystem implements Distributable { */ public function ifDhtIsBooting () { // Call state instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!'); $isBooting = $this->getStateInstance()->ifDhtIsBooting(); // Return status - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: isBooting=' . intval($isBooting)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DHT: isBooting=%d - EXIT!', intval($isBooting))); return $isBooting; } @@ -220,10 +236,15 @@ abstract class BaseDht extends BaseHubSystem implements Distributable { * * @return void * @todo Find out if loadDescriptorXml() can be called only once to avoid a lot methods working. + * @throws BadMethodCallException If this method was called but no pending entries publication */ public function publishEntry () { // This test must not fail - assert($this->hasEntriesPendingPublication()); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!'); + if (!$this->hasEntriesPendingPublication()) { + // Bad invocation + throw new BadMethodCallException('Has no entries pending publication but method was called.'); + } // Is there an instance? if (!$this->publishHelperInstance instanceof HelpableDht) { @@ -241,9 +262,9 @@ abstract class BaseDht extends BaseHubSystem implements Distributable { assert(is_array($entry)); // Remove any non-public data the database layer desires - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: Calling this->getFrontendInstance()->removeNonPublicDataFromArray(data) ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: Calling this->getFrontendInstance()->removeNonPublicDataFromArray(data) ...'); $entry = $this->getFrontendInstance()->removeNonPublicDataFromArray($entry); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: entry[]=' . gettype($entry)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: entry[]=' . gettype($entry)); // Some sanity-checks again assert(is_array($entry)); @@ -252,7 +273,7 @@ abstract class BaseDht extends BaseHubSystem implements Distributable { $this->publishHelperInstance->getTemplateInstance()->assignMultipleVariables($entry); // "Publish" the descriptor by sending it to the bootstrap/list nodes - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: Calling this->publishHelperInstance->sendPackage(' . $this->__toString() . ') ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: Calling this->publishHelperInstance->sendPackage(' . $this->__toString() . ') ...'); $this->publishHelperInstance->sendPackage($this); } @@ -264,6 +285,7 @@ abstract class BaseDht extends BaseHubSystem implements Distributable { */ public function hasFullyBootstrapped () { // Get state and check it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!'); $this->partialStub('Please implement this method.'); } diff --git a/application/hub/classes/dht/node/class_NodeDhtFacade.php b/application/hub/classes/dht/node/class_NodeDhtFacade.php index 2d43958cf..918b68bad 100644 --- a/application/hub/classes/dht/node/class_NodeDhtFacade.php +++ b/application/hub/classes/dht/node/class_NodeDhtFacade.php @@ -170,7 +170,7 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { * @param $sessionId Session id to lookup * @return $nodeData Node-data array */ - public function findNodeLocalBySessionId ($sessionId) { + public function findNodeLocalBySessionId (string $sessionId) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: sessionId=%s - CALLED!', $sessionId)); if (empty($sessionId)) { @@ -253,7 +253,7 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { * @return void * @throws NodeSessionIdVerficationException If the node was not found and update is forced */ - public function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, $forceUpdate = FALSE) { + public function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, bool $forceUpdate = FALSE) { // Get a search criteria class /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: state=%s,messageInstance=%s,handlerInstance=%s,forceUpdate=%d - CALLED', $this->getPrintableState(), $messageInstance->__toString(), $handlerInstance->__toString(), intval($forceUpdate))); $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); @@ -309,7 +309,7 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { * @param $separator Sepator char (1st parameter for explode() call) * @return $nodeList An array with all found nodes */ - public function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, $excludeKey, $andKey, $separator) { + public function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, string $excludeKey, string $andKey, string $separator) { // Get a search criteria class /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: messageInstance=%s,handlerInstance=%s,excludeKey=%s,andKey=%s,separator=%s - CALLED!', $messageInstance->__toString(), $handlerInstance->__toString(), $excludeKey, $andKey, $separator)); /* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: state=%s,messageInstance=%s', $this->getPrintableState(), print_r($messageInstance, TRUE))); @@ -400,6 +400,7 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { */ public function findRecipientsByPackageInstance (DeliverablePackage $packageInstance) { // Query get a result instance back from DHT database frontend + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: packageInstance=%s - CALLED!', $packageInstance->__toString())); $resultInstance = $this->getFrontendInstance()->getResultFromExcludedSender($packageInstance); // Make sure the result instance is valid @@ -464,6 +465,7 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { } // Return filled array + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: recipients()=%d - EXIT!', count($recipients))); return $recipients; } diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index f72c639db..8e06d53d6 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -108,6 +108,7 @@ class SocketFactory extends ObjectFactory { */ public static final function createListenFileSocket (Listenable $listenerInstance) { // Create SplFileInfo + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - EXIT!', $listenerInstance->__toString())); $fileInfo = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('ipc_socket_file_name')); // Create file name @@ -141,13 +142,13 @@ class SocketFactory extends ObjectFactory { if (!$socketInstance->isValidSocket()) { // Something bad happened throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); - } // END - if + } // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Is the file there? if ((FrameworkBootstrap::isReachableFilePath($socketInstance->getSocketFile())) && (file_exists($socketInstance->getSocketFile()))) { @@ -159,7 +160,7 @@ class SocketFactory extends ObjectFactory { // Quit here exit; - } // END - if + } // Debug message self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...'); @@ -168,26 +169,24 @@ class SocketFactory extends ObjectFactory { if (!$socketInstance->bindSocketToFile()) { // Handle error here $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Start listen for connections self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); if (!$socketInstance->listenToSocket()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Allow non-blocking I/O self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if - - // Trace message - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); + } // Return socket instance + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } @@ -201,10 +200,8 @@ class SocketFactory extends ObjectFactory { * @todo Rewrite this to also handle IPv6 addresses and sockets */ public static final function createTcpOutgoingSocketFromPackageInstance (DeliverablePackage $packageInstance) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString())); - // Create a socket instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString())); $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // Debug message @@ -226,20 +223,20 @@ class SocketFactory extends ObjectFactory { * InvalidSocketException back. */ throw new SocketCreationException(array($factoryInstance, $socketInstance->getSocketResource()), StorableSocket::EXCEPTION_SOCKET_CREATION_FAILED); - } // END - if + } // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { // Handle this socket error $helperInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Set the option to reuse the port //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (!$socketInstance->enableSocketReuseAddress()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } /* * Set socket to non-blocking mode before trying to establish a link to @@ -250,12 +247,10 @@ class SocketFactory extends ObjectFactory { if (!$socketInstance->enableSocketNonBlocking()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if - - // Trace message - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); + } // Return it + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } @@ -267,10 +262,8 @@ class SocketFactory extends ObjectFactory { * @throws InvalidSocketException Thrown if the socket could not be initialized */ public static function createListenTcpSocket (Listenable $listenerInstance) { - // Trace message - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); - // Create a streaming socket, of type TCP/IP + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // Init fake "package" instance, the SocketContainer class requires this @@ -291,20 +284,20 @@ class SocketFactory extends ObjectFactory { if (!$socketInstance->isValidSocket()) { // Something bad happened throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET); - } // END - if + } // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Set the option to reuse the port self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (!$socketInstance->enableSocketReuseAddress()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } /* * "Bind" the socket to the given address, on given port so this means @@ -315,26 +308,24 @@ class SocketFactory extends ObjectFactory { if (!$socketInstance->bindSocketToListener()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Start listen for connections self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); if (!$socketInstance->listenToSocket()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Now, we want non-blocking mode self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if - - // Trace message - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); + } // Return prepepared socket + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } @@ -370,20 +361,20 @@ class SocketFactory extends ObjectFactory { if (!$socketInstance->isValidSocket()) { // Something bad happened throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET); - } // END - if + } // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Set the option to reuse the port self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (!$socketInstance->enableSocketReuseAddress()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } /* * "Bind" the socket to the given address, on given port so this means @@ -394,14 +385,14 @@ class SocketFactory extends ObjectFactory { if (!$socketInstance->bindSocketToListener()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Now, we want non-blocking mode self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Trace message //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); @@ -443,7 +434,7 @@ class SocketFactory extends ObjectFactory { if (!$iteratorInstance->valid()) { // Try to rewind it $iteratorInstance->rewind(); - } // END - if + } // Get current socket instance $current = $iteratorInstance->current(); @@ -509,31 +500,31 @@ class SocketFactory extends ObjectFactory { if (!$socketInstance->isValidSocket()) { // Something bad happened throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET); - } // END - if + } // Try to identify socket peer if (!$socketInstance->identifySocketPeer()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Set timeout to configured seconds if (!$socketInstance->setSocketTimeoutOptions()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Enable SO_OOBINLINE if (!$socketInstance->enableSocketOutOfBandData()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Set non-blocking if (!$socketInstance->enableSocketNonBlocking()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if + } // Return found socket instance /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); diff --git a/application/hub/classes/handler/class_BaseHubHandler.php b/application/hub/classes/handler/class_BaseHubHandler.php index b9b301a0d..2539a063e 100644 --- a/application/hub/classes/handler/class_BaseHubHandler.php +++ b/application/hub/classes/handler/class_BaseHubHandler.php @@ -4,11 +4,12 @@ namespace Org\Shipsimu\Hub\Handler; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; -use Org\Shipsimu\Hub\Listener\Listenable; use Org\Shipsimu\Hub\Locator\Node\LocateableNode; use Org\Shipsimu\Hub\Network\Package\Receiver\Assembler\Assembler; use Org\Shipsimu\Hub\Node\Node; use Org\Shipsimu\Hub\Pool\Poolable; +use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait; +use Org\Shipsimu\Hub\Traits\Node\NodeTrait; // Import framework stuff use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException; @@ -41,23 +42,15 @@ use Org\Mxchange\CoreFramework\Traits\Stacker\StackableTrait; */ abstract class BaseHubHandler extends BaseHandler implements Handleable, HubInterface { // Load traits + use ListenableTrait; + use NodeTrait; use StackableTrait; - /** - * Listener instance - */ - private $listenerInstance = NULL; - /** * Listener pool instance */ private $listenerPoolInstance = NULL; - /** - * Node instance - */ - private $nodeInstance = NULL; - /** * An instance of a LocateableNode class */ @@ -124,44 +117,6 @@ abstract class BaseHubHandler extends BaseHandler implements Handleable, HubInte return $this->universalNodeLocatorInstance; } - /** - * Setter for listener instance - * - * @param $listenerInstance A Listenable instance - * @return void - */ - public final function setListenerInstance (Listenable $listenerInstance) { - $this->listenerInstance = $listenerInstance; - } - - /** - * Getter for listener instance - * - * @return $listenerInstance A Listenable instance - */ - public function getListenerInstance () { - return $this->listenerInstance; - } - - /** - * Setter for node instance - * - * @param $nodeInstance A Node instance - * @return void - */ - public final function setNodeInstance (Node $nodeInstance) { - $this->nodeInstance = $nodeInstance; - } - - /** - * Getter for node instance - * - * @return $nodeInstance A Node instance - */ - public function getNodeInstance () { - return $this->nodeInstance; - } - /** * Setter for assembler instance * 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 66e3696ed..3ae8e5bf7 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -132,6 +132,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection } // END - if // Okay, that should be it. Return it... + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } diff --git a/application/hub/classes/info/connection/class_ConnectionInfo.php b/application/hub/classes/info/connection/class_ConnectionInfo.php index 0c0f1845d..31f46d3e3 100644 --- a/application/hub/classes/info/connection/class_ConnectionInfo.php +++ b/application/hub/classes/info/connection/class_ConnectionInfo.php @@ -10,6 +10,7 @@ use Org\Shipsimu\Hub\Information\ShareableInfo; use Org\Shipsimu\Hub\Listener\Listenable; use Org\Shipsimu\Hub\Locator\Node\LocateableNode; use Org\Shipsimu\Hub\Traits\Container\Socket\StorableSocketTrait; +use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait; // Import framework stuff use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint; @@ -43,13 +44,9 @@ use \InvalidArgumentException; */ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { // Load traits + use ListenableTrait; use StorableSocketTrait; - /** - * Listener instance - */ - private $listenerInstance = NULL; - /** * Connection type: 'incoming', 'outgoing', 'server' */ @@ -134,25 +131,6 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { $this->protocolName = $protocolName; } - /** - * Setter for listener instance - * - * @param $listenerInstance A Listenable instance - * @return void - */ - public final function setListenerInstance (Listenable $listenerInstance) { - $this->listenerInstance = $listenerInstance; - } - - /** - * Getter for listener instance - * - * @return $listenerInstance A Listenable instance - */ - public final function getListenerInstance () { - return $this->listenerInstance; - } - /** * Getter for address * diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index 08384f31a..2b942b67d 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -6,8 +6,9 @@ namespace Org\Shipsimu\Hub\Listener; use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Handler\Network\NetworkPackageHandlerFactory; use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory; -use Org\Shipsimu\Hub\Listener\Listenable; use Org\Shipsimu\Hub\Network\Networkable; +use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait; +use Org\Shipsimu\Hub\Traits\Container\Socket\StorableSocketTrait; // Import framework stuff use Org\Mxchange\CoreFramework\Generic\BaseDecorator; @@ -42,6 +43,10 @@ use \LogicException; * along with this program. If not, see . */ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable { + // Load traits + use ListenableTrait; + use StorableSocketTrait; + /** * Listener type */ @@ -52,11 +57,6 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable */ private $protocolName = 'invalid'; - /** - * The decorated listener instance - */ - private $listenerInstance = NULL; - /** * Protected constructor * @@ -205,44 +205,6 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: EXIT!', strtoupper($this->getProtocolName()))); } - /** - * 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 listener instance - * - * @param $listenerInstance A Listenable instance - * @return void - */ - public final function setListenerInstance (Listenable $listenerInstance) { - $this->listenerInstance = $listenerInstance; - } - - /** - * Getter for listener instance - * - * @return $listenerInstance A Listenable instance - */ - public final function getListenerInstance () { - return $this->listenerInstance; - } - /** * Getter for protocol name * diff --git a/application/hub/classes/locator/class_UniversalNodeLocator.php b/application/hub/classes/locator/class_UniversalNodeLocator.php index db0302bd0..b94120832 100644 --- a/application/hub/classes/locator/class_UniversalNodeLocator.php +++ b/application/hub/classes/locator/class_UniversalNodeLocator.php @@ -61,12 +61,14 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi */ public final static function createUniversalNodeLocator (array $current = []) { // Get new instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: current()=%d - CALLED!', count($current))); $locatorInstance = new UniversalNodeLocator(); // Init instance $locatorInstance->initUniversalNodeLocator($current); // Return the prepared instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: locatorInstance=%s - EXIT!', $locatorInstance->__toString())); return $locatorInstance; } @@ -90,15 +92,13 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi * @param $unl Universal Node Locator (UNL) to "parse" * @throws InvalidArgumentException If given UNL is not valid */ - private function parseUniversalNodeLocator ($unl) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=[%s]=%s - CALLED!', gettype($unl), $unl)); - + private function parseUniversalNodeLocator (string $unl) { // Make sure the UNL is valid + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=[%s]=%s - CALLED!', gettype($unl), $unl)); if (!NodeLocatorUtils::isValidUniversalNodeLocator($unl)) { // UNL is not valid throw new InvalidArgumentException(sprintf('unl[%s]=%s is not valid.', gettype($unl), $unl)); - } // END - if + } /* * "Parse" the UNL "generically", sadly this cannot be done by using preg_match() :-( @@ -110,17 +110,18 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi $unlParts[1] = explode(':', $unlParts[1]); // Now there is an almost useable array which then can be copied to the "real" array. - $unlData = array( + $unlData = [ 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('UNIVERSAL-NODE-LOCATOR: unlData=' . print_r($unlData, TRUE) . ' - EXIT!'); + ]; // Call init method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: unlData=' . print_r($unlData, TRUE) . ' - EXIT!'); $this->initUniversalNodeLocator($unlData); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: EXIT!'); } /** @@ -132,6 +133,7 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi */ private function initUniversalNodeLocator (array $unlData = []) { // Init UNL array + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: CALLED!'); $this->unlData = []; // Copy all found entries @@ -146,17 +148,19 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi NodeInformationDatabaseFrontend::DB_COLUMN_INTERNAL_UNL, NodeInformationDatabaseFrontend::DB_COLUMN_EXTERNAL_UNL) as $key) { // Init entry + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: key=%s', $key)); $this->unlData[$key] = NULL; // Is the key found? if (isset($unlData[$key])) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: Copying unlData[' . $key . ']=' . $unlData[$key] . ' ...'); - // The copy the entry + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: Copying unlData[%s]=%s ...', $key, $unlData[$key])); $this->unlData[$key] = $unlData[$key]; - } // END - if - } // END - foreach + } + } + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: EXIT!'); } /** @@ -221,7 +225,7 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi if (isset($unlData[LocateableNode::UNL_PART_PROTOCOL])) { // Use it $unkProtocol = $unlData[LocateableNode::UNL_PART_PROTOCOL]; - } // END - if + } // Return it return $unkProtocol; @@ -243,7 +247,7 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi if (isset($unlData[LocateableNode::UNL_PART_ADDRESS])) { // Use it $unkAddress = $unlData[LocateableNode::UNL_PART_ADDRESS]; - } // END - if + } // Return it return $unkAddress; @@ -266,7 +270,7 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi if (isset($unlData[LocateableNode::UNL_PART_EXTRA])) { // Use it $unlPort = $unlData[LocateableNode::UNL_PART_EXTRA]; - } // END - if + } // Return it return $unlPort; @@ -286,7 +290,7 @@ class UniversalNodeLocator extends BaseHubSystem implements LocateableNode, Regi if (!is_string($unl)) { // Abort here throw new InvalidArgumentException(sprintf('unl[]=%s is not string.', gettype($unl))); - } // END - if + } // Parse it $this->parseUniversalNodeLocator($unl); diff --git a/application/hub/classes/pools/class_BasePool.php b/application/hub/classes/pools/class_BasePool.php index 278337cc9..c9767346d 100644 --- a/application/hub/classes/pools/class_BasePool.php +++ b/application/hub/classes/pools/class_BasePool.php @@ -4,8 +4,8 @@ namespace Org\Shipsimu\Hub\Pool; // Import application-specificl stuff use Org\Shipsimu\Hub\Generic\BaseHubSystem; -use Org\Shipsimu\Hub\Listener\Listenable; use Org\Shipsimu\Hub\Pool\Poolable; +use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; @@ -36,10 +36,8 @@ use Org\Mxchange\CoreFramework\Visitor\Visitor; * along with this program. If not, see . */ abstract class BasePool extends BaseHubSystem implements Poolable, Visitable { - /** - * Listener instance - */ - private $listenerInstance = NULL; + // Load traits + use ListenableTrait; /** * A list of pool entries @@ -124,25 +122,6 @@ abstract class BasePool extends BaseHubSystem implements Poolable, Visitable { return $this->poolEntriesInstance; } - /** - * Setter for listener instance - * - * @param $listenerInstance A Listenable instance - * @return void - */ - public final function setListenerInstance (Listenable $listenerInstance) { - $this->listenerInstance = $listenerInstance; - } - - /** - * Getter for listener instance - * - * @return $listenerInstance A Listenable instance - */ - public final function getListenerInstance () { - return $this->listenerInstance; - } - /** * Accepts the visitor to process the visit "request" * diff --git a/application/hub/classes/registry/socket/class_SocketRegistry.php b/application/hub/classes/registry/socket/class_SocketRegistry.php index 598e305c9..4d337c034 100644 --- a/application/hub/classes/registry/socket/class_SocketRegistry.php +++ b/application/hub/classes/registry/socket/class_SocketRegistry.php @@ -72,7 +72,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke if (is_null(self::$registryInstance)) { // Not yet, so create one self::$registryInstance = new SocketRegistry(); - } // END - if + } // Return the instance return self::$registryInstance; @@ -197,8 +197,8 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: Final result: isRegistered(' . $socketInstance->getSocketResource() . ')=' . intval($isRegistered)); - } // END - if - } // END - if + } + } // Return the result //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); @@ -219,7 +219,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke if ($this->isSocketRegistered($infoInstance, $socketInstance)) { // Throw the exception throw new SocketAlreadyRegisteredException(array($infoInstance, $socketInstance->getSocketResource()), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED); - } // END - if + } // Does the instance exist? if (!$this->isInfoRegistered($infoInstance)) { @@ -260,7 +260,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke if (!$this->isInfoRegistered($listenerInstance)) { // Throw the exception throw new NoSocketRegisteredException ($listenerInstance, self::EXCEPTION_SOCKET_NOT_REGISTERED); - } // END - if + } // Now get the key from the listener $key = $this->getRegistryKeyFromInfo($listenerInstance); @@ -327,15 +327,15 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',infoInstance[' . gettype($infoInstance) . ']=' . $infoInstance->__toString() . ' with protocol ' . $infoInstance->getProtocolName() . ' - FOUND!'); break; - } // END - if - } // END - foreach + } + } // Is no longer NULL set? if (!is_null($infoInstance)) { // Then skip here, too break; - } // END - if - } // END - foreach + } + } // Return the info instance //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: infoInstance[]=' . gettype($infoInstance) . ' - EXIT!'); diff --git a/application/hub/classes/tasks/class_BaseHubTask.php b/application/hub/classes/tasks/class_BaseHubTask.php index dc0470e59..ea3e71092 100644 --- a/application/hub/classes/tasks/class_BaseHubTask.php +++ b/application/hub/classes/tasks/class_BaseHubTask.php @@ -3,7 +3,7 @@ namespace Org\Shipsimu\Hub\Task; // Import application-specific stuff -use Org\Shipsimu\Hub\Node\Node; +use Org\Shipsimu\Hub\Traits\Node\NodeTrait; // Import framework stuff use Org\Mxchange\CoreFramework\Task\BaseTask; @@ -31,11 +31,8 @@ use Org\Mxchange\CoreFramework\Task\BaseTask; * along with this program. If not, see . */ abstract class BaseHubTask extends BaseTask { - - /** - * Node instance - */ - private $nodeInstance = NULL; + // Load traits + use NodeTrait; /** * Protected constructor @@ -48,23 +45,4 @@ abstract class BaseHubTask extends BaseTask { parent::__construct($className); } - /** - * Setter for node instance - * - * @param $nodeInstance A Node instance - * @return void - */ - public final function setNodeInstance (Node $nodeInstance) { - $this->nodeInstance = $nodeInstance; - } - - /** - * Getter for node instance - * - * @return $nodeInstance A Node instance - */ - public function getNodeInstance () { - return $this->nodeInstance; - } - } diff --git a/application/hub/classes/tools/hub/class_HubTools.php b/application/hub/classes/tools/hub/class_HubTools.php index adcfae52c..5f6b4af51 100644 --- a/application/hub/classes/tools/hub/class_HubTools.php +++ b/application/hub/classes/tools/hub/class_HubTools.php @@ -106,7 +106,7 @@ class HubTools extends BaseHubSystem { * @return $recipientUniversalNodeLocator Recipient as Universal Node Locator * @throws InvalidArgumentException If $sessionId is not valid */ - protected function resolveUniversalNodeLocatorBySessionId ($sessionId) { + protected function resolveUniversalNodeLocatorBySessionId (string $sessionId) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - CALLED!', $sessionId)); if (empty($sessionId)) { @@ -149,7 +149,7 @@ class HubTools extends BaseHubSystem { * @return $nodeId Node id * @throws InvalidArgumentException If $sessionId is not valid */ - public static function resolveNodeIdBySessionId ($sessionId) { + public static function resolveNodeIdBySessionId (string $sessionId) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - CALLED!', $sessionId)); if (empty($sessionId)) { diff --git a/application/hub/interfaces/database/frontend/node_dht/class_NodeDhtFrontend.php b/application/hub/interfaces/database/frontend/node_dht/class_NodeDhtFrontend.php index def6db848..186541f0f 100644 --- a/application/hub/interfaces/database/frontend/node_dht/class_NodeDhtFrontend.php +++ b/application/hub/interfaces/database/frontend/node_dht/class_NodeDhtFrontend.php @@ -72,7 +72,7 @@ interface NodeDhtFrontend extends DatabaseFrontend { * @return $resultInstance An instance of a SearchableResult class * @throws InvalidArgumentException If parameter $sessionId is not valid */ - function findNodeLocalBySessionId ($sessionId); + function findNodeLocalBySessionId (string $sessionId); /** * Finds a node locally by given UNL instance diff --git a/application/hub/interfaces/distributable/node/class_DistributableNode.php b/application/hub/interfaces/distributable/node/class_DistributableNode.php index b1fc19791..6bf839272 100644 --- a/application/hub/interfaces/distributable/node/class_DistributableNode.php +++ b/application/hub/interfaces/distributable/node/class_DistributableNode.php @@ -40,7 +40,7 @@ interface DistributableNode extends Distributable { * @return $nodeData Node-data array * @throws InvalidArgumentException If parameter $sessionId is not valid */ - function findNodeLocalBySessionId ($sessionId); + function findNodeLocalBySessionId (string $sessionId); /** * Finds a node locally by given UNL instance @@ -58,25 +58,25 @@ interface DistributableNode extends Distributable { * - external-address (hostname or IP number) * - listen-port (TCP/UDP listen port for inbound connections) * - * @param $messageData An instance of a DeliverableMessage class + * @param $messageInstance An instance of a DeliverableMessage class * @param $handlerInstance An instance of a HandleableDataSet class * @param $forceUpdate Optionally force update, don't register (default: register if not found) * @return void */ - function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, $forceUpdate = FALSE); + function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, bool $forceUpdate = FALSE); /** * Queries the local DHT data(base) for a node list with all supported * object types except the node by given session id. * - * @param $messageData An instance of a DeliverableMessage class + * @param $messageInstance An instance of a DeliverableMessage class * @param $handlerInstance An instance of a HandleableDataSet class * @param $excludeKey Array key which should be excluded * @param $andKey Array of $separator-separated list of elements which all must match * @param $separator Sepator char (1st parameter for explode() call) * @return $nodeList An array with all found nodes */ - function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, $excludeKey, $andKey, $separator); + function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, string $excludeKey, string $andKey, string $separator); /** * Inserts given node list array (from earlier database result produced by diff --git a/application/hub/interfaces/pool/listener/class_PoolableListener.php b/application/hub/interfaces/pool/listener/class_PoolableListener.php index 0ab2ed898..72d5fac63 100644 --- a/application/hub/interfaces/pool/listener/class_PoolableListener.php +++ b/application/hub/interfaces/pool/listener/class_PoolableListener.php @@ -31,7 +31,6 @@ use Org\Shipsimu\Hub\Pool\Poolable; * along with this program. If not, see . */ interface PoolableListener extends Poolable { - /** * Adds a listener instance to this pool * diff --git a/application/hub/traits/listener/class_ListenableTrait.php b/application/hub/traits/listener/class_ListenableTrait.php new file mode 100644 index 000000000..ab998304d --- /dev/null +++ b/application/hub/traits/listener/class_ListenableTrait.php @@ -0,0 +1,55 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 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 . + */ +trait ListenableTrait { + /** + * The decorated listener instance + */ + private $listenerInstance = NULL; + + /** + * Setter for listener instance + * + * @param $listenerInstance A Listenable instance + * @return void + */ + public 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/traits/node/class_NodeTrait.php b/application/hub/traits/node/class_NodeTrait.php new file mode 100644 index 000000000..acfaed11c --- /dev/null +++ b/application/hub/traits/node/class_NodeTrait.php @@ -0,0 +1,55 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2011 - 2014 - 2018 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 . + */ +trait NodeTrait { + /** + * Node instance + */ + private $nodeInstance = NULL; + + /** + * Setter for node instance + * + * @param $nodeInstance A Node instance + * @return void + */ + public final function setNodeInstance (Node $nodeInstance) { + $this->nodeInstance = $nodeInstance; + } + + /** + * Getter for node instance + * + * @return $nodeInstance A Node instance + */ + public function getNodeInstance () { + return $this->nodeInstance; + } + +}