3 * A NetworkPackage class. This class implements the Deliverable class because
4 * all network packages should be deliverable to other nodes. It further
5 * provides methods for reading raw content from template engines and feeding it
6 * to the stacker for undeclared packages.
8 * The factory method requires you to provide a compressor class (which must
9 * implement the Compressor interface). If you don't want any compression (not
10 * adviceable due to increased network load), please use the NullCompressor
11 * class and encode it with BASE64 for a more error-free transfer over the
14 * For performance reasons, this class should only be instantiated once and then
15 * used as a "pipe-through" class.
17 * @author Roland Haeder <webmaster@ship-simu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.ship-simu.org
22 * @todo Needs to add functionality for handling the object's type
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registerable {
39 * Package mask for compressing package data
41 const PACKAGE_MASK = '%s:%s:%s';
44 * Stacker name for "undeclared" packages
46 const STACKER_NAME_UNDECLARED = 'undeclared';
49 * Protected constructor
53 protected function __construct () {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
57 // We need to initialize a stack here for our packages even those
58 // which have no recipient address and stamp... ;-)
59 $stackerInstance = ObjectFactory::createObjectByConfiguredName('package_stacker_class');
61 // At last, set it in this class
62 $this->setStackerInstance($stackerInstance);
66 * Creates an instance of this class
68 * @param $compressorInstance A Compressor instance for compressing the content
69 * @return $packageInstance An instance of a Deliverable class
71 public final static function createNetworkPackage (Compressor $compressorInstance) {
73 $packageInstance = new NetworkPackage();
75 // Now set the compressor instance if set
76 if ($compressorInstance instanceof Compressor) {
78 $packageInstance->setCompressorInstance($compressorInstance);
81 // Return the prepared instance
82 return $packageInstance;
86 * "Enqueues" raw content into this delivery class by reading the raw content
87 * from given template instance and pushing it on the 'undeclared' stack.
89 * @param $helperInstance A BaseHubHelper instance
92 public function enqueueRawDataFromTemplate (BaseHubHelper $helperInstance) {
93 // Get the raw content ...
94 $content = $helperInstance->getTemplateInstance()->getRawTemplateData();
96 // ... and compress it
97 $content = $this->getCompressorInstance()->compressStream($content);
99 // Add magic in front of it and hash behind it, including BASE64 encoding
100 $content = sprintf(self::PACKAGE_MASK,
101 $this->getCompressorInstance()->getCompressorExtension(),
102 base64_encode($content),
103 crc32($content) // @TODO Not so good, but needs to be fast!
106 // Now prepare the temporary array and push it on the 'undeclared' stack including a call-back helper instance
107 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
108 'sender' => $helperInstance->getNodeInstance()->getSessionId(),
110 'content' => $content,
111 'callback' => $helperInstance