]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/listener/tcp/class_TcpListener.php
More debugging, ignore *.serialized in node_list
[hub.git] / application / hub / main / listener / tcp / class_TcpListener.php
index c972c1f5f6d6a156e33f1ae71ab38ce8e4f17141..2f6a3224a5eac209b85184faf92872870f463d0b 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -30,6 +30,9 @@ class TcpListener extends BaseListener implements Listenable {
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
+
+               // Set the protocol to TCP
+               $this->setProtocol('tcp');
        }
 
        /**
@@ -45,9 +48,6 @@ class TcpListener extends BaseListener implements Listenable {
                // Set the application instance
                $listenerInstance->setNodeInstance($nodeInstance);
 
-               // Set the protocol to TCP
-               $listenerInstance->setProtocol('tcp');
-
                // Return the prepared instance
                return $listenerInstance;
        }
@@ -65,7 +65,7 @@ class TcpListener extends BaseListener implements Listenable {
                // Is the socket resource valid?
                if (!is_resource($mainSocket)) {
                        // Something bad happened
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), 0, 'invalid'), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
                // Get socket error code for verification
@@ -73,12 +73,19 @@ 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'));
+                       /*
                        // Then throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
                // 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'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -89,14 +96,18 @@ class TcpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
                // "Bind" the socket to the given address, on given port so this means
                // that all connections on this port are now our resposibility to
                // send/recv data, disconnect, etc..
-               $this->debugOutput('LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
+               $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'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -107,12 +118,16 @@ class TcpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
                // Start listen for connections
-               $this->debugOutput('LISTENER: Listening for connections.');
+               $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'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -123,12 +138,16 @@ class TcpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
                // Now, we want non-blocking mode
-               $this->debugOutput('LISTENER: Setting non-blocking mode.');
+               $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'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -139,14 +158,15 @@ class TcpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
                // Set the main socket
                $this->registerServerSocketResource($mainSocket);
 
                // Initialize the peer pool instance
-               $poolInstance = ObjectFactory::createObjectByConfiguredName('peer_pool_class', array($this));
+               $poolInstance = ObjectFactory::createObjectByConfiguredName('node_pool_class', array($this));
 
                // Add main socket
                $poolInstance->addPeer($mainSocket);
@@ -162,13 +182,13 @@ class TcpListener extends BaseListener implements Listenable {
                $this->setIteratorInstance($iteratorInstance);
 
                // Initialize the network package handler
-               $packageInstance = ObjectFactory::createObjectByConfiguredName('tcp_network_package_handler_class');
+               $handlerInstance = ObjectFactory::createObjectByConfiguredName('tcp_raw_data_handler_class');
 
                // Set it in this class
-               $this->setPackageInstance($packageInstance);
+               $this->setHandlerInstance($handlerInstance);
 
                // Output message
-               $this->debugOutput('LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
+               $this->debugOutput('TCP-LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
        }
 
        /**
@@ -202,10 +222,13 @@ class TcpListener extends BaseListener implements Listenable {
                if (in_array($this->getSocketResource(), $readers)) {
                        // Then accept it
                        $newSocket = socket_accept($this->getSocketResource());
-                       //* NOISY-DEBUG: */ $this->debugOutput('LISTENER: newSocket=' . $newSocket);
+                       //* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: newSocket=' . $newSocket);
 
                        // We want non-blocking here, too
                        if (!socket_set_nonblock($newSocket)) {
+                               // Handle this socket error with a faked recipientData array
+                               $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
+                               /*
                                // Get socket error code for verification
                                $socketError = socket_last_error($newSocket);
 
@@ -216,7 +239,8 @@ class TcpListener extends BaseListener implements Listenable {
                                $this->shutdownSocket($newSocket);
 
                                // And throw the exception
-                               throw new InvalidSocketException(array($this, gettype($newSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                               throw new InvalidSocketException(array($this, $newSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                               */
                        } // END - if
 
                        // Add it to the peers
@@ -235,8 +259,8 @@ class TcpListener extends BaseListener implements Listenable {
                // Handle it here, if not main socket
                if ($currentSocket != $this->getSocketResource()) {
                        // ... or else it will raise warnings like 'Transport endpoint is not connected'
-                       //* NOISY-DEBUG: */ $this->debugOutput('LISTENER: currentSocket=' . $currentSocket);
-                       $this->getPackageInstance()->processResourceRawData($currentSocket);
+                       //* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: currentSocket=' . $currentSocket . ',server=' . $this->getSocketResource());
+                       $this->getHandlerInstance()->processRawDataFromResource($currentSocket);
                } // END - if
 
                // Advance to next entry. This should be the last line
@@ -244,13 +268,13 @@ class TcpListener extends BaseListener implements Listenable {
        }
 
        /**
-        * Checks wether the listener would accept the given package data array
+        * Checks whether the listener would accept the given package data array
         *
         * @param       $packageData    Raw package data
-        * @return      $accepts                Wether this listener does accept
+        * @return      $accepts                Whether this listener does accept
         */
-       function ifListenerAcceptsPackageData (array $packageData) {
-               $this->partialStub('This call should not happen. Please report.');
+       public function ifListenerAcceptsPackageData (array $packageData) {
+               $this->debugBackTrace('This call should not happen. Please report it.');
        }
 }