]> git.mxchange.org Git - hub.git/blob - application/hub/main/package/class_NetworkPackage.php
Corrected assert, added debug line
[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 - 2012 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 BaseHubSystem 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          * Size of an array created by invoking
56          * explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $content).
57          */
58         const PACKAGE_CONTENT_ARRAY_SIZE = 4;
59
60         /**
61          * Separator for checksum
62          */
63         const PACKAGE_CHECKSUM_SEPARATOR = '_';
64
65         /**
66          * Array indexes for above mask, start with zero
67          */
68         const INDEX_COMPRESSOR_EXTENSION = 0;
69         const INDEX_PACKAGE_DATA         = 1;
70         const INDEX_TAGS                 = 2;
71         const INDEX_CHECKSUM             = 3;
72
73         /**
74          * Array indexes for raw package array
75          */
76         const INDEX_PACKAGE_SENDER    = 0;
77         const INDEX_PACKAGE_RECIPIENT = 1;
78         const INDEX_PACKAGE_CONTENT   = 2;
79         const INDEX_PACKAGE_STATUS    = 3;
80         const INDEX_PACKAGE_SIGNATURE = 4;
81
82         /**
83          * Size of the decoded data array ('status' is not included)
84          */
85         const DECODED_DATA_ARRAY_SIZE = 4;
86
87         /**
88          * Named array elements for decoded package content
89          */
90         const PACKAGE_CONTENT_EXTENSION = 'compressor';
91         const PACKAGE_CONTENT_MESSAGE   = 'message';
92         const PACKAGE_CONTENT_TAGS      = 'tags';
93         const PACKAGE_CONTENT_CHECKSUM  = 'checksum';
94
95         /**
96          * Named array elements for package data
97          */
98         const PACKAGE_DATA_SENDER    = 'sender';
99         const PACKAGE_DATA_RECIPIENT = 'recipient';
100         const PACKAGE_DATA_PROTOCOL  = 'protocol';
101         const PACKAGE_DATA_CONTENT   = 'content';
102         const PACKAGE_DATA_STATUS    = 'status';
103         const PACKAGE_DATA_SIGNATURE = 'signature';
104
105         /**
106          * All package status
107          */
108         const PACKAGE_STATUS_NEW     = 'new';
109         const PACKAGE_STATUS_FAILED  = 'failed';
110         const PACKAGE_STATUS_DECODED = 'decoded';
111         const PACKAGE_STATUS_FAKED   = 'faked';
112
113         /**
114          * Constants for message data array
115          */
116         const MESSAGE_ARRAY_DATA = 'message_data';
117         const MESSAGE_ARRAY_TYPE = 'message_type';
118
119         /**
120          * Generic answer status field
121          */
122
123         /**
124          * Tags separator
125          */
126         const PACKAGE_TAGS_SEPARATOR = ';';
127
128         /**
129          * Raw package data separator
130          */
131         const PACKAGE_DATA_SEPARATOR = '#';
132
133         /**
134          * Separator for more than one recipient
135          */
136         const PACKAGE_RECIPIENT_SEPARATOR = ':';
137
138         /**
139          * Network target (alias): 'upper nodes'
140          */
141         const NETWORK_TARGET_UPPER = 'upper';
142
143         /**
144          * Network target (alias): 'self'
145          */
146         const NETWORK_TARGET_SELF = 'self';
147
148         /**
149          * Network target (alias): 'dht'
150          */
151         const NETWORK_TARGET_DHT = 'dht';
152
153         /**
154          * TCP package size in bytes
155          */
156         const TCP_PACKAGE_SIZE = 512;
157
158         /**************************************************************************
159          *                    Stacker for out-going packages                      *
160          **************************************************************************/
161
162         /**
163          * Stacker name for "undeclared" packages
164          */
165         const STACKER_NAME_UNDECLARED = 'package_undeclared';
166
167         /**
168          * Stacker name for "declared" packages (which are ready to send out)
169          */
170         const STACKER_NAME_DECLARED = 'package_declared';
171
172         /**
173          * Stacker name for "out-going" packages
174          */
175         const STACKER_NAME_OUTGOING = 'package_outgoing';
176
177         /**************************************************************************
178          *                     Stacker for incoming packages                      *
179          **************************************************************************/
180
181         /**
182          * Stacker name for "incoming" decoded raw data
183          */
184         const STACKER_NAME_DECODED_INCOMING = 'package_decoded_data';
185
186         /**
187          * Stacker name for handled decoded raw data
188          */
189         const STACKER_NAME_DECODED_HANDLED = 'package_handled_decoded';
190
191         /**
192          * Stacker name for "chunked" decoded raw data
193          */
194         const STACKER_NAME_DECODED_CHUNKED = 'package_chunked_decoded';
195
196         /**************************************************************************
197          *                     Stacker for incoming messages                      *
198          **************************************************************************/
199
200         /**
201          * Stacker name for new messages
202          */
203         const STACKER_NAME_NEW_MESSAGE = 'package_new_message';
204
205         /**
206          * Stacker name for processed messages
207          */
208         const STACKER_NAME_PROCESSED_MESSAGE = 'package_processed_message';
209
210         /**************************************************************************
211          *                   Stacker for other/internal purposes                  *
212          **************************************************************************/
213
214         /**
215          * Stacker name for "back-buffered" packages
216          */
217         const STACKER_NAME_BACK_BUFFER = 'package_backbuffer';
218
219         /**************************************************************************
220          *                            Protocol names                              *
221          **************************************************************************/
222         const PROTOCOL_TCP = 'TCP';
223         const PROTOCOL_UDP = 'UDP';
224
225         /**
226          * Protected constructor
227          *
228          * @return      void
229          */
230         protected function __construct () {
231                 // Call parent constructor
232                 parent::__construct(__CLASS__);
233         }
234
235         /**
236          * Creates an instance of this class
237          *
238          * @param       $compressorInstance             A Compressor instance for compressing the content
239          * @return      $packageInstance                An instance of a Deliverable class
240          */
241         public static final function createNetworkPackage (Compressor $compressorInstance) {
242                 // Get new instance
243                 $packageInstance = new NetworkPackage();
244
245                 // Now set the compressor instance
246                 $packageInstance->setCompressorInstance($compressorInstance);
247
248                 /*
249                  * We need to initialize a stack here for our packages even for those
250                  * which have no recipient address and stamp... ;-) This stacker will
251                  * also be used for incoming raw data to handle it.
252                  */
253                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('network_package_stacker_class');
254
255                 // At last, set it in this class
256                 $packageInstance->setStackerInstance($stackerInstance);
257
258                 // Init all stacker
259                 $packageInstance->initStackers();
260
261                 // Get a visitor instance for speeding up things and set it
262                 $visitorInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_monitor_visitor_class', array($packageInstance));
263                 $packageInstance->setVisitorInstance($visitorInstance);
264
265                 // Get crypto instance and set it, too
266                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
267                 $packageInstance->setCryptoInstance($cryptoInstance);
268
269                 // Get a singleton package assembler instance from factory and set it here, too
270                 $assemblerInstance = PackageAssemblerFactory::createAssemblerInstance($packageInstance);
271                 $packageInstance->setAssemblerInstance($assemblerInstance);
272
273                 // Return the prepared instance
274                 return $packageInstance;
275         }
276
277         /**
278          * Initialize all stackers
279          *
280          * @param       $forceReInit    Whether to force reinitialization of all stacks
281          * @return      void
282          */
283         protected function initStackers ($forceReInit = FALSE) {
284                 // Initialize all
285                 foreach (
286                         array(
287                                 self::STACKER_NAME_UNDECLARED,
288                                 self::STACKER_NAME_DECLARED,
289                                 self::STACKER_NAME_OUTGOING,
290                                 self::STACKER_NAME_DECODED_INCOMING,
291                                 self::STACKER_NAME_DECODED_HANDLED,
292                                 self::STACKER_NAME_DECODED_CHUNKED,
293                                 self::STACKER_NAME_NEW_MESSAGE,
294                                 self::STACKER_NAME_PROCESSED_MESSAGE,
295                                 self::STACKER_NAME_BACK_BUFFER
296                         ) as $stackerName) {
297                                 // Init this stacker
298                                 //* DEBUG: */ print(__METHOD__ . ': stackerName=' . $stackerName . ',forceReInit=' . intval($forceReInit) . PHP_EOL);
299                                 $this->getStackerInstance()->initStacker($stackerName, $forceReInit);
300                 } // END - foreach
301         }
302
303         /**
304          * "Getter" for hash from given content
305          *
306          * @param       $content        Raw package content
307          * @return      $hash           Hash for given package content
308          */
309         private function getHashFromContent ($content) {
310                 // Debug message
311                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension());
312
313                 // Create the hash
314                 // @TODO crc32() is very weak, but it needs to be fast
315                 $hash = crc32(
316                         $content .
317                         self::PACKAGE_CHECKSUM_SEPARATOR .
318                         $this->getSessionId() .
319                         self::PACKAGE_CHECKSUM_SEPARATOR .
320                         $this->getCompressorInstance()->getCompressorExtension()
321                 );
322
323                 // Debug message
324                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension());
325
326                 // And return it
327                 return $hash;
328         }
329
330         /**
331          * Checks whether the checksum (sometimes called "hash") is the same
332          *
333          * @param       $decodedContent         Package raw content
334          * @param       $decodedData            Whole raw package data array
335          * @return      $isChecksumValid        Whether the checksum is the same
336          */
337         private function isChecksumValid (array $decodedContent, array $decodedData) {
338                 // Get checksum
339                 $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER]);
340
341                 // Is it the same?
342                 $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]);
343
344                 // Return it
345                 return $isChecksumValid;
346         }
347
348         /**
349          * Change the package with given status in given stack
350          *
351          * @param       $packageData    Raw package data in an array
352          * @param       $stackerName    Name of the stacker
353          * @param       $newStatus              New status to set
354          * @return      void
355          */
356         private function changePackageStatus (array $packageData, $stackerName, $newStatus) {
357                 // Skip this for empty stacks
358                 if ($this->getStackerInstance()->isStackEmpty($stackerName)) {
359                         // This avoids an exception after all packages has failed
360                         return;
361                 } // END - if
362
363                 // Pop the entry (it should be it)
364                 $nextData = $this->getStackerInstance()->popNamed($stackerName);
365
366                 // Compare both signatures
367                 assert($nextData[self::PACKAGE_DATA_SIGNATURE] == $packageData[self::PACKAGE_DATA_SIGNATURE]);
368
369                 // Temporary set the new status
370                 $packageData[self::PACKAGE_DATA_STATUS] = $newStatus;
371
372                 // And push it again
373                 $this->getStackerInstance()->pushNamed($stackerName, $packageData);
374         }
375
376         /**
377          * "Getter" for hash from given content and sender's session id
378          *
379          * @param       $decodedContent         Raw package content
380          * @param       $sessionId                      Session id of the sender
381          * @return      $hash                           Hash for given package content
382          */
383         public function getHashFromContentSessionId (array $decodedContent, $sessionId) {
384                 // Debug message
385                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_EXTENSION]);
386
387                 // Create the hash
388                 // @TODO crc32() is very weak, but it needs to be fast
389                 $hash = crc32(
390                         $decodedContent[self::PACKAGE_CONTENT_MESSAGE] .
391                         self::PACKAGE_CHECKSUM_SEPARATOR .
392                         $sessionId .
393                         self::PACKAGE_CHECKSUM_SEPARATOR .
394                         $decodedContent[self::PACKAGE_CONTENT_EXTENSION]
395                 );
396
397                 // And return it
398                 return $hash;
399         }
400
401         ///////////////////////////////////////////////////////////////////////////
402         //                   Delivering packages / raw data
403         ///////////////////////////////////////////////////////////////////////////
404
405         /**
406          * Delivers the given raw package data.
407          *
408          * @param       $packageData    Raw package data in an array
409          * @return      void
410          */
411         private function declareRawPackageData (array $packageData) {
412                 // Make sure the required field is there
413                 assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT]));
414
415                 /*
416                  * We need to disover every recipient, just in case we have a
417                  * multi-recipient entry like 'upper' is. 'all' may be a not so good
418                  * target because it causes an overload on the network and may be
419                  * abused for attacking the network with large packages.
420                  */
421                 $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
422
423                 // Discover all recipients, this may throw an exception
424                 $discoveryInstance->discoverRecipients($packageData);
425
426                 // Now get an iterator
427                 $iteratorInstance = $discoveryInstance->getIterator();
428
429                 // Rewind back to the beginning
430                 $iteratorInstance->rewind();
431
432                 // ... and begin iteration
433                 while ($iteratorInstance->valid()) {
434                         // Get current entry
435                         $currentRecipient = $iteratorInstance->current();
436
437                         // Debug message
438                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Setting recipient to ' . $currentRecipient . ',previous=' . $packageData[self::PACKAGE_DATA_RECIPIENT]);
439
440                         // Set the recipient
441                         $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient;
442
443                         // And enqueue it to the writer class
444                         $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData);
445
446                         // Debug message
447                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Package declared for recipient ' . $currentRecipient);
448
449                         // Skip to next entry
450                         $iteratorInstance->next();
451                 } // END - while
452
453                 /*
454                  * The recipient list can be cleaned up here because the package which
455                  * shall be delivered has already been added for all entries from the
456                  * list.
457                  */
458                 $discoveryInstance->clearRecipients();
459         }
460
461         /**
462          * Delivers raw package data. In short, this will discover the raw socket
463          * resource through a discovery class (which will analyse the receipient of
464          * the package), register the socket with the connection (handler/helper?)
465          * instance and finally push the raw data on our outgoing queue.
466          *
467          * @param       $packageData    Raw package data in an array
468          * @return      void
469          */
470         private function deliverRawPackageData (array $packageData) {
471                 /*
472                  * This package may become big, depending on the shared object size or
473                  * delivered message size which shouldn't be so long (to save
474                  * bandwidth). Because of the nature of the used protocol (TCP) we need
475                  * to split it up into smaller pieces to fit it into a TCP frame.
476                  *
477                  * So first we need (again) a discovery class but now a protocol
478                  * discovery to choose the right socket resource. The discovery class
479                  * should take a look at the raw package data itself and then decide
480                  * which (configurable!) protocol should be used for that type of
481                  * package.
482                  */
483                 $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance();
484
485                 // Now discover the right protocol
486                 $socketResource = $discoveryInstance->discoverSocket($packageData, BaseConnectionHelper::CONNECTION_TYPE_OUTGOING);
487
488                 // Debug message
489                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after discoverSocket() has been called.');
490
491                 // We have to put this socket in our registry, so get an instance
492                 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
493
494                 // Get the listener from registry
495                 $helperInstance = Registry::getRegistry()->getInstance('connection');
496
497                 // Debug message
498                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: stateInstance=' . $helperInstance->getStateInstance());
499                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.');
500
501                 // Is it not there?
502                 if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) {
503                         // Debug message
504                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Registering socket ' . $socketResource . ' ...');
505
506                         // Then register it
507                         $registryInstance->registerSocket($helperInstance, $socketResource, $packageData);
508                 } elseif (!$helperInstance->getStateInstance()->isPeerStateConnected()) {
509                         // Is not connected, then we cannot send
510                         self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Unexpected peer state ' . $helperInstance->getStateInstance()->__toString() . ' detected.');
511
512                         // Shutdown the socket
513                         $this->shutdownSocket($socketResource);
514                 }
515
516                 // Debug message
517                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.');
518
519                 // Make sure the connection is up
520                 $helperInstance->getStateInstance()->validatePeerStateConnected();
521
522                 // Debug message
523                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.');
524
525                 // Enqueue it again on the out-going queue, the connection is up and working at this point
526                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData);
527
528                 // Debug message
529                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after pushNamed() has been called.');
530         }
531
532         /**
533          * Sends waiting packages
534          *
535          * @param       $packageData    Raw package data
536          * @return      void
537          */
538         private function sendOutgoingRawPackageData (array $packageData) {
539                 // Init sent bytes
540                 $sentBytes = 0;
541
542                 // Get the right connection instance
543                 $helperInstance = SocketRegistryFactory::createSocketRegistryInstance()->getHandlerInstanceFromPackageData($packageData);
544
545                 // Is this connection still alive?
546                 if ($helperInstance->isShuttedDown()) {
547                         // This connection is shutting down
548                         // @TODO We may want to do somthing more here?
549                         return;
550                 } // END - if
551
552                 // Sent out package data
553                 $sentBytes = $helperInstance->sendRawPackageData($packageData);
554
555                 // Remember unsent raw bytes in back-buffer, if any
556                 $this->storeUnsentBytesInBackBuffer($packageData, $sentBytes);
557         }
558
559         /**
560          * Generates a signature for given raw package content and sender id
561          *
562          * @param       $content        Raw package data
563          * @param       $senderId       Sender id to generate a signature for
564          * @return      $signature      Signature as BASE64-encoded string
565          */
566         private function generatePackageSignature ($content, $senderId) {
567                 // Hash content and sender id together, use md5() as last algo
568                 $hash = md5($this->getCryptoInstance()->hashString($senderId . $content, $this->getPrivateKey(), false));
569
570                 // Encrypt the content again with the hash as a key
571                 $encryptedContent = $this->getCryptoInstance()->encryptString($content, $hash);
572
573                 // Encode it with BASE64
574                 $signature = base64_encode($encryptedContent);
575
576                 // Return it
577                 return $signature;
578         }
579
580         /**
581          * Checks whether the signature of given package data is 'valid', here that
582          * means it is the same or not.
583          *
584          * @param       $decodedArray           An array with 'decoded' (explode() was mostly called) data
585          * @return      $isSignatureValid       Whether the signature is valid
586          * @todo        Unfinished area, signatures are currently NOT fully supported
587          */
588         private function isPackageSignatureValid (array $decodedArray) {
589                 // Generate the signature of comparing it
590                 $signature = $this->generatePackageSignature($decodedArray[self::INDEX_PACKAGE_CONTENT], $decodedArray[self::INDEX_PACKAGE_SENDER]);
591
592                 // Is it the same?
593                 //$isSignatureValid = 
594                 exit(__METHOD__.': signature='.$signature.chr(10).',decodedArray='.print_r($decodedArray,true));
595         }
596
597         /**
598          * "Enqueues" raw content into this delivery class by reading the raw content
599          * from given helper's template instance and pushing it on the 'undeclared'
600          * stack.
601          *
602          * @param       $helperInstance         An instance of a Helper class
603          * @param       $protocol                       Name of used protocol (TCP/UDP)
604          * @return      void
605          */
606         public function enqueueRawDataFromTemplate (Helper $helperInstance, $protocolName) {
607                 // Get the raw content ...
608                 $content = $helperInstance->getTemplateInstance()->getRawTemplateData();
609                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('content(' . strlen($content) . ')=' . $content);
610
611                 // ... and compress it
612                 $content = $this->getCompressorInstance()->compressStream($content);
613
614                 // Add magic in front of it and hash behind it, including BASE64 encoding
615                 $packageContent = sprintf(self::PACKAGE_MASK,
616                         // 1.) Compressor's extension
617                         $this->getCompressorInstance()->getCompressorExtension(),
618                         // - separator
619                         self::PACKAGE_MASK_SEPARATOR,
620                         // 2.) Raw package content, encoded with BASE64
621                         base64_encode($content),
622                         // - separator
623                         self::PACKAGE_MASK_SEPARATOR,
624                         // 3.) Tags
625                         implode(self::PACKAGE_TAGS_SEPARATOR, $helperInstance->getPackageTags()),
626                         // - separator
627                         self::PACKAGE_MASK_SEPARATOR,
628                         // 4.) Checksum
629                         $this->getHashFromContent($content)
630                 );
631
632                 // Now prepare the temporary array and push it on the 'undeclared' stack
633                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
634                         self::PACKAGE_DATA_SENDER    => $this->getSessionId(),
635                         self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(),
636                         self::PACKAGE_DATA_PROTOCOL  => $protocolName,
637                         self::PACKAGE_DATA_CONTENT   => $packageContent,
638                         self::PACKAGE_DATA_STATUS    => self::PACKAGE_STATUS_NEW,
639                         self::PACKAGE_DATA_SIGNATURE => $this->generatePackageSignature($packageContent, $this->getSessionId())
640                 ));
641         }
642
643         /**
644          * Checks whether a package has been enqueued for delivery.
645          *
646          * @return      $isEnqueued             Whether a package is enqueued
647          */
648         public function isPackageEnqueued () {
649                 // Check whether the stacker is not empty
650                 $isEnqueued = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED)));
651
652                 // Return the result
653                 return $isEnqueued;
654         }
655
656         /**
657          * Checks whether a package has been declared
658          *
659          * @return      $isDeclared             Whether a package is declared
660          */
661         public function isPackageDeclared () {
662                 // Check whether the stacker is not empty
663                 $isDeclared = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_DECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECLARED)));
664
665                 // Return the result
666                 return $isDeclared;
667         }
668
669         /**
670          * Checks whether a package should be sent out
671          *
672          * @return      $isWaitingDelivery      Whether a package is waiting for delivery
673          */
674         public function isPackageWaitingForDelivery () {
675                 // Check whether the stacker is not empty
676                 $isWaitingDelivery = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)));
677
678                 // Return the result
679                 return $isWaitingDelivery;
680         }
681
682         /**
683          * Delivers an enqueued package to the stated destination. If a non-session
684          * id is provided, recipient resolver is being asked (and instanced once).
685          * This allows that a single package is being delivered to multiple targets
686          * without enqueueing it for every target. If no target is provided or it
687          * can't be determined a NoTargetException is being thrown.
688          *
689          * @return      void
690          * @throws      NoTargetException       If no target can't be determined
691          */
692         public function declareEnqueuedPackage () {
693                 // Make sure this method isn't working if there is no package enqueued
694                 if (!$this->isPackageEnqueued()) {
695                         // This is not fatal but should be avoided
696                         // @TODO Add some logging here
697                         return;
698                 } // END - if
699
700                 /*
701                  * Now there are for sure packages to deliver, so start with the first
702                  * one.
703                  */
704                 $packageData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
705
706                 // Declare the raw package data for delivery
707                 $this->declareRawPackageData($packageData);
708         }
709
710         /**
711          * Delivers the next declared package. Only one package per time will be sent
712          * because this may take time and slows down the whole delivery
713          * infrastructure.
714          *
715          * @return      void
716          */
717         public function deliverDeclaredPackage () {
718                 // Sanity check if we have packages declared
719                 if (!$this->isPackageDeclared()) {
720                         // This is not fatal but should be avoided
721                         self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: No package has been declared, but ' . __METHOD__ . ' has been called!');
722                         return;
723                 } // END - if
724
725                 // Get the package
726                 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
727
728                 // Assert on it
729                 assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT]));
730
731                 // Try to deliver the package
732                 try {
733                         // And try to send it
734                         $this->deliverRawPackageData($packageData);
735
736                         // And remove it finally
737                         $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
738                 } catch (InvalidStateException $e) {
739                         // The state is not excepected (shall be 'connected')
740                         self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Caught ' . $e->__toString() . ',message=' . $e->getMessage());
741
742                         // Mark the package with status failed
743                         $this->changePackageStatus($packageData, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED);
744                 }
745         }
746
747         /**
748          * Sends waiting packages out for delivery
749          *
750          * @return      void
751          */
752         public function sendWaitingPackage () {
753                 // Send any waiting bytes in the back-buffer before sending a new package
754                 $this->sendBackBufferBytes();
755
756                 // Sanity check if we have packages waiting for delivery
757                 if (!$this->isPackageWaitingForDelivery()) {
758                         // This is not fatal but should be avoided
759                         self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: No package is waiting for delivery, but ' . __METHOD__ . ' was called.');
760                         return;
761                 } // END - if
762
763                 // Get the package
764                 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_OUTGOING);
765
766                 try {
767                         // Now try to send it
768                         $this->sendOutgoingRawPackageData($packageData);
769
770                         // And remove it finally
771                         $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
772                 } catch (InvalidSocketException $e) {
773                         // Output exception message
774                         self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Package was not delivered: ' . $e->getMessage());
775
776                         // Mark package as failed
777                         $this->changePackageStatus($packageData, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED);
778                 }
779         }
780
781         ///////////////////////////////////////////////////////////////////////////
782         //                   Receiving packages / raw data
783         ///////////////////////////////////////////////////////////////////////////
784
785         /**
786          * Checks whether decoded raw data is pending
787          *
788          * @return      $isPending      Whether decoded raw data is pending
789          */
790         private function isRawDataPending () {
791                 // Just return whether the stack is not empty
792                 $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_INCOMING));
793
794                 // Return the status
795                 return $isPending;
796         }
797
798         /**
799          * Checks whether new raw package data has arrived at a socket
800          *
801          * @param       $poolInstance   An instance of a PoolableListener class
802          * @return      $hasArrived             Whether new raw package data has arrived for processing
803          */
804         public function isNewRawDataPending (PoolableListener $poolInstance) {
805                 // Visit the pool. This monitors the pool for incoming raw data.
806                 $poolInstance->accept($this->getVisitorInstance());
807
808                 // Check for new data arrival
809                 $hasArrived = $this->isRawDataPending();
810
811                 // Return the status
812                 return $hasArrived;
813         }
814
815         /**
816          * Handles the incoming decoded raw data. This method does not "convert" the
817          * decoded data back into a package array, it just "handles" it and pushs it
818          * on the next stack.
819          *
820          * @return      void
821          */
822         public function handleIncomingDecodedData () {
823                 /*
824                  * This method should only be called if decoded raw data is pending,
825                  * so check it again.
826                  */
827                 if (!$this->isRawDataPending()) {
828                         // This is not fatal but should be avoided
829                         // @TODO Add some logging here
830                         return;
831                 } // END - if
832
833                 // Very noisy debug message:
834                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Stacker size is ' . $this->getStackerInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.');
835
836                 // "Pop" the next entry (the same array again) from the stack
837                 $decodedData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING);
838
839                 // Make sure both array elements are there
840                 assert(
841                         (is_array($decodedData)) &&
842                         (isset($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA])) &&
843                         (isset($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE]))
844                 );
845
846                 /*
847                  * Also make sure the error code is SOCKET_ERROR_UNHANDLED because we
848                  * only want to handle unhandled packages here.
849                  */
850                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: errorCode=' . $decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] . '(' . BaseRawDataHandler::SOCKET_ERROR_UNHANDLED . ')');
851                 assert($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] == BaseRawDataHandler::SOCKET_ERROR_UNHANDLED);
852
853                 // Remove the last chunk SEPARATOR (because it is being added and we don't need it)
854                 if (substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
855                         // It is there and should be removed
856                         $decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA] = substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], 0, -1);
857                 } // END - if
858
859                 // This package is "handled" and can be pushed on the next stack
860                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $decodedData);
861         }
862
863         /**
864          * Adds raw decoded data from the given handler instance to this receiver
865          *
866          * @param       $handlerInstance        An instance of a Networkable class
867          * @return      void
868          */
869         public function addRawDataToIncomingStack (Networkable $handlerInstance) {
870                 /*
871                  * Get the decoded data from the handler, this is an array with
872                  * 'raw_data' and 'error_code' as elements.
873                  */
874                 $decodedData = $handlerInstance->getNextRawData();
875
876                 // Very noisy debug message:
877                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, true));
878
879                 // And push it on our stack
880                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $decodedData);
881         }
882
883         /**
884          * Checks whether incoming decoded data is handled.
885          *
886          * @return      $isHandled      Whether incoming decoded data is handled
887          */
888         public function isIncomingRawDataHandled () {
889                 // Determine if the stack is not empty
890                 $isHandled = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_HANDLED));
891
892                 // Return it
893                 return $isHandled;
894         }
895
896         /**
897          * Checks whether the assembler has pending data left
898          *
899          * @return      $isHandled      Whether the assembler has pending data left
900          */
901         public function ifAssemblerHasPendingDataLeft () {
902                 // Determine if the stack is not empty
903                 $isHandled = (!$this->getAssemblerInstance()->isPendingDataEmpty());
904
905                 // Return it
906                 return $isHandled;
907         }
908
909         /**
910          * Handles the attached assemler's pending data queue to be finally
911          * assembled to the raw package data back.
912          *
913          * @return      void
914          */
915         public function handleAssemblerPendingData () {
916                 // Handle it
917                 $this->getAssemblerInstance()->handlePendingData();
918         }
919
920         /**
921          * Assembles incoming decoded data so it will become an abstract network
922          * package again. The assembler does later do it's job by an other task,
923          * not this one to keep best speed possible.
924          *
925          * @return      void
926          */
927         public function assembleDecodedDataToPackage () {
928                 // Make sure the raw decoded package data is handled
929                 assert($this->isIncomingRawDataHandled());
930
931                 // Get current package content (an array with two elements; see handleIncomingDecodedData() for details)
932                 $packageContent = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED);
933
934                 // Start assembling the raw package data array by chunking it
935                 $this->getAssemblerInstance()->chunkPackageContent($packageContent);
936
937                 // Remove the package from 'handled_decoded' stack ...
938                 $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_HANDLED);
939
940                 // ... and push it on the 'chunked' stacker
941                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageContent);
942         }
943
944         /**
945          * Accepts the visitor to process the visit "request"
946          *
947          * @param       $visitorInstance        An instance of a Visitor class
948          * @return      void
949          */
950         public function accept (Visitor $visitorInstance) {
951                 // Debug message
952                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START');
953
954                 // Visit the package
955                 $visitorInstance->visitNetworkPackage($this);
956
957                 // Debug message
958                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
959         }
960
961         /**
962          * Clears all stacker
963          *
964          * @return      void
965          */
966         public function clearAllStacker () {
967                 // Call the init method to force re-initialization
968                 $this->initStackers(true);
969
970                 // Debug message
971                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: All stacker have been re-initialized.');
972         }
973
974         /**
975          * Removes the first failed outoging package from the stack to continue
976          * with next one (it will never work until the issue is fixed by you).
977          *
978          * @return      void
979          * @throws      UnexpectedPackageStatusException        If the package status is not 'failed'
980          * @todo        This may be enchanced for outgoing packages?
981          */
982         public function removeFirstFailedPackage () {
983                 // Get the package again
984                 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
985
986                 // Is the package status 'failed'?
987                 if ($packageData[self::PACKAGE_DATA_STATUS] != self::PACKAGE_STATUS_FAILED) {
988                         // Not failed!
989                         throw new UnexpectedPackageStatusException(array($this, $packageData, self::PACKAGE_STATUS_FAILED), BaseListener::EXCEPTION_UNEXPECTED_PACKAGE_STATUS);
990                 } // END - if
991
992                 // Remove this entry
993                 $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
994         }
995
996         /**
997          * "Decode" the package content into the same array when it was sent.
998          *
999          * @param       $rawPackageContent      The raw package content to be "decoded"
1000          * @return      $decodedData            An array with 'sender', 'recipient', 'content' and 'status' elements
1001          */
1002         public function decodeRawContent ($rawPackageContent) {
1003                 // Use the separator '#' to "decode" it
1004                 $decodedArray = explode(self::PACKAGE_DATA_SEPARATOR, $rawPackageContent);
1005
1006                 // Assert on count (should be always 3)
1007                 assert(count($decodedArray) == self::DECODED_DATA_ARRAY_SIZE);
1008
1009                 // Generate the signature of comparing it
1010                 /*
1011                  * @todo Unsupported feature of "signed" messages commented out
1012                 if (!$this->isPackageSignatureValid($decodedArray)) {
1013                         // Is not valid, so throw an exception here
1014                         exit(__METHOD__ . ':INVALID SIG! UNDER CONSTRUCTION!' . chr(10));
1015                 } // END - if
1016                 */
1017
1018                 /*
1019                  * Create 'decodedData' array with all assoziative array elements,
1020                  * except signature.
1021                  */
1022                 $decodedData = array(
1023                         self::PACKAGE_DATA_SENDER    => $decodedArray[self::INDEX_PACKAGE_SENDER],
1024                         self::PACKAGE_DATA_RECIPIENT => $decodedArray[self::INDEX_PACKAGE_RECIPIENT],
1025                         self::PACKAGE_DATA_CONTENT   => $decodedArray[self::INDEX_PACKAGE_CONTENT],
1026                         self::PACKAGE_DATA_STATUS    => self::PACKAGE_STATUS_DECODED
1027                 );
1028
1029                 // And return it
1030                 return $decodedData;
1031         }
1032
1033         /**
1034          * Handles decoded data for this node by "decoding" the 'content' part of
1035          * it. Again this method uses explode() for the "decoding" process.
1036          *
1037          * @param       $decodedData    An array with decoded raw package data
1038          * @return      void
1039          * @throws      InvalidDataChecksumException    If the checksum doesn't match
1040          */
1041         public function handleRawData (array $decodedData) {
1042                 /*
1043                  * "Decode" the package's content by a simple explode() call, for
1044                  * details of the array elements, see comments for constant
1045                  * PACKAGE_MASK.
1046                  */
1047                 $decodedContent = explode(self::PACKAGE_MASK_SEPARATOR, $decodedData[self::PACKAGE_DATA_CONTENT]);
1048
1049                 // Assert on array count for a very basic validation
1050                 assert(count($decodedContent) == self::PACKAGE_CONTENT_ARRAY_SIZE);
1051
1052                 /*
1053                  * Convert the indexed array into an associative array. This is much
1054                  * better to remember than plain numbers, isn't it?
1055                  */
1056                 $decodedContent = array(
1057                         // Compressor's extension used to compress the data
1058                         self::PACKAGE_CONTENT_EXTENSION => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION],
1059                         // Package data (aka "message") in BASE64-decoded form but still compressed
1060                         self::PACKAGE_CONTENT_MESSAGE   => base64_decode($decodedContent[self::INDEX_PACKAGE_DATA]),
1061                         // Tags as an indexed array for "tagging" the message
1062                         self::PACKAGE_CONTENT_TAGS      => explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]),
1063                         // Checksum of the _decoded_ data
1064                         self::PACKAGE_CONTENT_CHECKSUM  => $decodedContent[self::INDEX_CHECKSUM]
1065                 );
1066
1067                 // Is the checksum valid?
1068                 if (!$this->isChecksumValid($decodedContent, $decodedData)) {
1069                         // Is not the same, so throw an exception here
1070                         throw new InvalidDataChecksumException(array($this, $decodedContent, $decodedData), BaseListener::EXCEPTION_INVALID_DATA_CHECKSUM);
1071                 } // END - if
1072
1073                 /*
1074                  * The checksum is the same, then it can be decompressed safely. The
1075                  * original message is at this point fully decoded.
1076                  */
1077                 $decodedContent[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedContent[self::PACKAGE_CONTENT_MESSAGE]);
1078
1079                 // And push it on the next stack
1080                 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedContent);
1081         }
1082
1083         /**
1084          * Checks whether a new message has arrived
1085          *
1086          * @return      $hasArrived             Whether a new message has arrived for processing
1087          */
1088         public function isNewMessageArrived () {
1089                 // Determine if the stack is not empty
1090                 $hasArrived = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_NEW_MESSAGE));
1091
1092                 // Return it
1093                 return $hasArrived;
1094         }
1095
1096         /**
1097          * Handles newly arrived messages
1098          *
1099          * @return      void
1100          * @todo        Implement verification of all sent tags here?
1101          */
1102         public function handleNewlyArrivedMessage () {
1103                 // Get it from the stacker, it is the full array with the decoded message
1104                 $decodedContent = $this->getStackerInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
1105
1106                 // Now get a filter chain back from factory with given tags array
1107                 $chainInstance = PackageFilterChainFactory::createChainByTagsArray($decodedContent[self::PACKAGE_CONTENT_TAGS]);
1108
1109                 /*
1110                  * Process the message through all filters, note that all other
1111                  * elements from $decodedContent are no longer needed.
1112                  */
1113                 $chainInstance->processMessage($decodedContent[self::PACKAGE_CONTENT_MESSAGE], $this);
1114         }
1115
1116         /**
1117          * Checks whether a processed message is pending for "interpretation"
1118          *
1119          * @return      $isPending      Whether a processed message is pending
1120          */
1121         public function isProcessedMessagePending () {
1122                 // Check it
1123                 $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE));
1124
1125                 // Return it
1126                 return $isPending;
1127         }
1128
1129         /**
1130          * Handle processed messages by "interpreting" the 'message_type' element
1131          *
1132          * @return      void
1133          */
1134         public function handleProcessedMessage () {
1135                 // Get it from the stacker, it is the full array with the processed message
1136                 $messageArray = $this->getStackerInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE);
1137
1138                 // Add type for later easier handling
1139                 $messageArray[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_TYPE] = $messageArray[self::MESSAGE_ARRAY_TYPE];
1140
1141                 // Debug message
1142                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: messageArray=' . print_r($messageArray, true));
1143
1144                 // Create a handler instance from given message type
1145                 $handlerInstance = MessageTypeHandlerFactory::createMessageTypeHandlerInstance($messageArray[self::MESSAGE_ARRAY_TYPE]);
1146
1147                 // Handle message data
1148                 $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this);
1149         }
1150 }
1151
1152 // [EOF]
1153 ?>