* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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 BaseListener extends BaseHubSystem implements Visitable { // Exception code constants const EXCEPTION_INVALID_SOCKET = 0xa00; const EXCEPTION_SOCKET_ALREADY_REGISTERED = 0xa01; /** * Used protocol (Default: invalid, which is indeed invalid...) */ private $protcol = 'invalid'; /** * Address (IP mostly) we shall listen on */ private $listenAddress = '0.0.0.0'; // This is the default and listens on all interfaces /** * Port we shall listen on (or wait for incoming data) */ private $listenPort = 0; // This port MUST be changed by your application /** * Wether we are in blocking or non-blocking mode (default: non-blocking */ private $blockingMode = false; /** * A client pool instance */ private $poolInstance = null; /** * A network package handler instance */ private $packageInstance = null; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); } /** * Checks wether the given socket resource is a server socket * * @param $socketResource A valid socket resource * @return $isServerSocket Wether the socket resource is a server socket */ protected function isServerSocketResource ($socketResource) { // Check it $isServerSocket = ((is_resource($socketResource)) && (!@socket_getpeername($socketResource, $peerName))); // We need to clear the error here socket_clear_error($socketResource); // Check peer name, it must be empty $isServerSocket = (($isServerSocket) && (empty($peerName))); // Return result return $isServerSocket; } /** * Setter for listen address * * @param $listenAddress The address this listener should listen on * @return void */ protected final function setListenAddress ($listenAddress) { $this->listenAddress = (string) $listenAddress; } /** * Getter for listen address * * @return $listenAddress The address this listener should listen on */ public final function getListenAddress () { return $this->listenAddress; } /** * Setter for listen port * * @param $listenPort The port this listener should listen on * @return void */ protected final function setListenPort ($listenPort) { $this->listenPort = (int) $listenPort; } /** * Getter for listen port * * @return $listenPort The port this listener should listen on */ public final function getListenPort () { return $this->listenPort; } /** * "Setter" to set listen address from configuration entry * * @param $configEntry The configuration entry holding our listen address * @return void */ public final function setListenAddressByConfiguration ($configEntry) { $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry)); } /** * "Setter" to set listen port from configuration entry * * @param $configEntry The configuration entry holding our listen port * @return void */ public final function setListenPortByConfiguration ($configEntry) { $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry)); } /** * Setter for protocol * * @param $protocol Used protocol * @return void */ protected final function setProtocol ($protocol) { $this->protocol = (string) $protocol; } /** * Getter for protocol * * @return $protocol Used protocol */ public final function getProtocol () { return $this->protocol; } /** * Setter for blocking-mode * * @param $blockingMode Wether blocking-mode is disabled (default) or enabled * @return void */ protected final function setBlockingMode ($blockingMode) { $this->blockingMode = (boolean) $blockingMode; } /** * Checks wether blocking-mode is enabled or disabled * * @return $blockingMode Wether blocking mode is disabled or enabled */ public final function isBlockingModeEnabled () { return $this->blockingMode; } /** * Setter for client pool instance * * @param $poolInstance The client pool instance we shall set * @return void */ protected final function setPoolInstance (PoolableClient $poolInstance) { $this->poolInstance = $poolInstance; } /** * Getter for client pool instance * * @return $poolInstance The client pool instance we shall set */ protected final function getPoolInstance () { return $this->poolInstance; } /** * Setter for network package handler instance * * @param $packageInstance The network package handler instance we shall set * @return void */ protected final function setPackageInstance (Networkable $packageInstance) { $this->packageInstance = $packageInstance; } /** * Getter for network package handler instance * * @return $packageInstance The network package handler instance we shall set */ protected final function getPackageInstance () { return $this->packageInstance; } /** * Registeres the given socket resource for "this" listener instance. This * will be done in a seperate class to allow package writers to use it * again. * * @param $socketResource A valid server socket resource * @return void * @throws InvalidServerSocketException If the given resource is no server socket * @throws SocketAlreadyRegisteredException If the given resource is already registered */ protected function registerServerSocketResource ($socketResource) { // First check if it is valid if (!$this->isServerSocketResource($socketResource)) { // No server socket throw new InvalidServerSocketException(array($this, $socketResource), self::EXCEPTION_INVALID_SOCKET); } elseif ($this->isServerSocketRegistered($socketResource)) { // Already registered throw new SocketAlreadyRegisteredException($this, self::EXCEPTION_SOCKET_ALREADY_REGISTERED); } // Get a socket registry instance (singleton) $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); // Register the socket $registryInstance->registerSocket($this, $socketResource); } /** * Checks wether given socket resource is registered in socket registry * * @param $socketResource A valid server socket resource * @return $isRegistered Wether given server socket is registered */ protected function isServerSocketRegistered ($socketResource) { // Get a socket registry instance (singleton) $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); // Check it $isRegistered = $registryInstance->isSocketRegistered($this, $socketResource); // Return result return $isRegistered; } /** * Getter for "this" socket resource * * @return $socketResource A valid socket resource */ public final function getSocketResource () { // Get a socket registry instance (singleton) $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); // Get the socket resource $socketResource = $registryInstance->getSocketResource($this); // Return it return $socketResource; } /** * Accepts the visitor to process the visit "request" * * @param $visitorInstance An instance of a Visitor class * @return void */ public function accept (Visitor $visitorInstance) { // Debug message //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - START'); // Visit this listener $visitorInstance->visitListener($this); // Visit the pool if set if ($this->getPoolInstance() instanceof Poolable) { $this->getPoolInstance()->accept($visitorInstance); } // END - if // Debug message //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED'); } } // [EOF] ?>