From: Roland Häder Date: Sat, 19 May 2012 15:39:22 +0000 (+0000) Subject: More debugging (yupp, more is required), rewrote isSocketRegistered() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=43cd0f3f991322c7ab4d48bafe0967e0b8f3022f;p=hub.git More debugging (yupp, more is required), rewrote isSocketRegistered() --- diff --git a/application/hub/interfaces/protocol/class_ProtocolHandler.php b/application/hub/interfaces/protocol/class_ProtocolHandler.php index ddaa43fd3..0e80f9a29 100644 --- a/application/hub/interfaces/protocol/class_ProtocolHandler.php +++ b/application/hub/interfaces/protocol/class_ProtocolHandler.php @@ -35,13 +35,6 @@ interface ProtocolHandler extends FrameworkInterface { * @return $protocol Used protocol */ function getProtocol (); - - /** - * Getter for "this" socket resource - * - * @return $socketResource A valid socket resource - */ - function getSocketResource (); } // [EOF] diff --git a/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php b/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php index e57ec7955..ef20e5c3a 100644 --- a/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php +++ b/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php @@ -129,7 +129,11 @@ class PackageSocketDiscovery extends BaseHubDiscovery implements DiscoverableSoc $socketResource = $listenerInstance->getPoolInstance()->getSocketFromPackageData($packageData); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('socketResource=' . $socketResource . ',packageData=' . print_r($packageData, true)); + if (is_resource($socketResource)) { + //* NOISY-DEBUG: */ $this->debugOutput('socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',error=' . socket_strerror(socket_last_error($socketResource)) . ',packageData=' . print_r($packageData, true)); + } else { + //* NOISY-DEBUG: */ $this->debugOutput('socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',packageData=' . print_r($packageData, true)); + } // Is it false, the recipient isn't known to us and we have no connection to it if (($socketResource === false) || (!is_resource($socketResource)) || (socket_last_error($socketResource) > 0)) { diff --git a/application/hub/main/handler/chunks/class_ChunkHandler.php b/application/hub/main/handler/chunks/class_ChunkHandler.php index 033fa51af..f74cf99da 100644 --- a/application/hub/main/handler/chunks/class_ChunkHandler.php +++ b/application/hub/main/handler/chunks/class_ChunkHandler.php @@ -170,7 +170,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable */ private function isSerialNumberValid ($serialNumber) { // Check it - $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->bigintval($serialNumber, false) === $serialNumber)); + $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->hexval($serialNumber, false) === $serialNumber)); // Return result return $isValid; @@ -444,7 +444,7 @@ class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable // Is the serial number valid (chars 0-9, length equals PackageFragmenter::MAX_SERIAL_LENGTH)? if (!$this->isSerialNumberValid($chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL])) { // Do some logging - $this->debugOutput('CHUNK-HANDLER: Chunk serial number�for hash ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] . ' is invalid.'); + $this->debugOutput('CHUNK-HANDLER: Chunk serial number ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ' for hash ' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] . ' is invalid.'); // Re-request this chunk $this->rerequestChunkBySplitsArray($chunkSplits); diff --git a/application/hub/main/listener/class_BaseListener.php b/application/hub/main/listener/class_BaseListener.php index 4761d8324..61b231e86 100644 --- a/application/hub/main/listener/class_BaseListener.php +++ b/application/hub/main/listener/class_BaseListener.php @@ -242,6 +242,9 @@ class BaseListener extends BaseHubSystem implements Visitable { // Register the socket $registryInstance->registerSocket($this, $socketResource); + + // And set it here + $this->setSocketResource($socketResource); } /** @@ -261,22 +264,6 @@ class BaseListener extends BaseHubSystem implements Visitable { return $isRegistered; } - /** - * Getter for "this" socket resource - * - * @return $socketResource A valid socket resource - */ - public final function getSocketResource () { - // Get a socket registry instance (singleton) - $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); - - // Get the socket resource - $socketResource = $registryInstance->getRegisteredSocketResource($this); - - // Return it - return $socketResource; - } - /** * Accepts the visitor to process the visit "request" * diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index 731de7f9b..0ad6eccd6 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -230,7 +230,9 @@ class TcpListener extends BaseListener implements Listenable { * and wait ... */ $newSocket = socket_accept($this->getSocketResource()); - /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: newSocket=' . $newSocket); + + // Debug message + /* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: newSocket=' . $newSocket . ',server=' .$this->getSocketResource()); // Array for timeout settings $options = array( @@ -299,9 +301,9 @@ class TcpListener extends BaseListener implements Listenable { $currentSocket = $this->getIteratorInstance()->current(); // Handle it here, if not main socket + /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: currentSocket=' . $currentSocket . ',server=' . $this->getSocketResource()); if ($currentSocket != $this->getSocketResource()) { // ... or else it will raise warnings like 'Transport endpoint is not connected' - /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: currentSocket=' . $currentSocket . ',server=' . $this->getSocketResource()); $this->getHandlerInstance()->processRawDataFromResource($currentSocket); } // END - if diff --git a/application/hub/main/pools/peer/class_DefaultPeerPool.php b/application/hub/main/pools/peer/class_DefaultPeerPool.php index 67b02b89c..7d0a92cee 100644 --- a/application/hub/main/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/main/pools/peer/class_DefaultPeerPool.php @@ -153,14 +153,14 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { assert(count($recipientIpArray) == 2); // Debug message - /* NOISY-DEBUG: */ $this->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s), recipientIpArray[0]=' . $recipientIpArray[0] . ' ...'); + /* NOISY-DEBUG: */ $this->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),recipientIpArray[0]=' . $recipientIpArray[0] . ',recipientIpArray[1]=' . $recipientIpArray[1] . ' ...'); // Get all sockets and check them, skip the server socket foreach ($this->getAllSockets() as $socket) { // Is this a server socket? if ($socket === $this->getListenerInstance()->getSocketResource()) { // Skip 'server' sockets (local socket) - /* NOISY-DEBUG: */ $this->debugOutput('POOL: Skipping local socket ' . $socket . ' ...'); + /* NOISY-DEBUG: */ $this->debugOutput('POOL: Skipping server socket ' . $socket . ' ...'); continue; } // END - if @@ -170,6 +170,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $this->handleSocketError(__METHOD__, __LINE__, $socket, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); } // END - if + // Get // If the "peer" IP and recipient is same, use it if ($peerIp == $recipientIpArray[0]) { // IPs match, so take the socket and quit this loop diff --git a/application/hub/main/registry/socket/class_SocketRegistry.php b/application/hub/main/registry/socket/class_SocketRegistry.php index 5aa79d7e5..d3c530371 100644 --- a/application/hub/main/registry/socket/class_SocketRegistry.php +++ b/application/hub/main/registry/socket/class_SocketRegistry.php @@ -64,10 +64,27 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @return $key A string representation of the socket for the registry */ private function getSubRegistryKey (ProtocolHandler $protocolInstance) { + // Default address is invalid + $address = '*invalid*'; + + // Which instance is it? + // @TODO Tested again base class, rewrite it to a generic interface! + if ($protocolInstance instanceof BaseConnectionHelper) { + // Get address + $address = $protocolInstance->getAddress(); + } elseif ($protocolInstance instanceof BaseListener) { + // Get listen address + $address = $protocolInstance->getListenAddress(); + } else { + // Not supported! + $this->debugBackTrace('Unsupported protocolInstance=' . $protocolInstance->__toString() . ' detected. Please fix this!'); + } + // Get protocol and port number and add both together - $key = sprintf("%s:%s:%s", + $key = sprintf("%s:%s:%s:%s", $protocolInstance->__toString(), $protocolInstance->getProtocol(), + $address, $protocolInstance->getPort() ); @@ -96,12 +113,18 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @return $isRegistered Whether the protocol is registered */ private function isProtocolRegistered (ProtocolHandler $protocolInstance) { + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ' - ENTERED!'); + // Get the key $key = $this->getRegistryKeyFromProtocol($protocolInstance); // Determine it $isRegistered = $this->instanceExists($key); + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); + // Return result return $isRegistered; } @@ -115,6 +138,9 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @return $isRegistered Whether the given socket resource is registered */ public function isSocketRegistered (ProtocolHandler $protocolInstance, $socketResource) { + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!'); + // Default is not registered $isRegistered = false; @@ -123,16 +149,37 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke // That one is found so "get" a registry key from it $key = $this->getRegistryKeyFromProtocol($protocolInstance); + // Debug message + /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...'); + // Get the registry $registryInstance = $this->getInstance($key); // "Get" a key for the socket $socketKey = $this->getSubRegistryKey($protocolInstance); - // And simply ask it - $isRegistered = $registryInstance->instanceExists($socketKey); + // Debug message + /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...'); + + // Is it there? + if ($registryInstance->instanceExists($socketKey)) { + // Debug message + /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey)); + + // Get the instance + $registeredInstance = $registryInstance->getInstance($socketKey); + + // Is it SocketContainer and same socket? + $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource))); + + // Debug message + /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered)); + } // END - if } // END - if + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); + // Return the result return $isRegistered; } @@ -147,6 +194,9 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @return void */ public function registerSocket (ProtocolHandler $protocolInstance, $socketResource, array $packageData = array()) { + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!'); + // Is the socket already registered? if ($this->isSocketRegistered($protocolInstance, $socketResource)) { // Throw the exception @@ -172,7 +222,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke $socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $protocolInstance, $packageData)); // We have a sub-registry, the socket key and the socket, now we need to put all together - //* DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: socketKey=' . $socketKey . ',socketResource=' . $socketResource . ' - adding socket container instance ...'); + /* DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...'); $registryInstance->addInstance($socketKey, $socketInstance); } @@ -184,8 +234,11 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @throws NoSocketRegisteredException If the requested socket is not registered */ public function getRegisteredSocketResource (ProtocolHandler $protocolInstance) { + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ' - ENTERED!'); + // The socket must be registered before we can return it - if (!$this->isSocketRegistered($protocolInstance, false)) { + if (!$this->isProtocolRegistered($protocolInstance)) { // Throw the exception throw new NoSocketRegisteredException ($protocolInstance, self::SOCKET_NOT_REGISTERED); } // END - if @@ -202,6 +255,9 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke // And the final socket resource $socketResource = $registryInstance->getInstance($socketKey)->getSocketResource(); + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!'); + // Return the resource return $socketResource; }