]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/listener/tcp/class_TcpListener.php
Updated 'core'.
[hub.git] / application / hub / main / listener / tcp / class_TcpListener.php
index 8d0a97596260c1d3d2507a0ccaa367eaea9ccebc..d3fbcf35c6dfb11a391de971372d47c781842f1c 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A TCP connection listener
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -32,7 +32,7 @@ class TcpListener extends BaseListener implements Listenable {
                parent::__construct(__CLASS__);
 
                // Set the protocol to TCP
-               $this->setProtocol('tcp');
+               $this->setProtocolName('tcp');
        }
 
        /**
@@ -45,9 +45,6 @@ class TcpListener extends BaseListener implements Listenable {
                // Get new instance
                $listenerInstance = new TcpListener();
 
-               // Set the application instance
-               $listenerInstance->setNodeInstance($nodeInstance);
-
                // Return the prepared instance
                return $listenerInstance;
        }
@@ -105,7 +102,7 @@ class TcpListener extends BaseListener implements Listenable {
                 * that all connections on this port are now our resposibility to
                 * send/recv data, disconnect, etc..
                 */
-               $this->debugOutput('TCP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: 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(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
@@ -125,7 +122,7 @@ class TcpListener extends BaseListener implements Listenable {
                } // END - if
 
                // Start listen for connections
-               $this->debugOutput('TCP-LISTENER: Listening for connections.');
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Listening for connections.');
                if (!socket_listen($mainSocket)) {
                        // Handle this socket error with a faked recipientData array
                        $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
@@ -145,7 +142,7 @@ class TcpListener extends BaseListener implements Listenable {
                } // END - if
 
                // Now, we want non-blocking mode
-               $this->debugOutput('TCP-LISTENER: Setting non-blocking mode.');
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.');
                if (!socket_set_nonblock($mainSocket)) {
                        // Handle this socket error with a faked recipientData array
                        $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
@@ -190,7 +187,7 @@ class TcpListener extends BaseListener implements Listenable {
                $this->setHandlerInstance($handlerInstance);
 
                // Output message
-               $this->debugOutput('TCP-LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
+               self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
        }
 
        /**
@@ -217,14 +214,14 @@ class TcpListener extends BaseListener implements Listenable {
                // Some new peers found?
                if ($left < 1) {
                        // Debug message
-                       //* EXTREME-NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: left=' . $left . ',server=' . $this->getSocketResource() . ',readers=' . print_r($readers, true));
+                       //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: left=' . $left . ',serverSocket=' . $this->getSocketResource() . ',readers=' . print_r($readers, TRUE));
 
                        // Nothing new found
                        return;
                } // END - if
 
                // Debug message
-               /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER:server=' . $this->getSocketResource() . ',readers=' . print_r($readers, true));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: serverSocket=' . $this->getSocketResource() . ',readers=' . print_r($readers, TRUE));
 
                // Do we have changed peers?
                if (in_array($this->getSocketResource(), $readers)) {
@@ -238,7 +235,7 @@ class TcpListener extends BaseListener implements Listenable {
                        $newSocket = socket_accept($this->getSocketResource());
 
                        // Debug message
-                       /* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: newSocket=' . $newSocket . ',server=' .$this->getSocketResource());
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: newSocket=' . $newSocket . ',serverSocket=' .$this->getSocketResource());
 
                        // Array for timeout settings
                        $options  = array(
@@ -258,7 +255,7 @@ class TcpListener extends BaseListener implements Listenable {
                        // Output result (only for debugging!)
                        /*
                        $option = socket_get_option($newSocket, SOL_SOCKET, SO_RCVTIMEO);
-                       $this->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, true));
+                       self::createDebugInstance(__CLASS__)->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, TRUE));
                        */
 
                        // Enable SO_OOBINLINE
@@ -282,19 +279,27 @@ class TcpListener extends BaseListener implements Listenable {
                                $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
                        } // END - if
 
+                       // Get node instance
+                       $nodeInstance = Registry::getRegistry()->getInstance('node');
+
                        // Create a faked package data array
                        $packageData = array(
                                NetworkPackage::PACKAGE_DATA_SENDER    => $peerName . ':0',
-                               NetworkPackage::PACKAGE_DATA_RECIPIENT => $this->getSessionId(),
-                               NetworkPackage::PACKAGE_DATA_PROTOCOL  => $this->getProtocol(),
+                               NetworkPackage::PACKAGE_DATA_RECIPIENT => $nodeInstance->getSessionId(),
                                NetworkPackage::PACKAGE_DATA_STATUS    => NetworkPackage::PACKAGE_STATUS_FAKED
                        );
 
+                       // Get a connection info instance
+                       $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
+
+                       // Will the info instance with listener data
+                       $infoInstance->fillWithListenerInformation($this);
+
                        // Get a socket registry
                        $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
 
                        // Register the socket with the registry and with the faked array
-                       $registryInstance->registerSocket($this, $newSocket, $packageData);
+                       $registryInstance->registerSocket($infoInstance, $newSocket, $packageData);
                } // END - if
 
                // Do we have to rewind?
@@ -306,14 +311,14 @@ class TcpListener extends BaseListener implements Listenable {
                // Get the current value
                $currentSocket = $this->getIteratorInstance()->current();
 
-               // Handle it here, if not main socket
-               /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: currentSocket=' . $currentSocket[BasePool::SOCKET_ARRAY_RESOURCE] . ',type=' . $currentSocket[BasePool::SOCKET_ARRAY_CONN_TYPE] . ',server=' . $this->getSocketResource());
+               // Handle it here, if not main server socket
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: currentSocket=' . $currentSocket[BasePool::SOCKET_ARRAY_RESOURCE] . ',type=' . $currentSocket[BasePool::SOCKET_ARRAY_CONN_TYPE] . ',serverSocket=' . $this->getSocketResource());
                if (($currentSocket[BasePool::SOCKET_ARRAY_CONN_TYPE] != BaseConnectionHelper::CONNECTION_TYPE_SERVER) && ($currentSocket[BasePool::SOCKET_ARRAY_RESOURCE] != $this->getSocketResource())) {
                        // ... or else it will raise warnings like 'Transport endpoint is not connected'
                        $this->getHandlerInstance()->processRawDataFromResource($currentSocket);
                } // END - if
 
-               // Advance to next entry. This should be the last line
+               // Advance to next entry. This should be the last line.
                $this->getIteratorInstance()->next();
        }
 
@@ -322,9 +327,11 @@ class TcpListener extends BaseListener implements Listenable {
         *
         * @param       $packageData    Raw package data
         * @return      $accepts                Whether this listener does accept
+        * @throws      UnsupportedOperationException   If this method is called
         */
        public function ifListenerAcceptsPackageData (array $packageData) {
-               $this->debugBackTrace('This call should not happen. Please report it.');
+               // Please don't call this
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
        }
 }