]> git.mxchange.org Git - hub.git/blob - application/hub/main/package/assembler/class_PackageAssembler.php
Rewrote debug lines (even more), re-enabled debugging lines in socket layer
[hub.git] / application / hub / main / package / assembler / class_PackageAssembler.php
1 <?php
2 /**
3  * A PackageAssembler class to assemble a package content stream fragemented
4  * by PackageFragmenter back to a raw package data array.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 class PackageAssembler extends BaseHubSystem implements Assembler, Registerable {
26         /**
27          * Pending data
28          */
29         private $pendingData = '';
30
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         protected function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates an instance of this class
43          *
44          * @param       $packageInstance        An instance of a Receivable class
45          * @return      $assemblerInstance      An instance of an Assembler class
46          */
47         public static final function createPackageAssembler (Receivable $packageInstance) {
48                 // Get new instance
49                 $assemblerInstance = new PackageAssembler();
50
51                 // Set package instance here
52                 $assemblerInstance->setPackageInstance($packageInstance);
53
54                 // Return the prepared instance
55                 return $assemblerInstance;
56         }
57
58         /**
59          * Checks whether the input buffer (stacker to be more preceise) is empty.
60          *
61          * @return      $isInputBufferEmpty             Whether the input buffer is empty
62          */
63         private function ifInputBufferIsEmpty () {
64                 // Check it
65                 $isInputBufferEmpty = $this->getPackageInstance()->getStackerInstance()->isStackEmpty(NetworkPackage::STACKER_NAME_DECODED_HANDLED);
66
67                 // Debug message
68                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER: isInputBufferEmpty=' . intval($isInputBufferEmpty));
69
70                 // Return it
71                 return $isInputBufferEmpty;
72         }
73
74         /**
75          * Assembles the content from $packageContent. This method does only
76          * initialize the whole process by creating a call-back which will then
77          * itself (99.9% of all cases) "explode" the decoded data stream and add
78          * it to a chunk assembler queue.
79          *
80          * If the call-back method or this would attempt to assemble the package
81          * chunks and (maybe) re-request some chunks from the sender, this would
82          * take to much time and therefore slow down this node again.
83          *
84          * @param       $packageContent         An array with two elements: 'decoded_data' and 'error_code'
85          * @return      void
86          * @throws      UnsupportedPackageCodeHandlerException  If the package code handler is not implemented
87          */
88         public function chunkPackageContent (array $packageContent) {
89                 // Validate the package content array again
90                 assert(
91                         (isset($packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA])) &&
92                         (isset($packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]))
93                 );
94
95                 // Construct call-back name from package error code
96                 $methodName = 'handlePackageBy' . $this->convertToClassName($packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]);
97
98                 // Abort if the call-back method is not there
99                 if (!method_exists($this, $methodName)) {
100                         // Throw an exception
101                         throw new UnsupportedPackageCodeHandlerException(array($this, $methodName, $packageContent), BaseListener::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER);
102                 } // END - if
103
104                 // Call it back
105                 call_user_func(array($this, $methodName), $packageContent);
106         }
107
108         /**************************************************************************
109          *                 Call-back methods for above method                     *
110          **************************************************************************/
111
112         /**
113          * Call-back handler to handle unhandled packages. This method "explodes"
114          * the string with the chunk separator from PackageFragmenter class, does
115          * some low checks on it and feeds it into another queue for verification
116          * and re-request for bad chunks.
117          *
118          * @param       $packageContent         An array with two elements: 'decoded_data' and 'error_code'
119          * @return      void
120          * @throws      FinalChunkVerificationException         If the final chunk does not start with 'EOP:'
121          */
122         private function handlePackageByUnhandledPackage (array $packageContent) {
123                 // Debug message
124                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER: packageData[' . BaseRawDataHandler::PACKAGE_DECODED_DATA . ']=' . $packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA]);
125
126                 // Check for some conditions
127                 if (!$this->ifInputBufferIsEmpty()) {
128                         // Last chunk is not valid, so wait for more
129                         $this->pendingData .= $packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA];
130
131                         // Debug message
132                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER: Partial data received. Waiting for more ... ( ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA]) . ' bytes)');
133                 } else {
134                         // Debug message
135                         //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('packageContent=' . print_r($packageContent,true) . ',chunks='.print_r($chunks,true));
136
137                         /*
138                          * "explode" the string from 'decoded_data' with chunk separator to
139                          * get an array of chunks. These chunks must then be verified by
140                          * their checksums. Also the final chunk must be handled.
141                          */
142                         $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA]);
143
144                         // Now get a chunk handler instance
145                         $handlerInstance = ChunkHandlerFactory::createChunkHandlerInstance();
146
147                         // Add all chunks because the last final chunk is found
148                         $handlerInstance->addAllChunksWithFinal($chunks);
149                 }
150         }
151
152         /**
153          * Checks whether the assembler's pending data is empty which means it has
154          * no pending data left for handling ... ;-)
155          *
156          * @return      $ifPendingDataIsEmpty   Whether pending data is empty
157          */
158         public function isPendingDataEmpty () {
159                 // A simbple check
160                 $ifPendingDataIsEmpty = empty($this->pendingData);
161
162                 // Return it
163                 return $ifPendingDataIsEmpty;
164         }
165
166         /**
167          * Handles the assembler's pending data
168          *
169          * @return      void
170          */
171         public function handlePendingData () {
172                 // Assert on condition
173                 assert(!$this->isPendingDataEmpty());
174
175                 // Init fake array
176                 $packageContent = array(
177                         BaseRawDataHandler::PACKAGE_DECODED_DATA => $this->pendingData,
178                         BaseRawDataHandler::PACKAGE_ERROR_CODE   => BaseRawDataHandler::SOCKET_ERROR_UNHANDLED
179                 );
180
181                 // Clear pending data
182                 $this->pendingData = '';
183
184                 // Debug message
185                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('PACKAGE-ASSEMBLER: Last block of partial data received. A total of ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA]) . ' bytes has been received.');
186
187                 // Call the real handler method
188                 $this->handlePackageByUnhandledPackage($packageContent);
189         }
190 }
191
192 // [EOF]
193 ?>