]> git.mxchange.org Git - hub.git/blob - application/hub/main/package/assembler/class_PackageAssembler.php
e518008aeeef57668d11e0dcd96cb10886df01db
[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 - 2011 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          * Protected constructor
28          *
29          * @return      void
30          */
31         protected function __construct () {
32                 // Call parent constructor
33                 parent::__construct(__CLASS__);
34         }
35
36         /**
37          * Creates an instance of this class
38          *
39          * @return      $assemblerInstance      An instance of an Assembler class
40          */
41         public static final function createPackageAssembler () {
42                 // Get new instance
43                 $assemblerInstance = new PackageAssembler();
44
45                 // Return the prepared instance
46                 return $assemblerInstance;
47         }
48
49         /**
50          * Assembles the content from $packageContent. This method does only
51          * initialize the whole process by creating a call-back which will then
52          * itself (99.9% of all cases) "explode" the decoded data stream and add
53          * it to a chunk assembler queue.
54          *
55          * If the call-back method or this would attempt to assemble the package
56          * chunks and (maybe) re-request some chunks from the sender, this would
57          * take to much time and therefore slow down this node again.
58          *
59          * @param       $packageContent         An array with two elements: 'decoded_data' and 'error_code'
60          * @return      void
61          * @throws      UnsupportedPackageCodeHandlerException  If the package code handler is not implemented
62          */
63         public function chunkPackageContent (array $packageContent) {
64                 // Validate the package content array again
65                 assert(
66                         (isset($packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA])) &&
67                         (isset($packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]))
68                 );
69
70                 // Construct call-back name from package error code
71                 $methodName = 'handlePackageBy' . $this->convertToClassName($packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE]);
72
73                 // Abort if the call-back method is not there
74                 if (!method_exists($this, $methodName)) {
75                         // Throw an exception
76                         throw new UnsupportedPackageCodeHandlerException(array($this, $methodName, $packageContent), BaseListener::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER);
77                 } // END - if
78
79                 // Call it back
80                 call_user_func(array($this, $methodName), $packageContent);
81         }
82
83         /**
84          * Call-back handler to handle unhandled packages. This method "explodes"
85          * the string with the chunk separator from PackageFragmenter class, does
86          * some low checks on it and feeds it into another queue for verification
87          * and re-request for bad chunks.
88          *
89          * @param       $packageContent         An array with two elements: 'decoded_data' and 'error_code'
90          * @return      void
91          * @throws      FinalChunkVerificationException         If the final chunk does not start with 'EOP:'
92          */
93         private function handlePackageByUnhandledPackage (array $packageContent) {
94                 /*
95                  * "explode" the string from 'decoded_data' with chunk separator to
96                  * get an array of chunks. These chunks must then be verified by
97                  * their checksums. Also the final chunk must be handled.
98                  */
99                 $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageContent[BaseRawDataHandler::PACKAGE_DECODED_DATA]);
100                 //* NOISY-DEBUG */ $this->debugOutput('chunks='.print_r($chunks,true));
101
102                 // Validate final chunk
103                 if (!$this->isValidFinalChunk($chunks)) {
104                         // Last chunk is not valid
105                         throw new FinalChunkVerificationException(array($this, $chunks), BaseListener::EXCEPTION_FINAL_CHUNK_VERIFICATION);
106                 } // END - if
107
108                 // Now get a chunk handler instance
109                 $handlerInstance = ChunkHandlerFactory::createChunkHandlerInstance();
110
111                 // Add all chunks because the last final chunk is found
112                 $handlerInstance->addAllChunksWithFinal($chunks);
113         }
114 }
115
116 // [EOF]
117 ?>