From 11fc38d92a08d90a078cae3cc3edf0763c21d523 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 3 Nov 2020 20:08:17 +0100 Subject: [PATCH] Continued: - the SocketFactory class was invoking complicated createConnectionFromPackageInstance() method which didn't fit naming-convention. It is now renamed to "create" scheme and can be created by default ObjectFactory's factory methods. - improved some debug lines MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../socket/class_SocketContainer.php | 37 ++++++------------- .../factories/socket/class_SocketFactory.php | 11 ++---- .../ipv4/tcp/class_TcpConnectionHelper.php | 2 +- .../ipv4/udp/class_UdpConnectionHelper.php | 2 +- 4 files changed, 18 insertions(+), 34 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index e3e9506cf..c45ebf4ef 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -384,7 +384,6 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientPort=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($recipientPort))); return $recipientPort; - } /** @@ -781,11 +780,10 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * PHP_NORMAL_READ, this line will endless block. This script does only * provide simultanous or faked threads, not real. */ - /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $rawData = socket_read($this->getSocketResource(), $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ); // Return it - /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: rawData[%s]()=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($rawData), strlen($rawData))); + /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s,rawData[%s]=%s - EXIT!', strtoupper($this->getSocketProtocol()), $this->getSocketResource(), gettype($rawData), $rawData)); return $rawData; } @@ -823,19 +821,14 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $result Whether the option has been set */ public function setSocketTimeoutOptions () { - // Trace message + // Call setter method with configured values //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - - // Array for timeout settings - $options = array( + $result = $this->setSocketOption(SOL_SOCKET, SO_RCVTIMEO, [ // Seconds 'sec' => $this->getConfigInstance()->getConfigEntry($this->getSocketProtocol() . '_socket_accept_wait_sec'), // Milliseconds - 'usec' => $this->getConfigInstance()->getConfigEntry($this->getSocketProtocol() . '_socket_accept_wait_usec') - ); - - // Call inner method - $result = $this->setSocketOption(SOL_SOCKET, SO_RCVTIMEO, $options); + 'usec' => $this->getConfigInstance()->getConfigEntry($this->getSocketProtocol() . '_socket_accept_wait_usec'), + ]); // Return result //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); @@ -849,13 +842,11 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function enableSocketOutOfBandData () { // Call inner method - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $result = $this->setSocketOption(SOL_SOCKET, SO_OOBINLINE, 1); - // Trace message - // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -868,7 +859,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function clearLastSocketError () { // Should be valid socket - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!'); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -881,7 +872,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita socket_clear_error($this->getSocketResource()); // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!'); } /** @@ -892,17 +883,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return void */ public function registerInfoInstance (ShareableInfo $infoInstance) { - // Trace message - //* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling infoInstance->getListenerInstance() - CALLED!', strtoupper($this->getSocketProtocol()))); - // Get listener/helper from info class + //* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling infoInstance->getListenerInstance() - CALLED!', strtoupper($this->getSocketProtocol()))); $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? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance)); if ($listenerInstance instanceof Listenable) { // Set it here for later usage //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...'); @@ -934,7 +921,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita public function writeBufferToSocketByArray (array &$socketBuffer, &$sentBytes) { // Should be valid socket /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketBuffer()=%d,sentBytes=%d - CALLED!', strtoupper($this->getSocketProtocol()), count($socketBuffer), $sentBytes)); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketBuffer=%s', strtoupper($this->getSocketProtocol()), print_r($socketBuffer, TRUE))); + //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketBuffer=%s', strtoupper($this->getSocketProtocol()), print_r($socketBuffer, TRUE))); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 5e5281c93..b00926d4d 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -64,12 +64,13 @@ class SocketFactory extends ObjectFactory { public static function createSocketFromPackageInstance (DeliverablePackage $packageInstance, HandleableProtocol $protocolInstance) { // Init instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s,protocolInstance=%s - CALLED!', $packageInstance->__toString(), $protocolInstance->__toString())); + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE))); $socketInstance = NULL; // Construct registry key $registryKey = sprintf('socket_%s_%s', $protocolInstance->getProtocolName(), - $packageInstance->getRecipientType() + $packageInstance->getRecipientUnl() ); // Is the key there? @@ -81,12 +82,8 @@ class SocketFactory extends ObjectFactory { // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Using socketResource[%s]=%s from registry.', gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource())); } else { - // Construct configuration entry for object factory and get it - $className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($protocolInstance->getProtocolName() . '_connection_helper_class'); - - // And call the static method - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Going to use className=%s for creating a socket resource ...', $className)); - $socketInstance = call_user_func($className . '::createConnectionFromPackageInstance', $packageInstance); + // Invoke inner factory + $socketInstance = self::createObjectByConfiguredName(sprintf('%s_connection_helper_class', $protocolInstance->getProtocolName()), array($packageInstance)); // Register it with the registry GenericRegistry::getRegistry()->addInstance($registryKey, $socketInstance); 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 1a764f509..27d8b751b 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -58,7 +58,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection * @param $packageInstance An instance of a DeliverablePackage class * @return $socketInstance An instance of a StorableSocket class */ - public static function createConnectionFromPackageInstance (DeliverablePackage $packageInstance) { + public static function createTcpConnectionHelper (DeliverablePackage $packageInstance) { // Create an instance $helperInstance = new TcpConnectionHelper(); diff --git a/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php index 39ac62817..07baaec70 100644 --- a/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php @@ -53,7 +53,7 @@ class UdpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection * @param $packageInstance An instance of a DeliverablePackage class * @return $socketInstance An instance of a StorableSocket class */ - public static function createConnectionFromPackageInstance (DeliverablePackage $packageInstance) { + public static function createUdpConnectionHelper (DeliverablePackage $packageInstance) { $this->debugBackTrace('Unfinished method, packageInstance=' . print_r($packageInstance, TRUE)); } -- 2.39.5