]> git.mxchange.org Git - hub.git/blob - application/hub/main/decoder/package/class_PackageDecoder.php
06bc7ccadb2a4b57d4696aefe467f7358192f40b
[hub.git] / application / hub / main / decoder / package / class_PackageDecoder.php
1 <?php
2 /**
3  * A Package decoder class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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.ship-simu.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 {
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       $stackerInstance        An instance of a Stackable class
44          * @return      $decoderInstance        An instance of a Decodeable class
45          */
46         public final static function createPackageDecoder (Stackable $stackerInstance) {
47                 // Get new instance
48                 $decoderInstance = new PackageDecoder();
49
50                 // Init stacker for received packages
51                 $stackerInstance->initStack(self::STACKER_NAME_DECODED_PACKAGE);
52
53                 // Set the stacker instance here
54                 $decoderInstance->setStackerInstance($stackerInstance);
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->getStackerInstance()->isStackEmpty(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA));
74
75                 // Return it
76                 return $unhandledDataLeft;
77         }
78
79         /**
80          * Handles raw package data by decoding it
81          *
82          * @return      void
83          */
84         public function handleRawPackageData () {
85                 // Assert on it to make sure the next popNamed() call won't throw an exception
86                 assert($this->ifUnhandledRawPackageDataLeft());
87
88                 // "Pop" the next raw package content
89                 $rawPackageContent = $this->getStackerInstance()->popNamed(ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA);
90
91                 // "Decode" the raw package content by using the NetworkPackage instance
92                 $decodedData = $this->getPackageInstance()->decodeRawContent($rawPackageContent);
93
94                 // Next get a recipient-discovery instance
95                 $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
96
97                 // ... then disover all recipient (might be only one), this package may shall be forwarded
98                 $discoveryInstance->discoverRawRecipients($decodedData);
99
100                 // Check for 'recipient' field (the 'sender' field and others are ignored here)
101                 if ($discoveryInstance->isRecipientListEmpty()) {
102                         // The recipient is this node so next stack it on 'decoded_package'
103                         $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_PACKAGE, $decodedData);
104                 } else {
105                         // Forward the package to the next node
106                         $this->getPackageInstance()->forwardRawPackage($decodedData);
107                 }
108         }
109
110         /**
111          * Checks whether decoded packages have arrived (for this peer)
112          *
113          * @return      $ifRawPackagesLeft      Whether decoded packages have arrived
114          */
115         public function ifDeocedPackagesLeft () {
116                 // Check it ...
117                 $ifRawPackagesLeft = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_PACKAGE));
118
119                 // ... return it
120                 return $ifRawPackagesLeft;
121         }
122
123         /**
124          * Handles received decoded packages internally (recipient is this node)
125          *
126          * @return      void
127          */
128         public function handleDecodedPackage () {
129                 // Assert on amount
130                 assert($this->ifDeocedPackagesLeft());
131
132                 // Get the next entry (assoziative array)
133                 $decodedData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_PACKAGE);
134
135                 // Handle it
136                 $this->getPackageInstance()->handleRawData($decodedData);
137         }
138 }
139
140 // [EOF]
141 ?>