Some minor improvements:
authorRoland Häder <roland@mxchange.org>
Fri, 4 Nov 2011 19:30:52 +0000 (19:30 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 4 Nov 2011 19:30:52 +0000 (19:30 +0000)
- $socket renamed to $socketResource
- InvalidSocketException enhanced to accept a socket resource at index 1

inc/classes/exceptions/socket/class_InvalidSocketException.php
inc/classes/exceptions/socket/class_SocketShutdownException.php
inc/classes/main/console/class_ConsoleTools.php

index 27ccb5d..448d2cb 100644 (file)
@@ -26,18 +26,32 @@ class InvalidSocketException extends FrameworkException {
        /**
         * A Constructor for this exception
         *
-        * @param               $messageArray   Error message array
-        * @param               $code                   Error code
+        * @param       $messageArray   Error message array
+        * @param       $code                   Error code
         * @return      void
         */
        public function __construct (array $messageData, $code) {
-               // Construct the message
-               $message = sprintf("[%s:] Invalid socket, type=%s, errno=%s, errstr=%s",
-                       $messageData[0]->__toString(),
-                       $messageData[1],
-                       $messageData[2],
-                       $messageData[3]
-               );
+               // Is it a resource?
+               if (is_resource($message[1])) {
+                       // Get last error
+                       $lastError = socket_last_error($messageData[1]);
+
+                       // Construct the message
+                       $message = sprintf("[%s:] Invalid socket, type=%s, errno=%s, errstr=%s",
+                               $messageData[0]->__toString(),
+                               gettype($messageData[1]),
+                               $lastError,
+                               socket_strerror($lastError)
+                       );
+               } else {
+                       // Construct the message
+                       $message = sprintf("[%s:] Invalid socket, type=%s, errno=%s, errstr=%s",
+                               $messageData[0]->__toString(),
+                               $messageData[1],
+                               $messageData[2],
+                               $messageData[3]
+                       );
+               }
 
                // Call parent exception constructor
                parent::__construct($message, $code);
index 9311677..256001b 100644 (file)
@@ -26,7 +26,7 @@ class SocketShutdownException extends FrameworkException {
        /**
         * A Constructor for this exception
         *
-        * @param       $helperInstance         A ConnectionHelper instance
+        * @param       $helperInstance         An instance of a ConnectionHelper class
         * @param       $code                           Error code
         * @return      void
         */
index ab7df3d..825f5fa 100644 (file)
@@ -153,12 +153,12 @@ class ConsoleTools extends BaseFrameworkSystem {
 
                // First get a socket
                // @TODO Add some DNS caching here
-               $socket = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 5);
+               $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 5);
 
                // Check if there was an error else
                if ($errorNo > 0) {
                        // Then throw again
-                       throw new InvalidSocketException(array($helperInstance, gettype($socket), $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
                // Prepare the GET request
@@ -169,18 +169,18 @@ class ConsoleTools extends BaseFrameworkSystem {
                $request .= self::HTTP_EOL;
 
                // Send it to the socket
-               fwrite($socket, $request);
+               fwrite($socketResource, $request);
 
                // Init IP (this will always be the last line)
                $externalAddress = 'invalid';
 
                // And read the reply
-               while (!feof($socket)) {
-                       $externalAddress = fgets($socket, 128);
+               while (!feof($socketResource)) {
+                       $externalAddress = fgets($socketResource, 128);
                } // END - while
 
                // Close socket
-               fclose($socket);
+               fclose($socketResource);
 
                // Return determined external IP
                return $externalAddress;