From: Roland Häder Date: Sat, 19 May 2012 10:54:02 +0000 (+0000) Subject: More debugging lines enabled (need to hunt down those bugs already :( ), added check... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=89f7a4909d3103489f1f04f9550eff3b4ffd3b06;p=hub.git More debugging lines enabled (need to hunt down those bugs already :( ), added check if peer state is 'active' or 'reachable' --- diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index b1fdccdbd..a60716026 100644 --- a/application/hub/main/class_BaseHubSystem.php +++ b/application/hub/main/class_BaseHubSystem.php @@ -354,13 +354,15 @@ class BaseHubSystem extends BaseFrameworkSystem { * but assumes valid data in array $recipientData, except that * count($recipientData) is always 2. * + * @param $method Value of __METHOD__ from calling method + * @param $line Source code line where this method was called * @param $socketResource A valid socket resource * @param $recipientData An array with two elements: 0=IP number, 1=port number * @return void * @throws InvalidSocketException If $socketResource is no socket resource * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back */ - protected final function handleSocketError ($socketResource, array $recipientData) { + protected final function handleSocketError ($method, $line, $socketResource, array $recipientData) { // This method handles only socket resources if (!is_resource($socketResource)) { // No resource, abort here diff --git a/application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php b/application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php index 36b883d6b..304c9775a 100644 --- a/application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php +++ b/application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php @@ -82,7 +82,17 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { /* NOISY-DEBUG: */ $this->debugOutput('TCP-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',resource=' . $resource . ',error=' . socket_strerror(socket_last_error($resource))); // Is it valid? - if (($rawData === false) || (socket_last_error($resource) > 0)) { + if (socket_last_error($resource) == 11) { + // Debug message + /* NOISY-DEBUG: */ $this->debugOutput('TCP-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $resource); + + /* + * Error code 11 (Resource temporary unavailable) can be safely + * ignored on non-blocking sockets. The socket is currently not + * sending any data. + */ + socket_clear_error($resource); + } elseif (($rawData === false) || (socket_last_error($resource) > 0)) { // Network error or connection lost $this->setErrorCode(socket_last_error($resource)); } elseif (empty($rawData)) { diff --git a/application/hub/main/helper/connection/class_BaseConnectionHelper.php b/application/hub/main/helper/connection/class_BaseConnectionHelper.php index 6fa2861b0..c6f5ceef4 100644 --- a/application/hub/main/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/main/helper/connection/class_BaseConnectionHelper.php @@ -182,7 +182,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc // Set the option to reuse the port if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) { // Handle this socket error with a faked recipientData array - $this->handleSocketError($socketResource, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); // And throw again // @TODO Move this to the socket error handler @@ -196,7 +196,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc */ if (!socket_set_nonblock($socketResource)) { // Handle this socket error with a faked recipientData array - $helperInstance->handleSocketError($socketResource, array('0.0.0.0', '0')); + $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); // And throw again throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); @@ -415,7 +415,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc // If there was an error, we don't continue here if ($sentBytes === false) { // Handle the error with a faked recipientData array - $this->handleSocketError($socketResource, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); // And throw it throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); diff --git a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php index 8603b693c..5b10f3fd4 100644 --- a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php +++ b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php @@ -71,7 +71,7 @@ class TcpConnectionHelper extends BaseConnectionHelper implements ConnectionHelp // Check if there was an error else if ($socketError > 0) { // Handle this socket error with a faked recipientData array - $helperInstance->handleSocketError($socketResource, array('0.0.0.0', '0')); + $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); // Then throw again throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_SOCKET_CREATION_FAILED); @@ -127,7 +127,7 @@ class TcpConnectionHelper extends BaseConnectionHelper implements ConnectionHelp // Now connect to it if (!$helperInstance->connectToPeerByRecipientDataArray($recipientData)) { // Handle socket error - $helperInstance->handleSocketError($socketResource, $recipientData); + $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, $recipientData); } // END - if // Okay, that should be it. Return it... diff --git a/application/hub/main/helper/hub/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php b/application/hub/main/helper/hub/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php index 08ff14441..e174be152 100644 --- a/application/hub/main/helper/hub/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php +++ b/application/hub/main/helper/hub/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php @@ -85,8 +85,8 @@ class NodeAnnouncementMessageAnswerHelper extends BaseHubAnswerHelper implements * @return void */ public function sendPackage (NodeHelper $nodeInstance) { - // Sanity check: Is the node in the approx. state? (active) - $nodeInstance->getStateInstance()->validateNodeStateIsActive(); + // Sanity check: Is the node in the approx. state? (active/reachable) + $nodeInstance->getStateInstance()->validateNodeStateIsActiveOrReachable(); // Compile the template, this inserts the loaded node data into the gaps. $this->getTemplateInstance()->compileTemplate(); diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index ce0b8632e..480e792da 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -74,7 +74,7 @@ class TcpListener extends BaseListener implements Listenable { // Check if there was an error else if ($socketError > 0) { // Handle this socket error with a faked recipientData array - $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Then throw again throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET); @@ -84,7 +84,7 @@ class TcpListener extends BaseListener implements Listenable { // Set the option to reuse the port if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) { // Handle this socket error with a faked recipientData array - $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification $socketError = socket_last_error($mainSocket); @@ -108,7 +108,7 @@ class TcpListener extends BaseListener implements Listenable { $this->debugOutput('TCP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort()); if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) { // Handle this socket error with a faked recipientData array - $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification $socketError = socket_last_error($mainSocket); @@ -128,7 +128,7 @@ class TcpListener extends BaseListener implements Listenable { $this->debugOutput('TCP-LISTENER: Listening for connections.'); if (!socket_listen($mainSocket)) { // Handle this socket error with a faked recipientData array - $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification $socketError = socket_last_error($mainSocket); @@ -148,7 +148,7 @@ class TcpListener extends BaseListener implements Listenable { $this->debugOutput('TCP-LISTENER: Setting non-blocking mode.'); if (!socket_set_nonblock($mainSocket)) { // Handle this socket error with a faked recipientData array - $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification $socketError = socket_last_error($mainSocket); @@ -244,17 +244,25 @@ class TcpListener extends BaseListener implements Listenable { // @TODO Does this work on Windozer boxes??? if (!socket_set_option($newSocket, SOL_SOCKET, SO_RCVTIMEO, $options)) { // Handle this socket error with a faked recipientData array - $this->handleSocketError($newSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0')); } // END - if - // Output result (only debugging!) + // Output result (only for debugging!) + /* $option = socket_get_option($newSocket, SOL_SOCKET, SO_RCVTIMEO); $this->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, true)); + */ // Enable SO_OOBINLINE if (!socket_set_option($newSocket, SOL_SOCKET, SO_OOBINLINE ,1)) { // Handle this socket error with a faked recipientData array - $this->handleSocketError($newSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0')); + } // END - if + + // Set non-blocking + if (!socket_set_nonblock($newSocket)) { + // Handle this socket error with a faked recipientData array + $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0')); } // END - if // Add it to the peers diff --git a/application/hub/main/listener/udp/class_UdpListener.php b/application/hub/main/listener/udp/class_UdpListener.php index 2fc44f302..757936434 100644 --- a/application/hub/main/listener/udp/class_UdpListener.php +++ b/application/hub/main/listener/udp/class_UdpListener.php @@ -77,7 +77,7 @@ class UdpListener extends BaseListener implements Listenable { $this->debugOutput('UDP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort()); if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) { // Handle the socket error with a faked recipientData array - $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification $socketError = socket_last_error($mainSocket); @@ -97,7 +97,7 @@ class UdpListener extends BaseListener implements Listenable { $this->debugOutput('UDP-LISTENER: Setting non-blocking mode.'); if (!socket_set_nonblock($mainSocket)) { // Handle the socket error with a faked recipientData array - $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification $socketError = socket_last_error($socket); @@ -117,7 +117,7 @@ class UdpListener extends BaseListener implements Listenable { $this->debugOutput('UDP-LISTENER: Setting re-use address option.'); if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) { // Handle the socket error with a faked recipientData array - $this->handleSocketError($mainSocket, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0')); /* // Get socket error code for verification $socketError = socket_last_error($mainSocket); diff --git a/application/hub/main/pools/peer/class_DefaultPeerPool.php b/application/hub/main/pools/peer/class_DefaultPeerPool.php index 6722bfe56..f80dfdb8e 100644 --- a/application/hub/main/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/main/pools/peer/class_DefaultPeerPool.php @@ -69,7 +69,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Is it without any errors? if ($errorCode > 0) { // Handle the socket error with a faked recipientData array - $this->handleSocketError($socketResource, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); /* // Get error message $errorMessage = socket_strerror($errorCode); @@ -102,7 +102,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Try to determine the peer's IP number if (!socket_getpeername($socketResource, $peerName)) { // Handle the socket error with a faked recipientData array - $this->handleSocketError($socketResource, array('0.0.0.0', '0')); + $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0')); /* // Get last error $lastError = socket_last_error($socketResource); @@ -167,7 +167,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Try to get the "peer"'s name if (!socket_getpeername($socket, $peerIp)) { // Handle the socket error with given package data - $this->handleSocketError($socket, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + $this->handleSocketError(__METHOD__, __LINE__, $socket, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); } // END - if // If the "peer" IP and recipient is same, use it diff --git a/application/hub/main/states/node/class_BaseNodeState.php b/application/hub/main/states/node/class_BaseNodeState.php index 3feeca035..2109465eb 100644 --- a/application/hub/main/states/node/class_BaseNodeState.php +++ b/application/hub/main/states/node/class_BaseNodeState.php @@ -53,7 +53,7 @@ class BaseNodeState extends BaseState { * exception if it is every other state. * * @return void - * @throws InvalidStateException If the state is not 'active' + * @throws InvalidStateException If the state is not 'active' and not 'announced' */ public function validateNodeStateIsActiveOrAnnounced () { // Just compare it... @@ -62,6 +62,21 @@ class BaseNodeState extends BaseState { throw new InvalidStateException($this, self::EXCEPTION_INVALID_STATE); } // END - if } + + /** + * Validates whether the state is 'active' or 'reachable' or throws an + * exception if it is every other state. + * + * @return void + * @throws InvalidStateException If the state is not 'active' and not 'reachable' + */ + public function validateNodeStateIsActiveOrReachable () { + // Just compare it... + if ((!$this instanceof NodeActiveState) && (!$this instanceof NodeReachableState)) { + // Throw the exception + throw new InvalidStateException($this, self::EXCEPTION_INVALID_STATE); + } // END - if + } } // [EOF]