+++ /dev/null
-<?php
-/**
- * A Package decoder class
- *
- * @author Roland Haeder <webmaster@shipsimu.org>
- * @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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/>.
- */
-class PackageDecoder extends BaseDecoder implements Decodeable, Registerable {
- /**
- * 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 $handlerInstance An instance of a HandleableChunks class
- * @return $decoderInstance An instance of a Decodeable class
- */
- public final static function createPackageDecoder (HandleableChunks $handlerInstance) {
- // Get new instance
- $decoderInstance = new PackageDecoder();
-
- // Init stacker for received packages
- $handlerInstance->getStackInstance()->initStack(self::STACKER_NAME_DECODED_PACKAGE);
-
- // Set the handler instance here
- $decoderInstance->setHandlerInstance($handlerInstance);
-
- // 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->getHandlerInstance()->getStackInstance()->isStackEmpty(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA));
-
- // Return it
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: unhandledDataLeft=' . intval($unhandledDataLeft));
- 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->getHandlerInstance()->getStackInstance()->popNamed(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA);
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: Got ' . strlen($rawPackageContent) . ' bytes from stack ' . ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA . ', decoding it ...');
-
- // "Decode" the raw package content by using the NetworkPackage instance
- $decodedData = $this->getPackageInstance()->decodeRawContent($rawPackageContent);
-
- // Some checks
- assert(
- (is_array($decodedData)) &&
- (isset($decodedData[NetworkPackage::PACKAGE_DATA_SENDER])) &&
- (isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) &&
- (isset($decodedData[NetworkPackage::PACKAGE_DATA_CONTENT])) &&
- (isset($decodedData[NetworkPackage::PACKAGE_DATA_STATUS]))
- );
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: decodedData(' . count($decodedData) . ')=' . print_r($decodedData, TRUE));
-
- // Next get a recipient-discovery instance
- $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
-
- // ... then disover all recipient (might be only one), this package may shall be forwarded
- $discoveryInstance->discoverRawRecipients($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'
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: Pushing ' . strlen($decodedData) . ' bytes to stack ' . self::STACKER_NAME_DECODED_PACKAGE . ' ...');
- $this->getHandlerInstance()->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_PACKAGE, $decodedData);
- } else {
- // Forward the package to the next node
- $this->getPackageInstance()->forwardRawPackage($decodedData);
- }
- }
-
- /**
- * Checks whether decoded packages have arrived (for this peer)
- *
- * @return $ifRawPackagesLeft Whether decoded packages have arrived
- */
- public function ifDeocedPackagesLeft () {
- // Check it ...
- $ifRawPackagesLeft = (!$this->getHandlerInstance()->getStackInstance()->isStackEmpty(self::STACKER_NAME_DECODED_PACKAGE));
-
- // ... return it
- return $ifRawPackagesLeft;
- }
-
- /**
- * Handles received decoded packages internally (recipient is this node)
- *
- * @return void
- */
- public function handleDecodedPackage () {
- // Assert on amount
- assert($this->ifDeocedPackagesLeft());
-
- // Get the next entry (assoziative array)
- $decodedData = $this->getHandlerInstance()->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_PACKAGE);
-
- // Handle it
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: decodedData(' . count($decodedData) . ')=' . print_r($decodedData, TRUE));
- $this->getPackageInstance()->handleRawData($decodedData);
- }
-}
-
-// [EOF]
-?>