]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Thu, 26 Dec 2024 05:38:14 +0000 (06:38 +0100)
committerRoland Häder <roland@mxchange.org>
Thu, 26 Dec 2024 05:41:42 +0000 (06:41 +0100)
- starting with PHP 8, sockets are now no longer a scalar resource type, but a
  Socket class
- updated 'core' framework

application/hub/classes/container/socket/class_SocketContainer.php
application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseFrontend.php
application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php
application/hub/classes/factories/socket/class_SocketFactory.php
application/hub/classes/pools/peer/class_DefaultPeerPool.php
application/hub/interfaces/container/socket/class_StorableSocket.php
core

index 49752835a51785db26c60126b350042e52f7da8b..8619967bd1acdf1c1d8cb658fc3ddd6ced258c33 100644 (file)
@@ -37,6 +37,7 @@ use Org\Mxchange\CoreFramework\Visitor\Visitor;
 use \BadMethodCallException;
 use \InvalidArgumentException;
 use \LogicException;
+use \Socket;
 use \SplFileInfo;
 
 /**
@@ -118,9 +119,9 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
        private $senderPort;
 
        /**
-        * Socket resource
+        * Socket instance
         */
-       private $socketResource = false;
+       private $socketResource = NULL;
 
        /**
         * Protected constructor
@@ -148,20 +149,17 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
        /**
         * Creates an instance of this Container class and prepares it for usage
         *
-        * @param       $socketResource         A valid socket resource
+        * @param       $socketResource         An instance of a Socket class
         * @param       $socketProtocol         Socket protocol (TCP, UDP, file)
         * @param       $packageInstance        An instance of a DeliverablePackage class
         * @param       $socketType                     Socket type (incoming, outgoing, server, file, ...)
         * @return      $socketInstance         An instance of a StorableSocket class
         * @throws      InvalidArgumentException        If a parameter is not valid
         */
-       public static final function createSocketContainer ($socketResource, string $socketProtocol, DeliverablePackage $packageInstance, string $socketType) {
+       public static final function createSocketContainer (Socket $socketResource, string $socketProtocol, DeliverablePackage $packageInstance, string $socketType) {
                // Validate parameter
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-CONTAINER: socketResource[%s]=%s,socketProtocol=%s,packageInstance=%s,socketType=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol, $packageInstance->__toString(), $socketType));
-               if (!is_resource($socketResource)) {
-                       // Throw exception
-                       throw new InvalidArgumentException(sprintf('socketResource[]=%s is not valid', $socketResource));
-               } elseif (empty($socketProtocol)) {
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-CONTAINER: socketResource[]=%s,socketProtocol=%s,packageInstance=%s,socketType=%s - CALLED!', gettype($socketResource), $socketProtocol, $packageInstance->__toString(), $socketType));
+               if (empty($socketProtocol)) {
                        // Throw again
                        throw new InvalidArgumentException('Parameter "socketProtocol" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                } elseif (empty($socketType)) {
@@ -219,13 +217,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
        /**
         * Checks whether the given socket matches with stored
         *
-        * @param       $socketResource         A valid socket resource
+        * @param       $socketResource         An instance of a Socket class
         * @return      $matches                        Whether given socket matches
         */
-       public function ifSocketResourceMatches ($socketResource) {
+       public function ifSocketResourceMatches (Socket $socketResource) {
                // So, does both match?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketResource[' . gettype($socketResource) . ']=' .$socketResource . ',storedResource[' . gettype($this->getSocketResource()) . ']=' . $this->getSocketResource());
-               $matches = ((is_resource($socketResource)) && ($socketResource === $this->getSocketResource()));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('%s-SOCKET: socketResource[]=%s,storedResource[]=%s - CALLED!', strtoupper($this->getSocketProtocol()), gettype($socketResource)), gettype($this->getSocketResource()));
+               $matches = ($socketResource === $this->getSocketResource());
 
                // Return result
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches)));
@@ -781,7 +779,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
                $socketResource = socket_accept($this->getSocketResource());
 
                // Create socket instance from it
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('%s-SOCKET: this->socketResource=%s,serverSocket=%s', strtoupper($this->getSocketProtocol()), $socketResource, $this->getSocketResource()));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('%s-SOCKET: this->socketResource[]=%s,serverSocket=%s', strtoupper($this->getSocketProtocol()), gettype($socketResource), $this->getSocketResource()));
                $socketInstance = SocketFactory::createIncomingSocketInstance($socketResource, $this->getSocketProtocol());
 
                // Return accepted socket instance
@@ -1525,10 +1523,10 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
        /**
         * Setter for socket resource
         *
-        * @param       $socketResource         A valid socket resource
+        * @param       $socketResource         An instance of a Socket class
         * @return      void
         */
-       public final function setSocketResource ($socketResource) {
+       public final function setSocketResource (Socket $socketResource) {
                $this->socketResource = $socketResource;
        }
 
index 746056ef9d3655b47686c425e944f903c3e3afd8..8d2d6f905468228a490ecb03f1b9b5122169199c 100644 (file)
@@ -137,7 +137,7 @@ class PeerStateLookupDatabaseFrontend extends BaseHubDatabaseFrontend implements
         * Registers a new peer with given package data. We use the session id from it.
         *
         * @param       $packageInstance        An instance of a DeliverablePackage class
-        * @param       $socketResource         An instance of a StorableSocket class
+        * @param       $socketInstance         An instance of a StorableSocket class
         * @return      void
         * @throws      PeerAlreadyRegisteredException  If a peer is already registered
         */
index dd5aeae34c1454c3ba1dc88678b9a6a1b240d088..f62c0ca5ccbf3e215aea163343c22ee7ab6c7182 100644 (file)
@@ -142,7 +142,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera
         *
         * @param       $packageInstance        An instance of a DeliverablePackage class array
         * @param       $connectionType         Type of connection, can be either 'incoming' or 'outgoing', but *NEVER* 'server'!
-        * @return      $socketResource         A valid socket resource or FALSE if an error occured
+        * @return      $socketInstance         An instance of a StorableSocket class
         * @throws      NoListGroupException    If the procol group is not found in peer list
         * @throws      NullPointerException    If listenerInstance is NULL
         * @throws      InvalidUnlException             If the provided UNL cannot be validated by the protocol handler
index ebc874cbf093c2cee11870a258d681e9c0e4d44b..4e9ab32f75068d8351bc109e8149e7d812cd0d21 100644 (file)
@@ -22,6 +22,7 @@ use Org\Mxchange\CoreFramework\Socket\InvalidSocketException;
 // Import SPL stuff
 use \InvalidArgumentException;
 use \LogicException;
+use \Socket;
 use \SplFileInfo;
 use \UnexpectedValueException;
 
@@ -173,7 +174,7 @@ class SocketFactory extends BaseFactory {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketInstance=%s', $socketInstance->__toString()));
                if (!$socketInstance->isValidSocket()) {
                        // Something bad happened
-                       throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException([$listenerInstance, $socketInstance->getSocketResource()], self::EXCEPTION_INVALID_SOCKET);
                }
 
                // Check if there was an error else
@@ -235,14 +236,8 @@ class SocketFactory extends BaseFactory {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString()));
                $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 
-               // Is it a socket resource?
-               /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s', gettype($socketResource)));
-               if (!is_resource($socketResource)) {
-                       // Throw UVE
-                       throw new UnexpectedValueException(sprintf('socketResource[]=%s is not expected, could not create socket', gettype($socketResource)));
-               }
-
                // Create socket instance
+               /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s', gettype($socketResource)));
                $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', [
                        $socketResource,
                        StorableSocket::SOCKET_PROTOCOL_TCP,
@@ -303,12 +298,8 @@ class SocketFactory extends BaseFactory {
                /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
                $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 
-               // Is it a socket resource?
+               // Debug message
                /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s', gettype($socketResource)));
-               if (!is_resource($socketResource)) {
-                       // Throw UVE
-                       throw new UnexpectedValueException(sprintf('socketResource[]=%s is not expected, could not create socket', gettype($socketResource)));
-               }
 
                // Init fake "package" instance, the SocketContainer class requires this
                $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class');
@@ -389,12 +380,8 @@ class SocketFactory extends BaseFactory {
                /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
                $socketResource = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
 
-               // Is it a socket resource?
+               // Debug message
                /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s', gettype($socketResource)));
-               if (!is_resource($socketResource)) {
-                       // Throw UVE
-                       throw new UnexpectedValueException(sprintf('socketResource[]=%s is not expected, could not create socket', gettype($socketResource)));
-               }
 
                // Init fake "package" instance, the SocketContainer class requires this
                $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class');
@@ -416,7 +403,7 @@ class SocketFactory extends BaseFactory {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SOCKET-FACTORY: socketInstance=%s', $socketInstance->__toString()));
                if (!$socketInstance->isValidSocket()) {
                        // Something bad happened
-                       throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException([$listenerInstance, $socketInstance], self::EXCEPTION_INVALID_SOCKET);
                }
 
                // Check if there was an error else
@@ -532,13 +519,10 @@ class SocketFactory extends BaseFactory {
         * @return      $socketInstance         An instance of a StorableSocket class
         * @throws      InvalidArgumentException        If a parameter is not valid
         */
-       public static final function createIncomingSocketInstance ($socketResource, string $socketProtocol) {
+       public static final function createIncomingSocketInstance (Socket $socketResource, string $socketProtocol) {
                // Validate parameter
-               /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketResource[%s]=%s,socketProtocol=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol));
-               if (!is_resource($socketResource)) {
-                       // Throw exception
-                       throw new InvalidArgumentException(sprintf('socketResource[]=%s is not valid', gettype($socketResource)));
-               } elseif (empty($socketProtocol)) {
+               /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SOCKET-FACTORY: socketResource[]=%s,socketProtocol=%s - CALLED!', gettype($socketResource), $socketProtocol));
+               if (empty($socketProtocol)) {
                        // Throw it again
                        throw new InvalidArgumentException('Parameter "socketProtocol" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
index 053d419cd79a375cb507935993c9d46f9fe65224..bd2ccd5c9203d67f32b66ec13077b5f1cc40296f 100644 (file)
@@ -83,7 +83,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: socketInstance->socketResource=%s - CALLED!', $socketInstance->getSocketResource()));
                if (!$socketInstance->isValidSocket()) {
                        // Throw an exception
-                       throw new InvalidSocketException(array($this, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException([$this, $socketInstance->getSocketResource()], self::EXCEPTION_INVALID_SOCKET);
                }
 
                // Get error code
index 94013338fb977bf6990e567f4bd523cf09ec2f3c..f6754ece3f1fcbc26ec0af928f30b60ababb3de8 100644 (file)
@@ -9,6 +9,7 @@ use Org\Shipsimu\Hub\Information\ShareableInfo;
 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 
 // Import PHP stuff
+use \Socket;
 use \SplFileInfo;
 
 /**
@@ -224,10 +225,10 @@ interface StorableSocket extends FrameworkInterface {
        /**
         * Checks whether the given socket matches with stored
         *
-        * @param       $socketResource         A valid socket resource
+        * @param       $socketResource         An instance of a Socket class
         * @return      $matches                        Whether given socket matches
         */
-       function ifSocketResourceMatches ($socketResource);
+       function ifSocketResourceMatches (Socket $socketResource);
 
        /**
         * Shuts down a given socket resource. This method does only ease calling
diff --git a/core b/core
index f993e439e838c8cea3a98b1254c2cf340d68bc77..3ad2578dc223f97de06547be49d66e7adfea677c 160000 (submodule)
--- a/core
+++ b/core
@@ -1 +1 @@
-Subproject commit f993e439e838c8cea3a98b1254c2cf340d68bc77
+Subproject commit 3ad2578dc223f97de06547be49d66e7adfea677c