]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Mon, 26 Oct 2020 03:54:19 +0000 (04:54 +0100)
committerRoland Häder <roland@mxchange.org>
Mon, 26 Oct 2020 03:56:44 +0000 (04:56 +0100)
- SocketContainer->bindSocketTo() is now split into bindSocketToFile() and
  bindSocketToListener()
- also fixed wrong getter/setter invocation

Signed-off-by: Roland Häder <roland@mxchange.org>
application/hub/classes/container/socket/class_SocketContainer.php
application/hub/classes/factories/socket/class_SocketFactory.php
application/hub/classes/package/deliverable/class_PackageData.php
application/hub/interfaces/container/socket/class_StorableSocket.php
application/hub/interfaces/delivery/package/class_DeliverablePackage.php

index 170bcb0201aebe840543666ca44f86e9a7df0c3d..3de25c65eab3ac56893279d99b619369085a1d2f 100644 (file)
@@ -16,6 +16,7 @@ use Org\Shipsimu\Hub\Network\Package\NetworkPackageHandler;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\NullPointerException;
 use Org\Mxchange\CoreFramework\Registry\Registerable;
 use Org\Mxchange\CoreFramework\Socket\InvalidSocketException;
 use Org\Mxchange\CoreFramework\Socket\NoSocketErrorDetectedException;
@@ -30,6 +31,7 @@ use Org\Mxchange\CoreFramework\Visitor\Visitor;
 use \BadMethodCallException;
 use \InvalidArgumentException;
 use \LogicException;
+use \SplFileInfo;
 
 /**
  * A Socket Container class
@@ -76,6 +78,16 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
         */
        private $packageDataInstance = NULL;
 
+       /**
+        * An instance of a SplFileInfo class
+        */
+       private $socketFile;
+
+       /**
+        * Socket type
+        */
+       private $socketType;
+
        /**
         * Protected constructor
         *
@@ -507,30 +519,48 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
        }
 
        /**
-        * Tries to bind the socket.
+        * Tries to bind to socket file
         *
         * @param       $bindAddress    Where to bind the socket to (e.g. Uni* socket file)
         * @param       $bindPort               Optional port to bind to
         * @return      $result         Result from binding socket
         * @throws      InvalidSocketException  If socket is invalid
         */
-       public function bindSocketTo ($bindAddress, $bindPort = 0) {
+       public function bindSocketToFile () {
                // Trace message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+       }
 
+       /**
+        * Tries to bind to listener's address and port
+        *
+        * @param       $bindAddress    Where to bind the socket to (e.g. Uni* socket file)
+        * @param       $bindPort               Optional port to bind to
+        * @return      $result         Result from binding socket
+        * @throws      InvalidSocketException  If socket is invalid
+        * @throws      NullPointerException    If listener instance is not given
+        */
+       public function bindSocketToListener () {
                // Should be valid socket
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
                if (!$this->isValidSocket()) {
                        // Throw exception
                        throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
-               } // END - if
+               } elseif (is_null($this->getListenerInstance())) {
+                       // Required listener not set
+                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
+               }
 
-               // Try to bind it to
-               $result = socket_bind($this->getSocketResource(), $bindAddress, $bindPort);
+               // Get bind address and port
+               $address = $this->getListenerInstance()->getListenAddress();
+               $port = $this->getListenerInstance()->getListenAddress();
 
-               // Trace message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
+               // Try to bind it to
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Binding socket to %s:%d ...', strtoupper($this->getSocketProtocol()), $address, $port));
+               $result = socket_bind($this->getSocketResource(), $address, $port);
 
                // Return result
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
                return $result;
        }
 
@@ -1485,4 +1515,44 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
                return $result;
        }
 
+       /**
+        * Getter for socket file instance
+        *
+        * @return      $socketFile An instance of a SplFileInfo class
+        */
+       public function getSocketFile () {
+               return $this->socketFile;
+       }
+
+       /**
+        * Setter for socket file instance
+        *
+        * @param       $socketFile An instance of a SplFileInfo class
+        * @return      void
+        */
+       public function setSocketFile (SplFileInfo $socketFile) {
+               /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Setting socketFile=%s ...', strtoupper($this->getSocketType()), $socketFile));
+               $this->socketFile = $socketFile;
+       }
+
+       /**
+        * Getter for socket type
+        *
+        * @return      $socketType             Stocket type
+        */
+       public function getSocketType () {
+               return $this->socketType;
+       }
+
+       /**
+        * Setter for socket type
+        *
+        * @param       $socketType             Socket type
+        * @return      void
+        */
+       public function setSocketType ($socketType) {
+               /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Setting socketType=%s ...', strtoupper($this->getSocketType()), $socketType));
+               $this->socketType = $socketType;
+       }
+
 }
index c41de754545e5b7c38eab8087c5b458b5cb4bca9..65e00eaa90f3cc74557e39d4773b84eb64f70538 100644 (file)
@@ -127,10 +127,6 @@ class SocketFactory extends ObjectFactory {
                // Init package instance
                $packageInstance = PackageDataFactory::createPackageDataInstance();
 
-               // Set file socket data
-               $packageInstance->setSocketFile($socketFile);
-               $packageInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
-
                // Init main socket
                $socketResource = socket_create(AF_UNIX, SOCK_STREAM, 0);
 
@@ -141,6 +137,10 @@ class SocketFactory extends ObjectFactory {
                        $packageInstance,
                ));
 
+               // Set file socket data
+               $socketInstance->setSocketFile($socketFile);
+               $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
+
                // Debug message
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance)));
 
@@ -157,9 +157,9 @@ class SocketFactory extends ObjectFactory {
                } // END - if
 
                // Is the file there?
-               if ((FrameworkBootstrap::isReachableFilePath($packageInstance->getSocketFile())) && (file_exists($packageInstance->getSocketFile()))) {
+               if ((FrameworkBootstrap::isReachableFilePath($socketInstance->getSocketFile())) && (file_exists($socketInstance->getSocketFile()))) {
                        // Old socket found
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $packageInstance->getSocketFile() . ' found. Will not start.');
+                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $socketInstance->getSocketFile() . ' found. Will not start.');
 
                        // Shutdown this socket
                        $socketInstance->shutdownSocket();
@@ -169,10 +169,10 @@ class SocketFactory extends ObjectFactory {
                } // END - if
 
                // Debug message
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $packageInstance->getSocketFile() . ' ...');
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...');
 
                // Try to bind to it
-               if (!$socketInstance->bindSocketTo($packageInstance->getSocketFile())) {
+               if (!$socketInstance->bindSocketToFile()) {
                        // Handle error here
                        $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
                } // END - if
@@ -295,8 +295,6 @@ class SocketFactory extends ObjectFactory {
                // Set listener instance and type
                $socketInstance->setListenerInstance($listenerInstance);
                $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
-               $socketInstance->setSocketListenAddress($listenerInstance->getListenAddress());
-               $socketInstance->setSocketListenPort($listenerInstance->getListenPort());
 
                // Is the socket resource valid?
                if (!$socketInstance->isValidSocket()) {
@@ -323,7 +321,7 @@ class SocketFactory extends ObjectFactory {
                 * send/recv data, disconnect, etc..
                 */
                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
-               if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) {
+               if (!$socketInstance->bindSocketToListener()) {
                        // Handle this socket error with a faked recipientData array
                        $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
                } // END - if
@@ -376,8 +374,6 @@ class SocketFactory extends ObjectFactory {
                // Set listener instance and socket type
                $socketInstance->setListenerInstance($listenerInstance);
                $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
-               $socketInstance->setSocketListenAddress($listenerInstance->getListenAddress());
-               $socketInstance->setSocketListenPort($listenerInstance->getListenPort());
 
                // Is the socket resource valid?
                if (!$socketInstance->isValidSocket()) {
@@ -404,7 +400,7 @@ class SocketFactory extends ObjectFactory {
                 * send/recv data, disconnect, etc..
                 */
                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
-               if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) {
+               if (!$socketInstance->bindSocketToListener()) {
                        // Handle this socket error with a faked recipientData array
                        $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
                } // END - if
index 911536ff078101b32f0ffecaec93ac124cb10f91..e512e18359c9d01edbce2672220d7c49ad26d460 100644 (file)
@@ -8,9 +8,6 @@ use Org\Shipsimu\Hub\Generic\BaseHubSystem;
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Registry\Registerable;
 
-// Import SPL stuff
-use \SplFileInfo;
-
 /**
  * A DeliverablePackage class for raw package data
  *
@@ -45,16 +42,6 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera
         */
        private $packageContent;
 
-       /**
-        * An instance of a SplFileInfo class
-        */
-       private $socketFile;
-
-       /**
-        * Socket type
-        */
-       private $socketType;
-
        /**
         * Protected constructor
         *
@@ -78,46 +65,6 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera
                return $packageInstance;
        }
 
-       /**
-        * Getter for socket file instance
-        *
-        * @return      $socketFile An instance of a SplFileInfo class
-        */
-       public function getSocketFile () {
-               return $this->socketFile;
-       }
-
-       /**
-        * Setter for socket file instance
-        *
-        * @param       $socketFile An instance of a SplFileInfo class
-        * @return      void
-        */
-       public function setSocketFile (SplFileInfo $socketFile) {
-               /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-DATA: Setting socketFile=%s ...', $socketFile));
-               $this->socketFile = $socketFile;
-       }
-
-       /**
-        * Getter for socket type
-        *
-        * @return      $socketType             Stocket type
-        */
-       public function getSocketType () {
-               return $this->socketType;
-       }
-
-       /**
-        * Setter for socket type
-        *
-        * @param       $socketType             Socket type
-        * @return      void
-        */
-       public function setSocketType ($socketType) {
-               /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-DATA: Setting socketType=%s ...', $socketType));
-               $this->socketType = $socketType;
-       }
-
        /**
         * Getter for package content
         *
index c67c53a5c074309ca97aeeef485882c1b7b22ab1..943648258fee57db9e27c3db6bb3e92b78718e41 100644 (file)
@@ -8,6 +8,9 @@ use Org\Shipsimu\Hub\Information\ShareableInfo;
 // Inport frameworks stuff
 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 
+// Import PHP stuff
+use \SplFileInfo;
+
 /**
  * An interface for socket containers
  *
@@ -72,13 +75,21 @@ interface StorableSocket extends FrameworkInterface {
        const CONNECTION_TYPE_SERVER   = 'server';
 
        /**
-        * Tries to bind the socket.
+        * Tries to bind to attached socket file
+        *
+        * @return      $result         Result from binding socket
+        * @throws      InvalidSocketException  If stored socket is invalid
+        */
+       function bindSocketToFile ();
+
+       /**
+        * Tries to bind to attached listener's address and port
         *
-        * @param       $bindAddress    Where to bind the socket to (e.g. Uni* socket file)
         * @return      $result         Result from binding socket
         * @throws      InvalidSocketException  If stored socket is invalid
+        * @throws      NullPointerException    If listener instance is not given
         */
-       function bindSocketTo ($bindAddress);
+       function bindSocketToListener ();
 
        /**
         * Tries to listen on the socket
@@ -318,4 +329,34 @@ interface StorableSocket extends FrameworkInterface {
         */
        function readDataFromSocket ();
 
+       /**
+        * Getter for socket file instance
+        *
+        * @return      $socketFile     An instance of a SplFileInfo class
+        */
+       public function getSocketFile ();
+
+       /**
+        * Setter for socket file instance
+        *
+        * @param       $socketFile     An instance of a SplFileInfo class
+        * @return      void
+        */
+       public function setSocketFile (SplFileInfo $socketFile);
+
+       /**
+        * Getter for socket type
+        *
+        * @return      $socketFile     Stocket type
+        */
+       public function getSocketType ();
+
+       /**
+        * Setter for socket type
+        *
+        * @param       $socketType     Socket type
+        * @return      void
+        */
+       public function setSocketType ($socketType);
+
 }
index 41a7402c6bdbe16b9ae37aefa4684a2399f7512b..16df801be8f80595911e204fa885525346f14025 100644 (file)
@@ -5,9 +5,6 @@ namespace Org\Shipsimu\Hub\Network\Package;
 // Import application-specific stuff
 use Org\Shipsimu\Hub\Generic\HubInterface;
 
-// Import PHP stuff
-use \SplFileInfo;
-
 /**
  * An interface for package delivery boys... ;-)
  *
@@ -32,34 +29,4 @@ use \SplFileInfo;
  */
 interface DeliverablePackage extends HubInterface {
 
-       /**
-        * Getter for socket file instance
-        *
-        * @return      $socketFile     An instance of a SplFileInfo class
-        */
-       public function getSocketFile ();
-
-       /**
-        * Setter for socket file instance
-        *
-        * @param       $socketFile     An instance of a SplFileInfo class
-        * @return      void
-        */
-       public function setSocketFile (SplFileInfo $socketFile);
-
-       /**
-        * Getter for socket type
-        *
-        * @return      $socketFile     Stocket type
-        */
-       public function getSocketType ();
-
-       /**
-        * Setter for socket type
-        *
-        * @param       $socketType     Socket type
-        * @return      void
-        */
-       public function setSocketType ($socketType);
-
 }