From 616bbe5d6ae53d9bf38f4840ec722d33be604ba5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 26 Oct 2020 16:04:50 +0100 Subject: [PATCH] Continued: - further rewrites from type-unsafe array $rawData to $packageInstance MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../package/class_NetworkPackageHandler.php | 16 ++--- .../assembler/class_PackageAssembler.php | 58 +++++++-------- .../hub/classes/tasks/class_BaseHubTask.php | 70 +++++++++++++++++++ ...UnsupportedPackageCodeHandlerException.php | 2 +- .../raw-data/class_HandleableRawData.php | 8 +-- .../receiver/assembler/class_Assembler.php | 5 +- 6 files changed, 107 insertions(+), 52 deletions(-) create mode 100644 application/hub/classes/tasks/class_BaseHubTask.php diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index e7cf86680..dea95cc7b 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -1280,25 +1280,17 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Make sure the raw decoded package data is handled assert($this->isIncomingRawDataHandled()); - // Get current package content (an array with two elements; see handleIncomingDecodedData() for details) - $packageContent = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED); - - // Assert on some elements - assert( - (is_array($packageContent)) && - (isset($packageContent[HandleableRawData::PACKAGE_RAW_DATA])) && - (isset($packageContent[HandleableRawData::PACKAGE_ERROR_CODE])) - ); + // Get current package instance (an array with two elements; see handleIncomingDecodedData() for details) + $packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED); // Start assembling the raw package data array by chunking it - $this->getAssemblerInstance()->chunkPackageContent($packageContent); + $this->getAssemblerInstance()->chunkPackageContent($packageInstance); // Remove the package from 'handled_decoded' stack ... $this->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_HANDLED); // ... and push it on the 'chunked' stacker - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Pushing ' . strlen($packageContent[HandleableRawData::PACKAGE_RAW_DATA]) . ' bytes on stack ' . self::STACKER_NAME_DECODED_CHUNKED . ',packageContent=' . print_r($packageContent, TRUE)); - $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageContent); + $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageInstance); } /** diff --git a/application/hub/classes/package/assembler/class_PackageAssembler.php b/application/hub/classes/package/assembler/class_PackageAssembler.php index b14d0095b..3e3af8383 100644 --- a/application/hub/classes/package/assembler/class_PackageAssembler.php +++ b/application/hub/classes/package/assembler/class_PackageAssembler.php @@ -7,6 +7,7 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Handler\Chunk\ChunkHandlerFactory; use Org\Shipsimu\Hub\Generic\BaseHubSystem; use Org\Shipsimu\Hub\Handler\Network\RawData\HandleableRawData; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Network\Package\Delivery\Fragment\PackageFragmenter; use Org\Shipsimu\Hub\Network\Receive\Receivable; @@ -126,12 +127,12 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, /** * Checks whether given package content is completed (start/end markers are found) * - * @param $packageContent An array with two elements: 'raw_data' and 'error_code' + * @param $packageInstance An instance of a DeliverablePackage class * @return $isCompleted Whether the given package content is completed */ - private function isPackageContentCompleted (array $packageContent) { + private function isPackageContentCompleted (DeliverablePackage $packageInstance) { // Check both - $isCompleted = $this->ifStartEndMarkersSet($packageContent[HandleableRawData::PACKAGE_RAW_DATA]); + $isCompleted = $this->ifStartEndMarkersSet($packageInstance->getRawData()); // Return status return $isCompleted; @@ -147,28 +148,22 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, * chunks and (maybe) re-request some chunks from the sender, this would * take to much time and therefore slow down this node again. * - * @param $packageContent An array with two elements: 'raw_data' and 'error_code' + * @param $packageInstance An array with two elements: 'raw_data' and 'error_code' * @return void * @throws UnsupportedPackageCodeHandlerException If the package code handler is not implemented */ - public function chunkPackageContent (array $packageContent) { - // Validate the package content array again - assert( - (isset($packageContent[HandleableRawData::PACKAGE_RAW_DATA])) && - (isset($packageContent[HandleableRawData::PACKAGE_ERROR_CODE])) - ); - + public function chunkPackageInstance (DeliverablePackage $packageInstance) { // Construct call-back name from package error code - $this->callbacks[$packageContent[HandleableRawData::PACKAGE_ERROR_CODE]] = 'handlePackageBy' . self::convertToClassName($packageContent[HandleableRawData::PACKAGE_ERROR_CODE]); + $this->callbacks[$packageInstance->getErrorCode()] = 'handlePackageBy' . self::convertToClassName($packageInstance->getErrorCode()); // Abort if the call-back method is not there - if (!method_exists($this, $this->callbacks[$packageContent[HandleableRawData::PACKAGE_ERROR_CODE]])) { + if (!method_exists($this, $this->callbacks[$packageInstance->getErrorCode()])) { // Throw an exception - throw new UnsupportedPackageCodeHandlerException(array($this, $this->callbacks[$packageContent[HandleableRawData::PACKAGE_ERROR_CODE]], $packageContent), self::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER); + throw new UnsupportedPackageCodeHandlerException(array($this, $this->callbacks[$packageInstance->getErrorCode()], $packageInstance), self::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER); } // END - if // Call it back - call_user_func(array($this, $this->callbacks[$packageContent[HandleableRawData::PACKAGE_ERROR_CODE]]), $packageContent); + call_user_func(array($this, $this->callbacks[$packageInstance->getErrorCode()]), $packageInstance); } /************************************************************************** @@ -181,23 +176,23 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, * class, does some low checks on it and feeds it into another queue for * verification and re-request for bad chunks. * - * @param $packageContent An array with two elements: 'raw_data' and 'error_code' + * @param $packageInstance An array with two elements: 'raw_data' and 'error_code' * @return void */ - private function handlePackageByUnhandledPackage (array $packageContent) { + private function handlePackageByUnhandledPackage (Deliverable $packageInstance) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageContent[' . HandleableRawData::PACKAGE_RAW_DATA . ']=' . $packageContent[HandleableRawData::PACKAGE_RAW_DATA]); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageInstance->rawData[]=' . $packageInstance->getRawData()); // Check for some conditions - if ((!$this->ifInputBufferIsEmpty()) || (!$this->isPackageContentCompleted($packageContent))) { + if ((!$this->ifInputBufferIsEmpty()) || (!$this->isPackageContentCompleted($packageInstance))) { // Last chunk is not valid, so wait for more - $this->pendingData .= $packageContent[HandleableRawData::PACKAGE_RAW_DATA]; + $this->pendingData .= $packageInstance->getRawData(); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Partial data received. Waiting for more ... ( ' . strlen($packageContent[HandleableRawData::PACKAGE_RAW_DATA]) . ' bytes)'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Partial data received. Waiting for more ... ( ' . strlen($packageInstance->getRawData()) . ' bytes)'); } else { // Debug message - //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageContent=' . print_r($packageContent, TRUE) . ',chunks='.print_r($chunks, TRUE)); + //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageContent=' . print_r($packageInstance, TRUE) . ',chunks='.print_r($chunks, TRUE)); } } @@ -277,11 +272,12 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, return; } - // Init fake array - $packageContent = array( - HandleableRawData::PACKAGE_RAW_DATA => $this->getInputStreamInstance()->streamData($this->pendingData), - HandleableRawData::PACKAGE_ERROR_CODE => StorableSocket::SOCKET_ERROR_UNHANDLED - ); + // Init fake package instance + $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class'); + + // Set all data + $packageInstance->setRawData($this->getInputStreamInstance()->streamData($this->pendingData)); + $packageInstance->setErrorCode(StorableSocket::SOCKET_ERROR_UNHANDLED); /* * Clear pending data as it has been processed and will be handled some @@ -290,12 +286,12 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, $this->clearPendingData(); // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Last block of partial data received. A total of ' . strlen($packageContent[HandleableRawData::PACKAGE_RAW_DATA]) . ' bytes has been received.'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Last block of partial data received. A total of ' . strlen($packageInstance->getRawData()) . ' bytes has been received.'); // Make sure last CHUNK_SEPARATOR is not there - if (substr($packageContent[HandleableRawData::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) { + if (substr($packageInstance->getRawData(), -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) { // Remove it - $packageContent[HandleableRawData::PACKAGE_RAW_DATA] = substr($packageContent[HandleableRawData::PACKAGE_RAW_DATA], 0, -1); + $packageInstance->setRawData(substr($packageInstance->getRawData(), 0, -1)); } // END - if /* @@ -303,7 +299,7 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, * array of chunks. These chunks must then be verified by their * checksums. Also the final chunk must be handled. */ - $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageContent[HandleableRawData::PACKAGE_RAW_DATA]); + $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageInstance->getRawData()); // Add all chunks because the last final chunk is found /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Going to add ' . count($chunks) . ' to chunk handler ...'); diff --git a/application/hub/classes/tasks/class_BaseHubTask.php b/application/hub/classes/tasks/class_BaseHubTask.php new file mode 100644 index 000000000..8b944aa10 --- /dev/null +++ b/application/hub/classes/tasks/class_BaseHubTask.php @@ -0,0 +1,70 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +abstract class BaseHubTask extends BaseTask { + + /** + * Node instance + */ + private $nodeInstance = NULL; + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } + + /** + * Setter for node instance + * + * @param $nodeInstance A Node instance + * @return void + */ + public final function setNodeInstance (Node $nodeInstance) { + $this->nodeInstance = $nodeInstance; + } + + /** + * Getter for node instance + * + * @return $nodeInstance A Node instance + */ + public function getNodeInstance () { + return $this->nodeInstance; + } + +} diff --git a/application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php b/application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php index cfa0e8386..8a7d0102a 100644 --- a/application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php +++ b/application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php @@ -44,7 +44,7 @@ class UnsupportedPackageCodeHandlerException extends FrameworkException { $messageArray[0]->__toString(), $this->getLine(), $messageArray[1], - $messageArray[2][HandleableRawData::PACKAGE_ERROR_CODE] + $messageArray[2]->getErrorCode() ); // Call parent exception constructor diff --git a/application/hub/interfaces/handler/raw-data/class_HandleableRawData.php b/application/hub/interfaces/handler/raw-data/class_HandleableRawData.php index 1c7566cd9..cf86be122 100644 --- a/application/hub/interfaces/handler/raw-data/class_HandleableRawData.php +++ b/application/hub/interfaces/handler/raw-data/class_HandleableRawData.php @@ -35,10 +35,6 @@ interface HandleableRawData extends HubInterface { const PACKAGE_ERROR_RECIPIENT_MISMATCH = 'recipient_error'; // Recipient is not us const PACKAGE_LEVEL_CHECK_OKAY = 'checked_package'; // Package is fine - // Package data - const PACKAGE_RAW_DATA = 'raw_data'; - const PACKAGE_ERROR_CODE = 'error_code'; - // Start/end marker const STREAM_START_MARKER = '[[S]]'; const STREAM_END_MARKER = '[[E]]'; @@ -58,9 +54,9 @@ interface HandleableRawData extends HubInterface { /** * "Getter" for next raw data from the stacker * - * @return $rawData Raw data from the stacker + * @return $packageInstance An instance of a DeliverablePackage class */ - function getNextRawData (); + function getNextPackageInstance (); /** * Setter for error code diff --git a/application/hub/interfaces/receiver/assembler/class_Assembler.php b/application/hub/interfaces/receiver/assembler/class_Assembler.php index 3fe218326..5efbc8f95 100644 --- a/application/hub/interfaces/receiver/assembler/class_Assembler.php +++ b/application/hub/interfaces/receiver/assembler/class_Assembler.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Network\Package\Receiver\Assembler; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface for a package assembler @@ -32,10 +33,10 @@ interface Assembler extends HubInterface { * Chunks the content from $packageContent and feeds it into another queue * for verification and possible re-requesting. * - * @param $packageContent An array with two elements: 'raw_data' and 'error_code' + * @param $packageInstance An instance of a DeliverablePackage class * @return void */ - function chunkPackageContent (array $packageContent); + function chunkPackageInstance (DeliverablePackage $packageInstance); /** * Checks whether the assembler's pending data is empty which means it has -- 2.39.5