From: Roland Häder Date: Tue, 3 Nov 2020 13:57:18 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=87cd25373a339b6e358962ed6656ce73419d7dc3;p=hub.git Continued: - introduced handleIncomingSocket() which currently just invokes the handler's method - Improved or added/commented-in debug lines Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 54f9008df..7268f614e 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -768,8 +768,6 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); } // END - if - // Debug message - // Init all arrays, at least readers /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $readers = array($this->getSocketResource()); diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index c21780d31..01f0c11ea 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -43,6 +43,7 @@ use Org\Mxchange\CoreFramework\Visitor\Visitable; use Org\Mxchange\CoreFramework\Visitor\Visitor; // Import SPL stuff +use \BadMethodCallException; use \InvalidArgumentException; use \Iterator; @@ -1159,22 +1160,21 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * on the next stack. * * @return void + * @throws BadMethodCallException If no incoming decoded data was on stack */ public function handleIncomingDecodedData () { /* * This method should only be called if decoded raw data is pending, * so check it again. */ + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!'); if (!$this->isRawDataPending()) { - // This is not fatal but should be avoided - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: No raw (decoded?) data is pending, but ' . __METHOD__ . ' has been called!'); - return; + // Should not be invoked anymore + throw new BadMethodCallException('No incoming decoded data on stack but method was called.'); } // END - if - // Very noisy debug message: - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Stacker size is ' . $this->getStackInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.'); - // "Pop" the next entry (the same array again) from the stack + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Stacker size is %d entries.', $this->getStackInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING))); $packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING); /* @@ -1182,15 +1182,19 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * only want to handle unhandled packages here. */ // Remove the last chunk SEPARATOR (because there is no need for it) - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: errorCode=' . $packageInstance->getErrorCode() . '(' . StorableSocket::SOCKET_ERROR_UNHANDLED . ')'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: errorCode=%s/%s', $packageInstance->getErrorCode(), StorableSocket::SOCKET_ERROR_UNHANDLED)); if (substr($packageInstance->getRawData(), -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) { // It is there and should be removed + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Raw data contains CHUNK_SEPARATOR=%s - Removing ...', PackageFragmenter::CHUNK_SEPARATOR)); $packageInstance->setRawData(substr($packageInstance->getRawData(), 0, -1)); } // END - if // This package is "handled" and can be pushed on the next stack - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Pushing ' . strlen($packageInstance->getRawData()) . ' bytes to stack ' . self::STACKER_NAME_DECODED_HANDLED . ' ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Pushing %d(%s) bytes to stack %s ...', strlen($packageInstance->getRawData()), $packageInstance->getRawData(), self::STACKER_NAME_DECODED_HANDLED)); $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $packageInstance); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!'); } /** @@ -1204,13 +1208,14 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * Get the decoded data from the handler, this is an array with * 'raw_data' and 'error_code' as elements. */ + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: handlerInstance=%s - CALLED!', $handlerInstance->__toString())); $packageInstance = $handlerInstance->getNextPackageInstance(); - // Very noisy debug message: - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: decodedData[' . gettype($packageInstance) . ']=' . print_r($packageInstance, TRUE)); - // And push it on our stack $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $packageInstance); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!'); } /** @@ -1259,10 +1264,8 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @return void */ public function handleAssemblerPendingData () { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling this->getAssemblerInstance()->handlePendingData() ...'); - // Handle it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling this->getAssemblerInstance()->handlePendingData() ...'); $this->getAssemblerInstance()->handlePendingData(); } @@ -1272,10 +1275,8 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @return void */ public function handleMultipleMessages () { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling this->getAssemblerInstance()->handleMultipleMessages() ...'); - // Handle it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling this->getAssemblerInstance()->handleMultipleMessages() ...'); $this->getAssemblerInstance()->handleMultipleMessages(); } @@ -1310,10 +1311,8 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @return void */ public function accept (Visitor $visitorInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: ' . $visitorInstance->__toString() . ' has visited - CALLED!'); - // Visit the package + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: ' . $visitorInstance->__toString() . ' has visited - CALLED!'); $visitorInstance->visitNetworkPackageHandler($this); // Then visit the assembler to handle multiple packages diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index 87206dd4b..8cd3f4bb0 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -92,6 +92,21 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { $this->setRegistryInstance($registryInstance); } + /** + * Handles incoming socket instance + * + * @param $socketInstance An instance of a StorableSocket class + * @return void + */ + private function handleIncomingSocket (StorableSocket $socketInstance) { + // Handle it here, if not main server socket + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $socketInstance->__toString())); + $this->getHandlerInstance()->processRawDataFromSocketInstance($socketInstance); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName()))); + } + /** * Setter for listen address * @@ -304,7 +319,7 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { // Is NULL returned? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s', strtoupper($this->getProtocolName()), gettype($socketInstance))); - if (is_null($socketInstance)) { + if (!($socketInstance instanceof StorableSocket)) { // Then abort here /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=NULL - EXIT!', strtoupper($this->getProtocolName()))); return; @@ -317,10 +332,11 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { return; } - // @TODO Unfinished: - /* DEBUG-DIE: */ die(sprintf('[%s:%d]: socketInstance=%s', __METHOD__, __LINE__, print_r($socketInstance, TRUE))); + // Invoke private method + $this->handleIncomingSocket($socketInstance); // Advance to next + // @TODO $infoInstance is unused $iteratorInstance->next(); // Trace message @@ -351,21 +367,6 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { return; } // END - if - // Init peer address/port - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance->socketResource=%s accepted.', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); - $peerAddress = '0.0.0.0'; - $peerPort = '0'; - - // Get peer name - if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) { - // Handle this socket error - $socketInstance->handleSocketError(__METHOD__, __LINE__); - } // END - if - - // Get node instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Creating node instance ...', strtoupper($this->getProtocolName()))); - $nodeInstance = NodeObjectFactory::createNodeInstance(); - // Get a connection info instance $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), StorableSocket::CONNECTION_TYPE_INCOMING); @@ -379,6 +380,16 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { // Will the info instance with listener data $infoInstance->fillWithSocketPeerInformation($socketInstance); + // Init peer address/port + $peerAddress = '0.0.0.0'; + $peerPort = '0'; + + // Get peer name + if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) { + // Handle this socket error + $socketInstance->handleSocketError(__METHOD__, __LINE__); + } // END - if + // Set all required data //* DEBUG-DIE: */ $infoInstance->debugInstance(); $socketInstance->setSenderAddress($peerAddress . $peerSuffix); @@ -387,9 +398,8 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { // Register the socket with the registry and with the faked array $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance); - // Handle it here, if not main server socket - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Calling this.handlerInstance->processRawDataFromSocketInstance(resource=%s) ...', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); - $this->getHandlerInstance()->processRawDataFromSocketInstance($socketInstance); + // Invoke private method + $this->handleIncomingSocket($socketInstance); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName()))); diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index 3a67d97e3..f22ecf958 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -195,7 +195,7 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: handlerInstance=%s', strtoupper($this->getProtocolName()), $handlerInstance->__toString())); if (!$handlerInstance->isRawDataPending()) { // No data is pending so skip further code silently - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: No data pending on handlerInstance=' . $handlerInstance->__toString() . ' - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: No data pending on handlerInstance=' . $handlerInstance->__toString() . ' - EXIT!'); return; } // END - if