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
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
15 * For performance reasons, this class should only be instanciated once and then
16 * used as a "pipe-through" class.
18 * @author Roland Haeder <webmaster@shipsimu.org>
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.shipsimu.org
23 * @todo Needs to add functionality for handling the object's type
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.
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.
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/>.
38 class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, Registerable, Visitable {
40 * Package mask for compressing package data:
41 * 0: Compressor extension
43 * 2: Tags, seperated by semicolons, no semicolon is required if only one tag is needed
47 const PACKAGE_MASK = '%s%s%s%s%s%s%s';
50 * Separator for the above mask
52 const PACKAGE_MASK_SEPARATOR = '^';
55 * Size of an array created by invoking
56 * explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $content).
58 const PACKAGE_CONTENT_ARRAY_SIZE = 4;
61 * Separator for checksum
63 const PACKAGE_CHECKSUM_SEPARATOR = '_';
66 * Array indexes for above mask, start with zero
68 const INDEX_COMPRESSOR_EXTENSION = 0;
69 const INDEX_PACKAGE_DATA = 1;
71 const INDEX_CHECKSUM = 3;
74 * Array indexes for raw package array
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;
83 * Size of the decoded data array ('status' is not included)
85 const DECODED_DATA_ARRAY_SIZE = 4;
88 * Named array elements for decoded package content
90 const PACKAGE_CONTENT_EXTENSION = 'compressor';
91 const PACKAGE_CONTENT_MESSAGE = 'message';
92 const PACKAGE_CONTENT_TAGS = 'tags';
93 const PACKAGE_CONTENT_CHECKSUM = 'checksum';
96 * Named array elements for package data
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';
108 const PACKAGE_STATUS_NEW = 'new';
109 const PACKAGE_STATUS_FAILED = 'failed';
110 const PACKAGE_STATUS_DECODED = 'decoded';
111 const PACKAGE_STATUS_FAKED = 'faked';
114 * Constants for message data array
116 const MESSAGE_ARRAY_DATA = 'message_data';
117 const MESSAGE_ARRAY_TYPE = 'message_type';
120 * Generic answer status field
126 const PACKAGE_TAGS_SEPARATOR = ';';
129 * Raw package data separator
131 const PACKAGE_DATA_SEPARATOR = '#';
134 * Separator for more than one recipient
136 const PACKAGE_RECIPIENT_SEPARATOR = ':';
139 * Network target (alias): 'upper nodes'
141 const NETWORK_TARGET_UPPER = 'upper';
144 * Network target (alias): 'self'
146 const NETWORK_TARGET_SELF = 'self';
149 * Network target (alias): 'dht'
151 const NETWORK_TARGET_DHT = 'dht';
154 * TCP package size in bytes
156 const TCP_PACKAGE_SIZE = 512;
158 /**************************************************************************
159 * Stacker for out-going packages *
160 **************************************************************************/
163 * Stacker name for "undeclared" packages
165 const STACKER_NAME_UNDECLARED = 'package_undeclared';
168 * Stacker name for "declared" packages (which are ready to send out)
170 const STACKER_NAME_DECLARED = 'package_declared';
173 * Stacker name for "out-going" packages
175 const STACKER_NAME_OUTGOING = 'package_outgoing';
177 /**************************************************************************
178 * Stacker for incoming packages *
179 **************************************************************************/
182 * Stacker name for "incoming" decoded raw data
184 const STACKER_NAME_DECODED_INCOMING = 'package_decoded_data';
187 * Stacker name for handled decoded raw data
189 const STACKER_NAME_DECODED_HANDLED = 'package_handled_decoded';
192 * Stacker name for "chunked" decoded raw data
194 const STACKER_NAME_DECODED_CHUNKED = 'package_chunked_decoded';
196 /**************************************************************************
197 * Stacker for incoming messages *
198 **************************************************************************/
201 * Stacker name for new messages
203 const STACKER_NAME_NEW_MESSAGE = 'package_new_message';
206 * Stacker name for processed messages
208 const STACKER_NAME_PROCESSED_MESSAGE = 'package_processed_message';
210 /**************************************************************************
211 * Stacker for other/internal purposes *
212 **************************************************************************/
215 * Stacker name for "back-buffered" packages
217 const STACKER_NAME_BACK_BUFFER = 'package_backbuffer';
219 /**************************************************************************
221 **************************************************************************/
222 const PROTOCOL_TCP = 'TCP';
223 const PROTOCOL_UDP = 'UDP';
226 * Protected constructor
230 protected function __construct () {
231 // Call parent constructor
232 parent::__construct(__CLASS__);
236 * Creates an instance of this class
238 * @param $compressorInstance A Compressor instance for compressing the content
239 * @return $packageInstance An instance of a Deliverable class
241 public static final function createNetworkPackage (Compressor $compressorInstance) {
243 $packageInstance = new NetworkPackage();
245 // Now set the compressor instance
246 $packageInstance->setCompressorInstance($compressorInstance);
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.
253 $stackerInstance = ObjectFactory::createObjectByConfiguredName('network_package_stacker_class');
255 // At last, set it in this class
256 $packageInstance->setStackerInstance($stackerInstance);
259 $packageInstance->initStacks();
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);
265 // Get crypto instance and set it, too
266 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
267 $packageInstance->setCryptoInstance($cryptoInstance);
269 // Get a singleton package assembler instance from factory and set it here, too
270 $assemblerInstance = PackageAssemblerFactory::createAssemblerInstance($packageInstance);
271 $packageInstance->setAssemblerInstance($assemblerInstance);
273 // Return the prepared instance
274 return $packageInstance;
278 * Initialize all stackers
280 * @param $forceReInit Whether to force reinitialization of all stacks
283 protected function initStacks ($forceReInit = FALSE) {
285 $this->getStackerInstance()->initStacks(array(
286 self::STACKER_NAME_UNDECLARED,
287 self::STACKER_NAME_DECLARED,
288 self::STACKER_NAME_OUTGOING,
289 self::STACKER_NAME_DECODED_INCOMING,
290 self::STACKER_NAME_DECODED_HANDLED,
291 self::STACKER_NAME_DECODED_CHUNKED,
292 self::STACKER_NAME_NEW_MESSAGE,
293 self::STACKER_NAME_PROCESSED_MESSAGE,
294 self::STACKER_NAME_BACK_BUFFER
299 * "Getter" for hash from given content
301 * @param $content Raw package content
302 * @return $hash Hash for given package content
304 private function getHashFromContent ($content) {
306 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension());
309 // @TODO md5() is very weak, but it needs to be fast
312 self::PACKAGE_CHECKSUM_SEPARATOR .
313 $this->getSessionId() .
314 self::PACKAGE_CHECKSUM_SEPARATOR .
315 $this->getCompressorInstance()->getCompressorExtension()
319 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension());
326 * Checks whether the checksum (sometimes called "hash") is the same
328 * @param $decodedContent Package raw content
329 * @param $decodedData Whole raw package data array
330 * @return $isChecksumValid Whether the checksum is the same
332 private function isChecksumValid (array $decodedContent, array $decodedData) {
334 $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER]);
337 $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]);
340 return $isChecksumValid;
344 * Change the package with given status in given stack
346 * @param $packageData Raw package data in an array
347 * @param $stackerName Name of the stacker
348 * @param $newStatus New status to set
351 private function changePackageStatus (array $packageData, $stackerName, $newStatus) {
352 // Skip this for empty stacks
353 if ($this->getStackerInstance()->isStackEmpty($stackerName)) {
354 // This avoids an exception after all packages has failed
358 // Pop the entry (it should be it)
359 $nextData = $this->getStackerInstance()->popNamed($stackerName);
361 // Compare both signatures
362 assert($nextData[self::PACKAGE_DATA_SIGNATURE] == $packageData[self::PACKAGE_DATA_SIGNATURE]);
364 // Temporary set the new status
365 $packageData[self::PACKAGE_DATA_STATUS] = $newStatus;
368 $this->getStackerInstance()->pushNamed($stackerName, $packageData);
372 * "Getter" for hash from given content and sender's session id
374 * @param $decodedContent Raw package content
375 * @param $sessionId Session id of the sender
376 * @return $hash Hash for given package content
378 public function getHashFromContentSessionId (array $decodedContent, $sessionId) {
380 //* 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]);
383 // @TODO md5() is very weak, but it needs to be fast
385 $decodedContent[self::PACKAGE_CONTENT_MESSAGE] .
386 self::PACKAGE_CHECKSUM_SEPARATOR .
388 self::PACKAGE_CHECKSUM_SEPARATOR .
389 $decodedContent[self::PACKAGE_CONTENT_EXTENSION]
396 ///////////////////////////////////////////////////////////////////////////
397 // Delivering packages / raw data
398 ///////////////////////////////////////////////////////////////////////////
401 * Delivers the given raw package data.
403 * @param $packageData Raw package data in an array
406 private function declareRawPackageData (array $packageData) {
407 // Make sure the required field is there
408 assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT]));
411 * We need to disover every recipient, just in case we have a
412 * multi-recipient entry like 'upper' is. 'all' may be a not so good
413 * target because it causes an overload on the network and may be
414 * abused for attacking the network with large packages.
416 $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
418 // Discover all recipients, this may throw an exception
419 $discoveryInstance->discoverRecipients($packageData);
421 // Now get an iterator
422 $iteratorInstance = $discoveryInstance->getIterator();
424 // Rewind back to the beginning
425 $iteratorInstance->rewind();
427 // ... and begin iteration
428 while ($iteratorInstance->valid()) {
430 $currentRecipient = $iteratorInstance->current();
433 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Setting recipient to ' . $currentRecipient . ',previous=' . $packageData[self::PACKAGE_DATA_RECIPIENT]);
436 $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient;
438 // And enqueue it to the writer class
439 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData);
442 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Package declared for recipient ' . $currentRecipient);
444 // Skip to next entry
445 $iteratorInstance->next();
449 * The recipient list can be cleaned up here because the package which
450 * shall be delivered has already been added for all entries from the
453 $discoveryInstance->clearRecipients();
457 * Delivers raw package data. In short, this will discover the raw socket
458 * resource through a discovery class (which will analyse the receipient of
459 * the package), register the socket with the connection (handler/helper?)
460 * instance and finally push the raw data on our outgoing queue.
462 * @param $packageData Raw package data in an array
465 private function deliverRawPackageData (array $packageData) {
467 * This package may become big, depending on the shared object size or
468 * delivered message size which shouldn't be so long (to save
469 * bandwidth). Because of the nature of the used protocol (TCP) we need
470 * to split it up into smaller pieces to fit it into a TCP frame.
472 * So first we need (again) a discovery class but now a protocol
473 * discovery to choose the right socket resource. The discovery class
474 * should take a look at the raw package data itself and then decide
475 * which (configurable!) protocol should be used for that type of
478 $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance();
480 // Now discover the right protocol
481 $socketResource = $discoveryInstance->discoverSocket($packageData, BaseConnectionHelper::CONNECTION_TYPE_OUTGOING);
484 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after discoverSocket() has been called.');
486 // We have to put this socket in our registry, so get an instance
487 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
489 // Get the listener from registry
490 $helperInstance = Registry::getRegistry()->getInstance('connection');
493 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: stateInstance=' . $helperInstance->getStateInstance());
494 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.');
497 if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) {
499 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Registering socket ' . $socketResource . ' ...');
502 $registryInstance->registerSocket($helperInstance, $socketResource, $packageData);
503 } elseif (!$helperInstance->getStateInstance()->isPeerStateConnected()) {
504 // Is not connected, then we cannot send
505 self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Unexpected peer state ' . $helperInstance->getStateInstance()->__toString() . ' detected.');
507 // Shutdown the socket
508 $this->shutdownSocket($socketResource);
512 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.');
514 // Make sure the connection is up
515 $helperInstance->getStateInstance()->validatePeerStateConnected();
518 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.');
520 // Enqueue it again on the out-going queue, the connection is up and working at this point
521 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData);
524 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Reached line ' . __LINE__ . ' after pushNamed() has been called.');
528 * Sends waiting packages
530 * @param $packageData Raw package data
533 private function sendOutgoingRawPackageData (array $packageData) {
537 // Get the right connection instance
538 $helperInstance = SocketRegistryFactory::createSocketRegistryInstance()->getHandlerInstanceFromPackageData($packageData);
540 // Is this connection still alive?
541 if ($helperInstance->isShuttedDown()) {
542 // This connection is shutting down
543 // @TODO We may want to do somthing more here?
547 // Sent out package data
548 $sentBytes = $helperInstance->sendRawPackageData($packageData);
550 // Remember unsent raw bytes in back-buffer, if any
551 $this->storeUnsentBytesInBackBuffer($packageData, $sentBytes);
555 * Generates a signature for given raw package content and sender id
557 * @param $content Raw package data
558 * @param $senderId Sender id to generate a signature for
559 * @return $signature Signature as BASE64-encoded string
561 private function generatePackageSignature ($content, $senderId) {
562 // Hash content and sender id together, use md5() as last algo
563 $hash = md5($this->getCryptoInstance()->hashString($senderId . $content, $this->getPrivateKey(), FALSE));
565 // Encrypt the content again with the hash as a key
566 $encryptedContent = $this->getCryptoInstance()->encryptString($content, $hash);
568 // Encode it with BASE64
569 $signature = base64_encode($encryptedContent);
576 * Checks whether the signature of given package data is 'valid', here that
577 * means it is the same or not.
579 * @param $decodedArray An array with 'decoded' (explode() was mostly called) data
580 * @return $isSignatureValid Whether the signature is valid
581 * @todo Unfinished area, signatures are currently NOT fully supported
583 private function isPackageSignatureValid (array $decodedArray) {
584 // Generate the signature of comparing it
585 $signature = $this->generatePackageSignature($decodedArray[self::INDEX_PACKAGE_CONTENT], $decodedArray[self::INDEX_PACKAGE_SENDER]);
588 //$isSignatureValid =
589 exit(__METHOD__.': signature='.$signature.chr(10).',decodedArray='.print_r($decodedArray, TRUE));
593 * "Enqueues" raw content into this delivery class by reading the raw content
594 * from given helper's template instance and pushing it on the 'undeclared'
597 * @param $helperInstance An instance of a Helper class
598 * @param $protocol Name of used protocol (TCP/UDP)
601 public function enqueueRawDataFromTemplate (Helper $helperInstance, $protocolName) {
602 // Get the raw content ...
603 $content = $helperInstance->getTemplateInstance()->getRawTemplateData();
604 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('content(' . strlen($content) . ')=' . $content);
606 // ... and compress it
607 $content = $this->getCompressorInstance()->compressStream($content);
609 // Add magic in front of it and hash behind it, including BASE64 encoding
610 $packageContent = sprintf(self::PACKAGE_MASK,
611 // 1.) Compressor's extension
612 $this->getCompressorInstance()->getCompressorExtension(),
614 self::PACKAGE_MASK_SEPARATOR,
615 // 2.) Raw package content, encoded with BASE64
616 base64_encode($content),
618 self::PACKAGE_MASK_SEPARATOR,
620 implode(self::PACKAGE_TAGS_SEPARATOR, $helperInstance->getPackageTags()),
622 self::PACKAGE_MASK_SEPARATOR,
624 $this->getHashFromContent($content)
627 // Now prepare the temporary array and push it on the 'undeclared' stack
628 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
629 self::PACKAGE_DATA_SENDER => $this->getSessionId(),
630 self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(),
631 self::PACKAGE_DATA_PROTOCOL => $protocolName,
632 self::PACKAGE_DATA_CONTENT => $packageContent,
633 self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_NEW,
634 self::PACKAGE_DATA_SIGNATURE => $this->generatePackageSignature($packageContent, $this->getSessionId())
639 * Checks whether a package has been enqueued for delivery.
641 * @return $isEnqueued Whether a package is enqueued
643 public function isPackageEnqueued () {
644 // Check whether the stacker is not empty
645 $isEnqueued = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED)));
652 * Checks whether a package has been declared
654 * @return $isDeclared Whether a package is declared
656 public function isPackageDeclared () {
657 // Check whether the stacker is not empty
658 $isDeclared = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_DECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECLARED)));
665 * Checks whether a package should be sent out
667 * @return $isWaitingDelivery Whether a package is waiting for delivery
669 public function isPackageWaitingForDelivery () {
670 // Check whether the stacker is not empty
671 $isWaitingDelivery = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)));
674 return $isWaitingDelivery;
678 * Delivers an enqueued package to the stated destination. If a non-session
679 * id is provided, recipient resolver is being asked (and instanced once).
680 * This allows that a single package is being delivered to multiple targets
681 * without enqueueing it for every target. If no target is provided or it
682 * can't be determined a NoTargetException is being thrown.
685 * @throws NoTargetException If no target can't be determined
687 public function declareEnqueuedPackage () {
688 // Make sure this method isn't working if there is no package enqueued
689 if (!$this->isPackageEnqueued()) {
690 // This is not fatal but should be avoided
691 // @TODO Add some logging here
696 * Now there are for sure packages to deliver, so start with the first
699 $packageData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
701 // Declare the raw package data for delivery
702 $this->declareRawPackageData($packageData);
706 * Delivers the next declared package. Only one package per time will be sent
707 * because this may take time and slows down the whole delivery
712 public function deliverDeclaredPackage () {
713 // Sanity check if we have packages declared
714 if (!$this->isPackageDeclared()) {
715 // This is not fatal but should be avoided
716 self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: No package has been declared, but ' . __METHOD__ . ' has been called!');
721 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
724 assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT]));
726 // Try to deliver the package
728 // And try to send it
729 $this->deliverRawPackageData($packageData);
731 // And remove it finally
732 $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
733 } catch (InvalidStateException $e) {
734 // The state is not excepected (shall be 'connected')
735 self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Caught ' . $e->__toString() . ',message=' . $e->getMessage());
737 // Mark the package with status failed
738 $this->changePackageStatus($packageData, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED);
743 * Sends waiting packages out for delivery
747 public function sendWaitingPackage () {
748 // Send any waiting bytes in the back-buffer before sending a new package
749 $this->sendBackBufferBytes();
751 // Sanity check if we have packages waiting for delivery
752 if (!$this->isPackageWaitingForDelivery()) {
753 // This is not fatal but should be avoided
754 self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: No package is waiting for delivery, but ' . __METHOD__ . ' was called.');
759 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_OUTGOING);
762 // Now try to send it
763 $this->sendOutgoingRawPackageData($packageData);
765 // And remove it finally
766 $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
767 } catch (InvalidSocketException $e) {
768 // Output exception message
769 self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Package was not delivered: ' . $e->getMessage());
771 // Mark package as failed
772 $this->changePackageStatus($packageData, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED);
776 ///////////////////////////////////////////////////////////////////////////
777 // Receiving packages / raw data
778 ///////////////////////////////////////////////////////////////////////////
781 * Checks whether decoded raw data is pending
783 * @return $isPending Whether decoded raw data is pending
785 private function isRawDataPending () {
786 // Just return whether the stack is not empty
787 $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_INCOMING));
794 * Checks whether new raw package data has arrived at a socket
796 * @param $poolInstance An instance of a PoolableListener class
797 * @return $hasArrived Whether new raw package data has arrived for processing
799 public function isNewRawDataPending (PoolableListener $poolInstance) {
800 // Visit the pool. This monitors the pool for incoming raw data.
801 $poolInstance->accept($this->getVisitorInstance());
803 // Check for new data arrival
804 $hasArrived = $this->isRawDataPending();
811 * Handles the incoming decoded raw data. This method does not "convert" the
812 * decoded data back into a package array, it just "handles" it and pushs it
817 public function handleIncomingDecodedData () {
819 * This method should only be called if decoded raw data is pending,
822 if (!$this->isRawDataPending()) {
823 // This is not fatal but should be avoided
824 // @TODO Add some logging here
828 // Very noisy debug message:
829 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: Stacker size is ' . $this->getStackerInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.');
831 // "Pop" the next entry (the same array again) from the stack
832 $decodedData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING);
834 // Make sure both array elements are there
836 (is_array($decodedData)) &&
837 (isset($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA])) &&
838 (isset($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE]))
842 * Also make sure the error code is SOCKET_ERROR_UNHANDLED because we
843 * only want to handle unhandled packages here.
845 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: errorCode=' . $decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] . '(' . BaseRawDataHandler::SOCKET_ERROR_UNHANDLED . ')');
846 assert($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] == BaseRawDataHandler::SOCKET_ERROR_UNHANDLED);
848 // Remove the last chunk SEPARATOR (because it is being added and we don't need it)
849 if (substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
850 // It is there and should be removed
851 $decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA] = substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], 0, -1);
854 // This package is "handled" and can be pushed on the next stack
855 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $decodedData);
859 * Adds raw decoded data from the given handler instance to this receiver
861 * @param $handlerInstance An instance of a Networkable class
864 public function addRawDataToIncomingStack (Networkable $handlerInstance) {
866 * Get the decoded data from the handler, this is an array with
867 * 'raw_data' and 'error_code' as elements.
869 $decodedData = $handlerInstance->getNextRawData();
871 // Very noisy debug message:
872 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, TRUE));
874 // And push it on our stack
875 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $decodedData);
879 * Checks whether incoming decoded data is handled.
881 * @return $isHandled Whether incoming decoded data is handled
883 public function isIncomingRawDataHandled () {
884 // Determine if the stack is not empty
885 $isHandled = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_HANDLED));
892 * Checks whether the assembler has pending data left
894 * @return $isHandled Whether the assembler has pending data left
896 public function ifAssemblerHasPendingDataLeft () {
897 // Determine if the stack is not empty
898 $isHandled = (!$this->getAssemblerInstance()->isPendingDataEmpty());
905 * Handles the attached assemler's pending data queue to be finally
906 * assembled to the raw package data back.
910 public function handleAssemblerPendingData () {
912 $this->getAssemblerInstance()->handlePendingData();
916 * Assembles incoming decoded data so it will become an abstract network
917 * package again. The assembler does later do it's job by an other task,
918 * not this one to keep best speed possible.
922 public function assembleDecodedDataToPackage () {
923 // Make sure the raw decoded package data is handled
924 assert($this->isIncomingRawDataHandled());
926 // Get current package content (an array with two elements; see handleIncomingDecodedData() for details)
927 $packageContent = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED);
929 // Start assembling the raw package data array by chunking it
930 $this->getAssemblerInstance()->chunkPackageContent($packageContent);
932 // Remove the package from 'handled_decoded' stack ...
933 $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_HANDLED);
935 // ... and push it on the 'chunked' stacker
936 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageContent);
940 * Accepts the visitor to process the visit "request"
942 * @param $visitorInstance An instance of a Visitor class
945 public function accept (Visitor $visitorInstance) {
947 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START');
950 $visitorInstance->visitNetworkPackage($this);
953 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
961 public function clearAllStacks () {
962 // Call the init method to force re-initialization
963 $this->initStacks(TRUE);
966 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: All stacker have been re-initialized.');
970 * Removes the first failed outoging package from the stack to continue
971 * with next one (it will never work until the issue is fixed by you).
974 * @throws UnexpectedPackageStatusException If the package status is not 'failed'
975 * @todo This may be enchanced for outgoing packages?
977 public function removeFirstFailedPackage () {
978 // Get the package again
979 $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
981 // Is the package status 'failed'?
982 if ($packageData[self::PACKAGE_DATA_STATUS] != self::PACKAGE_STATUS_FAILED) {
984 throw new UnexpectedPackageStatusException(array($this, $packageData, self::PACKAGE_STATUS_FAILED), BaseListener::EXCEPTION_UNEXPECTED_PACKAGE_STATUS);
988 $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
992 * "Decode" the package content into the same array when it was sent.
994 * @param $rawPackageContent The raw package content to be "decoded"
995 * @return $decodedData An array with 'sender', 'recipient', 'content' and 'status' elements
997 public function decodeRawContent ($rawPackageContent) {
998 // Use the separator '#' to "decode" it
999 $decodedArray = explode(self::PACKAGE_DATA_SEPARATOR, $rawPackageContent);
1001 // Assert on count (should be always 3)
1002 assert(count($decodedArray) == self::DECODED_DATA_ARRAY_SIZE);
1004 // Generate the signature of comparing it
1006 * @todo Unsupported feature of "signed" messages commented out
1007 if (!$this->isPackageSignatureValid($decodedArray)) {
1008 // Is not valid, so throw an exception here
1009 exit(__METHOD__ . ':INVALID SIG! UNDER CONSTRUCTION!' . chr(10));
1014 * Create 'decodedData' array with all assoziative array elements,
1017 $decodedData = array(
1018 self::PACKAGE_DATA_SENDER => $decodedArray[self::INDEX_PACKAGE_SENDER],
1019 self::PACKAGE_DATA_RECIPIENT => $decodedArray[self::INDEX_PACKAGE_RECIPIENT],
1020 self::PACKAGE_DATA_CONTENT => $decodedArray[self::INDEX_PACKAGE_CONTENT],
1021 self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_DECODED
1025 return $decodedData;
1029 * Handles decoded data for this node by "decoding" the 'content' part of
1030 * it. Again this method uses explode() for the "decoding" process.
1032 * @param $decodedData An array with decoded raw package data
1034 * @throws InvalidDataChecksumException If the checksum doesn't match
1036 public function handleRawData (array $decodedData) {
1038 * "Decode" the package's content by a simple explode() call, for
1039 * details of the array elements, see comments for constant
1042 $decodedContent = explode(self::PACKAGE_MASK_SEPARATOR, $decodedData[self::PACKAGE_DATA_CONTENT]);
1044 // Assert on array count for a very basic validation
1045 assert(count($decodedContent) == self::PACKAGE_CONTENT_ARRAY_SIZE);
1048 * Convert the indexed array into an associative array. This is much
1049 * better to remember than plain numbers, isn't it?
1051 $decodedContent = array(
1052 // Compressor's extension used to compress the data
1053 self::PACKAGE_CONTENT_EXTENSION => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION],
1054 // Package data (aka "message") in BASE64-decoded form but still compressed
1055 self::PACKAGE_CONTENT_MESSAGE => base64_decode($decodedContent[self::INDEX_PACKAGE_DATA]),
1056 // Tags as an indexed array for "tagging" the message
1057 self::PACKAGE_CONTENT_TAGS => explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]),
1058 // Checksum of the _decoded_ data
1059 self::PACKAGE_CONTENT_CHECKSUM => $decodedContent[self::INDEX_CHECKSUM]
1062 // Is the checksum valid?
1063 if (!$this->isChecksumValid($decodedContent, $decodedData)) {
1064 // Is not the same, so throw an exception here
1065 throw new InvalidDataChecksumException(array($this, $decodedContent, $decodedData), BaseListener::EXCEPTION_INVALID_DATA_CHECKSUM);
1069 * The checksum is the same, then it can be decompressed safely. The
1070 * original message is at this point fully decoded.
1072 $decodedContent[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedContent[self::PACKAGE_CONTENT_MESSAGE]);
1074 // And push it on the next stack
1075 $this->getStackerInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedContent);
1079 * Checks whether a new message has arrived
1081 * @return $hasArrived Whether a new message has arrived for processing
1083 public function isNewMessageArrived () {
1084 // Determine if the stack is not empty
1085 $hasArrived = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_NEW_MESSAGE));
1092 * Handles newly arrived messages
1095 * @todo Implement verification of all sent tags here?
1097 public function handleNewlyArrivedMessage () {
1098 // Get it from the stacker, it is the full array with the decoded message
1099 $decodedContent = $this->getStackerInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
1101 // Now get a filter chain back from factory with given tags array
1102 $chainInstance = PackageFilterChainFactory::createChainByTagsArray($decodedContent[self::PACKAGE_CONTENT_TAGS]);
1105 * Process the message through all filters, note that all other
1106 * elements from $decodedContent are no longer needed.
1108 $chainInstance->processMessage($decodedContent[self::PACKAGE_CONTENT_MESSAGE], $this);
1112 * Checks whether a processed message is pending for "interpretation"
1114 * @return $isPending Whether a processed message is pending
1116 public function isProcessedMessagePending () {
1118 $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE));
1125 * Handle processed messages by "interpreting" the 'message_type' element
1129 public function handleProcessedMessage () {
1130 // Get it from the stacker, it is the full array with the processed message
1131 $messageArray = $this->getStackerInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE);
1133 // Add type for later easier handling
1134 $messageArray[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_TYPE] = $messageArray[self::MESSAGE_ARRAY_TYPE];
1137 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __LINE__ . ']: messageArray=' . print_r($messageArray, TRUE));
1139 // Create a handler instance from given message type
1140 $handlerInstance = MessageTypeHandlerFactory::createMessageTypeHandlerInstance($messageArray[self::MESSAGE_ARRAY_TYPE]);
1142 // Handle message data
1143 $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this);