3 * A Package decoder class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class PackageDecoder extends BaseDecoder implements Decodeable {
26 * Name for stacker for received packages
28 const STACKER_NAME_DECODED_PACKAGE = 'decoded_package';
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
41 * Creates an instance of this class
43 * @param $stackerInstance An instance of a Stackable class
44 * @return $decoderInstance An instance of a Decodeable class
46 public final static function createPackageDecoder (Stackable $stackerInstance) {
48 $decoderInstance = new PackageDecoder();
50 // Init stacker for received packages
51 $stackerInstance->initStack(self::STACKER_NAME_DECODED_PACKAGE);
53 // Set the stacker instance here
54 $decoderInstance->setStackerInstance($stackerInstance);
56 // Get a singleton network package instance
57 $packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
59 // And set it in this decoder
60 $decoderInstance->setPackageInstance($packageInstance);
62 // Return the prepared instance
63 return $decoderInstance;
67 * Checks whether the assoziated stacker for raw package data has some entries left
69 * @return $unhandledDataLeft Whether some unhandled raw package data is left
71 public function ifUnhandledRawPackageDataLeft () {
73 $unhandledDataLeft = (!$this->getStackerInstance()->isStackEmpty(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA));
76 return $unhandledDataLeft;
80 * Handles raw package data by decoding it
84 public function handleRawPackageData () {
85 // Assert on it to make sure the next popNamed() call won't throw an exception
86 assert($this->ifUnhandledRawPackageDataLeft());
88 // "Pop" the next raw package content
89 $rawPackageContent = $this->getStackerInstance()->popNamed(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA);
92 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: Got ' . strlen($rawPackageContent) . ' bytes from stack ' . ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA . ', decoding it ...');
94 // "Decode" the raw package content by using the NetworkPackage instance
95 $decodedData = $this->getPackageInstance()->decodeRawContent($rawPackageContent);
98 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: decodedData()=' . strlen($decodedData) . ' ...');
100 // Next get a recipient-discovery instance
101 $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
103 // ... then disover all recipient (might be only one), this package may shall be forwarded
104 $discoveryInstance->discoverRawRecipients($decodedData);
106 // Check for 'recipient' field (the 'sender' field and others are ignored here)
107 if ($discoveryInstance->isRecipientListEmpty()) {
108 // The recipient is this node so next stack it on 'decoded_package'
109 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: Pushing ' . strlen($decodedData) . ' bytes to stack ' . self::STACKER_NAME_DECODED_PACKAGE . ' ...');
110 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_PACKAGE, $decodedData);
112 // Forward the package to the next node
113 $this->getPackageInstance()->forwardRawPackage($decodedData);
118 * Checks whether decoded packages have arrived (for this peer)
120 * @return $ifRawPackagesLeft Whether decoded packages have arrived
122 public function ifDeocedPackagesLeft () {
124 $ifRawPackagesLeft = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_PACKAGE));
127 return $ifRawPackagesLeft;
131 * Handles received decoded packages internally (recipient is this node)
135 public function handleDecodedPackage () {
137 assert($this->ifDeocedPackagesLeft());
139 // Get the next entry (assoziative array)
140 $decodedData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_PACKAGE);
143 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: decodedData()=' . strlen($decodedData));
144 $this->getPackageInstance()->handleRawData($decodedData);