From 7dd1092c808a9d8a1b6c10cc2257f899f0b34350 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Wed, 2 Dec 2020 04:47:42 +0100
Subject: [PATCH] Continued: - package and message are different things, so
 there is no "new package arrived"   but more decompressed XML - renamed
 methods for better understanding them - added missing config key - updated
 core framework
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../package/class_NetworkPackageHandler.php   | 105 +++++++++++++-----
 .../class_NetworkPackageReaderTask.php        |   8 ++
 application/hub/config.php                    |   7 +-
 .../interfaces/receiver/class_Receivable.php  |  14 +++
 core                                          |   2 +-
 5 files changed, 106 insertions(+), 30 deletions(-)

diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php
index 3e6d1c5e1..8e6115393 100644
--- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php
+++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php
@@ -247,6 +247,11 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 	 */
 	const STACKER_NAME_DECODED_CHUNKED = 'package_chunked_decoded';
 
+	/**
+	 * Stacker name for decompressed XML
+	 */
+	const STACKER_NAME_PACKAGE_DECOMPRESSED_XML = 'package_decompressed_xml';
+
 	/**************************************************************************
 	 *                     Stacker for incoming messages                      *
 	 **************************************************************************/
@@ -446,7 +451,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 			self::STACKER_NAME_DECODED_INCOMING,
 			self::STACKER_NAME_DECODED_HANDLED,
 			self::STACKER_NAME_DECODED_CHUNKED,
-			self::STACKER_NAME_NEW_MESSAGE,
+			self::STACKER_NAME_PACKAGE_DECOMPRESSED_XML,
 			self::STACKER_NAME_PROCESSED_MESSAGE,
 			self::STACKER_NAME_OUTGOING_STREAM
 		), $forceReInit);
@@ -1463,7 +1468,6 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 	 * @param	$packageInstance	An instance of a DeliverablePackage class
 	 * @return	void
 	 * @throws	InvalidArgumentException	If the package content does not have expected amount of entries
-	 * @throws	InvalidDataChecksumException	If the checksum doesn't match
 	 */
 	public function handleReceivedPackageInstance (DeliverablePackage $packageInstance) {
 		// Is the package content set?
@@ -1503,20 +1507,11 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 		// Checksum of the _decoded_ data
 		$packageInstance->setContentChecksum($decodedContent[self::INDEX_CHECKSUM]);
 
-		// Is the checksum valid?
-		if (!$this->isChecksumValid($packageInstance)) {
-			// Is not the same, so throw an exception here
-			throw new InvalidDataChecksumException(array($this, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM);
-		}
-
-		/*
-		 * The checksum is the same, then it can be decompressed safely. The
-		 * original message is at this point fully decoded.
-		 */
+		// Decompress raw XML stream
 		$packageInstance->setRawXml($this->getCompressorInstance()->decompressStream($packageInstance->getRawXml()));
 
 		// And push it on the next stack
-		$this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $packageInstance);
+		$this->getStackInstance()->pushNamed(self::STACKER_NAME_PACKAGE_DECOMPRESSED_XML, $packageInstance);
 
 		// Trace message
 		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
@@ -1530,7 +1525,22 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 	public function isNewMessageArrived () {
 		// Determine if the stack is not empty
 		//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
-		$hasArrived = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_NEW_MESSAGE));
+		$hasArrived = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PACKAGE_DECOMPRESSED_XML));
+
+		// Return it
+		//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: hasArrived=%d - EXIT!', intval($hasArrived)));
+		return $hasArrived;
+	}
+
+	/**
+	 * Checks whether decoded package's XML is pending for parsing
+	 *
+	 * @return	$hasArrived		Whether a new package has arrived for processing
+	 */
+	public function isDecodedPackageXmlPending () {
+		// Determine if the stack is not empty
+		//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
+		$hasArrived = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PACKAGE_DECOMPRESSED_XML));
 
 		// Return it
 		//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: hasArrived=%d - EXIT!', intval($hasArrived)));
@@ -1538,7 +1548,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 	}
 
 	/**
-	 * Handles newly arrived messages
+	 * Handles newly arrived message
 	 *
 	 * @return	void
 	 * @throws	BadMethodCallException	If no new message has arrived
@@ -1555,30 +1565,30 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 		}
 
 		// Get it from the stacker, it is the full array with the decoded message
-		$packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
+		$messageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
 
 		// Generate the hash of comparing it
-		/* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)));
-		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
-		if (empty($packageInstance->getSenderId())) {
-			// Invalid $packageInstance
-			throw new InvalidArgumentException('packageInstance does not contain senderId');
-		} elseif (empty($packageInstance->getSenderPrivateKeyHash())) {
+		/* DEBUG-DIE: */ die(sprintf('[%s:%d]: messageInstance=%s', __METHOD__, __LINE__, print_r($messageInstance, TRUE)));
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: messageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $messageInstance->__toString(), $messageInstance->getSenderId(), $messageInstance->getSenderPrivateKeyHash()));
+		if (empty($messageInstance->getSenderId())) {
+			// Invalid $messageInstance
+			throw new InvalidArgumentException('messageInstance does not contain senderId');
+		} elseif (empty($messageInstance->getSenderPrivateKeyHash())) {
 			// This needs fixing
 			throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in decodedData'), BaseHubSystem::EXCEPTION_INVALID_PRIVATE_KEY_HASH);
-		} elseif (!$this->isPackageHashValid($packageInstance)) {
+		} elseif (!$this->isPackageHashValid($messageInstance)) {
 			// Is not valid, so throw an exception here
-			exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . chr(10));
+			exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . PHP_EOL);
 		}
 
 		// Now get a filter chain back from factory with given tags array
-		$chainInstance = PackageFilterChainFactory::createChainByTagsArray($packageInstance->getContentHash());
+		$chainInstance = PackageFilterChainFactory::createChainByTagsArray($messageInstance->getContentHash());
 
 		/*
 		 * Process the message through all filters, note that all other
-		 * elements from $packageInstance are no longer needed.
+		 * elements from $messageInstance are no longer needed.
 		 */
-		$chainInstance->processMessage($packageInstance, $this);
+		$chainInstance->processMessage($messageInstance, $this);
 
 		/*
 		 * Post-processing of message data (this won't remove the message from
@@ -1590,6 +1600,47 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei
 		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
 	}
 
+	/**
+	 * Handles package's decoded XML
+	 *
+	 * @return	void
+	 * @throws	BadMethodCallException	If no new package has arrived
+	 * @throws	InvalidArgumentException	If $packageInstance contains no senderId
+	 * @throws	InvalidPrivateKeyHashException	If the private key-hash is empty
+	 * @todo	Implement verification of all sent tags here?
+	 */
+	public function handleDecodedPackageXml () {
+		// Make sure there is at least one package
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
+		if (!$this->isDecodedPackageXmlPending()) {
+			// Bad method call
+			throw new BadMethodCallException('No package waiting XML parsing');
+		}
+
+		// Get it from the stacker, it is the full array with the decoded package (raw XML)
+		$packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_PACKAGE_DECOMPRESSED_XML);
+
+		// Generate the hash of comparing it
+		/* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)));
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
+		if (empty($packageInstance->getSenderId())) {
+			// Invalid $packageInstance
+			throw new InvalidArgumentException('packageInstance does not contain senderId');
+		} elseif (empty($packageInstance->getSenderPrivateKeyHash())) {
+			// This needs fixing
+			throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in decodedData'), BaseHubSystem::EXCEPTION_INVALID_PRIVATE_KEY_HASH);
+		} elseif (!$this->isPackageHashValid($packageInstance)) {
+			// Is not valid, so throw an exception here
+			exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . PHP_EOL);
+		}
+
+		// Parse package's XML into a message instance
+		/* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)));
+
+		// Trace message
+		/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
+	}
+
 	/**
 	 * Checks whether a processed message is pending for "interpretation"
 	 *
diff --git a/application/hub/classes/tasks/network/class_NetworkPackageReaderTask.php b/application/hub/classes/tasks/network/class_NetworkPackageReaderTask.php
index e9f87725d..e315aa3bd 100644
--- a/application/hub/classes/tasks/network/class_NetworkPackageReaderTask.php
+++ b/application/hub/classes/tasks/network/class_NetworkPackageReaderTask.php
@@ -98,6 +98,14 @@ class NetworkPackageReaderTask extends BaseHubTask implements Taskable, Visitabl
 			//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-READER-TASK: Calling handlerInstance->handleProcessedMessage() ...');
 			$handlerInstance->handleProcessedMessage();
 			//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-READER-TASK: After handlerInstance->handleProcessedMessage() ...');
+		} elseif ($handlerInstance->isDecodedPackageXmlPending()) {
+			/*
+			 * A fully "decoded" package has arrived. This is raw XML and needs
+			 * to be parsed to a message.
+			 */
+			//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-READER-TASK: Calling handlerInstance->handleDecodedPackageXml() ...');
+			$handlerInstance->handleDecodedPackageXml();
+			//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-READER-TASK: After handlerInstance->handleDecodedPackageXml() ...');
 		} elseif ($handlerInstance->isNewMessageArrived()) {
 			/*
 			 * A fully "decoded" message has been received and added for being
diff --git a/application/hub/config.php b/application/hub/config.php
index d93a7199a..e1e219258 100644
--- a/application/hub/config.php
+++ b/application/hub/config.php
@@ -453,8 +453,11 @@ $cfg->setConfigEntry('stacker_package_handled_decoded_max_size', 200);
 // CFG: STACKER-PACKAGE-CHUNKED-DECODED-MAX-SIZE
 $cfg->setConfigEntry('stacker_package_chunked_decoded_max_size', 800);
 
-// CFG: STACKER-PACKAGE-NEW-MESSAGE-MAX-SIZE
-$cfg->setConfigEntry('stacker_package_new_message_max_size', 400);
+// CFG: STACKER-PACKAGE-DECOMPRESSED-XML-MAX-SIZE
+$cfg->setConfigEntry('stacker_package_decompressed_xml_max_size', 500);
+
+// CFG: STACKER-PACKAGE-NEW-PACKAGE-MAX-SIZE
+$cfg->setConfigEntry('stacker_package_new_package_max_size', 400);
 
 // CFG: STACKER-PACKAGE-PROCESSED-MESSAGE-MAX-SIZE
 $cfg->setConfigEntry('stacker_package_processed_message_max_size', 400);
diff --git a/application/hub/interfaces/receiver/class_Receivable.php b/application/hub/interfaces/receiver/class_Receivable.php
index efaa98af9..8cc0826dd 100644
--- a/application/hub/interfaces/receiver/class_Receivable.php
+++ b/application/hub/interfaces/receiver/class_Receivable.php
@@ -71,6 +71,13 @@ interface Receivable extends HubInterface {
 	 */
 	function isNewMessageArrived ();
 
+	/**
+	 * Checks whether pending decoded package's XML is pending parsing
+	 *
+	 * @return	$hasArrived		Whether a new package has arrived for processing
+	 */
+	function isDecodedPackageXmlPending ();
+
 	/**
 	 * Handle newly arrived message
 	 *
@@ -78,6 +85,13 @@ interface Receivable extends HubInterface {
 	 */
 	function handleNewlyArrivedMessage ();
 
+	/**
+	 * Handles package's decoded XML
+	 *
+	 * @return	void
+	 */
+	function handleDecodedPackageXml ();
+
 	/**
 	 * Checks whether a processed message is pending for "interpretation"
 	 *
diff --git a/core b/core
index 8de526234..57b4b400d 160000
--- a/core
+++ b/core
@@ -1 +1 @@
-Subproject commit 8de52623465245375075410e5c14e36c1df1f621
+Subproject commit 57b4b400d2e4a35f92d83270477157df4ee7c2c4
-- 
2.39.5