]> git.mxchange.org Git - hub.git/commitdiff
Continued rewrite:
authorRoland Häder <roland@mxchange.org>
Sun, 21 May 2017 20:51:32 +0000 (22:51 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 21 Aug 2020 16:50:06 +0000 (18:50 +0200)
- rewrote TcpListener (should be working, untested) towards SocketFactory
- added missing but needed methods in BaseListenerDecorator which extends
  BaseDecorator which is framework (generic)
- renamed method to enableSocketReuseAddress() to make it similar to existings
- added missing namespaces

Signed-off-by: Roland Häder <roland@mxchange.org>
application/hub/classes/factories/socket/class_SocketFactory.php
application/hub/classes/listener/class_BaseListenerDecorator.php
application/hub/classes/listener/socket/class_SocketFileListener.php
application/hub/classes/listener/tcp/class_TcpListener.php
application/hub/config.php

index 44cd11ff7818240ffef9893fbfb24bd9da80c5f0..19301d24a01abc0b50c1fbc321821416d8c852e4 100644 (file)
@@ -222,7 +222,7 @@ class SocketFactory extends ObjectFactory {
                } // END - if
 
                // Set the option to reuse the port
-               if (!$socketInstance->enableReuseAddress()) {
+               if (!$socketInstance->enableSocketReuseAddress()) {
                        // Handle this socket error with a faked recipientData array
                        $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
                } // END - if
@@ -241,4 +241,73 @@ class SocketFactory extends ObjectFactory {
                return $socketInstance;
        }
 
+       /**
+        * Creates a listening socket instance from given listener instance
+        *
+        * @param       $listenerInstance       An instance of a Listenable class
+        * @return      $socketInstance         An instance of a StorableSocket class
+        * @throws      InvalidSocketException  Thrown if the socket could not be initialized
+        */
+       public static function createListenTcpSocket (Listenable $listenerInstance) {
+               // Create a streaming socket, of type TCP/IP
+               $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+
+               // @TODO Init fake package data with address/port from listener
+               $packageData = array(
+               );
+
+               // Create socket instance
+               $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, 'file', $packageData, NULL));
+
+               // Is the socket resource valid?
+               if (!$socketInstance->isValidSocket()) {
+                       // Something bad happened
+                       throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
+               // Get socket error code for verification
+               $socketError = $socketInstance->getLastSocketErrorCode();
+
+               // Check if there was an error else
+               if ($socketError > 0) {
+                       // Handle this socket error with a faked recipientData array
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+               } // END - if
+
+               // Set the option to reuse the port
+               // @TODO: , SOL_SOCKET, SO_REUSEADDR, 1
+               if (!$socketInstance->enableSocketReuseAddress()) {
+                       // Handle this socket error with a faked recipientData array
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+               } // 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..
+                */
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
+               if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) {
+                       // Handle this socket error with a faked recipientData array
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+               } // END - if
+
+               // Start listen for connections
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Listening for connections.');
+               if (!$socketInstance->listenToSocket()) {
+                       // Handle this socket error with a faked recipientData array
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+               } // END - if
+
+               // Now, we want non-blocking mode
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Setting non-blocking mode.');
+               if (!$socketInstance->enableSocketNonBlocking()) {
+                       // Handle this socket error with a faked recipientData array
+                       $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+               } // END - if
+
+               // Return prepepared socket
+               return $socketInstance;
+       }
+
 }
index 0fb77065aa0512814932f1d4a4dc64ba4c3c7ed2..2b438e5652bb91da5e97df1417cff2475e451836 100644 (file)
@@ -3,6 +3,8 @@
 namespace Hub\Listener;
 
 // Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
+use Hub\Listener\Listenable;
 use Hub\Network\Networkable;
 
 // Import framework stuff
@@ -38,6 +40,16 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable {
         */
        private $listenerType = 'invalid';
 
+       /**
+        * Name of used protocol
+        */
+       private $protocolName = 'invalid';
+
+       /**
+        * The decorated listener instance
+        */
+       private $listenerInstance = NULL;
+
        /**
         * Protected constructor
         *
@@ -159,4 +171,125 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable {
                $receiverInstance->addRawDataToIncomingStack($handlerInstance);
        }
 
+       /**
+        * Setter for socket instance
+        *
+        * @param       $socketInstance A StorableSocket instance
+        * @return      void
+        */
+       public final function setSocketInstance (StorableSocket $socketInstance) {
+               $this->socketInstance = $socketInstance;
+       }
+
+       /**
+        * Getter for socket instance
+        *
+        * @return      $socketInstance An instance of a StorableSocket class
+        */
+       public final function getSocketInstance () {
+               return $this->socketInstance;
+       }
+
+       /**
+        * Checks whether start/end marker are set
+        *
+        * @param       $data   Data to be checked
+        * @return      $isset  Whether start/end marker are set
+        */
+       public function ifStartEndMarkersSet ($data) {
+               $this->partialSub('Please implement this method.');
+       }
+
+       /**
+        * Getter for listener pool instance
+        *
+        * @return      $listenerPoolInstance   Our current listener pool instance
+        */
+       public function getListenerPoolInstance () {
+               $this->partialSub('Please implement this method.');
+       }
+
+       /**
+        * Getter for info instance
+        *
+        * @return      $infoInstance   An instance of a ShareableInfo class
+        */
+       public function getInfoInstance () {
+               $this->partialSub('Please implement this method.');
+       }
+
+       /**
+        * Getter for node id
+        *
+        * @return      $nodeId         Current node id
+        */
+       public function getNodeId () {
+               $this->partialSub('Please implement this method.');
+       }
+
+       /**
+        * Getter for private key
+        *
+        * @return      $privateKey             Current private key
+        */
+       public function getPrivateKey () {
+               $this->partialSub('Please implement this method.');
+       }
+
+       /**
+        * Getter for private key hash
+        *
+        * @return      $privateKeyHash         Current private key hash
+        */
+       public function getPrivateKeyHash () {
+               $this->partialSub('Please implement this method.');
+       }
+
+       /**
+        * Getter for session id
+        *
+        * @return      $sessionId      Current session id
+        */
+       public function getSessionId () {
+               $this->partialSub('Please implement this method.');
+       }
+
+       /**
+        * Setter for listener instance
+        *
+        * @param       $listenerInstance       A Listenable instance
+        * @return      void
+        */
+       protected final function setListenerInstance (Listenable $listenerInstance) {
+               $this->listenerInstance = $listenerInstance;
+       }
+
+       /**
+        * Getter for listener instance
+        *
+        * @return      $listenerInstance       A Listenable instance
+        */
+       protected final function getListenerInstance () {
+               return $this->listenerInstance;
+       }
+
+       /**
+        * Getter for protocol name
+        *
+        * @return      $protocolName   Name of used protocol
+        */
+       public final function getProtocolName () {
+               return $this->protocolName;
+       }
+
+       /**
+        * Setter for protocol name
+        *
+        * @param       $protocolName   Name of used protocol
+        * @return      void
+        */
+       protected final function setProtocolName ($protocolName) {
+               $this->protocolName = $protocolName;
+       }
+
 }
index 82392497755206ac62d598de0ec03c4684007a20..62a086ae33fc7985ba87b8ac895905a8dcb10f34 100644 (file)
@@ -96,7 +96,7 @@ class SocketFileListener extends BaseListener implements Listenable {
                $this->setHandlerInstance($handlerInstance);
 
                // Output message
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Socket listener now ready on socket ' . $socketFile . ' for service.');
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Socket listener now ready on socket ' . $socketInstance->getSocketResource() . ' for service.');
        }
 
        /**
index 58c9021723aa08423aa8f7760717f5b96b1f175e..9c39cad4dada79546edd3739274d86b6e92e1ee2 100644 (file)
@@ -3,6 +3,7 @@
 namespace Hub\Listener\Tcp;
 
 // Import application-specific stuff
+use Hub\Factory\Socket\SocketFactory;
 use Hub\Helper\Connection\BaseConnectionHelper;
 use Hub\Listener\BaseListener;
 use Hub\Listener\Listenable;
@@ -68,119 +69,17 @@ class TcpListener extends BaseListener implements Listenable {
         * @todo        Needs rewrite!
         */
        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, $mainSocket), self::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) {
-                       // Handle this socket error with a faked recipientData array
-                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
-                       /*
-                       // Then throw again
-                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), self::EXCEPTION_INVALID_SOCKET);
-                       */
-               } // END - if
-
-               // Set the option to reuse the port
-               if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
-                       // Handle this socket error with a faked recipientData array
-                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
-                       /*
-                       // 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, $mainSocket, $socketError, $errorMessage), self::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..
-                */
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: 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'));
-                       /*
-                       // 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, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET);
-                       */
-               } // END - if
-
-               // Start listen for connections
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: 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'));
-                       /*
-                       // 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, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET);
-                       */
-               } // END - if
-
-               // Now, we want non-blocking mode
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: 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'));
-                       /*
-                       // 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, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET);
-                       */
-               } // END - if
-
-               // Set the main socket
-               $this->registerServerSocketInstance($mainSocket);
+               // Get instance from socket factory
+               $socketInstance = SocketFactory::createListenTcpSocket($this);
+
+               // Set socket instance
+               $this->registerServerSocketInstance($socketInstance);
 
                // Initialize the peer pool instance
                $poolInstance = ObjectFactory::createObjectByConfiguredName('node_pool_class', array($this));
 
                // Add main socket
-               $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
+               $poolInstance->addPeer($socketInstance, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
 
                // And add it to this listener
                $this->setPoolInstance($poolInstance);
index b3da984059283eda9451ff1670e29a678d987259..79997cf3d0e77d655f71682f1c199fe28aa86fb5 100644 (file)
@@ -70,10 +70,10 @@ $cfg->setConfigEntry('web_content_type', '');
 $cfg->setConfigEntry('listener_pool_class', 'Hub\Pool\Listener\DefaultListenerPool');
 
 // CFG: TCP-LISTENER-CLASS
-$cfg->setConfigEntry('tcp_listener_class', 'TcpListener');
+$cfg->setConfigEntry('tcp_listener_class', 'Hub\Listener\Tcp\TcpListener');
 
 // CFG: UDP-LISTENER-CLASS
-$cfg->setConfigEntry('udp_listener_class', 'UdpListener');
+$cfg->setConfigEntry('udp_listener_class', 'Hub\Listener\Udp\UdpListener');
 
 // CFG: SOCKET-FILE-LISTENER-CLASS
 $cfg->setConfigEntry('socket_file_listener_class', 'Hub\Listener\Socket\SocketFileListener');