]> git.mxchange.org Git - hub.git/blob - application/hub/main/package/class_NetworkPackage.php
Fixed a typo, added an assert()
[hub.git] / application / hub / main / package / class_NetworkPackage.php
1 <?php
2 /**
3  * A NetworkPackage class. This class implements Deliverable and Receivable
4  * because all network packages should be deliverable to other nodes and
5  * receivable from other nodes. It further provides methods for reading raw
6  * content from template engines and feeding it to the stacker for undeclared
7  * packages.
8  *
9  * The factory method requires you to provide a compressor class (which must
10  * implement the Compressor interface). If you don't want any compression (not
11  * adviceable due to increased network load), please use the NullCompressor
12  * class and encode it with BASE64 for a more error-free transfer over the
13  * Internet.
14  *
15  * For performance reasons, this class should only be instanciated once and then
16  * used as a "pipe-through" class.
17  *
18  * @author              Roland Haeder <webmaster@ship-simu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.ship-simu.org
23  * @todo                Needs to add functionality for handling the object's type
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37  */
38 class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receivable, Registerable, Visitable {
39         /**
40          * Package mask for compressing package data:
41          * 0: Compressor extension
42          * 1: Raw package data
43          * 2: Tags, seperated by semicolons, no semicolon is required if only one tag is needed
44          * 3: Checksum
45          *                     0  1  2  3
46          */
47         const PACKAGE_MASK = '%s%s%s%s%s%s%s';
48
49         /**
50          * Separator for the above mask
51          */
52         const PACKAGE_MASK_SEPARATOR = '^';
53
54         /**
55          * SEPARATOR for checksum
56          */
57         const PACKAGE_CHECKSUM_SEPARATOR = '_';
58
59         /**
60          * Array indexes for above mask, start with zero
61          */
62         const INDEX_COMPRESSOR_EXTENSION = 0;
63         const INDEX_PACKAGE_DATA         = 1;
64         const INDEX_TAGS                 = 2;
65         const INDEX_CHECKSUM             = 3;
66
67         /**
68          * Array indexes for raw package array
69          */
70         const INDEX_PACKAGE_SENDER    = 0;
71         const INDEX_PACKAGE_RECIPIENT = 1;
72         const INDEX_PACKAGE_CONTENT   = 2;
73         const INDEX_PACKAGE_STATUS    = 3;
74
75         /**
76          * Named array elements for package data
77          */
78         const PACKAGE_DATA_SENDER    = 'sender';
79         const PACKAGE_DATA_RECIPIENT = 'recipient';
80         const PACKAGE_DATA_CONTENT   = 'content';
81         const PACKAGE_DATA_STATUS    = 'status';
82
83         /**
84          * All package status
85          */
86         const PACKAGE_STATUS_NEW     = 'new';
87         const PACKAGE_STATUS_FAILED  = 'failed';
88
89         /**
90          * Tags SEPARATOR
91          */
92         const PACKAGE_TAGS_SEPARATOR = ';';
93
94         /**
95          * Raw package data SEPARATOR
96          */
97         const PACKAGE_DATA_SEPARATOR = '#';
98
99         /**
100          * Network target (alias): 'upper hubs'
101          */
102         const NETWORK_TARGET_UPPER_HUBS = 'upper';
103
104         /**
105          * Network target (alias): 'self'
106          */
107         const NETWORK_TARGET_SELF = 'self';
108
109         /**
110          * TCP package size in bytes
111          */
112         const TCP_PACKAGE_SIZE = 512;
113
114         /**************************************************************************
115          *                    Stacker for out-going packages                      *
116          **************************************************************************/
117
118         /**
119          * Stacker name for "undeclared" packages
120          */
121         const STACKER_NAME_UNDECLARED = 'package_undeclared';
122
123         /**
124          * Stacker name for "declared" packages (which are ready to send out)
125          */
126         const STACKER_NAME_DECLARED = 'package_declared';
127
128         /**
129          * Stacker name for "out-going" packages
130          */
131         const STACKER_NAME_OUTGOING = 'package_outgoing';
132
133         /**************************************************************************
134          *                     Stacker for incoming packages                      *
135          **************************************************************************/
136
137         /**
138          * Stacker name for "incoming" decoded raw data
139          */
140         const STACKER_NAME_DECODED_INCOMING = 'package_decoded_data';
141
142         /**
143          * Stacker name for handled decoded raw data
144          */
145         const STACKER_NAME_DECODED_HANDLED = 'package_handled_decoded';
146
147         /**
148          * Stacker name for "chunked" decoded raw data
149          */
150         const STACKER_NAME_DECODED_CHUNKED = 'package_chunked_decoded';
151
152         /**************************************************************************
153          *                   Stacker for other/internal purposes                  *
154          **************************************************************************/
155
156         /**
157          * Stacker name for "back-buffered" packages
158          */
159         const STACKER_NAME_BACK_BUFFER = 'package_backbuffer';
160
161         /**
162          * Protected constructor
163          *
164          * @return      void
165          */
166         protected function __construct () {
167                 // Call parent constructor
168                 parent::__construct(__CLASS__);
169         }
170
171         /**
172          * Creates an instance of this class
173          *
174          * @param       $compressorInstance             A Compressor instance for compressing the content
175          * @return      $packageInstance                An instance of a Deliverable class
176          */
177         public static final function createNetworkPackage (Compressor $compressorInstance) {
178                 // Get new instance
179                 $packageInstance = new NetworkPackage();
180
181                 // Now set the compressor instance
182                 $packageInstance->setCompressorInstance($compressorInstance);
183
184                 /*
185                  * We need to initialize a stack here for our packages even for those
186                  * which have no recipient address and stamp... ;-) This stacker will
187                  * also be used for incoming raw data to handle it.
188                  */
189                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('network_package_stacker_class');
190
191                 // At last, set it in this class
192                 $packageInstance->setStackerInstance($stackerInstance);
193
194                 // Init all stacker
195                 $packageInstance->initStackers();
196
197                 // Get a visitor instance for speeding up things
198                 $visitorInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_monitor_visitor_class', array($packageInstance));
199
200                 // Set it in this package
201                 $packageInstance->setVisitorInstance($visitorInstance);
202
203                 // Return the prepared instance
204                 return $packageInstance;
205         }
206
207         /**
208          * Initialize all stackers
209          *
210          * @param       $forceReInit    Whether to force reinitialization of all stacks
211          * @return      void
212          */
213         protected function initStackers ($forceReInit = false) {
214                 // Initialize all
215                 foreach (
216                         array(
217                                 self::STACKER_NAME_UNDECLARED,
218                                 self::STACKER_NAME_DECLARED,
219                                 self::STACKER_NAME_OUTGOING,
220                                 self::STACKER_NAME_DECODED_INCOMING,
221                                 self::STACKER_NAME_DECODED_HANDLED,
222                                 self::STACKER_NAME_DECODED_CHUNKED,
223                                 self::STACKER_NAME_BACK_BUFFER
224                         ) as $stackerName) {
225                                 // Init this stacker
226                                 $this->getStackerInstance()->initStacker($stackerName, $forceReInit);
227                 } // END - foreach
228         }
229
230         /**
231          * "Getter" for hash from given content and helper instance
232          *
233          * @param       $content                        Raw package content
234          * @param       $helperInstance         An instance of a HelpableHub class
235          * @param       $nodeInstance           An instance of a NodeHelper class
236          * @return      $hash                           Hash for given package content
237          * @todo        $helperInstance is unused
238          */
239         private function getHashFromContent ($content, HelpableHub $helperInstance, NodeHelper $nodeInstance) {
240                 // Create the hash
241                 // @TODO crc32() is very weak, but it needs to be fast
242                 $hash = crc32(
243                         $content .
244                         self::PACKAGE_CHECKSUM_SEPARATOR .
245                         $nodeInstance->getSessionId() .
246                         self::PACKAGE_CHECKSUM_SEPARATOR .
247                         $this->getCompressorInstance()->getCompressorExtension()
248                 );
249
250                 // And return it
251                 return $hash;
252         }
253
254         /**
255          * Change the package with given status in given stack
256          *
257          * @param       $packageData    Raw package data in an array
258          * @param       $stackerName    Name of the stacker
259          * @param       $newStatus              New status to set
260          * @return      void
261          */
262         private function changePackageStatus (array $packageData, $stackerName, $newStatus) {
263                 // Skip this for empty stacks
264                 if ($this->getStackerInstance()->isStackEmpty($stackerName)) {
265                         // This avoids an exception after all packages has failed
266                         return;
267                 } // END - if
268
269                 // Pop the entry (it should be it)
270                 $this->getStackerInstance()->popNamed($stackerName);
271
272                 // Temporary set the new status
273                 $packageData[self::PACKAGE_DATA_STATUS] = $newStatus;
274
275                 // And push it again
276                 $this->getStackerInstance()->pushNamed($stackerName, $packageData);
277         }
278
279         ///////////////////////////////////////////////////////////////////////////
280         //                   Delivering packages / raw data
281         ///////////////////////////////////////////////////////////////////////////
282
283         /**
284          * Delivers the given raw package data.
285          *
286          * @param       $packageData    Raw package data in an array
287          * @return      void
288          */
289         private function declareRawPackageData (array $packageData) {
290                 /*
291                  * We need to disover every recipient, just in case we have a
292                  * multi-recipient entry like 'upper' is. 'all' may be a not so good
293                  * target because it causes an overload on the network and may be
294                  * abused for attacking the network with large packages.
295                  */
296                 $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
297
298                 // Discover all recipients, this may throw an exception
299                 $discoveryInstance->discoverRecipients($packageData);
300
301                 // Now get an iterator
302                 $iteratorInstance = $discoveryInstance->getIterator();
303
304                 // ... and begin iteration
305                 while ($iteratorInstance->valid()) {
306                         // Get current entry
307                         $currentRecipient = $iteratorInstance->current();
308
309                         // Debug message
310                         $this->debugOutput('PACKAGE: Package declared for recipient ' . $currentRecipient);
311
312                         // Set the recipient
313                         $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient;
314
315                         // And enqueue it to the writer class
316                         $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData);
317
318                         // Skip to next entry
319                         $iteratorInstance->next();
320                 } // END - while
321
322                 /*
323                  * The recipient list can be cleaned up here because the package which
324                  * shall be delivered has already been added for all entries from the
325                  * list.
326                  */
327                 $discoveryInstance->clearRecipients();
328         }
329
330         /**
331          * Delivers raw package data. In short, this will discover the raw socket
332          * resource through a discovery class (which will analyse the receipient of
333          * the package), register the socket with the connection (handler/helper?)
334          * instance and finally push the raw data on our outgoing queue.
335          *
336          * @param       $packageData    Raw package data in an array
337          * @return      void
338          */
339         private function deliverRawPackageData (array $packageData) {
340                 /*
341                  * This package may become big, depending on the shared object size or
342                  * delivered message size which shouldn't be so long (to save
343                  * bandwidth). Because of the nature of the used protocol (TCP) we need
344                  * to split it up into smaller pieces to fit it into a TCP frame.
345                  *
346                  * So first we need (again) a discovery class but now a protocol
347                  * discovery to choose the right socket resource. The discovery class
348                  * should take a look at the raw package data itself and then decide
349                  * which (configurable!) protocol should be used for that type of
350                  * package.
351                  */
352                 $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance();
353
354                 // Now discover the right protocol
355                 $socketResource = $discoveryInstance->discoverSocket($packageData);
356
357                 // Debug message
358                 //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.');
359
360                 // We have to put this socket in our registry, so get an instance
361                 $registryInstance = SocketRegistry::createSocketRegistry();
362
363                 // Get the listener from registry
364                 $helperInstance = Registry::getRegistry()->getInstance('connection');
365
366                 // Debug message
367                 //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.');
368
369                 // Is it not there?
370                 if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) {
371                         // Then register it
372                         $registryInstance->registerSocket($helperInstance, $socketResource, $packageData);
373                 } // END - if
374
375                 // Debug message
376                 //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.');
377
378                 // Make sure the connection is up
379                 $helperInstance->getStateInstance()->validatePeerStateConnected();
380
381                 // Debug message
382                 //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.');
383
384                 // Enqueue it again on the out-going queue, the connection is up and working at this point
385                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData);
386         }
387
388         /**
389          * Sends waiting packages
390          *
391          * @param       $packageData    Raw package data
392          * @return      void
393          */
394         private function sendOutgoingRawPackageData (array $packageData) {
395                 // Init sent bytes
396                 $sentBytes = 0;
397
398                 // Get the right connection instance
399                 $helperInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($packageData);
400
401                 // Is this connection still alive?
402                 if ($helperInstance->isShuttedDown()) {
403                         // This connection is shutting down
404                         // @TODO We may want to do somthing more here?
405                         return;
406                 } // END - if
407
408                 // Sent out package data
409                 $sentBytes = $helperInstance->sendRawPackageData($packageData);
410
411                 // Remember unsent raw bytes in back-buffer, if any
412                 $this->storeUnsentBytesInBackBuffer($packageData, $sentBytes);
413         }
414
415         /**
416          * "Enqueues" raw content into this delivery class by reading the raw content
417          * from given template instance and pushing it on the 'undeclared' stack.
418          *
419          * @param       $helperInstance         An instance of a HelpableHub class
420          * @param       $nodeInstance           An instance of a NodeHelper class
421          * @return      void
422          */
423         public function enqueueRawDataFromTemplate (HelpableHub $helperInstance, NodeHelper $nodeInstance) {
424                 // Get the raw content ...
425                 $content = $helperInstance->getTemplateInstance()->getRawTemplateData();
426
427                 // ... and compress it
428                 $content = $this->getCompressorInstance()->compressStream($content);
429
430                 // Add magic in front of it and hash behind it, including BASE64 encoding
431                 $content = sprintf(self::PACKAGE_MASK,
432                         // 1.) Compressor's extension
433                         $this->getCompressorInstance()->getCompressorExtension(),
434                         // - separator
435                         self::PACKAGE_MASK_SEPARATOR,
436                         // 2.) Raw package content, encoded with BASE64
437                         base64_encode($content),
438                         // - separator
439                         self::PACKAGE_MASK_SEPARATOR,
440                         // 3.) Tags
441                         implode(self::PACKAGE_TAGS_SEPARATOR, $helperInstance->getPackageTags()),
442                         // - separator
443                         self::PACKAGE_MASK_SEPARATOR,
444                         // 4.) Checksum
445                         $this->getHashFromContent($content, $helperInstance, $nodeInstance)
446                 );
447
448                 // Now prepare the temporary array and push it on the 'undeclared' stack
449                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
450                         self::PACKAGE_DATA_SENDER    => $nodeInstance->getSessionId(),
451                         self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(),
452                         self::PACKAGE_DATA_CONTENT   => $content,
453                         self::PACKAGE_DATA_STATUS    => self::PACKAGE_STATUS_NEW
454                 ));
455         }
456
457         /**
458          * Checks whether a package has been enqueued for delivery.
459          *
460          * @return      $isEnqueued             Whether a package is enqueued
461          */
462         public function isPackageEnqueued () {
463                 // Check whether the stacker is not empty
464                 $isEnqueued = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED)));
465
466                 // Return the result
467                 return $isEnqueued;
468         }
469
470         /**
471          * Checks whether a package has been declared
472          *
473          * @return      $isDeclared             Whether a package is declared
474          */
475         public function isPackageDeclared () {
476                 // Check whether the stacker is not empty
477                 $isDeclared = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_DECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECLARED)));
478
479                 // Return the result
480                 return $isDeclared;
481         }
482
483         /**
484          * Checks whether a package should be sent out
485          *
486          * @return      $isWaitingDelivery      Whether a package is waiting for delivery
487          */
488         public function isPackageWaitingForDelivery () {
489                 // Check whether the stacker is not empty
490                 $isWaitingDelivery = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)));
491
492                 // Return the result
493                 return $isWaitingDelivery;
494         }
495
496         /**
497          * Delivers an enqueued package to the stated destination. If a non-session
498          * id is provided, recipient resolver is being asked (and instanced once).
499          * This allows that a single package is being delivered to multiple targets
500          * without enqueueing it for every target. If no target is provided or it
501          * can't be determined a NoTargetException is being thrown.
502          *
503          * @return      void
504          * @throws      NoTargetException       If no target can't be determined
505          */
506         public function declareEnqueuedPackage () {
507                 // Make sure this method isn't working if there is no package enqueued
508                 if (!$this->isPackageEnqueued()) {
509                         // This is not fatal but should be avoided
510                         // @TODO Add some logging here
511                         return;
512                 } // END - if
513
514                 // Now we know for sure there are packages to deliver, we can start
515                 // with the first one.
516                 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_UNDECLARED);
517
518                 // Declare the raw package data for delivery
519                 $this->declareRawPackageData($packageData);
520
521                 // And remove it finally
522                 $this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
523         }
524
525         /**
526          * Delivers the next declared package. Only one package per time will be sent
527          * because this may take time and slows down the whole delivery
528          * infrastructure.
529          *
530          * @return      void
531          */
532         public function deliverDeclaredPackage () {
533                 // Sanity check if we have packages declared
534                 if (!$this->isPackageDeclared()) {
535                         // This is not fatal but should be avoided
536                         // @TODO Add some logging here
537                         return;
538                 } // END - if
539
540                 // Get the package
541                 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
542
543                 try {
544                         // And try to send it
545                         $this->deliverRawPackageData($packageData);
546
547                         // And remove it finally
548                         $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
549                 } catch (InvalidStateException $e) {
550                         // The state is not excepected (shall be 'connected')
551                         $this->debugOutput('PACKAGE: Caught exception ' . $e->__toString() . ' with message=' . $e->getMessage());
552
553                         // Mark the package with status failed
554                         $this->changePackageStatus($packageData, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED);
555                 }
556         }
557
558         /**
559          * Sends waiting packages out for delivery
560          *
561          * @return      void
562          */
563         public function sendWaitingPackage () {
564                 // Send any waiting bytes in the back-buffer before sending a new package
565                 $this->sendBackBufferBytes();
566
567                 // Sanity check if we have packages waiting for delivery
568                 if (!$this->isPackageWaitingForDelivery()) {
569                         // This is not fatal but should be avoided
570                         $this->debugOutput('PACKAGE: No package is waiting for delivery, but ' . __METHOD__ . ' was called.');
571                         return;
572                 } // END - if
573
574                 // Get the package
575                 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_OUTGOING);
576
577                 try {
578                         // Now try to send it
579                         $this->sendOutgoingRawPackageData($packageData);
580
581                         // And remove it finally
582                         $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
583                 } catch (InvalidSocketException $e) {
584                         // Output exception message
585                         $this->debugOutput('PACKAGE: Package was not delivered: ' . $e->getMessage());
586
587                         // Mark package as failed
588                         $this->changePackageStatus($packageData, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED);
589                 }
590         }
591
592         ///////////////////////////////////////////////////////////////////////////
593         //                   Receiving packages / raw data
594         ///////////////////////////////////////////////////////////////////////////
595
596         /**
597          * Checks whether decoded raw data is pending
598          *
599          * @return      $isPending      Whether decoded raw data is pending
600          */
601         private function isDecodedDataPending () {
602                 // Just return whether the stack is not empty
603                 $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_INCOMING));
604
605                 // Return the status
606                 return $isPending;
607         }
608
609         /**
610          * Checks whether new raw package data has arrived at a socket
611          *
612          * @param       $poolInstance   An instance of a PoolableListener class
613          * @return      $hasArrived             Whether new raw package data has arrived for processing
614          */
615         public function isNewRawDataPending (PoolableListener $poolInstance) {
616                 // Visit the pool. This monitors the pool for incoming raw data.
617                 $poolInstance->accept($this->getVisitorInstance());
618
619                 // Check for new data arrival
620                 $hasArrived = $this->isDecodedDataPending();
621
622                 // Return the status
623                 return $hasArrived;
624         }
625
626         /**
627          * Handles the incoming decoded raw data. This method does not "convert" the
628          * decoded data back into a package array, it just "handles" it and pushs it
629          * on the next stack.
630          *
631          * @return      void
632          */
633         public function handleIncomingDecodedData () {
634                 /*
635                  * This method should only be called if decoded raw data is pending,
636                  * so check it again.
637                  */
638                 if (!$this->isDecodedDataPending()) {
639                         // This is not fatal but should be avoided
640                         // @TODO Add some logging here
641                         return;
642                 } // END - if
643
644                 // Very noisy debug message:
645                 /* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Stacker size is ' . $this->getStackerInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.');
646
647                 // "Pop" the next entry (the same array again) from the stack
648                 $decodedData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING);
649
650                 // Make sure both array elements are there
651                 assert(
652                         (is_array($decodedData)) &&
653                         (isset($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA])) &&
654                         (isset($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE]))
655                 );
656
657                 /*
658                  * Also make sure the error code is SOCKET_ERROR_UNHANDLED because we
659                  * only want to handle unhandled packages here.
660                  */
661                 assert($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] == BaseRawDataHandler::SOCKET_ERROR_UNHANDLED);
662
663                 // Remove the last chunk SEPARATOR (because it is being added and we don't need it)
664                 if (substr($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
665                         // It is there and should be removed
666                         $decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA] = substr($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA], 0, -1);
667                 } // END - if
668
669                 // This package is "handled" and can be pushed on the next stack
670                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $decodedData);
671         }
672
673         /**
674          * Adds raw decoded data from the given handler instance to this receiver
675          *
676          * @param       $handlerInstance        An instance of a Networkable class
677          * @return      void
678          */
679         public function addDecodedDataToIncomingStack (Networkable $handlerInstance) {
680                 /*
681                  * Get the decoded data from the handler, this is an array with
682                  * 'decoded_data' and 'error_code' as elements.
683                  */
684                 $decodedData = $handlerInstance->getNextDecodedData();
685
686                 // Very noisy debug message:
687                 //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, true));
688
689                 // And push it on our stack
690                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $decodedData);
691         }
692
693         /**
694          * Checks whether incoming decoded data is handled.
695          *
696          * @return      $isHandled      Whether incoming decoded data is handled
697          */
698         public function isIncomingDecodedDataHandled () {
699                 // Determine if the stack is not empty
700                 $isHandled = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_HANDLED));
701
702                 // Return it
703                 return $isHandled;
704         }
705
706         /**
707          * Assembles incoming decoded data so it will become an abstract network
708          * package again. The assembler does later do it's job by an other task,
709          * not this one to keep best speed possible.
710          *
711          * @return      void
712          */
713         public function assembleDecodedDataToPackage () {
714                 // Make sure the raw decoded package data is handled
715                 assert($this->isIncomingDecodedDataHandled());
716
717                 // Get current package content (an array with two elements; see handleIncomingDecodedData() for details)
718                 $packageContent = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED);
719
720                 // Get a singleton package assembler instance from factory
721                 $assemblerInstance = PackageAssemblerFactory::createAssemblerInstance();
722
723                 // Start assembling the raw package data array by chunking it
724                 $assemblerInstance->chunkPackageContent($packageContent);
725
726                 // Remove the package from 'handled_decoded' stack ...
727                 $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_HANDLED);
728
729                 // ... and push it on the 'chunked' stacker
730                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageContent);
731         }
732
733         /**
734          * Checks whether a new package has arrived
735          *
736          * @return      $hasArrived             Whether a new package has arrived for processing
737          */
738         public function isNewPackageArrived () {
739                 // @TODO Add some content here
740         }
741
742         /**
743          * Accepts the visitor to process the visit "request"
744          *
745          * @param       $visitorInstance        An instance of a Visitor class
746          * @return      void
747          */
748         public function accept (Visitor $visitorInstance) {
749                 // Debug message
750                 //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: ' . $visitorInstance->__toString() . ' has visited - START');
751
752                 // Visit the package
753                 $visitorInstance->visitNetworkPackage($this);
754
755                 // Debug message
756                 //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
757         }
758
759         /**
760          * Clears all stacker
761          *
762          * @return      void
763          */
764         public function clearAllStacker () {
765                 // Call the init method to force re-initialization
766                 $this->initStackers(true);
767
768                 // Debug message
769                 /* DEBUG: */ $this->debugOutput('PACKAGE: All stacker have been re-initialized.');
770         }
771
772         /**
773          * Removes the first failed outoging package from the stack to continue
774          * with next one (it will never work until the issue is fixed by you).
775          *
776          * @return      void
777          * @throws      UnexpectedPackageStatusException        If the package status is not 'failed'
778          * @todo        This may be enchanced for outgoing packages?
779          */
780         public function removeFirstFailedPackage () {
781                 // Get the package again
782                 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
783
784                 // Is the package status 'failed'?
785                 if ($packageData[self::PACKAGE_DATA_STATUS] != self::PACKAGE_STATUS_FAILED) {
786                         // Not failed!
787                         throw new UnexpectedPackageStatusException(array($this, $packageData, self::PACKAGE_STATUS_FAILED), BaseListener::EXCEPTION_UNEXPECTED_PACKAGE_STATUS);
788                 } // END - if
789
790                 // Remove this entry
791                 $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
792         }
793 }
794
795 // [EOF]
796 ?>