49ba434c5c5f37bbfc97d7ef9a35b99e59bc91a7
[hub.git] / application / hub / classes / decoder / package / class_PackageDecoder.php
1 <?php
2 /**
3  * A Package decoder class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class PackageDecoder extends BaseDecoder implements Decodeable, Registerable {
25         /**
26          * Name for stacker for received packages
27          */
28         const STACKER_NAME_DECODED_PACKAGE = 'decoded_package';
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this class
42          *
43          * @param       $handlerInstance        An instance of a HandleableChunks class
44          * @return      $decoderInstance        An instance of a Decodeable class
45          */
46         public final static function createPackageDecoder (HandleableChunks $handlerInstance) {
47                 // Get new instance
48                 $decoderInstance = new PackageDecoder();
49
50                 // Init stacker for received packages
51                 $handlerInstance->getStackInstance()->initStack(self::STACKER_NAME_DECODED_PACKAGE);
52
53                 // Set the handler instance here
54                 $decoderInstance->setHandlerInstance($handlerInstance);
55
56                 // Get a singleton network package instance
57                 $packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
58
59                 // And set it in this decoder
60                 $decoderInstance->setPackageInstance($packageInstance);
61
62                 // Return the prepared instance
63                 return $decoderInstance;
64         }
65
66         /**
67          * Checks whether the assoziated stacker for raw package data has some entries left
68          *
69          * @return      $unhandledDataLeft      Whether some unhandled raw package data is left
70          */
71         public function ifUnhandledRawPackageDataLeft () {
72                 // Check it
73                 $unhandledDataLeft = (!$this->getHandlerInstance()->getStackInstance()->isStackEmpty(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA));
74
75                 // Return it
76                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: unhandledDataLeft=' . intval($unhandledDataLeft));
77                 return $unhandledDataLeft;
78         }
79
80         /**
81          * Handles raw package data by decoding it
82          *
83          * @return      void
84          */
85         public function handleRawPackageData () {
86                 // Assert on it to make sure the next popNamed() call won't throw an exception
87                 assert($this->ifUnhandledRawPackageDataLeft());
88
89                 // "Pop" the next raw package content
90                 $rawPackageContent = $this->getHandlerInstance()->getStackInstance()->popNamed(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA);
91
92                 // Debug message
93                 //* 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
95                 // "Decode" the raw package content by using the NetworkPackage instance
96                 $decodedData = $this->getPackageInstance()->decodeRawContent($rawPackageContent);
97
98                 // Some checks
99                 assert(
100                         (is_array($decodedData)) &&
101                         (isset($decodedData[NetworkPackage::PACKAGE_DATA_SENDER])) &&
102                         (isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) &&
103                         (isset($decodedData[NetworkPackage::PACKAGE_DATA_CONTENT])) &&
104                         (isset($decodedData[NetworkPackage::PACKAGE_DATA_STATUS]))
105                 );
106
107                 // Debug message
108                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: decodedData(' . count($decodedData) . ')=' . print_r($decodedData, TRUE));
109
110                 // Next get a recipient-discovery instance
111                 $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
112
113                 // ... then disover all recipient (might be only one), this package may shall be forwarded
114                 $discoveryInstance->discoverRawRecipients($decodedData);
115
116                 // Check for 'recipient' field (the 'sender' field and others are ignored here)
117                 if ($discoveryInstance->isRecipientListEmpty()) {
118                         // The recipient is this node so next stack it on 'decoded_package'
119                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: Pushing ' . strlen($decodedData) . ' bytes to stack ' . self::STACKER_NAME_DECODED_PACKAGE . ' ...');
120                         $this->getHandlerInstance()->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_PACKAGE, $decodedData);
121                 } else {
122                         // Forward the package to the next node
123                         $this->getPackageInstance()->forwardRawPackage($decodedData);
124                 }
125         }
126
127         /**
128          * Checks whether decoded packages have arrived (for this peer)
129          *
130          * @return      $ifRawPackagesLeft      Whether decoded packages have arrived
131          */
132         public function ifDeocedPackagesLeft () {
133                 // Check it ...
134                 $ifRawPackagesLeft = (!$this->getHandlerInstance()->getStackInstance()->isStackEmpty(self::STACKER_NAME_DECODED_PACKAGE));
135
136                 // ... return it
137                 return $ifRawPackagesLeft;
138         }
139
140         /**
141          * Handles received decoded packages internally (recipient is this node)
142          *
143          * @return      void
144          */
145         public function handleDecodedPackage () {
146                 // Assert on amount
147                 assert($this->ifDeocedPackagesLeft());
148
149                 // Get the next entry (assoziative array)
150                 $decodedData = $this->getHandlerInstance()->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_PACKAGE);
151
152                 // Handle it
153                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-DECODER[' . __METHOD__ . ':' . __LINE__ . ']: decodedData(' . count($decodedData) . ')=' . print_r($decodedData, TRUE));
154                 $this->getPackageInstance()->handleRawData($decodedData);
155         }
156 }
157
158 // [EOF]
159 ?>