]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/helper/connection/class_BaseConnectionHelper.php
Added helper for sending out 'request-node-list' answers
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
index 60bb1cb293e3c31301f52d200bab56266c5b543c..0b5a0ab871185cb920611b7a7d0627b05874b520 100644 (file)
@@ -230,7 +230,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * @see         Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code
         * @todo        Rewrite the while() loop to a iterator to not let the software stay very long here
         */
-       protected function connectToPeerByRecipientDataArray (array $recipientData) {
+       protected function connectToPeerByRecipientData (array $recipientData) {
                // Only call this if the connection is initialized by initConnection()
                assert($this->isInitialized === true);
 
@@ -242,7 +242,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
 
                // Debug output
-               $this->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $recipientData[0] . ':' . $recipientData[1] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...');
+               self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $recipientData[0] . ':' . $recipientData[1] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...');
 
                // Try to connect until it is connected
                while ($isConnected = !@socket_connect($socketResource, $recipientData[0], $recipientData[1])) {
@@ -335,7 +335,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                } // END - if
 
                // Debug message
-               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: currentFinalHash=' . $this->currentFinalHash);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: currentFinalHash=' . $this->currentFinalHash);
 
                // Get the next raw data chunk from the fragmenter
                $rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash);
@@ -345,18 +345,18 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                $chunkData   = array_values($rawDataChunk);
 
                // Is the required data there?
-               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
-               //* NOISY-DEBUG: */ $this->debugOutput('chunkData='.print_r($chunkData,true));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('chunkData='.print_r($chunkData,true));
                if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
                        // Remember this chunk as queued
                        $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
 
                        // Return the raw data
-                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Returning ' . strlen($chunkData[0]) . ' bytes from ' . __METHOD__ . ' ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: Returning ' . strlen($chunkData[0]) . ' bytes from ' . __METHOD__ . ' ...');
                        return $chunkData[0];
                } else {
                        // Return zero string
-                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Returning zero bytes from ' . __METHOD__ . '!');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: Returning zero bytes from ' . __METHOD__ . '!');
                        return '';
                }
        }
@@ -398,10 +398,9 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                while (strlen($dataStream) > 0) {
                        // Convert the package data array to a raw data stream
                        $dataStream = $this->getRawDataFromPackageArray($packageData);
-                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
                        $rawData .= $dataStream;
                } // END - while
-               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: rawData[' . strlen($rawData) . ']=' . $rawData);
 
                // Nothing to sent is bad news, so assert on it
                assert(strlen($rawData) > 0);
@@ -409,6 +408,9 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                // Encode the raw data with our output-stream
                $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
 
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: rawData()=' . strlen($rawData) . ',encodedData()=' . strlen($encodedData));
+
                // Calculate difference
                $this->diff = $bufferSize - strlen($encodedData);
 
@@ -421,12 +423,15 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                // Deliver all data
                while ($sentBytes !== false) {
                        // And deliver it
-                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
+
                        if ($this->diff >= 0) {
                                // Send all out (encodedData is smaller than or equal buffer size)
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: MD5=' . md5(substr($encodedData, 0, ($bufferSize - $this->diff))));
                                $sentBytes = socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
                        } else {
                                // Send buffer size out
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: MD5=' . md5(substr($encodedData, 0, $bufferSize)));
                                $sentBytes = socket_write($socketResource, $encodedData, $bufferSize);
                        }
 
@@ -439,7 +444,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                                throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
                        } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
                                // Nothing sent means we are done
-                               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (LINE=' . __LINE__ . ')');
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')');
                                break;
                        }
 
@@ -450,7 +455,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                        $totalSentBytes += $sentBytes;
 
                        // Cut out the last unsent bytes
-                       //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
                        $encodedData = substr($encodedData, $sentBytes);
 
                        // Calculate difference again
@@ -459,13 +464,13 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                        // Can we abort?
                        if (strlen($encodedData) <= 0) {
                                // Abort here, all sent!
-                               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (LINE=' . __LINE__ . ')');
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')');
                                break;
                        } // END - if
                } // END - while
 
                // Return sent bytes
-               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: totalSentBytes=' . $totalSentBytes . ',diff=' . $this->diff);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: totalSentBytes=' . $totalSentBytes . ',diff=' . $this->diff);
                return $totalSentBytes;
        }
 
@@ -475,7 +480,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * @return      void
         */
        protected final function markConnectionShuttedDown () {
-               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ' has been marked as shutted down');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: ' . $this->__toString() . ' has been marked as shutted down');
                $this->shuttedDown = true;
 
                // And remove the (now invalid) socket
@@ -488,7 +493,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * @return      $shuttedDown    Whether this connection is shutted down
         */
        public final function isShuttedDown () {
-               //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
                return $this->shuttedDown;
        }
 
@@ -501,10 +506,11 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * later debugging purposes.
         *
         * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
         * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       protected function socketErrorConnectionTimedOutHandler ($socketResource) {
+       protected function socketErrorConnectionTimedOutHandler ($socketResource, array $recipientData) {
                // Get socket error code for verification
                $socketError = socket_last_error($socketResource);
 
@@ -523,10 +529,11 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * clear it for later debugging purposes.
         *
         * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
         * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       protected function socketErrorResourceUnavailableHandler ($socketResource) {
+       protected function socketErrorResourceUnavailableHandler ($socketResource, array $recipientData) {
                // Get socket error code for verification
                $socketError = socket_last_error($socketResource);
 
@@ -545,10 +552,11 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * later debugging purposes.
         *
         * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
         * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       protected function socketErrorConnectionRefusedHandler ($socketResource) {
+       protected function socketErrorConnectionRefusedHandler ($socketResource, array $recipientData) {
                // Get socket error code for verification
                $socketError = socket_last_error($socketResource);
 
@@ -567,10 +575,11 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * debugging purposes.
         *
         * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
         * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       protected function socketErrorNoRouteToHostHandler ($socketResource) {
+       protected function socketErrorNoRouteToHostHandler ($socketResource, array $recipientData) {
                // Get socket error code for verification
                $socketError = socket_last_error($socketResource);
 
@@ -586,14 +595,15 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
 
        /**
         * Handles socket error 'operation already in progress' which happens in
-        * method connectToPeerByRecipientDataArray() on timed out connection
+        * method connectToPeerByRecipientData() on timed out connection
         * attempts.
         *
         * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
         * @throws      SocketConnectionException       The connection attempts fails with a time-out
         */
-       protected function socketErrorOperationAlreadyProgressHandler ($socketResource) {
+       protected function socketErrorOperationAlreadyProgressHandler ($socketResource, array $recipientData) {
                // Get socket error code for verification
                $socketError = socket_last_error($socketResource);
 
@@ -612,10 +622,11 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
         * passed on with non-blocking connections.
         *
         * @param       $socketResource         A valid socket resource
+        * @param       $recipientData          An array with two elements: 0=IP number, 1=port number
         * @return      void
         */
-       protected function socketErrorOperationInProgressHandler ($socketResource) {
-               $this->debugOutput('CONNECTION: Operation is now in progress, this is usual for non-blocking connections and is no bug.');
+       protected function socketErrorOperationInProgressHandler ($socketResource, array $recipientData) {
+               self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.');
        }
 }