]> git.mxchange.org Git - hub.git/commitdiff
More debugging lines enabled (need to hunt down those bugs already :( ), added check...
authorRoland Häder <roland@mxchange.org>
Sat, 19 May 2012 10:54:02 +0000 (10:54 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 19 May 2012 10:54:02 +0000 (10:54 +0000)
application/hub/main/class_BaseHubSystem.php
application/hub/main/handler/network/tcp/class_TcpRawDataHandler.php
application/hub/main/helper/connection/class_BaseConnectionHelper.php
application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php
application/hub/main/helper/hub/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php
application/hub/main/listener/tcp/class_TcpListener.php
application/hub/main/listener/udp/class_UdpListener.php
application/hub/main/pools/peer/class_DefaultPeerPool.php
application/hub/main/states/node/class_BaseNodeState.php

index b1fdccdbd232cb763eab69b0554c1e1b01c2c9b4..a607160265d2e98fc354d32e78a97b74c93a48bf 100644 (file)
@@ -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
index 36b883d6bed4246e364c83a1109f797308d9fd7a..304c9775a55ea1aef5cf249db93672f27869f2c3 100644 (file)
@@ -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)) {
index 6fa2861b0b7fb1df194655895a8daba8bec729b3..c6f5ceef4003aadb63c410b27d76fcdcaacd39b5 100644 (file)
@@ -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);
index 8603b693cd07e0deaffb68a3b324bc0d9fbc80c7..5b10f3fd4e7e1a3fcf8ed421add8622588ecd63a 100644 (file)
@@ -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...
index 08ff144417b42225f64babea8a4fc7e25363af65..e174be152f64a2773396109d8d2e77cca8c92328 100644 (file)
@@ -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();
index ce0b8632ea9ba8bb8a822c3a8edd1b2937afd318..480e792da61aa98c10b07ac76a8ab1eb8c561a05 100644 (file)
@@ -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
index 2fc44f3029ac069e4303411dd89cd32a7695ed0e..757936434a3a279acf8e780e663d5e216036cc27 100644 (file)
@@ -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);
index 6722bfe56cf9678caf95b9ab313f68a877a75336..f80dfdb8edbad42f5736465e04f059379bfa6f79 100644 (file)
@@ -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
index 3feeca035c1ff8bcc2481217d296c6115a35737f..2109465ebb63e6c339174d5d1234587dcae15657 100644 (file)
@@ -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]