X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Flistener%2Fclass_BaseListener.php;h=b81ffd2027d98f28c52c1a78a443f4fb2b481cad;hb=9aa0ae335d8821392ae8a97f9a0c05638a131e66;hp=f86ff5100af73dce28d42750d3b9dd1ef8f62dd6;hpb=ffa45cc277ea3b09d12553e4257352a88b30f2c9;p=hub.git diff --git a/application/hub/main/listener/class_BaseListener.php b/application/hub/main/listener/class_BaseListener.php index f86ff5100..b81ffd202 100644 --- a/application/hub/main/listener/class_BaseListener.php +++ b/application/hub/main/listener/class_BaseListener.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -21,7 +21,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BaseListener extends BaseHubSystem { +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 $protocol = 'invalid'; + /** * Address (IP mostly) we shall listen on */ @@ -37,6 +46,11 @@ class BaseListener extends BaseHubSystem { */ private $blockingMode = false; + /** + * A peer pool instance + */ + private $poolInstance = null; + /** * Protected constructor * @@ -48,6 +62,30 @@ class BaseListener extends BaseHubSystem { 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 if it is a resource + if ($isServerSocket === true) { + // Clear the error + //* DEBUG: */ $this->debugOutput('socketResource[]=' . gettype($socketResource)); + socket_clear_error($socketResource); + } // END - if + + // Check peer name, it must be empty + $isServerSocket = (($isServerSocket) && (empty($peerName))); + + // Return result + return $isServerSocket; + } + /** * Setter for listen address * @@ -86,6 +124,15 @@ class BaseListener extends BaseHubSystem { return $this->listenPort; } + /** + * Getter for port number to satify ProtocolHandler + * + * @return $port The port number + */ + public final function getPort () { + return $this->getListenPort(); + } + /** * "Setter" to set listen address from configuration entry * @@ -93,7 +140,7 @@ class BaseListener extends BaseHubSystem { * @return void */ public final function setListenAddressByConfiguration ($configEntry) { - $this->setListenAddress($this->getConfigInstance()->readConfig($configEntry)); + $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry)); } /** @@ -103,7 +150,159 @@ class BaseListener extends BaseHubSystem { * @return void */ public final function setListenPortByConfiguration ($configEntry) { - $this->setListenPort($this->getConfigInstance()->readConfig($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 peer pool instance + * + * @param $poolInstance The peer pool instance we shall set + * @return void + */ + protected final function setPoolInstance (PoolablePeer $poolInstance) { + $this->poolInstance = $poolInstance; + } + + /** + * Getter for peer pool instance + * + * @return $poolInstance The peer pool instance we shall set + */ + public final function getPoolInstance () { + return $this->poolInstance; + } + + /** + * 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->getRegisteredSocketResource($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(strtoupper($this->getProtocol()) . '-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(strtoupper($this->getProtocol()) . '-LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED'); + } + + /** + * Monitors incoming raw data from the handler and transfers it to the + * given receiver instance. This method should not be called, please call + * the decorator's version instead to seperator node/client traffic. + * + * @param $receiverInstance An instance of a Receivable class + * @return void + * @throws UnsupportedOperatorException If this method is called by a mistake + */ + public function monitorIncomingRawData (Receivable $receiverInstance) { + throw new UnsupportedOperationException(array($this, __FUNCTION__, $receiverInstance), self::EXCEPTION_UNSPPORTED_OPERATION); } }