From 6aa2f4aca03aac92f3edfa444a65da381b68bea6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 2 Nov 2020 16:34:20 +0100 Subject: [PATCH] Continued: - isIncomingSocketRegistered() now requires a Listenable instance as parameter - further fixes for receiving raw package data. It seem to be a race-condition? - updated core framework MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../socket/class_SocketContainer.php | 169 +++++++----------- .../socket/class_PackageSocketDiscovery.php | 33 ++-- .../info/class_ConnectionInfoFactory.php | 5 +- .../factories/socket/class_SocketFactory.php | 83 ++++----- .../handler/data/class_BaseDataHandler.php | 8 +- .../package/class_NetworkPackageHandler.php | 50 ++---- .../ipv4/class_BaseIpV4ProtocolHandler.php | 19 +- .../raw_data/tcp/class_TcpRawDataHandler.php | 24 +-- .../ipv4/tcp/class_TcpConnectionHelper.php | 2 +- .../info/connection/class_ConnectionInfo.php | 24 +-- .../classes/listener/class_BaseListener.php | 80 +++------ .../listener/class_BaseListenerDecorator.php | 14 +- .../pools/peer/class_DefaultPeerPool.php | 36 ++-- .../recipient/dht/class_DhtRecipient.php | 23 +-- .../registry/socket/class_SocketRegistry.php | 30 +++- .../class_RawDataPoolMonitorVisitor.php | 6 +- .../container/socket/class_StorableSocket.php | 8 - .../socket/class_RegisterableSocket.php | 3 +- core | 2 +- 19 files changed, 244 insertions(+), 375 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index bb84047b6..54f9008df 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -139,24 +139,24 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @param $socketResource A valid socket resource * @param $socketProtocol Socket protocol (TCP, UDP, file) * @param $packageInstance An instance of a DeliverablePackage class + * @param $socketType Socket type (incoming, outgoing, server, file, ...) * @return $socketInstance An instance of this Container class * @throws InvalidArgumentException If socket type is not set in $packageData array */ - public static final function createSocketContainer ($socketResource, $socketProtocol, DeliverablePackage $packageInstance) { + public static final function createSocketContainer ($socketResource, $socketProtocol, DeliverablePackage $packageInstance, $socketType) { // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource[%s]=%s,socketProtocol=%s,packageInstance[]=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol, gettype($packageInstance))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource[%s]=%s,socketProtocol=%s,packageInstance[]=%s,socketType=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol, gettype($packageInstance), $socketType)); //* DEBUG-PRINT: */ printf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)); // Get a new instance $socketInstance = new SocketContainer(); - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource=%s,socketProtocol=%s', $socketResource, $socketProtocol)); - // Set all values + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource=%s,socketProtocol=%s', $socketResource, $socketProtocol)); $socketInstance->setSocketProtocol($socketProtocol); $socketInstance->setSocketResource($socketResource); $socketInstance->setPackageDataInstance($packageInstance); + $socketInstance->setSocketType($socketType); // Return the prepared instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketInstance=%s - EXIT!', strtoupper($socketProtocol), $socketInstance->__toString())); @@ -189,22 +189,16 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $matches Whether $address matches with the one from package data */ public function ifAddressMatches ($unl) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unl=%s - CALLED!', strtoupper($this->getSocketProtocol()), $unl)); - // Get current package data + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unl=%s - CALLED!', strtoupper($this->getSocketProtocol()), $unl)); $packageInstance = $this->getPackageDataInstance(); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: unl=' . $unl . ',packageInstance=' . print_r($packageInstance, true)); - // So, does both match? + //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: unl=' . $unl . ',packageInstance=' . print_r($packageInstance, true)); $matches = (($packageInstance->getRecipientUnl() !== '') && ($packageInstance->getRecipientUnl() === $unl)); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); - // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); return $matches; } @@ -215,16 +209,12 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $matches Whether given socket matches */ public function ifSocketResourceMatches ($socketResource) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource()); - // So, does both match? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource()); $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource())); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); - // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); return $matches; } @@ -344,10 +334,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If socket is invalid */ public function getSocketRecipient () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); - // Should be valid socket + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -356,10 +344,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Get package data $packageInstance = $this->getPackageDataInstance(); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance.recipient=%s - EXIT!', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipientUnl())); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance.recipient=%s - EXIT!', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipientUnl())); return $packageInstance->getRecipientUnl(); } @@ -371,7 +357,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function getSocketRecipientAddress () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Should be valid socket if (!$this->isValidSocket()) { @@ -403,7 +389,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function getSocketRecipientPort () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Should be valid socket if (!$this->isValidSocket()) { @@ -435,7 +421,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function isValidSocket () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Get socket resource $socketResource = $this->getSocketResource(); @@ -459,7 +445,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function getLastSocketErrorCode () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Should be valid socket if (!$this->isValidSocket()) { @@ -488,7 +474,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function getLastSocketErrorMessage () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Should be valid socket if (!$this->isValidSocket()) { @@ -513,7 +499,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function translateLastSocketErrorCodeToName () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Get last error code $errorCode = $this->getLastSocketErrorCode(); @@ -539,7 +525,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function bindSocketToFile () { // Should be valid socket - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -568,7 +554,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function bindSocketToListener () { // Should be valid socket - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -598,7 +584,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function listenToSocket () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Should be valid socket if (!$this->isValidSocket()) { @@ -624,7 +610,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function enableSocketNonBlocking () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Should be valid socket if (!$this->isValidSocket()) { @@ -650,7 +636,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function enableSocketReuseAddress () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Should be valid socket if (!$this->isValidSocket()) { @@ -675,10 +661,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If stored socket is invalid */ public function connectToSocketRecipient () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); - // Should be valid socket + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -687,22 +671,16 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Get recipient UNL $unlRecipient = $this->getSocketRecipient(); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlRecipient=%s', strtoupper($this->getSocketProtocol()), $unlRecipient)); - // Create UNL instance from it. This will validate the connection + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlRecipient=%s', strtoupper($this->getSocketProtocol()), $unlRecipient)); $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($unlRecipient); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlAddress=%s,unlPort=%d', strtoupper($this->getSocketProtocol()), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort())); - // Try to connect to it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unlAddress=%s,unlPort=%d', strtoupper($this->getSocketProtocol()), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort())); $result = socket_connect($this->getSocketResource(), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort()); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); - // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); return $result; } @@ -716,10 +694,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @todo rewrite this! */ public function doShutdown () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); - // Should be valid socket + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new BadMethodCallException(sprintf('[%s:%d]: Shutdown on invalid socket. Maybe called already?', __METHOD__, __LINE__), self::EXCEPTION_INVALID_SOCKET); @@ -755,7 +731,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->markConnectionShuttedDown(); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: EXIT!'); } /** @@ -785,27 +761,23 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If stored socket is invalid */ public function acceptNewIncomingSocket () { - // Trace message - //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); - // Should be valid socket + /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); } // END - if // Debug message - //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // 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()); $writers = array(); $excepts = array(); - // Trace message - //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling socket_select(%d,%d,%d,0%d) ...', strtoupper($this->getSocketProtocol()), count($readers), count($writers), count($excepts), $this->socketSelectTimeout)); - // Check if we have some peers left + /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling socket_select(%d,%d,%d,0%d) ...', strtoupper($this->getSocketProtocol()), count($readers), count($writers), count($excepts), $this->socketSelectTimeout)); $left = socket_select( $readers, $writers, @@ -815,10 +787,10 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita ); // Some new peers found? - //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: left=%d,readers()=%d,writers()=%d,except()=%d) ...', strtoupper($this->getSocketProtocol()), $left, count($readers), count($writers), count($excepts))); + /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: left=%d,readers()=%d,writers()=%d,except()=%d) ...', strtoupper($this->getSocketProtocol()), $left, count($readers), count($writers), count($excepts))); if ($left < 1) { // Nothing new found - //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: left=%d,serverSocket=%s,readers()=%d', strtoupper($this->getSocketProtocol()), $left, $this->getSocketResource(), count($readers))); + /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: left=%d,serverSocket=%s,readers()=%d', strtoupper($this->getSocketProtocol()), $left, $this->getSocketResource(), count($readers))); return NULL; } elseif (!in_array($this->getSocketResource(), $readers)) { // Abort here @@ -829,7 +801,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* * Then accept it, if this socket is set to non-blocking IO and the * connection is NOT sending any data, socket_read() may throw - * error 11 (Resource temporary unavailable). This really nasty + * error 11 (Resource temporary unavailable). This is really nasty * because if you have blocking IO socket_read() will wait and wait * and wait ... */ @@ -851,29 +823,23 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If the stored socket is not valid */ public function readDataFromSocket () { - // Trace message - /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); - // Should be valid socket + /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); } // END - if - // Debug message - /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - /* * Read raw data from socket. If you change PHP_BINARY_READ to * 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); - // Trace message - /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: rawData[%s]()=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($rawData), strlen($rawData))); - // Return it + /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: rawData[%s]()=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($rawData), strlen($rawData))); return $rawData; } @@ -885,6 +851,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function identifySocketPeer () { // Init peer address (IP)/port + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $peerAddress = '0.0.0.0'; $peerPort = '0'; @@ -892,16 +859,15 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = $this->getSocketPeerName($peerAddress, $peerPort); // Valid? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d,peerAddress[%s]=%s,peerPort[%s]=%d', strtoupper($this->getSocketProtocol()), intval($result), gettype($peerAddress), $peerAddress, gettype($peerPort), $peerPort)); if ($result === TRUE) { // Set both $this->setPeerAddress($peerAddress); $this->setPeerPort($peerPort); } // END - if - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); - // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); return $result; } @@ -912,7 +878,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ public function setSocketTimeoutOptions () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* 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( @@ -938,16 +904,14 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $result Whether OOB has been enabled */ public function enableSocketOutOfBandData () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); - // Call inner method + /* 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 - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); return $result; } @@ -959,10 +923,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws BadMethodCallException If no socket error was reported but method called */ public function clearLastSocketError () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!'); - // Should be valid socket + /* 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); @@ -975,7 +937,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!'); } /** @@ -1195,7 +1157,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorPermissionDenied () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Get socket error code for verification $socketError = $this->getLastSocketErrorCode(); @@ -1219,7 +1181,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorConnectionTimedOut () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Get socket error code for verification $socketError = $this->getLastSocketErrorCode(); @@ -1243,7 +1205,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorResourceUnavailable () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Get socket error code for verification $socketError = $this->getLastSocketErrorCode(); @@ -1267,7 +1229,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorConnectionRefused () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Get socket error code for verification $socketError = $this->getLastSocketErrorCode(); @@ -1291,7 +1253,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorNoRouteToHost () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Get socket error code for verification $socketError = $this->getLastSocketErrorCode(); @@ -1338,7 +1300,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ protected function handleSocketErrorConnectionResetByPeer () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); // Get socket error code for verification $socketError = $this->getLastSocketErrorCode(); @@ -1361,10 +1323,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws SocketOperationException The connection attempts fails with a time-out */ protected function handleSocketErrorOperationNotSupported () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); - // Get socket error code for verification + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketError = $this->getLastSocketErrorCode(); // Get error message @@ -1384,9 +1344,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return void */ protected function handleSocketErrorOperationInProgress () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); - + // Very common with non-blocking I/O + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.'); } @@ -1402,15 +1361,14 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $errorName The translated name (all lower-case, with underlines) */ private function translateSocketErrorCodeToName ($errorCode) { - // Trace message + // Unknown error code by default //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode)); - - // Nothing bad happened by default - $errorName = StorableSocket::SOCKET_CONNECTED; + $errorName = StorableSocket::SOCKET_ERROR_UNKNOWN; // Is the code a number, then we have to change it switch ($errorCode) { - case 0: // Silently ignored, the socket is connected + case 0: // No error, the socket is connected + $errorName = StorableSocket::SOCKET_CONNECTED; break; case 11: // "Resource temporary unavailable" @@ -1462,16 +1420,11 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita default: // Everything else <> 0 // Unhandled error code detected, so first debug it because we may want to handle it like the others self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf(strtoupper($this->getSocketProtocol()) . '-SOCKET: Unsupported errorCode=%d,message=%s', $errorCode, socket_strerror($errorCode))); - - // Change it only in this class - $errorName = StorableSocket::SOCKET_ERROR_UNKNOWN; break; } // END - switch - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $errorName)); - // Return translated name + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $errorName)); return $errorName; } @@ -1545,7 +1498,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @param $socketType Socket type * @return void */ - public function setSocketType ($socketType) { + protected function setSocketType ($socketType) { $this->socketType = $socketType; } diff --git a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php index 172c201b2..fc0a24148 100644 --- a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php +++ b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php @@ -102,7 +102,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera // Debugging //* DEBUG-DIE: */ die(__METHOD__.':poolInstance='.print_r($poolInstance, TRUE)); - //* DEBUG-DIE: */ die(__METHOD__.':poolInstance.arrayFromList('.$protocolName.')='.print_r($poolInstance->getArrayFromList($protocolName), TRUE)); + //* DEBUG-DIE: */ die(__METHOD__.':poolInstance->arrayFromList('.$protocolName.')='.print_r($poolInstance->getArrayFromList($protocolName), TRUE)); /* * Now we need to choose again. It is whether we are speaking with a hub @@ -149,11 +149,9 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera * @throws LogicException If the discovered listener instance has no pool set */ public function discoverSocket (DeliverablePackage $packageInstance, $connectionType) { - // Debug message + // Make sure all parameters are valid /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISOVERY: packageInstance=%s,connectionType=%s - CALLED!', $packageInstance->__toString(), $connectionType)); //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISOVERY: packageInstance=' . print_r($packageInstance, TRUE)); - - // Make sure all parameters are valid if (($connectionType != StorableSocket::CONNECTION_TYPE_INCOMING) && ($connectionType != StorableSocket::CONNECTION_TYPE_OUTGOING)) { // Abort here throw new InvalidArgumentException(sprintf('connectionType=%s is whether "%s" nor "%s".', @@ -161,30 +159,26 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera StorableSocket::CONNECTION_TYPE_INCOMING, StorableSocket::CONNECTION_TYPE_OUTGOING )); - } // END - if + } // Determine protocol instance $protocolInstance = ProtocolDiscovery::determineProtocolByPackageInstance($packageInstance); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('protocolInstance=' . $protocolInstance . ',packageInstance=' . print_r($packageInstance, TRUE)); - // Is it valid? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('protocolInstance=' . $protocolInstance . ',packageInstance=' . print_r($packageInstance, TRUE)); assert($protocolInstance instanceof HandleableProtocol); // Does the UNL validate? if (!$protocolInstance->isValidUniversalNodeLocatorByPackageInstance($packageInstance)) { // Not valid, throw exception throw new InvalidUnlException(array($this, $protocolInstance, $packageInstance), BaseHubSystem::EXCEPTION_INVALID_UNL); - } // END - if + } // Get the listener instance $listenerInstance = $this->discoverListenerInstance($protocolInstance, $packageInstance); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: listenerInstance=' . $listenerInstance . ',packageInstance=' . print_r($packageInstance, TRUE)); - // If there is no listener who wants to have that package, we simply drop it here + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: listenerInstance=' . $listenerInstance . ',packageInstance=' . print_r($packageInstance, TRUE)); if (is_null($listenerInstance)) { // Abort with no resource throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); @@ -201,23 +195,19 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera */ $socketInstance = $listenerInstance->getPoolInstance()->getSocketFromPackageInstance($packageInstance, $connectionType); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance[]=%s', gettype($socketInstance))); - // Is it FALSE, the recipient isn't known to us and we have no connection to it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance[]=%s', gettype($socketInstance))); if (!($socketInstance instanceof StorableSocket) || ($socketInstance->isValidSocket()) || ($socketInstance->getLastSocketErrorCode() > 0)) { // Try to create a new socket resource try { - // Possibly noisy debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Trying to establish a ' . strtoupper($listenerInstance->getProtocolName()) . ' connection to ' . $packageInstance->getRecipientUnl() . ' ...'); - // Get a socket resource from our factory (if succeeded) + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: Trying to establish a %s connection to %s ...', strtoupper($listenerInstance->getProtocolName()), $packageInstance->getRecipientUnl())); $socketInstance = SocketFactory::createSocketFromPackageInstance($packageInstance, $protocolInstance); } catch (SocketConnectionException $e) { // The connection fails of being established, so log it away - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Caught ' . $e->__toString() . ',message=' . $e->getMessage()); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: Caught %s,message=%s', $e->__toString(), $e->getMessage())); } - } // END - if + } // Try to resolve the socket resource try { @@ -225,6 +215,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera $helperInstance = GenericRegistry::getRegistry()->getInstance('connection'); // Is a state set and it's name is other than socket's error code? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: helperInstance->stateInstance[]=%s', gettype($helperInstance->getStateInstance()))); if ($helperInstance->getStateInstance() instanceof PeerStateable && $helperInstance->getPrintableState() !== $socketInstance->translateLastSocketErrorCodeToName()) { // Resolve the peer's state (but ignore return value) /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Going to resolve socket from peer state and given package data ...'); @@ -239,7 +230,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera } // And return it - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance=%s - EXIT!', $socketInstance->__toString())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance=%s,socketInstance->socketResource=%s - EXIT!', $socketInstance->__toString(), $socketInstance->getSocketResource())); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: socketResource=' . $socketInstance->getSocketResource() . ',packageInstance=' . print_r($packageInstance, TRUE)); return $socketInstance; } diff --git a/application/hub/classes/factories/info/class_ConnectionInfoFactory.php b/application/hub/classes/factories/info/class_ConnectionInfoFactory.php index b09960fba..719459c2e 100644 --- a/application/hub/classes/factories/info/class_ConnectionInfoFactory.php +++ b/application/hub/classes/factories/info/class_ConnectionInfoFactory.php @@ -67,7 +67,7 @@ class ConnectionInfoFactory extends ObjectFactory { * @todo Also validate protocol to be sure if there is really a protocol handler for it available */ public static final function createConnectionInfoInstance ($protocolName, $type) { - // Init instance + // Init factory instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: protocolName=%s,type=%s - CALLED!', $protocolName, $type)); $factoryInstance = new ConnectionInfoFactory(); @@ -84,12 +84,15 @@ class ConnectionInfoFactory extends ObjectFactory { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: key=%s', $key)); if (GenericRegistry::getRegistry()->instanceExists($key)) { // Get info from registry + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Getting instance for key=%s ...', $key)); $infoInstance = GenericRegistry::getRegistry()->getInstance($key); } else { // Get the info instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Creating info instance for type=%s ...', $type)); $infoInstance = self::createObjectByConfiguredName('connection_info_class', array($type)); // Add it to the registry + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Adding key=%s,infoInstance=%s ...', $key, $infoInstance->__toString())); GenericRegistry::getRegistry()->addInstance($key, $infoInstance); } diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index a26fe0c69..5e5281c93 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -63,6 +63,7 @@ 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())); $socketInstance = NULL; // Construct registry key @@ -71,37 +72,31 @@ class SocketFactory extends ObjectFactory { $packageInstance->getRecipientType() ); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Trying to find a socket with registryKey=' . $registryKey); - // Is the key there? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: Trying to find a socket with registryKey=%s', $registryKey)); if (GenericRegistry::getRegistry()->instanceExists($registryKey)) { // Get container instance $socketInstance = GenericRegistry::getRegistry()->getInstance($registryKey); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Using socket ' . $socketInstance->getSocketResource() . '(' . gettype($socketInstance->getSocketResource()) . ') from registry.'); + /* 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'); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Going to use class ' . $className . ' for creating a socket resource ...'); - // 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); // Register it with the registry GenericRegistry::getRegistry()->addInstance($registryKey, $socketInstance); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Socket is now registered in registry.'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Socket is now registered in registry.'); } - // Trace message - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); - // Return the socket (container) instance + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } @@ -136,16 +131,14 @@ class SocketFactory extends ObjectFactory { $socketResource, StorableSocket::SOCKET_PROTOCOL_FILE, $packageInstance, + StorableSocket::CONNECTION_TYPE_SERVER, )); - // Set file socket data + // Set socket file $socketInstance->setSocketFile($socketFile); - $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance))); // Is the socket resource valid? + //* 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); @@ -153,7 +146,7 @@ class SocketFactory extends ObjectFactory { // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -181,14 +174,14 @@ class SocketFactory extends ObjectFactory { // Start listen for connections self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); if (!$socketInstance->listenToSocket()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Allow non-blocking I/O self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -223,11 +216,9 @@ class SocketFactory extends ObjectFactory { $socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageInstance, + StorableSocket::CONNECTION_TYPE_OUTGOING, )); - // Set socket type to outgoing - $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_OUTGOING); - // Is the socket resource valid? if (!$socketInstance->isValidSocket()) { /* @@ -240,14 +231,14 @@ class SocketFactory extends ObjectFactory { // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $helperInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Set the option to reuse the port //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (!$socketInstance->enableSocketReuseAddress()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -258,7 +249,7 @@ class SocketFactory extends ObjectFactory { */ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -291,11 +282,11 @@ class SocketFactory extends ObjectFactory { $socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageInstance, + StorableSocket::CONNECTION_TYPE_SERVER, )); - // Set listener instance and type + // Set listener instance $socketInstance->setListenerInstance($listenerInstance); - $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER); // Is the socket resource valid? if (!$socketInstance->isValidSocket()) { @@ -305,14 +296,14 @@ class SocketFactory extends ObjectFactory { // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Set the option to reuse the port self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (!$socketInstance->enableSocketReuseAddress()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -323,21 +314,21 @@ class SocketFactory extends ObjectFactory { */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort()); if (!$socketInstance->bindSocketToListener()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Start listen for connections self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.'); if (!$socketInstance->listenToSocket()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Now, we want non-blocking mode self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -370,11 +361,11 @@ class SocketFactory extends ObjectFactory { $socketResource, StorableSocket::SOCKET_PROTOCOL_UDP, $packageInstance, + StorableSocket::CONNECTION_TYPE_SERVER, )); - // Set listener instance and socket type + // Set listener instance $socketInstance->setListenerInstance($listenerInstance); - $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER); // Is the socket resource valid? if (!$socketInstance->isValidSocket()) { @@ -384,14 +375,14 @@ class SocketFactory extends ObjectFactory { // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Set the option to reuse the port self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...'); if (!$socketInstance->enableSocketReuseAddress()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -402,14 +393,14 @@ class SocketFactory extends ObjectFactory { */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort()); if (!$socketInstance->bindSocketToListener()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Now, we want non-blocking mode self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.'); if (!$socketInstance->enableSocketNonBlocking()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -432,7 +423,7 @@ class SocketFactory extends ObjectFactory { */ public static final function createNextAcceptedSocketFromPool (Poolable $poolInstance, StorableSocket $socketInstance) { // Trace message - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: poolInstance=%s,socketInstance.socketResource=%s - CALLED!', $poolInstance->__toString(), $socketInstance->getSocketResource())); + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: poolInstance=%s,socketInstance->socketResource=%s - CALLED!', $poolInstance->__toString(), $socketInstance->getSocketResource())); // Get socket protocol $socketProtocol = $socketInstance->getSocketProtocol(); @@ -498,10 +489,10 @@ class SocketFactory extends ObjectFactory { $socketResource, $socketProtocol, $packageInstance, + StorableSocket::CONNECTION_TYPE_INCOMING, )); // Set all socket data - $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_INCOMING); $socketInstance->setPeerAddress('invalid'); $socketInstance->setPeerPort('0'); @@ -513,32 +504,30 @@ class SocketFactory extends ObjectFactory { // Try to identify socket peer if (!$socketInstance->identifySocketPeer()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Set timeout to configured seconds if (!$socketInstance->setSocketTimeoutOptions()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Enable SO_OOBINLINE if (!$socketInstance->enableSocketOutOfBandData()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if // Set non-blocking if (!$socketInstance->enableSocketNonBlocking()) { - // Handle this socket error with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if - // Trace message - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); - // Return found socket instance + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString())); return $socketInstance; } } diff --git a/application/hub/classes/handler/data/class_BaseDataHandler.php b/application/hub/classes/handler/data/class_BaseDataHandler.php index 48e04bc1c..4d4918424 100644 --- a/application/hub/classes/handler/data/class_BaseDataHandler.php +++ b/application/hub/classes/handler/data/class_BaseDataHandler.php @@ -109,11 +109,9 @@ abstract class BaseDataHandler extends BaseHubHandler implements HubInterface { * @return void */ protected function prepareAnswerMessage (DeliverableMessage $messageInstance, Deliverable $packageInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER:Going to send an answer message for ' . $this->getHandlerName() . ' ...'); - // Get a helper instance based on this handler's name - $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageInstance)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER: Going to send an answer message for ' . $this->getHandlerName() . ' ...'); + $helperInstance = ObjectFactory::createObjectByConfiguredName(sprintf('node_answer_%s_helper_class', $this->getHandlerName()), array($messageInstance)); // Get node instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER: Creating node instance ...'); @@ -141,7 +139,7 @@ abstract class BaseDataHandler extends BaseHubHandler implements HubInterface { $this->removeMessageConfigurationData($messageInstance); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER:Answer message has been prepared.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DATA-HANDLER: Answer message has been prepared.'); } /** diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index b5dcc5c14..c21780d31 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -405,53 +405,40 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @param $packageInstance An instance of a DeliverablePackage class * @return $hash Private key's hash * @throws InvalidPrivateKeyHashException If the private key's hash is not valid - * @throws InvalidArgumentException If $packageInstance does not contain senderId/senderAddress + * @throws InvalidArgumentException If $packageInstance does not contain senderId */ private function determineSenderPrivateKeyHash (DeliverablePackage $packageInstance) { + // Is the parameter valid? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); + if ($packageInstance->getSenderId() == '') { + // Invalid $packageInstance + throw new InvalidArgumentException('packageInstance does not contain senderId'); + } + // Get DHT instance $dhtInstance = DhtObjectFactory::createDhtInstance('node'); - // Init sender data array - $senderData = array(); - // Ask DHT for sender's id or address (both session id) - if ($packageInstance->getSenderId() != '') { - // Field senderId is given - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderId=%s ...', $packageInstance->getSenderId())); - $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderId()); - } elseif ($packageInstance->getSenderAddress() != '') { - // Field senderAddress is given - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderAddress=%s ...', $packageInstance->getSenderAddress())); - $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderAddress()); - } else { - // Invalid $packageInstance - throw new InvalidArgumentException('packageInstance does not contain senderId/senderAddress'); - } + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderId=%s ...', $packageInstance->getSenderId())); + $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderId()); // Is an entry found? + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: senderData()=%d', count($senderData))); //* DEBUG-DIE: */ die(sprintf('[%s:%d]: senderData=%s', __METHOD__, __LINE__, print_r($senderData, TRUE))); - if (count($senderData) > 0) { - // Make sure the element 'private_key_hash' is there - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: senderData=' . print_r($senderData, TRUE)); - assert(isset($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])); - - // Don't accept empty hashes - if (empty($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])) { - // Don't accept - throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in senderData'), BaseHubSystem::EXCEPTION_INVALID_PRIVATE_KEY_HASH); - } // END - if - + if (count($senderData) > 0 || !empty($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])) { // Return it + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Returning senderData[%s]=%s', NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH, $senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])); return $senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH]; - } // END - if + } // Don't accept empty keys if (empty($packageInstance->getSenderPrivateKeyHash())) { // This needs fixing throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in decodedData'), BaseHubSystem::EXCEPTION_INVALID_PRIVATE_KEY_HASH); - } // END - if + } // There is no DHT entry so, accept the hash from decoded data + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Returning packagheInstance->senderPrivateKeyHash=%s - EXIT!', $packageInstance->getSenderPrivateKeyHash())); return $packageInstance->getSenderPrivateKeyHash(); } @@ -849,8 +836,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class'); // Set all data - $packageInstance->setSenderAddress($this->getNodeInstance()->getSessionId()); - $packageInstance->setSenderPort('0'); + $packageInstance->setSenderId($this->getNodeInstance()->getSessionId()); $packageInstance->setRecipientId($helperInstance->getRecipientId()); $packageInstance->setRecipientType($helperInstance->getRecipientType()); /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Setting packageContent=%s ...', $packageContent)); @@ -1475,7 +1461,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei if (!$this->isChecksumValid($packageInstance)) { // Is not the same, so throw an exception here throw new InvalidDataChecksumException(array($this, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM); - } // END - if + } /* * The checksum is the same, then it can be decompressed safely. The diff --git a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php index d46513216..62c65a34e 100644 --- a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php +++ b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php @@ -9,6 +9,9 @@ use Org\Shipsimu\Hub\Locator\Node\LocateableNode; use Org\Shipsimu\Hub\Locator\Node\Tools\NodeLocatorUtils; use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; +// Import SPL stuff +use \BadMethodCallException; + /** * A general handler for IPv4 protocols such as TCP, UDP and others. * @@ -75,21 +78,21 @@ abstract class BaseIpV4ProtocolHandler extends BaseProtocolHandler { * * @param $packageInstance Valid raw package data * @return $isValid Whether the UNL can be validated + * @throws BadMethodCallException If protocol of package's recipient UNL is not matching handler's name */ public function isValidUniversalNodeLocatorByPackageInstance (DeliverablePackage $packageInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: CALLED!'); - // Is the correct handler choosen? - assert(substr($packageInstance->getRecipientUnl(), 0, strlen($this->getHandlerName())) != $this->getHandlerName()); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('IPV4-PROTOCOL-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); + if (substr($packageInstance->getRecipientUnl(), 0, strlen($this->getProtocolName())) != $this->getProtocolName()) { + // Not matching, throw exception + throw new BadMethodCallException(sprintf('packageInstance->recipientUnl=%s cannot be handled by this protocol handler (%s)', $packageInstance->getRecipientUnl(), $this->getProtocolName())); + } // Default is from generic validation $isValid = NodeLocatorUtils::isValidUniversalNodeLocator($packageInstance->getRecipientUnl()); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: packageInstance.recipient=' . $packageInstance->getRecipientUnl() . ',isValid[' . gettype($isValid) . ']=' . intval($isValid)); - // If this doesn't fail, continue validating the IP:port combination + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('IPV4-PROTOCOL-HANDLER: packageInstance->recipientUnl=%s,isValid[%s]=%d', $packageInstance->getRecipientUnl(), gettype($isValid), intval($isValid))); if ($isValid === TRUE) { // ... and validate IP:port, first "parse" the UNL $locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl()); @@ -103,7 +106,7 @@ abstract class BaseIpV4ProtocolHandler extends BaseProtocolHandler { } // END - if // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: isValid=' . intval($isValid) . ' - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('IPV4-PROTOCOL-HANDLER: isValid=%d - EXIT!', intval($isValid))); return $isValid; } diff --git a/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php b/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php index ba38d5d01..2c84eeb74 100644 --- a/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php +++ b/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php @@ -76,24 +76,19 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { * @return void */ public function processRawDataFromSocketInstance (StorableSocket $socketInstance) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: socketInstance=%s - CALLED!', $socketInstance->__toString())); - // Reset error code to unhandled + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: socketInstance=%s - CALLED!', $socketInstance->__toString())); $this->setErrorCode(StorableSocket::SOCKET_ERROR_UNHANDLED); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: Handling TCP package from resource=' . $socketInstance->getSocketResource() . ',last error=' . $socketInstance->getLastSocketErrorMessage()); - // Read the raw data from socket in a loop + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: Handling TCP package from resource=%s,last error=%s,socketType=%s', $socketInstance->getSocketResource(), $socketInstance->getLastSocketErrorMessage(), $socketInstance->getSocketType())); while ($rawData = $socketInstance->readDataFromSocket()) { // Get socket error code back + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: rawData(%d)[]=%s', strlen($rawData), gettype($rawData))); $this->lastSocketError = $socketInstance->getLastSocketErrorCode(); - // Debug output of read data length - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketInstance . ',this->lastSocketError=' . socket_strerror($this->lastSocketError)); - // Is not FALSE? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketInstance . ',this->lastSocketError=' . socket_strerror($this->lastSocketError)); if ($rawData !== FALSE) { // Output raw data /* NOISY-DEBUG: */self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: rawData=' . $rawData); @@ -101,14 +96,12 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { // Is it valid? if ($this->lastSocketError == 11) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketInstance); - /* * Error code 11 (Resource temporary unavailable) can be safely * ignored on non-blocking sockets. The socket is currently not * sending any data. */ + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketInstance); $socketInstance->clearLastSocketError(); // Skip any further processing @@ -118,10 +111,8 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { $this->setErrorCode($this->lastSocketError); break; } elseif (empty($rawData)) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: No raw data pending. - EXIT!'); - // The peer did send nothing to us which is now being ignored + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: No raw data pending. - EXIT!'); continue; } else { /* @@ -130,14 +121,13 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { * well-formed BASE64-encoded message with start and markers. This * will be checked later on. */ - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: Adding ' . strlen($rawData) . ' bytes to stacker ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-RAW-DATA-HANDLER: Adding %d bytes to stacker ...', strlen($rawData))); $this->addRawDataToStacker($rawData); } } // END - while // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-RAW-DATA-HANDLER: EXIT!'); - } } 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 ed58f054e..1a764f509 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -123,7 +123,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Now connect to it if (!$helperInstance->connectToPeerBySocketRecipient()) { // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: helperInstance=%s,locatorInstance.unlData=%s', $helperInstance->__toString(), print_r($locatorInstance->getUnlData(), TRUE))); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: helperInstance=%s,locatorInstance->unlData=%s', $helperInstance->__toString(), print_r($locatorInstance->getUnlData(), TRUE))); // Handle socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); diff --git a/application/hub/classes/info/connection/class_ConnectionInfo.php b/application/hub/classes/info/connection/class_ConnectionInfo.php index 917c28222..2952f829c 100644 --- a/application/hub/classes/info/connection/class_ConnectionInfo.php +++ b/application/hub/classes/info/connection/class_ConnectionInfo.php @@ -60,12 +60,14 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { */ public final static function createConnectionInfo ($connectionType) { // Get new instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: connectionType=%s - CALLED!', $connectionType)); $infoInstance = new ConnectionInfo(); // Set connection type here $infoInstance->setConnectionType($connectionType); // Return the prepared instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: infoInstance=%s - EXIT!', $infoInstance->__toString())); return $infoInstance; } @@ -95,10 +97,8 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { * @return void */ public function fillWithListenerInformation (Listenable $listenerInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: protocolName=' . $listenerInstance->getProtocolName() . ',listenerInstance=' . $listenerInstance->__toString() . ' - CALLED!'); - // Fill the generic array with several data from the listener: + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: protocolName=' . $listenerInstance->getProtocolName() . ',listenerInstance=' . $listenerInstance->__toString() . ' - CALLED!'); $this->setProtocolName($listenerInstance->getProtocolName()); $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $listenerInstance->getListenAddress()); $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $listenerInstance->getListenPort()); @@ -106,7 +106,7 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { // Set listener here $this->setListenerInstance($listenerInstance); - // Debug message + // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: EXIT!'); } @@ -117,10 +117,8 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { * @return void */ public function fillWithConnectionHelperInformation (ConnectionHelper $helperInstance) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: protocolName=' . $helperInstance->getProtocolName() . ',helperInstance=' . $helperInstance->__toString() . ',socketResource=' . $helperInstance->getSocketResource() . ' - CALLED!'); - // Fill the generic array with several data from the listener: + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: protocolName=' . $helperInstance->getProtocolName() . ',helperInstance=' . $helperInstance->__toString() . ',socketResource=' . $helperInstance->getSocketResource() . ' - CALLED!'); $this->setProtocolName($helperInstance->getSocketInstance()->getSocketProtocol()); $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getSocketInstance()->getSocketRecipientAddress()); $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $helperInstance->getSocketInstance()->getSocketRecipientPort()); @@ -140,10 +138,8 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { * @throws InvalidSocketException If no peer data can be retrived from socket */ public function fillWithSocketPeerInformation (StorableSocket $socketInstance) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketInstance.socketResource=%s - CALLED!', $socketInstance->getSocketResource())); - // Init variables + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource())); $socketAddress = '0.0.0.0'; $socketPort = '0'; @@ -153,19 +149,15 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketAddress=%s,socketPort=%d', $socketAddress, $socketPort)); - // Fill the generic array with several data from the listener: + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketAddress=%s,socketPort=%d', $socketAddress, $socketPort)); $this->setProtocolName($socketInstance->getSocketProtocol()); $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $socketAddress); $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $socketPort); // Set socket here - $this->setSocketInstance($socketInstance); - - // Debugging: //* DEBUG-DIE: */ die(sprintf('[%s:%d]: socketInstance=%s', __METHOD__, __LINE__, print_r($socketInstance, TRUE))); + $this->setSocketInstance($socketInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-INFO: EXIT!'); diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index 33118d2b9..52d92a55d 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -180,10 +180,8 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { * @throws LogicException If no info instance can be created */ protected function registerServerSocketInstance (StorableSocket $socketInstance) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $socketInstance->__toString())); - // First check if it is valid + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $socketInstance->__toString())); if ($this->isServerSocketRegistered($socketInstance)) { // Already registered throw new SocketAlreadyRegisteredException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_SOCKET_ALREADY_REGISTERED); @@ -202,16 +200,15 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { $infoInstance->fillWithListenerInformation($this); // Register the socket + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Registering socketInstance->socketResource=%s,socketType=%s', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource(), $socketInstance->getSocketType())); $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Setting socketInstance ... (socketResource=%s)', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); - // And set it here + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Setting socketInstance ... (socketResource=%s)', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); $this->setSocketInstance($socketInstance); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName()))); } /** @@ -275,51 +272,41 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { * @return void */ public function monitorIncomingRawData () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: CALLED!', strtoupper($this->getProtocolName()))); - // Check if an incoming socket is registered - if (!$this->getRegistryInstance()->isIncomingSocketRegistered()) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: No socket of type "%s" registered. - EXIT!', strtoupper($this->getProtocolName()), StorableSocket::CONNECTION_TYPE_INCOMING)); - + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: CALLED!', strtoupper($this->getProtocolName()))); + if (!$this->getRegistryInstance()->isIncomingSocketRegistered($this)) { // Skip further processing + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: No socket of type "%s" registered. - EXIT!', strtoupper($this->getProtocolName()), StorableSocket::CONNECTION_TYPE_INCOMING)); return; - } // END - if - - // Debugging: - //* DEBUG-DIE: */ $this->getRegistryInstance()->debugInstance(); + } // Get iterator from registry instance - $iteratorInstance = $this->getRegistryInstance()->getIterator(); - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: iteratorInstance[]=%s', strtoupper($this->getProtocolName()), gettype($iteratorInstance))); + //* DEBUG-DIE: */ $this->getRegistryInstance()->debugInstance(); + $iteratorInstance = $this->getRegistryInstance()->getIterator([$this->getProtocolName()]); // Should be valid instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: iteratorInstance[]=%s', strtoupper($this->getProtocolName()), gettype($iteratorInstance))); if (is_null($iteratorInstance)) { // Throw NPE throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } // END - if // Is it valid? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: iteratorInstance->valid()=%d', strtoupper($this->getProtocolName()), intval($iteratorInstance->valid()))); if (!$iteratorInstance->valid()) { // Then rewind it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Rewinding iterator ...', strtoupper($this->getProtocolName()))); $iteratorInstance->rewind(); } // END - if // Get current instance $socketInstance = $iteratorInstance->current(); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s', strtoupper($this->getProtocolName()), gettype($socketInstance))); - // Is NULL returned? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s', strtoupper($this->getProtocolName()), gettype($socketInstance))); if (is_null($socketInstance)) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=NULL - EXIT!', strtoupper($this->getProtocolName()))); - // Then abort here + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=NULL - EXIT!', strtoupper($this->getProtocolName()))); return; } // END - if @@ -327,7 +314,7 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { $iteratorInstance->next(); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName()))); } /** @@ -338,38 +325,30 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { * @throws LogicException If no info instance can be created */ protected function doListenSocketSelect ($peerSuffix) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: peerSuffix=%s - CALLED!', strtoupper($this->getProtocolName()), $peerSuffix)); - // Check on all instances + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: peerSuffix=%s - CALLED!', strtoupper($this->getProtocolName()), $peerSuffix)); assert($this->getPoolInstance() instanceof Poolable); assert($this->getSocketInstance()->isValidSocket()); // Get next socket instance from pool over the factory $socketInstance = SocketFactory::createNextAcceptedSocketFromPool($this->getPoolInstance(), $this->getSocketInstance()); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s accepted.', strtoupper($this->getProtocolName()), gettype($socketInstance))); - // Is socket instance set? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s accepted.', strtoupper($this->getProtocolName()), gettype($socketInstance))); if (!($socketInstance instanceof StorableSocket)) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: No new connection on listener ... - EXIT!', strtoupper($this->getProtocolName()))); - // Nothing has changed on the listener + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: No new connection on listener ... - EXIT!', strtoupper($this->getProtocolName()))); return; } // END - if - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance.socketResource=%s accepted.', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); - // 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 with a faked recipientData array + // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -380,10 +359,8 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { // Get a connection info instance $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), StorableSocket::CONNECTION_TYPE_INCOMING); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: infoInstance[]=%s', strtoupper($this->getProtocolName()), gettype($infoInstance))); - // Is the instance set? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: infoInstance[]=%s', strtoupper($this->getProtocolName()), gettype($infoInstance))); if (!($infoInstance instanceof ShareableInfo)) { // Should not happen throw new LogicException(sprintf('infoInstance[]=%s does not implement ShareableInfo', gettype($infoInstance))); @@ -392,25 +369,20 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { // Will the info instance with listener data $infoInstance->fillWithSocketPeerInformation($socketInstance); - // Debugging: - //* DEBUG-DIE: */ $infoInstance->debugInstance(); - // Set all required data + //* DEBUG-DIE: */ $infoInstance->debugInstance(); $socketInstance->setSenderAddress($peerAddress . $peerSuffix); $socketInstance->setSenderPort($peerPort); - $socketInstance->setRecipientId($nodeInstance->getSessionId()); // Register the socket with the registry and with the faked array $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Calling this.handlerInstance.processRawDataFromSocketInstance(resource=%s) ...', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); - // 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); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName()))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName()))); } /** @@ -433,7 +405,7 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { $unlProtocol = $locatorInstance->getUnlProtocol(); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: locatorInstance.unlProtocol=%s,socketProtocol=%s', strtoupper($this->getProtocolName()), $unlProtocol, $socketProtocol)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: locatorInstance->unlProtocol=%s,socketProtocol=%s', strtoupper($this->getProtocolName()), $unlProtocol, $socketProtocol)); // Is same protocol? $accepts = ($unlProtocol == $socketProtocol); diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index b0f3fc938..3a67d97e3 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -175,10 +175,8 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable * @throws LogicException If the given handler instance is not implementing a required interface */ public function monitorIncomingRawData () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: CALLED!'); - // Get the handler instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: CALLED!'); $handlerInstance = $this->getListenerInstance()->getHandlerInstance(); /* @@ -190,13 +188,11 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } elseif (!$handlerInstance instanceof Networkable) { // Bad, bad! - throw new LogicException(sprintf('listenerInstance.handlerInstance[]=%s is not imlementing Networkable.', gettype($handlerInstance))); + throw new LogicException(sprintf('listenerInstance->handlerInstance[]=%s is not imlementing Networkable.', gettype($handlerInstance))); } - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: handlerInstance=%s', strtoupper($this->getProtocolName()), $handlerInstance->__toString())); - // Does the handler have some decoded data pending? + /* 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!'); @@ -213,11 +209,11 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable * know if the decoded data origins from a TCP or UDP connection so it * can just be passed over to the network package receiver. */ - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: Going to handle over some raw data to receiverInstance=' . $receiverInstance->__toString() . ' ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: Going to handle over some raw data to receiverInstance=%s ...', strtoupper($this->getProtocolName()), $receiverInstance->__toString())); $receiverInstance->addRawDataToIncomingStack($handlerInstance); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER-DECORATOR: EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: EXIT!', strtoupper($this->getProtocolName()))); } /** diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index fd5500c4d..de0785ed9 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -78,7 +78,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { */ private function validateSocket (StorableSocket $socketInstance) { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: socketInstance.socketResource=%s - CALLED!', $socketInstance->getSocketResource())); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource())); // Is it a valid resource? if (!$socketInstance->isValidSocket()) { @@ -91,7 +91,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Is it without any errors? if ($errorCode > 0) { - // Handle the socket error with a faked recipientData array + // Handle the socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if } @@ -128,7 +128,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { if (!$this->getListenerInstance()->getSocketInstance()->equals($socketInstance)) { // Try to determine the peer's IP number if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) { - // Handle the socket error with a faked recipientData array + // Handle the socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if } else { @@ -227,10 +227,8 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { * @throws LogicException If an expected array element is missing */ public function getSocketFromPackageInstance (DeliverablePackage $packageInstance, $connectionType = NULL) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL:packageInstance=%s,connectionType[%s]=%s - CALLED!', $packageInstance->__toString(), gettype($connectionType), $connectionType)); - // Default is no socket + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL:packageInstance=%s,connectionType[%s]=%s - CALLED!', $packageInstance->__toString(), gettype($connectionType), $connectionType)); $socketInstance = NULL; // Resolve recipient (UNL) into a handler instance @@ -239,19 +237,15 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Get UNL data $locatorInstance = $handlerInstance->getUniversalNodeLocatorInstance(); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),locatorInstance.unl=' . $locatorInstance->generateUnl() . ' ...'); - // Default is all sockets + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),locatorInstance->unl=' . $locatorInstance->generateUnl() . ' ...'); $sockets = $this->getAllSockets(); // Get all sockets and check them, skip the server socket foreach ($sockets as $socketArray) { - // Debugging: - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketArray(%d)=%s', count($socketArray), implode(',', $socketArray))); - //* DEBUG-DIE: */ die(__METHOD__.':connectionType='.$connectionType.',socketArray='.print_r($socketArray, TRUE)); - // Is connection type set? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketArray(%d)=%s', count($socketArray), implode(',', $socketArray))); + //* DEBUG-DIE: */ die(__METHOD__.':connectionType='.$connectionType.',socketArray='.print_r($socketArray, TRUE)); if (!isset($socketArray[Poolable::SOCKET_ARRAY_INSTANCE])) { // Important array element missing throw new LogicException(sprintf('socketArray[%s] is not set.', Poolable::SOCKET_ARRAY_INSTANCE)); @@ -263,41 +257,35 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { throw new InvalidConnectionTypeException(array($this, $connectionType), self::EXCEPTION_INVALID_CONNECTION_TYPE); } elseif ((!empty($connectionType)) && ($socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE] !== $connectionType)) { // Skip unwanted sockets - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: Skipping unwanted socket %s of type %s/%s ...', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE], $connectionType)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: Skipping unwanted socket %s of type %s/%s ...', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE], $connectionType)); continue; } - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketInstance.socketResource=%s,socketInstance.socketProtocol=%s,socketArray[%s]=%s', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketProtocol(), Poolable::SOCKET_ARRAY_CONN_TYPE, $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE])); - // Init peer address/port $peerAddress = '0.0.0.0'; $peerPort = '0'; // Try to get the "peer"'s name + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketInstance->socketResource=%s,socketInstance->socketProtocol=%s,socketArray[%s]=%s', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketProtocol(), Poolable::SOCKET_ARRAY_CONN_TYPE, $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE])); if (!$socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketPeerName($peerAddress, $peerPort)) { // Handle the socket error with given package data $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->handleSocketError(__METHOD__, __LINE__); } // END - if - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s,peerPort=%d,locatorInstance.addressPart=%s', $peerAddress, $peerPort, $locatorInstance->getAddressPart())); - // If the "peer" IP and recipient is same, use it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s,peerPort=%d,locatorInstance->addressPart=%s', $peerAddress, $peerPort, $locatorInstance->getAddressPart())); if ($peerAddress == $locatorInstance->getAddressPart()) { // IPs match, so take the socket and quit this loop $socketInstance = $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]; // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s matches with recipient IP address. Taking socketResource=%s,type=%s', $peerAddress, $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE])); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: peerAddress=%s matches with recipient IP address. Taking socketResource=%s,type=%s', $peerAddress, $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE])); break; } // END - if } // END - foreach - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: socketInstance[]=' . gettype($socketInstance) . ' - EXIT!'); - // Return the determined socket instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketInstance[]=%s - EXIT!', gettype($socketInstance))); return $socketInstance; } diff --git a/application/hub/classes/recipient/dht/class_DhtRecipient.php b/application/hub/classes/recipient/dht/class_DhtRecipient.php index d6df5dd8e..edfae0774 100644 --- a/application/hub/classes/recipient/dht/class_DhtRecipient.php +++ b/application/hub/classes/recipient/dht/class_DhtRecipient.php @@ -15,6 +15,7 @@ use Org\Mxchange\CoreFramework\Lists\Listable; // Import SPL stuff use \InvalidArgumentException; +use \LogicException; /** * A DHT recipient @@ -88,22 +89,22 @@ class DhtRecipient extends BaseRecipient implements Recipient { $recipients = $discoverInstance->resolveRecipientsByPackageInstance($packageInstance); // Now "walk" through all elements and add them to the list + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-RECIPIENT: recipients()=%d', count($recipients))); foreach ($recipients as $recipient) { // These array elements must exist for this loop: - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: recipient=' . $recipient); - // @TODO Unfinished - die(__METHOD__ . ':recipient=' . print_r($recipient, TRUE)); - assert(!empty($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS])); - - // Put ip and port both together - $unl = $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS]; + //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-RECIPIENT: recipient=%s', print_r($recipient, TRUE))); + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: recipient=%s', __METHOD__, __LINE__, print_r($recipient, TRUE))); + if (empty($recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS])) { + // External UNL needs to be set + throw new LogicException(sprintf('recipient[%s] is not set', NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS)); + } // Add it to the list - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: Calling listInstance->addEntry(' . $unl . ') ...'); - $listInstance->addEntry('unl', $unl); - } // END - foreach + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-RECIPIENT: Calling listInstance->addEntry(%s) ...', $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS])); + $listInstance->addEntry('unl', $recipient[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_EXTERNAL_ADDRESS]); + } - // Debug message + // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: EXIT!'); } diff --git a/application/hub/classes/registry/socket/class_SocketRegistry.php b/application/hub/classes/registry/socket/class_SocketRegistry.php index 645b8aad4..2632e189c 100644 --- a/application/hub/classes/registry/socket/class_SocketRegistry.php +++ b/application/hub/classes/registry/socket/class_SocketRegistry.php @@ -213,10 +213,8 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @return void */ public function registerSocketInstance (ShareableInfo $infoInstance, StorableSocket $socketInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - CALLED!'); - // Is the socket already registered? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - CALLED!'); if ($this->isSocketRegistered($infoInstance, $socketInstance)) { // Throw the exception throw new SocketAlreadyRegisteredException(array($infoInstance, $socketInstance->getSocketResource()), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED); @@ -346,16 +344,34 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke /** * Checks whether a socket of a connection type 'incoming' is registered. * + * @param $listenerInstance An instance of a Listenable class * @return $isRegistered Whether such a socket is registered - * @todo 0% done + * @todo May extract this loop into private method and invoke it with CONNECTION_TYPE_INCOMING for later expansion also on outgoing/file sockets? */ - public function isIncomingSocketRegistered () { + public function isIncomingSocketRegistered (Listenable $listenerInstance) { // Default is not found - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: CALLED!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: listenerInstance=%s (protocol: %s) - CALLED!', $listenerInstance->__toString(), strtoupper($listenerInstance->getProtocolName()))); + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: this=%s', __METHOD__, __LINE__, print_r($this, TRUE))); $isRegistered = FALSE; + // Search listener's socket sub-registry + $registryInstance = $this->getInstance($listenerInstance->getProtocolName()); + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: listenerInstance->protocolName=%s,registryInstance=%s', __METHOD__, __LINE__, $listenerInstance->getProtocolName(), print_r($registryInstance, TRUE))); + + // Get all sockets from it + foreach ($registryInstance->getInstanceRegistry() as $instanceKey => $socketInstance) { + // Is this socket as requested? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: instanceKey=%s,socketInstance=%s,socketType=%s', $instanceKey, $socketInstance->__toString(), $socketInstance->getSocketType())); + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: socketInstance=%s', __METHOD__, __LINE__, print_r($socketInstance, TRUE))); + if ($socketInstance->getSocketType() == StorableSocket::CONNECTION_TYPE_INCOMING) { + // Is found! + $isRegistered = TRUE; + break; + } + } + // Return status - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: isRegistered=%d - EXIT!', intval($isRegistered))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: isRegistered=%d - EXIT!', intval($isRegistered))); return $isRegistered; } diff --git a/application/hub/classes/visitor/pool/monitor/class_RawDataPoolMonitorVisitor.php b/application/hub/classes/visitor/pool/monitor/class_RawDataPoolMonitorVisitor.php index b76df0f76..f3600c4a5 100644 --- a/application/hub/classes/visitor/pool/monitor/class_RawDataPoolMonitorVisitor.php +++ b/application/hub/classes/visitor/pool/monitor/class_RawDataPoolMonitorVisitor.php @@ -82,17 +82,15 @@ class RawDataPoolMonitorVisitor extends BaseVisitor implements PoolVisitor, List * @return void */ public function visitListener (Listenable $listenerInstance) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAWDATA-MONITOR-VISITOR: listenerInstance=' . $listenerInstance->__toString() . ' - CALLED!'); - /* * The TCP/UDP listener have a socket registry instance set. These * registered sockets needs to be checked for pending incoming data. */ + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAWDATA-MONITOR-VISITOR: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); $listenerInstance->monitorIncomingRawData(); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAWDATA-MONITOR-VISITOR: EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAWDATA-MONITOR-VISITOR: EXIT!'); } /** diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index a1fdc5143..73d39091c 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -351,12 +351,4 @@ interface StorableSocket extends FrameworkInterface { */ public function getSocketType (); - /** - * Setter for socket type - * - * @param $socketType Socket type - * @return void - */ - public function setSocketType ($socketType); - } diff --git a/application/hub/interfaces/registry/socket/class_RegisterableSocket.php b/application/hub/interfaces/registry/socket/class_RegisterableSocket.php index 73707b9db..0d283dfc2 100644 --- a/application/hub/interfaces/registry/socket/class_RegisterableSocket.php +++ b/application/hub/interfaces/registry/socket/class_RegisterableSocket.php @@ -74,8 +74,9 @@ interface RegisterableSocket extends Registerable { /** * Checks whether a socket of a connection type 'incoming' is registered. * + * @param $listenerInstance An instance of a Listenable class * @return $isRegistered Whether such a socket is registered */ - function isIncomingSocketRegistered (); + function isIncomingSocketRegistered (Listenable $listenerInstance); } diff --git a/core b/core index c53e9c725..b4a83b158 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit c53e9c7252290aa3e4107717419429c0418a6945 +Subproject commit b4a83b1583b9b3947633bbaf62b25883bfc3f9ea -- 2.39.5