From 8265c0310c851e8183e6df861eced09a3df6ed97 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 3 Nov 2020 18:57:15 +0100 Subject: [PATCH] Continued: - renamed getSocketPeerName() to determineSocketPeerName() - improved/added debug lines - sorted members a little MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../socket/class_SocketContainer.php | 241 ++++++------------ .../class_PeerStateLookupDatabaseWrapper.php | 2 +- .../info/connection/class_ConnectionInfo.php | 4 +- .../classes/listener/class_BaseListener.php | 2 +- .../assembler/class_PackageAssembler.php | 64 +++-- .../pools/peer/class_DefaultPeerPool.php | 4 +- ...UnsupportedPackageCodeHandlerException.php | 6 +- .../container/socket/class_StorableSocket.php | 4 +- 8 files changed, 135 insertions(+), 192 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 7268f614e..e3e9506cf 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -163,25 +163,6 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita return $socketInstance; } - /** - * Setter for package data instance - * - * @param $packageDataInstance An instance of a DeliverablePackage class - * @return void - */ - public function setPackageDataInstance (DeliverablePackage $packageDataInstance) { - $this->packageDataInstance = $packageDataInstance; - } - - /** - * Getter for package data instance - * - * @return $packageDataInstance An instance of a DeliverablePackage class - */ - public function getPackageDataInstance () { - return $this->packageDataInstance; - } - /** * Checks whether the given Universal Node Locator matches with the one from package data * @@ -238,7 +219,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->accept($visitorInstance); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -261,7 +242,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->accept($visitorInstance); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -287,9 +268,9 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $result Result from asking for peer address * @throws InvalidSocketException If socket is invalid */ - public function getSocketPeerName (&$peerAddress, &$peerPort) { + public function determineSocketPeerName (&$peerAddress, &$peerPort) { // Should be valid socket - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress[%s]=%s,peerPort[%s]=%d - CALLED!', strtoupper($this->getSocketProtocol()), gettype($peerAddress), $peerAddress, gettype($peerPort), $peerPort)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress[%s]=%s,peerPort[%s]=%d - CALLED!', strtoupper($this->getSocketProtocol()), gettype($peerAddress), $peerAddress, gettype($peerPort), $peerPort)); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -312,7 +293,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress=%s,peerPort=%d,result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), $peerAddress, $peerPort, gettype($result), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress=%s,peerPort=%d,result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), $peerAddress, $peerPort, gettype($result), intval($result))); return $result; } @@ -356,10 +337,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If socket is invalid */ public function getSocketRecipientAddress () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // 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); @@ -374,10 +353,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Get address part $recipientAddress = $locatorInstance->getUnlAddress(); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientAddress=%s - EXIT!', strtoupper($this->getSocketProtocol()), $recipientAddress)); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientAddress=%s - EXIT!', strtoupper($this->getSocketProtocol()), $recipientAddress)); return $recipientAddress; } @@ -388,10 +365,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If socket is invalid */ public function getSocketRecipientPort () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // 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); @@ -406,10 +381,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Get port part $recipientPort = $locatorInstance->getUnlPort(); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientPort=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($recipientPort))); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: recipientPort=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($recipientPort))); return $recipientPort; } @@ -420,20 +393,16 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $isValidSocket Whether the stored socket is valid */ public function isValidSocket () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // Get socket resource + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $socketResource = $this->getSocketResource(); // Is it valid? // @TODO maybe add more checks? is_resource() is still to less $isValidSocket = (is_resource($socketResource)); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValidSocket=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValidSocket))); - // Return status + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValidSocket=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValidSocket))); return $isValidSocket; } @@ -444,10 +413,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If socket is invalid */ public function getLastSocketErrorCode () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // 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); @@ -459,10 +426,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Get error code $errorCode = socket_last_error($socketResource); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($errorCode))); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($errorCode))); return $errorCode; } @@ -473,10 +438,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If socket is invalid */ public function getLastSocketErrorMessage () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // 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); @@ -489,6 +452,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $errorMessage = socket_strerror($this->getLastSocketErrorCode()); // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorMessage=%s - EXIT!', strtoupper($this->getSocketProtocol()), $errorMessage)); return $errorMessage; } @@ -498,19 +462,15 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @return $codeName Code name to used e.g. with some_$codeName_socket_error_class or so */ public function translateLastSocketErrorCodeToName () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // Get last error code + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); $errorCode = $this->getLastSocketErrorCode(); // Call "translate" method $codeName = $this->translateSocketErrorCodeToName($errorCode); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: codeName=%s - EXIT!', strtoupper($this->getSocketProtocol()), $codeName)); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: codeName=%s - EXIT!', strtoupper($this->getSocketProtocol()), $codeName)); return $codeName; } @@ -539,7 +499,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_bind($this->getSocketResource(), $this->getSocketFile()); // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -572,7 +532,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_bind($this->getSocketResource(), $address, $port); // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -583,10 +543,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If stored socket is invalid */ public function listenToSocket () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // 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); @@ -595,10 +553,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Try to listen on socket $result = socket_listen($this->getSocketResource()); - // 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[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -609,10 +565,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If stored socket is invalid */ public function enableSocketNonBlocking () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // 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); @@ -621,10 +575,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Try to set non-blocking I/O $result = socket_set_nonblock($this->getSocketResource()); - // 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[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -635,10 +587,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If stored socket is invalid */ public function enableSocketReuseAddress () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource())); - // 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); @@ -647,10 +597,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Tries to set option $result = $this->setSocketOption(SOL_SOCKET, SO_REUSEADDR, 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[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -680,7 +628,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_connect($this->getSocketResource(), $locatorInstance->getUnlAddress(), $locatorInstance->getUnlPort()); // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -722,7 +670,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita socket_set_block($this->getSocketResource()); // Drop all data (don't sent any on socket closure) - socket_set_option($this->getSocketResource(), SOL_SOCKET, SO_LINGER, array('l_onoff' => 1, 'l_linger' => 0)); + $this->setSocketOption(SOL_SOCKET, SO_LINGER, array('l_onoff' => 1, 'l_linger' => 0)); // Finally close socket to free some resources socket_close($this->getSocketResource()); @@ -731,7 +679,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(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -854,7 +802,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $peerPort = '0'; // Call other method - $result = $this->getSocketPeerName($peerAddress, $peerPort); + $result = $this->determineSocketPeerName($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)); @@ -865,7 +813,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // END - if // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -889,10 +837,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Call inner method $result = $this->setSocketOption(SOL_SOCKET, SO_RCVTIMEO, $options); - // 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[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -909,7 +855,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Trace message // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -968,7 +914,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -986,11 +932,9 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidArgumentException If an array element is missing */ public function writeBufferToSocketByArray (array &$socketBuffer, &$sentBytes) { - // Trace message + // Should be valid socket /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketBuffer()=%d,sentBytes=%d - CALLED!', strtoupper($this->getSocketProtocol()), count($socketBuffer), $sentBytes)); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketBuffer=%s', strtoupper($this->getSocketProtocol()), print_r($socketBuffer, TRUE))); - - // Should be valid socket if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -1008,21 +952,15 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackageHandler::RAW_INDEX_SENT_BYTES)); } - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sending out %d bytes,rawBufferSize=%d,diff=%d to socketResource=%s', strtoupper($this->getSocketProtocol()), strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]), $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE], $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF], $this->getSocketResource())); - // Is some data still pending or sent all out? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sending out %d bytes,rawBufferSize=%d,diff=%d to socketResource=%s', strtoupper($this->getSocketProtocol()), strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]), $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE], $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF], $this->getSocketResource())); if ($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF] >= 0) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, ($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]))))); - // Send all out (encodedData is smaller than or equal buffer size) + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, ($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]))))); $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], ($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF])); } else { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE])))); - // Send buffer size out + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE])))); $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE]); } @@ -1030,21 +968,15 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // @TODO Maybe check more? $result = ($sentBytes !== FALSE); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: sentBytes[%s]=%d,result=%d', strtoupper($this->getSocketProtocol()), gettype($sentBytes), $sentBytes, intval($result))); - // If there was an error, don't continue here + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: sentBytes[%s]=%d,result=%d', strtoupper($this->getSocketProtocol()), gettype($sentBytes), $sentBytes, intval($result))); if ($result === FALSE) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socket_write() failed, please evalutate. - EXIT!', strtoupper($this->getSocketProtocol()))); - // Failed delivery! + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socket_write() failed, please evalutate. - EXIT!', strtoupper($this->getSocketProtocol()))); return FALSE; } elseif (($sentBytes === 0) && (strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]) > 0)) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: All sent, returning TRUE ... - EXIT!', strtoupper($this->getSocketProtocol()))); - // Nothing sent means all data has been sent + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: All sent, returning TRUE ... - EXIT!', strtoupper($this->getSocketProtocol()))); return TRUE; } else { // The difference between sent bytes and length of raw data should not go below zero @@ -1053,20 +985,16 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Add total sent bytes $socketBuffer[NetworkPackageHandler::RAW_INDEX_SENT_BYTES] += $sentBytes; - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sent out %d of %d bytes ...', strtoupper($this->getSocketProtocol()), $sentBytes, strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]))); - // Cut out the last unsent bytes + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sent out %d of %d bytes ...', strtoupper($this->getSocketProtocol()), $sentBytes, strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]))); $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA] = substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], $sentBytes); // Calculate difference again $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF] = $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]); } - // 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[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } @@ -1083,10 +1011,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back */ public function handleSocketError ($method, $line) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: Handling socket errorCode=%d - CALLED!', $this->getLastSocketErrorCode())); - // This method handles only socket resources + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: Handling socket errorCode=%d - CALLED!', $this->getLastSocketErrorCode())); if (!$this->isValidSocket()) { // No resource, abort here throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -1111,7 +1037,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->clearLastSocketError(); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: EXIT!', strtoupper($this->getSocketProtocol()))); } /** @@ -1123,10 +1049,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented */ protected function getSocketErrorHandlerFromCode ($errorCode) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode)); - // Create a name from translated error code + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode)); $handlerName = 'handleSocketError' . self::convertToClassName($this->translateSocketErrorCodeToName($errorCode)); // Is the call-back method there? @@ -1135,10 +1059,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita throw new UnsupportedSocketErrorHandlerException(array($this, $handlerName, $errorCode), BaseConnectionHelper::EXCEPTION_UNSUPPORTED_ERROR_HANDLER); } // END - if - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: handlerName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $handlerName)); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: handlerName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $handlerName)); return $handlerName; } @@ -1154,10 +1076,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws SocketBindingException The socket could not be bind to */ protected function handleSocketErrorPermissionDenied () { - // Trace message - //* 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 + //* 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 @@ -1178,10 +1098,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws SocketConnectionException The connection attempts fails with a time-out */ protected function handleSocketErrorConnectionTimedOut () { - // Trace message - //* 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 + //* 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 @@ -1202,10 +1120,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws SocketConnectionException The connection attempts fails with a time-out */ protected function handleSocketErrorResourceUnavailable () { - // Trace message - //* 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 + //* 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 @@ -1226,10 +1142,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws SocketConnectionException The connection attempts fails with a time-out */ protected function handleSocketErrorConnectionRefused () { - // Trace message - //* 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 + //* 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 @@ -1250,10 +1164,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws SocketConnectionException The connection attempts fails with a time-out */ protected function handleSocketErrorNoRouteToHost () { - // Trace message - //* 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 + //* 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 @@ -1274,9 +1186,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws SocketConnectionException The connection attempts fails with a time-out */ protected function handleSocketErrorOperationAlreadyProgress () { - // Trace message - // 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 @@ -1297,10 +1208,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws SocketConnectionException The connection attempts fails with a time-out */ protected function handleSocketErrorConnectionResetByPeer () { - // Trace message - //* 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 + //* 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 @@ -1426,16 +1335,6 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita return $errorName; } - /** - * Setter for socket protocol field - * - * @param $socketProtocol Socket protocol - * @return void - */ - private function setSocketProtocol ($socketProtocol) { - $this->socketProtocol = $socketProtocol; - } - /** * Tries to set given socket option * @@ -1446,10 +1345,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * @throws InvalidSocketException If stored socket is invalid */ private function setSocketOption ($level, $optionName, $optionValue) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: level=%s,optionName=%s,optionValue[%s]=%s - CALLED!', strtoupper($this->getSocketProtocol()), $level, $optionName, gettype($optionValue), implode(',', $optionValue))); - // Should be valid socket + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: level=%s,optionName=%s,optionValue[]=%s - CALLED!', strtoupper($this->getSocketProtocol()), $level, $optionName, gettype($optionValue))); if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET); @@ -1459,9 +1356,39 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $result = socket_set_option($this->getSocketResource(), $level, $optionName, $optionValue); // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result))); return $result; } + /** + * Setter for socket protocol field + * + * @param $socketProtocol Socket protocol + * @return void + */ + private function setSocketProtocol ($socketProtocol) { + $this->socketProtocol = $socketProtocol; + } + + /** + * Setter for package data instance + * + * @param $packageDataInstance An instance of a DeliverablePackage class + * @return void + */ + public function setPackageDataInstance (DeliverablePackage $packageDataInstance) { + $this->packageDataInstance = $packageDataInstance; + } + + /** + * Getter for package data instance + * + * @return $packageDataInstance An instance of a DeliverablePackage class + */ + public function getPackageDataInstance () { + return $this->packageDataInstance; + } + /** * Getter for socket file instance * diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php index 3ff61b539..90d47ab2e 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php @@ -160,7 +160,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L $peerPort = '0'; // Get peer name - if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) { + if (!$socketInstance->determineSocketPeerName($peerAddress, $peerPort)) { // ... and cleartext message from it and put both into criteria $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $socketInstance->getLastSocketErrorCode()); $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_MSG , $socketInstance->getLastSocketErrorMessage()); diff --git a/application/hub/classes/info/connection/class_ConnectionInfo.php b/application/hub/classes/info/connection/class_ConnectionInfo.php index 2952f829c..87ab06435 100644 --- a/application/hub/classes/info/connection/class_ConnectionInfo.php +++ b/application/hub/classes/info/connection/class_ConnectionInfo.php @@ -139,12 +139,12 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable { */ public function fillWithSocketPeerInformation (StorableSocket $socketInstance) { // Init variables - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketInstance=%s - CALLED!', $socketInstance->__toString())); $socketAddress = '0.0.0.0'; $socketPort = '0'; // Get peer name - if (!$socketInstance->getSocketPeerName($socketAddress, $socketPort)) { + if (!$socketInstance->determineSocketPeerName($socketAddress, $socketPort)) { // Did not work throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); } // END - if diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index 8cd3f4bb0..cf91676fd 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -385,7 +385,7 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { $peerPort = '0'; // Get peer name - if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) { + if (!$socketInstance->determineSocketPeerName($peerAddress, $peerPort)) { // Handle this socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if diff --git a/application/hub/classes/package/assembler/class_PackageAssembler.php b/application/hub/classes/package/assembler/class_PackageAssembler.php index b2ea0caae..208c776ea 100644 --- a/application/hub/classes/package/assembler/class_PackageAssembler.php +++ b/application/hub/classes/package/assembler/class_PackageAssembler.php @@ -6,6 +6,7 @@ namespace Org\Shipsimu\Hub\Network\Package\Receiver\Assembler; use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Handler\Chunk\ChunkHandlerFactory; use Org\Shipsimu\Hub\Generic\BaseHubSystem; +use Org\Shipsimu\Hub\Handler\Network\PackageCode\UnsupportedPackageCodeHandlerException; use Org\Shipsimu\Hub\Handler\Network\RawData\HandleableRawData; use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; @@ -18,6 +19,9 @@ use Org\Mxchange\CoreFramework\Registry\Registerable; use Org\Mxchange\CoreFramework\Visitor\Visitable; use Org\Mxchange\CoreFramework\Visitor\Visitor; +// Import SPL stuff +use \BadMethodCallException; + /** * A PackageAssembler class to assemble a package content stream fragemented * by PackageFragmenter back to a raw package data array. @@ -51,7 +55,9 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, const EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER = 0xf2200; /** - * Pending data + * Pending raw data, when this data is finished being collected + * ("assembled") then there should be one start-marker at the beginning and + * one end-marker at the end of the string. */ private $pendingData = ''; @@ -117,10 +123,8 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, // Check it $isInputBufferEmpty = $this->getPackageHandlerInstance()->getStackInstance()->isStackEmpty(NetworkPackageHandler::STACKER_NAME_DECODED_HANDLED); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: isInputBufferEmpty=' . intval($isInputBufferEmpty)); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-ASSEMBLER: isInputBufferEmpty=%d - EXIT!', intval($isInputBufferEmpty))); return $isInputBufferEmpty; } @@ -148,22 +152,32 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, * chunks and (maybe) re-request some chunks from the sender, this would * take to much time and therefore slow down this node again. * - * @param $packageInstance An array with two elements: 'raw_data' and 'error_code' + * @param $packageInstance An instance of a DeliverablePackage class * @return void * @throws UnsupportedPackageCodeHandlerException If the package code handler is not implemented */ public function chunkPackageInstance (DeliverablePackage $packageInstance) { // Construct call-back name from package error code + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-ASSEMBLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); $this->callbacks[$packageInstance->getErrorCode()] = 'handlePackageBy' . self::convertToClassName($packageInstance->getErrorCode()); + // Init callable array + $callable = [ + $this, + $this->callbacks[$packageInstance->getErrorCode()], + ]; + // Abort if the call-back method is not there - if (!method_exists($this, $this->callbacks[$packageInstance->getErrorCode()])) { + if (!method_exists($callable[0], $callable[1])) { // Throw an exception - throw new UnsupportedPackageCodeHandlerException(array($this, $this->callbacks[$packageInstance->getErrorCode()], $packageInstance), self::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER); - } // END - if + throw new UnsupportedPackageCodeHandlerException(array($callable, $packageInstance), self::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER); + } // Call it back - call_user_func(array($this, $this->callbacks[$packageInstance->getErrorCode()]), $packageInstance); + call_user_func($callable, $packageInstance); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: EXIT!'); } /************************************************************************** @@ -176,24 +190,23 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, * class, does some low checks on it and feeds it into another queue for * verification and re-request for bad chunks. * - * @param $packageInstance An array with two elements: 'raw_data' and 'error_code' + * @param $packageInstance An instance of a DeliverablePackage class * @return void */ private function handlePackageByUnhandledPackage (DeliverablePackage $packageInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageInstance->rawData[]=' . $packageInstance->getRawData()); - // Check for some conditions + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-ASSEMBLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); if ((!$this->ifInputBufferIsEmpty()) || (!$this->isPackageContentCompleted($packageInstance))) { // Last chunk is not valid, so wait for more + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-ASSEMBLER: packageInstance->rawData()=%d being added to this->peningData ...', strlen($packageInstance->getRawData()))); $this->pendingData .= $packageInstance->getRawData(); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Partial data received. Waiting for more ... ( ' . strlen($packageInstance->getRawData()) . ' bytes)'); } else { // Debug message - //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageContent=' . print_r($packageInstance, TRUE) . ',chunks='.print_r($chunks, TRUE)); + //* PRINTR-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageInstance=' . print_r($packageInstance, TRUE) . ',chunks='.print_r($chunks, TRUE)); } + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: EXIT!'); } /** @@ -204,9 +217,11 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, */ public function isPendingDataEmpty () { // A simbple check + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: CALLED!'); $ifPendingDataIsEmpty = empty($this->pendingData); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-ASSEMBLER: ifPendingDataIsEmpty=%d - EXIT!', intval($ifPendingDataIsEmpty))); return $ifPendingDataIsEmpty; } @@ -217,12 +232,11 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, */ public function ifMultipleMessagesPending () { // Determine it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: CALLED!'); $isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_MULTIPLE_MESSAGE)); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: isPending=' . intval($isPending) . ' - EXIT!'); - // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-ASSEMBLER: isPending=%s - EXIT!', intval($isPending))); return $isPending; } @@ -230,13 +244,15 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, * Handles the assembler's pending data * * @return void + * @throws BadMethodCallException If this method was called but $this->pendingData is empty */ public function handlePendingData () { - // Debug output + // Wrong method invocation? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Going to decode ' . strlen($this->pendingData) . ' Bytes of pending data. pendingData=' . $this->pendingData); - - // Assert on condition - assert(!$this->isPendingDataEmpty()); + if ($this->isPendingDataEmpty()) { + // Should not happen + throw new BadMethodCallException('this->pendingData is empty but method was called..'); + } // No markers set? if (!$this->ifStartEndMarkersSet($this->pendingData)) { diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index de0785ed9..9bdd7cfde 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -127,7 +127,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // The socket resource should not match server socket if (!$this->getListenerInstance()->getSocketInstance()->equals($socketInstance)) { // Try to determine the peer's IP number - if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) { + if (!$socketInstance->determineSocketPeerName($peerAddress, $peerPort)) { // Handle the socket error $socketInstance->handleSocketError(__METHOD__, __LINE__); } // END - if @@ -267,7 +267,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // 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)) { + if (!$socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->determineSocketPeerName($peerAddress, $peerPort)) { // Handle the socket error with given package data $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->handleSocketError(__METHOD__, __LINE__); } // END - if diff --git a/application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php b/application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php index 8a7d0102a..bf7774f3c 100644 --- a/application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php +++ b/application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php @@ -41,10 +41,10 @@ class UnsupportedPackageCodeHandlerException extends FrameworkException { public function __construct (array $messageArray, $code) { // Construct the message $message = sprintf('[%s:%d] Unsupported package code handler %s for package code %s detected.', - $messageArray[0]->__toString(), + $messageArray[0][0]->__toString(), $this->getLine(), - $messageArray[1], - $messageArray[2]->getErrorCode() + $messageArray[0][1], + $messageArray[1]->getErrorCode() ); // Call parent exception constructor diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index 73d39091c..b6bec8e9a 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -117,14 +117,14 @@ interface StorableSocket extends FrameworkInterface { function enableSocketReuseAddress (); /** - * Some "getter" for peer address/port from stored socket + * Determines socket's "peer name", mostly address and port number. * * @param $peerAddress Peer address being determined * @param $peerPort Peer port being determined * @return $result Result from asking for peer address * @throws InvalidSocketException If stored socket is invalid */ - function getSocketPeerName (&$peerAddress, &$peerPort); + function determineSocketPeerName (&$peerAddress, &$peerPort); /** * Calls socket_select() on stored socket resource -- 2.39.5