From: Roland Häder Date: Tue, 10 May 2022 13:56:32 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=33ad56ef2de68fd7d40af33a9a35634517d3ba1f;p=hub.git Continued: - renamed getSubRegistryKey() -> generateSubRegistryKeyFromInfoInstance() as this is not a getter - also included connection type (e.g. 'server') into sub-registry key - more debug lines added - removed superflous $peerSuffix parameter as the ':0' may cause confusion Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index d0b51d437..3a7ffd8a5 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -379,13 +379,12 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { /** * "Listens" for incoming network packages * - * @param $peerSuffix Suffix for peer name (e.g. :0 for TCP(/UDP?) connections) * @return void * @throws LogicException If no info instance can be created */ - protected function doListenSocketSelect (string $peerSuffix) { + protected function doListenSocketSelect () { // Validate parameter and socket instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: peerSuffix=%s - CALLED!', strtoupper($this->getProtocolName()), $peerSuffix)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: CALLED!', strtoupper($this->getProtocolName()))); if(!$this->getSocketInstance()->isValidSocket()) { // Invalid socket throw new LogicException(sprintf('this->socketInstance->socketResource=%s is not valid', $this->getSocketInstance()->getSocketResource())); @@ -427,7 +426,7 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { // Set all required data //* DEBUG-DIE: */ $infoInstance->debugInstance(); - $socketInstance->setSenderAddress($peerAddress . $peerSuffix); + $socketInstance->setSenderAddress($peerAddress); $socketInstance->setSenderPort($peerPort); // Register the socket with the registry and with the faked array diff --git a/application/hub/classes/listener/tcp/class_TcpListener.php b/application/hub/classes/listener/tcp/class_TcpListener.php index 44cb6575e..514612d08 100644 --- a/application/hub/classes/listener/tcp/class_TcpListener.php +++ b/application/hub/classes/listener/tcp/class_TcpListener.php @@ -105,10 +105,10 @@ class TcpListener extends BaseListener implements Listenable { */ public function doListen () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Calling this->doListenSocketSelect(:0) ... - CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Calling this->doListenSocketSelect() ... - CALLED!'); // Call super method - $this->doListenSocketSelect(':0'); + $this->doListenSocketSelect(); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: EXIT!'); diff --git a/application/hub/classes/listener/udp/class_UdpListener.php b/application/hub/classes/listener/udp/class_UdpListener.php index d22f44339..79b2f662b 100644 --- a/application/hub/classes/listener/udp/class_UdpListener.php +++ b/application/hub/classes/listener/udp/class_UdpListener.php @@ -107,10 +107,10 @@ class UdpListener extends BaseListener implements Listenable { */ public function doListen () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UDP-LISTENER: Calling this->doListenSocketSelect(:0) ... - CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UDP-LISTENER: Calling this->doListenSocketSelect() ... - CALLED!'); // Call super method - $this->doListenSocketSelect(':0'); + $this->doListenSocketSelect(); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UDP-LISTENER: EXIT!'); diff --git a/application/hub/classes/registry/socket/class_SocketRegistry.php b/application/hub/classes/registry/socket/class_SocketRegistry.php index d7a5b9ff3..73f6f0ef9 100644 --- a/application/hub/classes/registry/socket/class_SocketRegistry.php +++ b/application/hub/classes/registry/socket/class_SocketRegistry.php @@ -85,29 +85,19 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @param $infoInstance An instance of a ShareableInfo class * @return $key A string representation of the socket for the registry */ - private function getSubRegistryKey (ShareableInfo $infoInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: infoInstance=' . $infoInstance->__toString() . ' - CALLED!'); - - // Get address and port - $address = $infoInstance->getAddress(); - $port = $infoInstance->getPort(); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: address=' . $address . ',port=' . $port); - + private function generateSubRegistryKeyFromInfoInstance (ShareableInfo $infoInstance) { // Get connection type and port number and add both together - $key = sprintf('%s:%s:%s:%s', + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: infoInstance=%s - CALLED!', $infoInstance->__toString())); + $key = sprintf('%s:%s:%s:%s:%s', $infoInstance->__toString(), $infoInstance->getProtocolName(), - $address, - $port + $infoInstance->getConnectionType(), + $infoInstance->getAddress(), + $infoInstance->getPort() ); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ' - EXIT!'); - // Return resulting key + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: key=%s - EXIT!', $key)); return $key; } @@ -177,7 +167,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke $registryInstance = $this->getInstance($key); // "Get" a key for the socket - $socketKey = $this->getSubRegistryKey($infoInstance); + $socketKey = $this->generateSubRegistryKeyFromInfoInstance($infoInstance); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...'); @@ -213,36 +203,39 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke */ public function registerSocketInstance (ShareableInfo $infoInstance, StorableSocket $socketInstance) { // Is the socket already registered? - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - CALLED!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: infoInstance->protocolName=%s,infoInstance->socketResource[%s]=%s - CALLED!', $infoInstance->getProtocolName(), gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource())); if ($this->isSocketRegistered($infoInstance, $socketInstance)) { // Throw the exception throw new SocketAlreadyRegisteredException(array($infoInstance, $socketInstance->getSocketResource()), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED); } // Does the instance exist? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: Checking if infoInstance=%s is registered ...', $infoInstance->__toString())); if (!$this->isInfoRegistered($infoInstance)) { // No, not found so we create a sub registry (not needed to configure!) $registryInstance = SubRegistry::createSubRegistry(); // Now we can create the sub-registry for this info + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: Adding infoInstance=%s to registry ...', $infoInstance->__toString())); $this->addInstance($this->getRegistryKeyFromInfo($infoInstance), $registryInstance); } else { // Get the sub-registry back + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: Getting sub-registry from infoInstance=%s ...', $infoInstance->__toString())); $registryInstance = $this->getInstance($this->getRegistryKeyFromInfo($infoInstance)); } // Get a key for sub-registries - $socketKey = $this->getSubRegistryKey($infoInstance); + $socketKey = $this->generateSubRegistryKeyFromInfoInstance($infoInstance); // We have a sub-registry, the socket key and the socket, now we need to put all together - /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: socketKey=' . $socketKey . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - adding socket container instance ...'); + /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: socketKey=%s,socketResource[%s]=%s - adding socket container instance ...', $socketKey, gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource())); $registryInstance->addInstance($socketKey, $socketInstance); // Also register all instances from info instance in socket $socketInstance->registerInfoInstance($infoInstance); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: EXIT!'); } /** @@ -254,26 +247,28 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke */ public function getRegisteredSocketResource (Listenable $listenerInstance) { // The socket must be registered before we can return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY:listener=' . $listenerInstance->getProtocolName() . ' - CALLED!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); if (!$this->isInfoRegistered($listenerInstance)) { // Throw the exception throw new NoSocketRegisteredException ($listenerInstance, self::EXCEPTION_SOCKET_NOT_REGISTERED); } // Now get the key from the listener - $key = $this->getRegistryKeyFromInfo($listenerInstance); - - // And get the registry - $registryInstance = $this->getInstance($key); + $registryKey = $this->getRegistryKeyFromInfo($listenerInstance); // Get a socket key - $socketKey = $this->getSubRegistryKey($listenerInstance); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: registryKey=%s', $registryKey)); + $socketKey = $this->generateSubRegistryKeyFromInfoInstance($listenerInstance); + + // Get the registry + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: socketKey=%s', $socketKey)); + $registryInstance = $this->getInstance($registryKey); // And the final socket resource $socketInstance = $registryInstance->getInstance($socketKey); // Return the resource - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: listener=' . $listenerInstance->getProtocolName() . ',socketInstance[]=' . gettype($socketInstance) . ' - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: listenerInstance=%s,socketInstance[]=%s - EXIT!', $listenerInstance->__toString(), gettype($socketInstance))); return $socketInstance; }