]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/listener/class_BaseListener.php
Removed deprecated code (see last commit why)
[hub.git] / application / hub / main / listener / class_BaseListener.php
index ababd57ed1e9880bc67f94451729e905cdf78c66..b81ffd2027d98f28c52c1a78a443f4fb2b481cad 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 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-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
+        */
+       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 peer pool instance
+        */
+       private $poolInstance = null;
+
        /**
         * Protected constructor
         *
@@ -32,6 +61,249 @@ class BaseListener extends BaseHubSystem {
                // 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 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
+        *
+        * @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;
+       }
+
+       /**
+        * 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
+        *
+        * @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 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);
+       }
 }
 
 // [EOF]