]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Mon, 26 Oct 2020 15:04:50 +0000 (16:04 +0100)
committerRoland Häder <roland@mxchange.org>
Mon, 26 Oct 2020 15:04:50 +0000 (16:04 +0100)
- further rewrites from type-unsafe array $rawData to $packageInstance

Signed-off-by: Roland Häder <roland@mxchange.org>
application/hub/classes/handler/package/class_NetworkPackageHandler.php
application/hub/classes/package/assembler/class_PackageAssembler.php
application/hub/classes/tasks/class_BaseHubTask.php [new file with mode: 0644]
application/hub/exceptions/package/class_UnsupportedPackageCodeHandlerException.php
application/hub/interfaces/handler/raw-data/class_HandleableRawData.php
application/hub/interfaces/receiver/assembler/class_Assembler.php

index e7cf86680fb50137500473fa4d326106e72cfa89..dea95cc7b2c98e302d60df8871e162d2f1c339f0 100644 (file)
@@ -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);
        }
 
        /**
index b14d0095b31782ba9276f821e4d7f12cb12dfcfa..3e3af8383787325d297ce4f016f537f9b9ead9a0 100644 (file)
@@ -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 (file)
index 0000000..8b944aa
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+// Own namespace
+namespace Org\Shipsimu\Hub\Task;
+
+// Import application-specific stuff
+use Org\Shipsimu\Hub\Node\Node;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Task\BaseTask;
+
+/**
+ * A general hub task
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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;
+       }
+
+}
index cfa0e8386f35054c3d1e8a943d14da0eef2fa05c..8a7d0102a802ebce88946d7787908624eb016624 100644 (file)
@@ -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
index 1c7566cd9a956e36eba42c1859a79a9d0390fa39..cf86be122affa4fbf1ab8c5a2960283c2b0523c9 100644 (file)
@@ -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
index 3fe218326971524e4bc1c3a96a256c2018b0d1e0..5efbc8f95aebf286ba10e083f1532bb57d9a0fe9 100644 (file)
@@ -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