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