3 * A NetworkPackage class. This class implements Deliverable because all network
4 * packages should be deliverable to other nodes. It further provides methods
5 * for reading raw content from template engines and feeding it to the stacker
6 * 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 instanciated 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 - 2011 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:
40 * 0: Compressor extension
42 * 2: Tags, seperated by semicolons, no semicolon is required if only one tag is needed
46 const PACKAGE_MASK = '%s:%s:%s:%s';
49 * Seperator for the above mask
51 const PACKAGE_MASK_SEPERATOR = ':';
54 * Array indexes for above mask, start with zero
56 const INDEX_COMPRESSOR_EXTENSION = 0;
57 const INDEX_PACKAGE_DATA = 1;
59 const INDEX_CHECKSUM = 3;
62 * Array indexes for raw package array
64 const INDEX_PACKAGE_SENDER = 0;
65 const INDEX_PACKAGE_RECIPIENT = 1;
66 const INDEX_PACKAGE_CONTENT = 2;
71 const PACKAGE_TAGS_SEPERATOR = ';';
74 * Raw package data seperator
76 const PACKAGE_DATA_SEPERATOR = '|';
79 * Stacker name for "undeclared" packages
81 const STACKER_NAME_UNDECLARED = 'undeclared';
84 * Stacker name for "declared" packages (which are ready to send out)
86 const STACKER_NAME_DECLARED = 'declared';
89 * Stacker name for "out-going" packages
91 const STACKER_NAME_OUTGOING = 'outgoing';
94 * Stacker name for "back-buffered" packages
96 const STACKER_NAME_BACK_BUFFER = 'backbuffer';
99 * Network target (alias): 'upper hubs'
101 const NETWORK_TARGET_UPPER_HUBS = 'upper';
104 * Network target (alias): 'self'
106 const NETWORK_TARGET_SELF = 'self';
109 * Protected constructor
113 protected function __construct () {
114 // Call parent constructor
115 parent::__construct(__CLASS__);
117 // We need to initialize a stack here for our packages even those
118 // which have no recipient address and stamp... ;-)
119 $stackerInstance = ObjectFactory::createObjectByConfiguredName('network_package_stacker_class');
121 // At last, set it in this class
122 $this->setStackerInstance($stackerInstance);
126 * Creates an instance of this class
128 * @param $compressorInstance A Compressor instance for compressing the content
129 * @return $packageInstance An instance of a Deliverable class
131 public static final function createNetworkPackage (Compressor $compressorInstance) {
133 $packageInstance = new NetworkPackage();
135 // Now set the compressor instance
136 $packageInstance->setCompressorInstance($compressorInstance);
138 // Return the prepared instance
139 return $packageInstance;
143 * "Getter" for hash from given content and helper instance
145 * @param $content Raw package content
146 * @param $helperInstance An instance of a BaseHubHelper class
147 * @param $nodeInstance An instance of a NodeHelper class
148 * @return $hash Hash for given package content
150 private function getHashFromContent ($content, BaseHubHelper $helperInstance, NodeHelper $nodeInstance) {
152 // @TODO crc32 is not good, but it needs to be fast
156 $nodeInstance->getSessionId() .
158 $this->getCompressorInstance()->getCompressorExtension()
166 * Delivers the given raw package data.
168 * @param $packageData Raw package data in an array
171 private function declareRawPackageData (array $packageData) {
173 * We need to disover every recipient, just in case we have a
174 * multi-recipient entry like 'upper' is. 'all' may be a not so good
175 * target because it causes an overload on the network and may be
176 * abused for attacking the network with large packages.
178 $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
180 // Discover all recipients, this may throw an exception
181 $discoveryInstance->discoverRecipients($packageData);
183 // Now get an iterator
184 $iteratorInstance = $discoveryInstance->getIterator();
186 // ... and begin iteration
187 while ($iteratorInstance->valid()) {
189 $currentRecipient = $iteratorInstance->current();
192 $this->debugOutput('PACKAGE: Package declared for recipient ' . $currentRecipient);
195 $packageData['recipient'] = $currentRecipient;
197 // And enqueue it to the writer class
198 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData);
200 // Skip to next entry
201 $iteratorInstance->next();
205 $discoveryInstance->clearRecipients();
209 * Delivers raw package data. In short, this will discover the raw socket
210 * resource through a discovery class (which will analyse the receipient of
211 * the package), register the socket with the connection (handler/helper?)
212 * instance and finally push the raw data on our outgoing queue.
214 * @param $packageData Raw package data in an array
217 private function deliverRawPackageData (array $packageData) {
219 * This package may become big, depending on the shared object size or
220 * delivered message size which shouldn't be so long (to save
221 * bandwidth). Because of the nature of the used protocol (TCP) we need
222 * to split it up into smaller pieces to fit it into a TCP frame.
224 * So first we need (again) a discovery class but now a protocol
225 * discovery to choose the right socket resource. The discovery class
226 * should take a look at the raw package data itself and then decide
227 * which (configurable!) protocol should be used for that type of
230 $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance();
232 // Now discover the right protocol
233 $socketResource = $discoveryInstance->discoverSocket($packageData);
235 // We have to put this socket in our registry, so get an instance
236 $registryInstance = SocketRegistry::createSocketRegistry();
238 // Get the listener from registry
239 $connectionInstance = Registry::getRegistry()->getInstance('connection');
242 if (!$registryInstance->isSocketRegistered($connectionInstance, $socketResource)) {
244 $registryInstance->registerSocket($connectionInstance, $socketResource, $packageData);
247 // We enqueue it again, but now in the out-going queue
248 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData);
252 * Sends waiting packages
254 * @param $packageData Raw package data
257 private function sendOutgoingRawPackageData (array $packageData) {
258 // Get the right connection instance
259 $connectionInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($packageData);
261 // Is this connection still alive?
262 if ($connectionInstance->isShuttedDown()) {
263 // This connection is shutting down
264 // @TODO We may want to do somthing more here?
268 // Sent it away (we catch exceptions one method above
269 $sentBytes = $connectionInstance->sendRawPackageData($packageData);
271 // Remember unsent raw bytes in back-buffer, if any
272 $this->storeUnsentBytesInBackBuffer($packageData, $sentBytes);
276 * "Enqueues" raw content into this delivery class by reading the raw content
277 * from given template instance and pushing it on the 'undeclared' stack.
279 * @param $helperInstance An instance of a BaseHubHelper class
280 * @param $nodeInstance An instance of a NodeHelper class
283 public function enqueueRawDataFromTemplate (BaseHubHelper $helperInstance, NodeHelper $nodeInstance) {
284 // Get the raw content ...
285 $content = $helperInstance->getTemplateInstance()->getRawTemplateData();
287 // ... and compress it
288 $content = $this->getCompressorInstance()->compressStream($content);
290 // Add magic in front of it and hash behind it, including BASE64 encoding
291 $content = sprintf(self::PACKAGE_MASK,
292 // 1.) Compressor's extension
293 $this->getCompressorInstance()->getCompressorExtension(),
294 // 2.) Raw package content, encoded with BASE64
295 base64_encode($content),
297 implode(self::PACKAGE_TAGS_SEPERATOR, $helperInstance->getPackageTags()),
299 $this->getHashFromContent($content, $helperInstance, $nodeInstance)
302 // Now prepare the temporary array and push it on the 'undeclared' stack
303 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
304 'sender' => $nodeInstance->getSessionId(),
305 'recipient' => $helperInstance->getRecipientType(),
306 'content' => $content,
311 * Checks wether a package has been enqueued for delivery.
313 * @return $isEnqueued Wether a package is enqueued
315 public function isPackageEnqueued () {
316 // Check wether the stacker is not empty
317 $isEnqueued = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED)));
324 * Checks wether a package has been declared
326 * @return $isDeclared Wether a package is declared
328 public function isPackageDeclared () {
329 // Check wether the stacker is not empty
330 $isDeclared = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_DECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECLARED)));
337 * Checks wether a package should be sent out
339 * @return $isWaitingDelivery Wether a package is waiting for delivery
341 public function isPackageWaitingForDelivery () {
342 // Check wether the stacker is not empty
343 $isWaitingDelivery = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)));
346 return $isWaitingDelivery;
350 * Delivers an enqueued package to the stated destination. If a non-session
351 * id is provided, recipient resolver is being asked (and instanced once).
352 * This allows that a single package is being delivered to multiple targets
353 * without enqueueing it for every target. If no target is provided or it
354 * can't be determined a NoTargetException is being thrown.
357 * @throws NoTargetException If no target can't be determined
359 public function declareEnqueuedPackage () {
360 // Make sure this method isn't working if there is no package enqueued
361 if (!$this->isPackageEnqueued()) {
362 // This is not fatal but should be avoided
363 // @TODO Add some logging here
367 // Now we know for sure there are packages to deliver, we can start
368 // with the first one.
369 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_UNDECLARED);
371 // Declare the raw package data for delivery
372 $this->declareRawPackageData($packageData);
374 // And remove it finally
375 $this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
379 * Delivers the next declared package. Only one package per time will be sent
380 * because this may take time and slows down the whole delivery
385 public function deliverDeclaredPackage () {
386 // Sanity check if we have packages declared
387 if (!$this->isPackageDeclared()) {
388 // This is not fatal but should be avoided
389 // @TODO Add some logging here
393 // Get the package again
394 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
397 $this->deliverRawPackageData($packageData);
399 // And remove it finally
400 $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
404 * Sends waiting packages out for delivery
408 public function sendWaitingPackage () {
409 // Send any waiting bytes in the back-buffer before sending a new package
410 $this->sendBackBufferBytes();
412 // Sanity check if we have packages waiting for delivery
413 if (!$this->isPackageWaitingForDelivery()) {
414 // This is not fatal but should be avoided
415 $this->debugOutput('PACKAGE: No package is waiting for delivery, but ' . __FUNCTION__ . ' was called.');
419 // Get the package again
420 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_OUTGOING);
423 // Now try to send it
424 $this->sendOutgoingRawPackageData($packageData);
427 // And remove it finally
428 $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
429 } catch (InvalidSocketException $e) {
430 // Output exception message
431 $this->debugOutput('PACKAGE: Package was not delivered: ' . $e->getMessage());