]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Tue, 27 Oct 2020 13:12:09 +0000 (14:12 +0100)
committerRoland Häder <roland@mxchange.org>
Tue, 27 Oct 2020 13:12:09 +0000 (14:12 +0100)
- added some debug lines
- no need to set NULL in a constructor (for this class)
- renamed local variable $errorName to $errorCode

Signed-off-by: Roland Häder <roland@mxchange.org>
application/hub/classes/container/socket/class_SocketContainer.php
application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php
application/hub/classes/factories/states/peer/class_PeerStateFactory.php
application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
application/hub/interfaces/resolver/state/class_StateResolver.php

index 91e7742cdcb8d0514c33eb452b3ec40ad6d2dd57..d4811a6f82bd2f30c282b123606cc2eefa038ade 100644 (file)
@@ -153,20 +153,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
                // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource=%s,socketProtocol=%s', $socketResource, $socketProtocol));
 
-               // Set protocol
+               // Set all values
                $socketInstance->setSocketProtocol($socketProtocol);
-
-               // Set content/hash to null
-               $packageInstance->setPackageContent(NULL);
-               $packageInstance->setContentHash(NULL);
-
-               // Set the resource ...
                $socketInstance->setSocketResource($socketResource);
-
-               // ... and package instance
                $socketInstance->setPackageDataInstance($packageInstance);
 
                // Return the prepared instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketInstance=%s - EXIT!', strtoupper($socketProtocol), $socketInstance->__toString()));
                return $socketInstance;
        }
 
@@ -288,13 +281,11 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
         * @return      void
         */
        public function accept (Visitor $visitorInstance) {
-               // Debug message
-               /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - CALLED!');
-
                // Visit this listener
+               /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - CALLED!');
                $visitorInstance->visitSocket($this);
 
-               // Debug message
+               // Trace message
                /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - EXIT!');
        }
 
@@ -307,10 +298,8 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
         * @throws      InvalidSocketException  If socket is invalid
         */
        public function getSocketPeerName (&$peerAddress, &$peerPort) {
-               // Trace message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress=%s,peerPort=%d - CALLED!', strtoupper($this->getSocketProtocol()), $peerAddress, $peerPort));
-
                // Should be valid socket
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress[%s]=%s,peerPort[%s]=%d - CALLED!', strtoupper($this->getSocketProtocol()), gettype($peerAddress), $peerAddress, gettype($peerPort), $peerPort));
                if (!$this->isValidSocket()) {
                        // Throw exception
                        throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
@@ -325,17 +314,15 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
                 * http://de2.php.net/manual/en/function.socket-getpeername.php#35656
                 */
                if ($this->getSocketProtocol() == StorableSocket::SOCKET_PROTOCOL_UDP) {
-                       // UDP is WIP:
+                       // UDP is work-in-progress:
                        $this->partialStub('UDP sockets are unfinished.');
                } else {
                        // Use socket_getpeername()
                        $result = socket_getpeername($this->getSocketResource(), $peerAddress, $peerPort);
                }
 
-               // Trace message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress=%s,peerPort=%d,result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), $peerAddress, $peerPort, gettype($result), intval($result)));
-
                // Return result
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress=%s,peerPort=%d,result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), $peerAddress, $peerPort, gettype($result), intval($result)));
                return $result;
        }
 
index 3416d4da46495c39f81767719fcc779758a2feb5..1fce55633dd92bd8bc3b76285c7f585d17759b6d 100644 (file)
@@ -156,7 +156,11 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera
                // Make sure all parameters are valid
                if (($connectionType != StorableSocket::CONNECTION_TYPE_INCOMING) && ($connectionType != StorableSocket::CONNECTION_TYPE_OUTGOING)) {
                        // Abort here
-                       throw new InvalidArgumentException(sprintf('connectionType=%s is whether "%s" nor "%s".', $connectionType, StorableSocket::CONNECTION_TYPE_INCOMING, StorableSocket::CONNECTION_TYPE_OUTGOING));
+                       throw new InvalidArgumentException(sprintf('connectionType=%s is whether "%s" nor "%s".',
+                               $connectionType,
+                               StorableSocket::CONNECTION_TYPE_INCOMING,
+                               StorableSocket::CONNECTION_TYPE_OUTGOING
+                       ));
                } // END - if
 
                // Determine protocol instance
@@ -220,11 +224,12 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera
                        // Get the helper instance from registry
                        $helperInstance = GenericRegistry::getRegistry()->getInstance('connection');
 
-                       // Possibly noisy debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Going to resolve socket from peer state and given package data ...');
-
-                       // Resolve the peer's state (but ignore return value)
-                       PeerStateResolver::resolveStateByPackage($helperInstance, $packageInstance, $socketInstance);
+                       // Is a state set and it's name is other than socket's error code?
+                       if ($helperInstance->getStateInstance() instanceof PeerStateable && $helperInstance->getPrintableState() !== $socketInstance->translateLastSocketErrorCodeToName()) {
+                               // Resolve the peer's state (but ignore return value)
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Going to resolve socket from peer state and given package data ...');
+                               PeerStateResolver::resolveStateByPackage($helperInstance, $packageInstance, $socketInstance);
+                       }
                } catch (InvalidSocketException $e) {
                        // This cannot be fixed, so log it away
                        self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Cannot discover socket resource for recipient ' . $packageInstance->getRecipientUnl() . ': ' . $e->getMessage());
@@ -234,6 +239,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera
                }
 
                // And return it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: socketResource=' . $socketInstance->getSocketResource() . ',packageInstance=' . print_r($packageInstance, TRUE));
                return $socketInstance;
        }
index f7ad94ea2daaa8c53501ed196dc2be947c46363f..b05911cdbde7ce57ee1822acb164769a79c2878f 100644 (file)
@@ -82,7 +82,7 @@ class PeerStateFactory extends ObjectFactory {
         */
        public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance, StorableSocket $socketInstance, $errorCode) {
                // Error code and helper's state name should not be the same
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: helperInstance=%s,packageInstance=%s,socketInstance=%s,errorCode=%s - CALLED!', $helperInstance->__toString(), $packageInstance->__toString(), $socketInstance->__to_string(), $errorCode));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: helperInstance=%s,packageInstance=%s,socketInstance=%s,errorCode=%s - CALLED!', $helperInstance->__toString(), $packageInstance->__toString(), $socketInstance->__toString(), $errorCode));
                if ($errorCode == $helperInstance->getPrintableState()) {
                        // Not valid!
                        throw new InvalidArgumentException(sprintf('Current state and errorCode=%s are the same.', $errorCode));
index 8706dc1eb9d020b686b10d391445aa9181c006b9..42ebd2fc949065d24eb44ce59a598e0fa428fe36 100644 (file)
@@ -68,7 +68,7 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver {
         * @param       $helperInstance         An instance of a ConnectionHelper class
         * @param       $packageInstance        An instance of a DeliverablePackage class
         * @param       $socketInstance         An instance of a StorableSocket class
-        * @return      $stateInstance          An instance of the resolved state
+        * @return      $stateInstance          An instance of a PeerStateable class
         * @throws      InvalidSocketException  If socketResource, even from getSocketResource() is no valid resource
         * @todo        ~30% done
         */
@@ -93,10 +93,10 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver {
                } // END - if
 
                // Translate the error code to an own name
-               $errorName = $socketInstance->translateLastSocketErrorCodeToName();
+               $errorCode = $socketInstance->translateLastSocketErrorCodeToName();
 
                // Create a state instance based on $errorCode. This factory does the hard work for us
-               $stateInstance = PeerStateFactory::createPeerStateInstanceBySocketStatusCode($helperInstance, $packageInstance, $socketInstance, $errorName);
+               $stateInstance = PeerStateFactory::createPeerStateInstanceBySocketStatusCode($helperInstance, $packageInstance, $socketInstance, $errorCode);
 
                // Return the prepared instance
                return $stateInstance;
index c361fdfc0b37b43f51740b8527aecc2311752911..d00d4a23ea9ec039e664d654ec30365fdd533f87 100644 (file)
@@ -39,7 +39,7 @@ interface StateResolver extends Resolver {
         * @param       $helperInstance         An instance of a ConnectionHelper class
         * @param       $packageInstance        An instance of a DeliverablePackage class
         * @param       $socketInstance         An instance of a StorableSocket class
-        * @return      $stateInstance          An instance of the resolved state
+        * @return      $stateInstance          An instance of a PeerStateable class
         */
        static function resolveStateByPackage (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance, StorableSocket $socketInstance);