]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/decoder/package/class_PackageDecoder.php
'hub' project continued:
[hub.git] / application / hub / main / decoder / package / class_PackageDecoder.php
diff --git a/application/hub/main/decoder/package/class_PackageDecoder.php b/application/hub/main/decoder/package/class_PackageDecoder.php
new file mode 100644 (file)
index 0000000..4d66345
--- /dev/null
@@ -0,0 +1,138 @@
+<?php
+/**
+ * A Package decoder class
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.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/>.
+ */
+class PackageDecoder extends BaseDecoder implements Decodeable {
+       /**
+        * Name for stacker for received packages
+        */
+       const STACKER_NAME_DECODED_PACKAGE = 'decoded_package';
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this class
+        *
+        * @param       $stackerInstance        An instance of a Stackable class
+        * @return      $decoderInstance        An instance of a Decodeable class
+        */
+       public final static function createPackageDecoder (Stackable $stackerInstance) {
+               // Get new instance
+               $decoderInstance = new PackageDecoder();
+
+               // Init stacker for received packages
+               $stackerInstance->initStacker(self::STACKER_NAME_DECODED_PACKAGE);
+
+               // Set the stacker instance here
+               $decoderInstance->setStackerInstance($stackerInstance);
+
+               // Get a singleton network package instance
+               $packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
+
+               // And set it in this decoder
+               $decoderInstance->setPackageInstance($packageInstance);
+
+               // Return the prepared instance
+               return $decoderInstance;
+       }
+
+       /**
+        * Checks whether the assoziated stacker for raw package data has some entries left
+        *
+        * @return      $unhandledDataLeft      Whether some unhandled raw package data is left
+        */
+       public function ifUnhandledRawPackageDataLeft () {
+               // Check it
+               $unhandledDataLeft = (!$this->getStackerInstance()->isStackEmpty(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA));
+
+               // Return it
+               return $unhandledDataLeft;
+       }
+
+       /**
+        * Handles raw package data by decoding it
+        *
+        * @return      void
+        */
+       public function handleRawPackageData () {
+               // Assert on it to make sure the next popNamed() call won't throw an exception
+               assert($this->ifUnhandledRawPackageDataLeft());
+
+               // "Pop" the next raw package content
+               $rawPackageContent = $this->getStackerInstance()->popNamed(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA);
+
+               // "Decode" the raw package content by using the NetworkPackage instance
+               $decodedData = $this->getPackageInstance()->decodeRawContent($rawPackageContent);
+
+               // Next get a recipient-discovery instance
+               $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
+
+               // ... then disover all recipient (might be only one), this package may shall be forwarded
+               $discoveryInstance->discoverDecodedRecipients($decodedData);
+
+               // Check for 'recipient' field (the 'sender' field and others are ignored here)
+               if ($discoveryInstance->isRecipientListEmpty()) {
+                       // The recipient is this node so next stack it on 'decoded_package'
+                       $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_PACKAGE, $decodedData);
+               } else {
+                       // Forward the package to the next node
+                       $this->getPackageInstance()->forwardDecodedPackage($decodedData);
+               }
+       }
+
+       /**
+        * Checks whether decoded packages have arrived (for this peer)
+        *
+        * @return      $ifDecodedPackagesLeft  Whether decoded packages have arrived
+        */
+       public function ifDeocedPackagesLeft () {
+               // Check it ...
+               $ifDecodedPackagesLeft = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_PACKAGE));
+
+               // ... return it
+               return $ifDecodedPackagesLeft;
+       }
+
+       /**
+        * Handles received decoded packages internally (recipient is this node)
+        *
+        * @return      void
+        */
+       public function handleDecodedPackage () {
+               // Get the next entry (assoziative array)
+               $decodedData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_PACKAGE);
+
+               // Handle it
+               die('decodedData='.print_r($decodedData,true));
+       }
+}
+
+// [EOF]
+?>