* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.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 * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class TcpListener extends BaseListener implements Listenable { /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this class * * @param $nodeInstance A NodeHelper instance * @return $listenerInstance An instance a prepared listener class */ public final static function createTcpListener (NodeHelper $nodeInstance) { // Get new instance $listenerInstance = new TcpListener(); // Set the application instance $listenerInstance->setNodeInstance($nodeInstance); // Set the protocol to TCP $listenerInstance->setProtocol('tcp'); // Return the prepared instance return $listenerInstance; } /** * Initializes the listener by setting up the required socket server * * @return void * @throws InvalidSocketException Thrown if the socket could not be initialized */ public function initListener() { // Create a streaming socket, of type TCP/IP $mainSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // 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); } // END - if // Get socket error code for verification $socketError = socket_last_error($mainSocket); // Check if there was an error else if ($socketError > 0) { // Then throw again throw new InvalidSocketException(array($this, gettype($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)) { // Get socket error code for verification $socketError = socket_last_error($mainSocket); // Get error message $errorMessage = socket_strerror($socketError); // Shutdown this socket $this->shutdownSocket($mainSocket); // And throw again throw new InvalidSocketException(array($this, gettype($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.. if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) { // Get socket error code for verification $socketError = socket_last_error($mainSocket); // Get error message $errorMessage = socket_strerror($socketError); // Shutdown this socket $this->shutdownSocket($mainSocket); // And throw again throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if // Start listen for connections if (!socket_listen($mainSocket)) { // Get socket error code for verification $socketError = socket_last_error($mainSocket); // Get error message $errorMessage = socket_strerror($socketError); // Shutdown this socket $this->shutdownSocket($mainSocket); // And throw again throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if // Set the main socket $this->setSocketResource($mainSocket); // Initialize the client pool instance $poolInstance = ObjectFactory::createObjectByConfiguredName('client_pool_class', array($this)); // Add main socket $poolInstance->addClient($mainSocket); // And add it to this listener $this->setPoolInstance($poolInstance); // Initialize iterator for listening on packages $iteratorInstance = ObjectFactory::createObjectByConfiguredName('network_listen_iterator_class', array($poolInstance->getPoolEntriesInstance())); // Rewind it and remember it in this class $iteratorInstance->rewind(); $this->setIteratorInstance($iteratorInstance); // Output message $this->debugOutput('LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.'); } /** * "Listens" for incoming network packages * * @return void * @todo 0% done */ public function doListen () { // Get all readers $readers = $this->getPoolInstance()->getAllSockets(); // Check if we have some clients left $left = socket_select($readers, $writers = null, $excepts = null, 0, 150); // Some new clients found? if ($left < 1) { // Nothing new found return; } // END - if // Do we have changed clients? if (in_array($this->getSocketResource(), $readers)) { // Then accept it $newSocket = socket_accept($this->getSocketResource()); // Add it to the clients $this->getPoolInstance()->addClient($newSocket); } // END - if // Do we have to rewind? if (!$this->getIteratorInstance()->valid()) { // Rewind the list $this->getIteratorInstance()->rewind(); } // END - if // Get the current value $current = $this->getIteratorInstance()->current(); // Handle it here $this->partialStub('current['.gettype($current).']='.$current); // Advance to next entry. This should be the last line $this->getIteratorInstance()->next(); die("OK!\n"); } } // [EOF] ?>