From 74f6e523eb75ad2ef1e0c0356221a41484087438 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 26 Feb 2023 04:54:44 +0100 Subject: [PATCH] Continued: - introduced some "static initializer" to class SocketFactory which youls hould run when you use these "locally cached" static instances - updated 'core' framework --- .../factories/socket/class_SocketFactory.php | 67 ++++++++++++++----- core | 2 +- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 3863ff4af..df1381ad6 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -48,6 +48,11 @@ use \UnexpectedValueException; * along with this program. If not, see . */ class SocketFactory extends BaseFactory { + /** + * "Cache" for socket factory + */ + private static $socketFactoryInstance = NULL; + /** * Protected constructor * @@ -58,6 +63,23 @@ class SocketFactory extends BaseFactory { parent::__construct(__CLASS__); } + /** + * Some "static initializer + * + * @return void + */ + public final static function staticInitializer () { + // Is it initialized? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: self::socketFactoryInstance[]=%s - CALLED!', gettype(self::$socketFactoryInstance))); + if (is_null(self::$socketFactoryInstance)) { + // No, then initialize it + self::$socketFactoryInstance = ObjectRegistry::getRegistry('factory'); + } + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: self::socketFactoryInstance=%s - EXIT!', self::$socketFactoryInstance)); + } + /** * Creates a valid socket resource from given packae data and protocol * @@ -66,8 +88,11 @@ class SocketFactory extends BaseFactory { * @return $socketInstance An instance of a StorableSocket class */ public static function createSocketFromPackageInstance (DeliverablePackage $packageInstance, HandleableProtocol $protocolInstance) { - // Init instance + // Invoke static initializer /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s,protocolInstance=%s - CALLED!', $packageInstance->__toString(), $protocolInstance->__toString())); + self::staticInitializer(); + + // Init instance //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE))); $socketInstance = NULL; @@ -79,9 +104,9 @@ class SocketFactory extends BaseFactory { // Is the key there? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Trying to find a socket with registryKey=%s', $registryKey)); - if (ObjectRegistry::getRegistry('factory')->instanceExists($registryKey)) { + if (self::$socketFactoryInstance->instanceExists($registryKey)) { // Get container instance - $socketInstance = ObjectRegistry::getRegistry('factory')->getInstance($registryKey); + $socketInstance = self::$socketFactoryInstance->getInstance($registryKey); // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Using socketResource[%s]=%s from registry.', gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource())); @@ -90,7 +115,7 @@ class SocketFactory extends BaseFactory { $socketInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_connection_helper_class', $protocolInstance->getProtocolName()), array($packageInstance)); // Register it with the registry - ObjectRegistry::getRegistry('factory')->addInstance($registryKey, $socketInstance); + self::$socketFactoryInstance->addInstance($registryKey, $socketInstance); // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Socket is now registered in registry.'); @@ -142,7 +167,7 @@ class SocketFactory extends BaseFactory { $socketInstance->setSocketFile($socketFile); // Is the socket resource valid? - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance))); if (!$socketInstance->isValidSocket()) { // Something bad happened throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); @@ -232,7 +257,7 @@ class SocketFactory extends BaseFactory { } // Set the option to reuse the port - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (FrameworkBootstrap::getConfigurationInstance()->isEnabled('tcp_socket_enable_reuse_address') && !$socketInstance->enableSocketReuseAddress()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); @@ -243,14 +268,14 @@ class SocketFactory extends BaseFactory { * it. This is now the default behaviour for all connection helpers who * call initConnection(); . */ - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // Return it - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } @@ -263,7 +288,7 @@ class SocketFactory extends BaseFactory { */ public static function createListenTcpSocket (Listenable $listenerInstance) { // Create a streaming socket, of type TCP/IP - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); + /* 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 @@ -325,7 +350,7 @@ class SocketFactory extends BaseFactory { } // Return prepepared socket - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } @@ -338,7 +363,7 @@ class SocketFactory extends BaseFactory { */ public static function createListenUdpSocket (Listenable $listenerInstance) { // Create a streaming socket, of type UDP - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); $socketResource = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // Init fake package instance @@ -393,7 +418,7 @@ class SocketFactory extends BaseFactory { } // Return prepepared socket - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } @@ -408,25 +433,31 @@ class SocketFactory extends BaseFactory { * @throws LogicException If the current instance is not valid */ public static final function createNextAcceptedSocketFromPool (Poolable $poolInstance, StorableSocket $socketInstance) { + // Invoke static initializer + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: poolInstance=%s,socketInstance->socketResource=%s - CALLED!', $poolInstance->__toString(), $socketInstance->getSocketResource())); + self::staticInitializer(); + // Get socket protocol - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: poolInstance=%s,socketInstance->socketResource=%s - CALLED!', $poolInstance->__toString(), $socketInstance->getSocketResource())); $socketProtocol = $socketInstance->getSocketProtocol(); // Is the an iterator instance? - if (!ObjectRegistry::getRegistry('factory')->instanceExists('pool_iterator_' . $socketProtocol)) { + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketProtocol=%s', $socketProtocol)); + if (!self::$socketFactoryInstance->instanceExists('pool_iterator_' . $socketProtocol)) { // Create iterator instance $iteratorInstance = $poolInstance->createListIteratorInstance($socketProtocol . '_pool'); // Now store it in registry - ObjectRegistry::getRegistry('factory')->addInstance('pool_iterator', $iteratorInstance); + self::$socketFactoryInstance->addInstance('pool_iterator', $iteratorInstance); } else { // Get iterator from registry - $iteratorInstance = ObjectRegistry::getRegistry('factory')->getInstance('pool_iterator_' . $socketProtocol); + $iteratorInstance = self::$socketFactoryInstance->getInstance('pool_iterator_' . $socketProtocol); } // Is it valid? + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: iteratorInstance=%s', $iteratorInstance->__toString())); if (!$iteratorInstance->valid()) { // Try to rewind it + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('SOCKET-FACTORY: Invoking iteratorInstance->rewind() ...'); $iteratorInstance->rewind(); } @@ -434,6 +465,7 @@ class SocketFactory extends BaseFactory { $current = $iteratorInstance->current(); // Make sure it is valid + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: current[]=%s', gettype($current))); if (!is_array($current)) { // Opps, should not happen throw new LogicException(sprintf('current[]=%s is not an array.', gettype($current))); @@ -449,10 +481,11 @@ class SocketFactory extends BaseFactory { } // Try to accept a new (incoming) socket from current listener instance + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: Invoking current[%s]->acceptNewIncomingSocket() ...', Poolable::SOCKET_ARRAY_INSTANCE)); $acceptedSocketInstance = $current[Poolable::SOCKET_ARRAY_INSTANCE]->acceptNewIncomingSocket(); // Return found socket instance - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: acceptedSocketInstance[]=%s - EXIT!', gettype($acceptedSocketInstance))); + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: acceptedSocketInstance[]=%s - EXIT!', gettype($acceptedSocketInstance))); return $acceptedSocketInstance; } diff --git a/core b/core index 4fc7b52c4..53f5e597b 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 4fc7b52c4eb7287dbfd78bd4bdc8adaea03f7d5c +Subproject commit 53f5e597b72bedb2995dca43e04d88f37eaa5e88 -- 2.39.5