From c3bdc748253e3a5e5df8481826dad045b041d033 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Tue, 27 Oct 2020 14:12:09 +0100
Subject: [PATCH] Continued: - added some debug lines - no need to set NULL in
 a constructor (for this class) - renamed local variable $errorName to
 $errorCode
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../socket/class_SocketContainer.php          | 27 +++++--------------
 .../socket/class_PackageSocketDiscovery.php   | 18 ++++++++-----
 .../states/peer/class_PeerStateFactory.php    |  2 +-
 .../state/peer/class_PeerStateResolver.php    |  6 ++---
 .../resolver/state/class_StateResolver.php    |  2 +-
 5 files changed, 24 insertions(+), 31 deletions(-)

diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php
index 91e7742cd..d4811a6f8 100644
--- a/application/hub/classes/container/socket/class_SocketContainer.php
+++ b/application/hub/classes/container/socket/class_SocketContainer.php
@@ -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;
 	}
 
diff --git a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php
index 3416d4da4..1fce55633 100644
--- a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php
+++ b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php
@@ -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;
 	}
diff --git a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php
index f7ad94ea2..b05911cdb 100644
--- a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php
+++ b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php
@@ -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));
diff --git a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
index 8706dc1eb..42ebd2fc9 100644
--- a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
+++ b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
@@ -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;
diff --git a/application/hub/interfaces/resolver/state/class_StateResolver.php b/application/hub/interfaces/resolver/state/class_StateResolver.php
index c361fdfc0..d00d4a23e 100644
--- a/application/hub/interfaces/resolver/state/class_StateResolver.php
+++ b/application/hub/interfaces/resolver/state/class_StateResolver.php
@@ -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);
 
-- 
2.39.5