X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fpackage%2Fclass_NetworkPackage.php;h=a08a1dc5524746d9b0bd9d61fa3874c1d02e1792;hb=aaea57bc3f64734a41670a779628a551d555dab4;hp=e40c73951baefd0cd054708f79fe3477c05c6f20;hpb=c838a7e2f94ae11a21710a2b1faecd3c6b563f5b;p=hub.git diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index e40c73951..a08a1dc55 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -15,11 +15,11 @@ * For performance reasons, this class should only be instanciated once and then * used as a "pipe-through" class. * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @link http://www.shipsimu.org * @todo Needs to add functionality for handling the object's type * * This program is free software: you can redistribute it and/or modify @@ -35,7 +35,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receivable, Registerable, Visitable { +class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, Registerable, Visitable { /** * Package mask for compressing package data: * 0: Compressor extension @@ -44,17 +44,23 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva * 3: Checksum * 0 1 2 3 */ - const PACKAGE_MASK = '%s:%s:%s:%s'; + const PACKAGE_MASK = '%s%s%s%s%s%s%s'; /** - * Seperator for the above mask + * Separator for the above mask */ - const PACKAGE_MASK_SEPERATOR = ':'; + const PACKAGE_MASK_SEPARATOR = '^'; /** - * Seperator for checksum + * Size of an array created by invoking + * explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $content). */ - const PACKAGE_CHECKSUM_SEPERATOR = ':'; + const PACKAGE_CONTENT_ARRAY_SIZE = 4; + + /** + * Separator for checksum + */ + const PACKAGE_CHECKSUM_SEPARATOR = '_'; /** * Array indexes for above mask, start with zero @@ -70,23 +76,88 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva const INDEX_PACKAGE_SENDER = 0; const INDEX_PACKAGE_RECIPIENT = 1; const INDEX_PACKAGE_CONTENT = 2; + const INDEX_PACKAGE_STATUS = 3; + const INDEX_PACKAGE_SIGNATURE = 4; + + /** + * Size of the decoded data array ('status' is not included) + */ + const DECODED_DATA_ARRAY_SIZE = 4; + + /** + * Named array elements for decoded package content + */ + const PACKAGE_CONTENT_EXTENSION = 'compressor'; + const PACKAGE_CONTENT_MESSAGE = 'message'; + const PACKAGE_CONTENT_TAGS = 'tags'; + const PACKAGE_CONTENT_CHECKSUM = 'checksum'; /** * Named array elements for package data */ const PACKAGE_DATA_SENDER = 'sender'; const PACKAGE_DATA_RECIPIENT = 'recipient'; + const PACKAGE_DATA_PROTOCOL = 'protocol'; const PACKAGE_DATA_CONTENT = 'content'; + const PACKAGE_DATA_STATUS = 'status'; + const PACKAGE_DATA_SIGNATURE = 'signature'; + + /** + * All package status + */ + const PACKAGE_STATUS_NEW = 'new'; + const PACKAGE_STATUS_FAILED = 'failed'; + const PACKAGE_STATUS_DECODED = 'decoded'; + const PACKAGE_STATUS_FAKED = 'faked'; + + /** + * Constants for message data array + */ + const MESSAGE_ARRAY_DATA = 'message_data'; + const MESSAGE_ARRAY_TYPE = 'message_type'; /** - * Tags seperator + * Generic answer status field */ - const PACKAGE_TAGS_SEPERATOR = ';'; /** - * Raw package data seperator + * Tags separator */ - const PACKAGE_DATA_SEPERATOR = '#'; + const PACKAGE_TAGS_SEPARATOR = ';'; + + /** + * Raw package data separator + */ + const PACKAGE_DATA_SEPARATOR = '#'; + + /** + * Separator for more than one recipient + */ + const PACKAGE_RECIPIENT_SEPARATOR = ':'; + + /** + * Network target (alias): 'upper nodes' + */ + const NETWORK_TARGET_UPPER = 'upper'; + + /** + * Network target (alias): 'self' + */ + const NETWORK_TARGET_SELF = 'self'; + + /** + * Network target (alias): 'dht' + */ + const NETWORK_TARGET_DHT = 'dht'; + + /** + * TCP package size in bytes + */ + const TCP_PACKAGE_SIZE = 512; + + /************************************************************************** + * Stacker for out-going packages * + **************************************************************************/ /** * Stacker name for "undeclared" packages @@ -103,6 +174,10 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva */ const STACKER_NAME_OUTGOING = 'package_outgoing'; + /************************************************************************** + * Stacker for incoming packages * + **************************************************************************/ + /** * Stacker name for "incoming" decoded raw data */ @@ -114,24 +189,38 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva const STACKER_NAME_DECODED_HANDLED = 'package_handled_decoded'; /** - * Stacker name for "back-buffered" packages + * Stacker name for "chunked" decoded raw data */ - const STACKER_NAME_BACK_BUFFER = 'package_backbuffer'; + const STACKER_NAME_DECODED_CHUNKED = 'package_chunked_decoded'; + + /************************************************************************** + * Stacker for incoming messages * + **************************************************************************/ /** - * Network target (alias): 'upper hubs' + * Stacker name for new messages */ - const NETWORK_TARGET_UPPER_HUBS = 'upper'; + const STACKER_NAME_NEW_MESSAGE = 'package_new_message'; /** - * Network target (alias): 'self' + * Stacker name for processed messages */ - const NETWORK_TARGET_SELF = 'self'; + const STACKER_NAME_PROCESSED_MESSAGE = 'package_processed_message'; + + /************************************************************************** + * Stacker for other/internal purposes * + **************************************************************************/ /** - * TCP package size in bytes + * Stacker name for "back-buffered" packages */ - const TCP_PACKAGE_SIZE = 512; + const STACKER_NAME_BACK_BUFFER = 'package_backbuffer'; + + /************************************************************************** + * Protocol names * + **************************************************************************/ + const PROTOCOL_TCP = 'TCP'; + const PROTOCOL_UDP = 'UDP'; /** * Protected constructor @@ -167,14 +256,20 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva $packageInstance->setStackerInstance($stackerInstance); // Init all stacker - $packageInstance->initStackers(); + $packageInstance->initStacks(); - // Get a visitor instance for speeding up things + // Get a visitor instance for speeding up things and set it $visitorInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_monitor_visitor_class', array($packageInstance)); - - // Set it in this package $packageInstance->setVisitorInstance($visitorInstance); + // Get crypto instance and set it, too + $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class'); + $packageInstance->setCryptoInstance($cryptoInstance); + + // Get a singleton package assembler instance from factory and set it here, too + $assemblerInstance = PackageAssemblerFactory::createAssemblerInstance($packageInstance); + $packageInstance->setAssemblerInstance($assemblerInstance); + // Return the prepared instance return $packageInstance; } @@ -182,44 +277,118 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva /** * Initialize all stackers * + * @param $forceReInit Whether to force reinitialization of all stacks * @return void */ - protected function initStackers () { + protected function initStacks ($forceReInit = FALSE) { // Initialize all - foreach ( - array( - self::STACKER_NAME_UNDECLARED, - self::STACKER_NAME_DECLARED, - self::STACKER_NAME_OUTGOING, - self::STACKER_NAME_DECODED_INCOMING, - self::STACKER_NAME_DECODED_HANDLED, - self::STACKER_NAME_BACK_BUFFER - ) as $stackerName) { - // Init this stacker - $this->getStackerInstance()->initStacker($stackerName); - } // END - foreach - } - - /** - * "Getter" for hash from given content and helper instance - * - * @param $content Raw package content - * @param $helperInstance An instance of a HelpableHub class - * @param $nodeInstance An instance of a NodeHelper class - * @return $hash Hash for given package content - * @todo $helperInstance is unused + $this->getStackerInstance()->initStacks(array( + self::STACKER_NAME_UNDECLARED, + self::STACKER_NAME_DECLARED, + self::STACKER_NAME_OUTGOING, + self::STACKER_NAME_DECODED_INCOMING, + self::STACKER_NAME_DECODED_HANDLED, + self::STACKER_NAME_DECODED_CHUNKED, + self::STACKER_NAME_NEW_MESSAGE, + self::STACKER_NAME_PROCESSED_MESSAGE, + self::STACKER_NAME_BACK_BUFFER + ), $forceReInit); + } + + /** + * "Getter" for hash from given content + * + * @param $content Raw package content + * @return $hash Hash for given package content */ - private function getHashFromContent ($content, HelpableHub $helperInstance, NodeHelper $nodeInstance) { + private function getHashFromContent ($content) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); + // Create the hash - // @TODO crc32() is not very strong, but it needs to be fast - $hash = crc32( + // @TODO md5() is very weak, but it needs to be fast + $hash = md5( $content . - self::PACKAGE_CHECKSUM_SEPERATOR . - $nodeInstance->getSessionId() . - self::PACKAGE_CHECKSUM_SEPERATOR . + self::PACKAGE_CHECKSUM_SEPARATOR . + $this->getSessionId() . + self::PACKAGE_CHECKSUM_SEPARATOR . $this->getCompressorInstance()->getCompressorExtension() ); + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); + + // And return it + return $hash; + } + + /** + * Checks whether the checksum (sometimes called "hash") is the same + * + * @param $decodedContent Package raw content + * @param $decodedData Whole raw package data array + * @return $isChecksumValid Whether the checksum is the same + */ + private function isChecksumValid (array $decodedContent, array $decodedData) { + // Get checksum + $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER]); + + // Is it the same? + $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]); + + // Return it + return $isChecksumValid; + } + + /** + * Change the package with given status in given stack + * + * @param $packageData Raw package data in an array + * @param $stackerName Name of the stacker + * @param $newStatus New status to set + * @return void + */ + private function changePackageStatus (array $packageData, $stackerName, $newStatus) { + // Skip this for empty stacks + if ($this->getStackerInstance()->isStackEmpty($stackerName)) { + // This avoids an exception after all packages has failed + return; + } // END - if + + // Pop the entry (it should be it) + $nextData = $this->getStackerInstance()->popNamed($stackerName); + + // Compare both signatures + assert($nextData[self::PACKAGE_DATA_SIGNATURE] == $packageData[self::PACKAGE_DATA_SIGNATURE]); + + // Temporary set the new status + $packageData[self::PACKAGE_DATA_STATUS] = $newStatus; + + // And push it again + $this->getStackerInstance()->pushNamed($stackerName, $packageData); + } + + /** + * "Getter" for hash from given content and sender's session id + * + * @param $decodedContent Raw package content + * @param $sessionId Session id of the sender + * @return $hash Hash for given package content + */ + public function getHashFromContentSessionId (array $decodedContent, $sessionId) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_EXTENSION]); + + // Create the hash + // @TODO md5() is very weak, but it needs to be fast + $hash = md5( + $decodedContent[self::PACKAGE_CONTENT_MESSAGE] . + self::PACKAGE_CHECKSUM_SEPARATOR . + $sessionId . + self::PACKAGE_CHECKSUM_SEPARATOR . + $decodedContent[self::PACKAGE_CONTENT_EXTENSION] + ); + // And return it return $hash; } @@ -235,6 +404,9 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva * @return void */ private function declareRawPackageData (array $packageData) { + // Make sure the required field is there + assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT])); + /* * We need to disover every recipient, just in case we have a * multi-recipient entry like 'upper' is. 'all' may be a not so good @@ -249,13 +421,16 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva // Now get an iterator $iteratorInstance = $discoveryInstance->getIterator(); + // Rewind back to the beginning + $iteratorInstance->rewind(); + // ... and begin iteration while ($iteratorInstance->valid()) { // Get current entry $currentRecipient = $iteratorInstance->current(); // Debug message - $this->debugOutput('PACKAGE: Package declared for recipient ' . $currentRecipient); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Setting recipient to ' . $currentRecipient . ',previous=' . $packageData[self::PACKAGE_DATA_RECIPIENT]); // Set the recipient $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient; @@ -263,11 +438,18 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva // And enqueue it to the writer class $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData); + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Package declared for recipient ' . $currentRecipient); + // Skip to next entry $iteratorInstance->next(); } // END - while - // Clean-up the list + /* + * The recipient list can be cleaned up here because the package which + * shall be delivered has already been added for all entries from the + * list. + */ $discoveryInstance->clearRecipients(); } @@ -296,37 +478,50 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance(); // Now discover the right protocol - $socketResource = $discoveryInstance->discoverSocket($packageData); + $socketResource = $discoveryInstance->discoverSocket($packageData, BaseConnectionHelper::CONNECTION_TYPE_OUTGOING); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); // We have to put this socket in our registry, so get an instance - $registryInstance = SocketRegistry::createSocketRegistry(); + $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); // Get the listener from registry $helperInstance = Registry::getRegistry()->getInstance('connection'); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: stateInstance=' . $helperInstance->getStateInstance()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); // Is it not there? if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Registering socket ' . $socketResource . ' ...'); + // Then register it $registryInstance->registerSocket($helperInstance, $socketResource, $packageData); - } // END - if + } elseif (!$helperInstance->getStateInstance()->isPeerStateConnected()) { + // Is not connected, then we cannot send + self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Unexpected peer state ' . $helperInstance->getStateInstance()->__toString() . ' detected.'); + + // Shutdown the socket + $this->shutdownSocket($socketResource); + } // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); // Make sure the connection is up $helperInstance->getStateInstance()->validatePeerStateConnected(); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); - // We enqueue it again, but now in the out-going queue + // Enqueue it again on the out-going queue, the connection is up and working at this point $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Reached line ' . __LINE__ . ' after pushNamed() has been called.'); } /** @@ -340,7 +535,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva $sentBytes = 0; // Get the right connection instance - $helperInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($packageData); + $helperInstance = SocketRegistryFactory::createSocketRegistryInstance()->getHandlerInstanceFromPackageData($packageData); // Is this connection still alive? if ($helperInstance->isShuttedDown()) { @@ -356,48 +551,97 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva $this->storeUnsentBytesInBackBuffer($packageData, $sentBytes); } + /** + * Generates a signature for given raw package content and sender id + * + * @param $content Raw package data + * @param $senderId Sender id to generate a signature for + * @return $signature Signature as BASE64-encoded string + */ + private function generatePackageSignature ($content, $senderId) { + // Hash content and sender id together, use md5() as last algo + $hash = md5($this->getCryptoInstance()->hashString($senderId . $content, $this->getPrivateKey(), FALSE)); + + // Encrypt the content again with the hash as a key + $encryptedContent = $this->getCryptoInstance()->encryptString($content, $hash); + + // Encode it with BASE64 + $signature = base64_encode($encryptedContent); + + // Return it + return $signature; + } + + /** + * Checks whether the signature of given package data is 'valid', here that + * means it is the same or not. + * + * @param $decodedArray An array with 'decoded' (explode() was mostly called) data + * @return $isSignatureValid Whether the signature is valid + * @todo Unfinished area, signatures are currently NOT fully supported + */ + private function isPackageSignatureValid (array $decodedArray) { + // Generate the signature of comparing it + $signature = $this->generatePackageSignature($decodedArray[self::INDEX_PACKAGE_CONTENT], $decodedArray[self::INDEX_PACKAGE_SENDER]); + + // Is it the same? + //$isSignatureValid = + exit(__METHOD__.': signature='.$signature.chr(10).',decodedArray='.print_r($decodedArray, TRUE)); + } + /** * "Enqueues" raw content into this delivery class by reading the raw content - * from given template instance and pushing it on the 'undeclared' stack. + * from given helper's template instance and pushing it on the 'undeclared' + * stack. * - * @param $helperInstance An instance of a HelpableHub class - * @param $nodeInstance An instance of a NodeHelper class + * @param $helperInstance An instance of a Helper class + * @param $protocol Name of used protocol (TCP/UDP) * @return void */ - public function enqueueRawDataFromTemplate (HelpableHub $helperInstance, NodeHelper $nodeInstance) { + public function enqueueRawDataFromTemplate (Helper $helperInstance, $protocolName) { // Get the raw content ... $content = $helperInstance->getTemplateInstance()->getRawTemplateData(); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('content(' . strlen($content) . ')=' . $content); // ... and compress it $content = $this->getCompressorInstance()->compressStream($content); // Add magic in front of it and hash behind it, including BASE64 encoding - $content = sprintf(self::PACKAGE_MASK, + $packageContent = sprintf(self::PACKAGE_MASK, // 1.) Compressor's extension $this->getCompressorInstance()->getCompressorExtension(), + // - separator + self::PACKAGE_MASK_SEPARATOR, // 2.) Raw package content, encoded with BASE64 base64_encode($content), + // - separator + self::PACKAGE_MASK_SEPARATOR, // 3.) Tags - implode(self::PACKAGE_TAGS_SEPERATOR, $helperInstance->getPackageTags()), + implode(self::PACKAGE_TAGS_SEPARATOR, $helperInstance->getPackageTags()), + // - separator + self::PACKAGE_MASK_SEPARATOR, // 4.) Checksum - $this->getHashFromContent($content, $helperInstance, $nodeInstance) + $this->getHashFromContent($content) ); // Now prepare the temporary array and push it on the 'undeclared' stack $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array( - self::PACKAGE_DATA_SENDER => $nodeInstance->getSessionId(), + self::PACKAGE_DATA_SENDER => $this->getSessionId(), self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(), - self::PACKAGE_DATA_CONTENT => $content, + self::PACKAGE_DATA_PROTOCOL => $protocolName, + self::PACKAGE_DATA_CONTENT => $packageContent, + self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_NEW, + self::PACKAGE_DATA_SIGNATURE => $this->generatePackageSignature($packageContent, $this->getSessionId()) )); } /** - * Checks wether a package has been enqueued for delivery. + * Checks whether a package has been enqueued for delivery. * - * @return $isEnqueued Wether a package is enqueued + * @return $isEnqueued Whether a package is enqueued */ public function isPackageEnqueued () { - // Check wether the stacker is not empty + // Check whether the stacker is not empty $isEnqueued = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED))); // Return the result @@ -405,12 +649,12 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva } /** - * Checks wether a package has been declared + * Checks whether a package has been declared * - * @return $isDeclared Wether a package is declared + * @return $isDeclared Whether a package is declared */ public function isPackageDeclared () { - // Check wether the stacker is not empty + // Check whether the stacker is not empty $isDeclared = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_DECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECLARED))); // Return the result @@ -418,12 +662,12 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva } /** - * Checks wether a package should be sent out + * Checks whether a package should be sent out * - * @return $isWaitingDelivery Wether a package is waiting for delivery + * @return $isWaitingDelivery Whether a package is waiting for delivery */ public function isPackageWaitingForDelivery () { - // Check wether the stacker is not empty + // Check whether the stacker is not empty $isWaitingDelivery = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING))); // Return the result @@ -444,19 +688,18 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva // Make sure this method isn't working if there is no package enqueued if (!$this->isPackageEnqueued()) { // This is not fatal but should be avoided - // @TODO Add some logging here + self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: No raw package data waiting declaration, but ' . __METHOD__ . ' has been called!'); return; } // END - if - // Now we know for sure there are packages to deliver, we can start - // with the first one. - $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_UNDECLARED); + /* + * Now there are for sure packages to deliver, so start with the first + * one. + */ + $packageData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED); // Declare the raw package data for delivery $this->declareRawPackageData($packageData); - - // And remove it finally - $this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED); } /** @@ -470,13 +713,17 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva // Sanity check if we have packages declared if (!$this->isPackageDeclared()) { // This is not fatal but should be avoided - // @TODO Add some logging here + self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: No package has been declared, but ' . __METHOD__ . ' has been called!'); return; } // END - if - // Get the package again + // Get the package $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED); + // Assert on it + assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT])); + + // Try to deliver the package try { // And try to send it $this->deliverRawPackageData($packageData); @@ -485,7 +732,10 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED); } catch (InvalidStateException $e) { // The state is not excepected (shall be 'connected') - $this->debugOutput('PACKAGE: Caught exception ' . $e->__toString() . ' with message=' . $e->getMessage()); + self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Caught ' . $e->__toString() . ',message=' . $e->getMessage()); + + // Mark the package with status failed + $this->changePackageStatus($packageData, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED); } } @@ -501,11 +751,11 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva // Sanity check if we have packages waiting for delivery if (!$this->isPackageWaitingForDelivery()) { // This is not fatal but should be avoided - $this->debugOutput('PACKAGE: No package is waiting for delivery, but ' . __METHOD__ . ' was called.'); + self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: No package is waiting for delivery, but ' . __METHOD__ . ' was called.'); return; } // END - if - // Get the package again + // Get the package $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_OUTGOING); try { @@ -516,7 +766,10 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING); } catch (InvalidSocketException $e) { // Output exception message - $this->debugOutput('PACKAGE: Package was not delivered: ' . $e->getMessage()); + self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Package was not delivered: ' . $e->getMessage()); + + // Mark package as failed + $this->changePackageStatus($packageData, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED); } } @@ -525,12 +778,12 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva /////////////////////////////////////////////////////////////////////////// /** - * Checks wether decoded raw data is pending + * Checks whether decoded raw data is pending * - * @return $isPending Wether decoded raw data is pending + * @return $isPending Whether decoded raw data is pending */ - private function isDecodedDataPending () { - // Just return wether the stack is not empty + private function isRawDataPending () { + // Just return whether the stack is not empty $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_INCOMING)); // Return the status @@ -538,17 +791,17 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva } /** - * Checks wether new raw package data has arrived at a socket + * Checks whether new raw package data has arrived at a socket * * @param $poolInstance An instance of a PoolableListener class - * @return $hasArrived Wether new raw package data has arrived for processing + * @return $hasArrived Whether new raw package data has arrived for processing */ public function isNewRawDataPending (PoolableListener $poolInstance) { // Visit the pool. This monitors the pool for incoming raw data. $poolInstance->accept($this->getVisitorInstance()); // Check for new data arrival - $hasArrived = $this->isDecodedDataPending(); + $hasArrived = $this->isRawDataPending(); // Return the status return $hasArrived; @@ -566,34 +819,40 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva * This method should only be called if decoded raw data is pending, * so check it again. */ - if (!$this->isDecodedDataPending()) { + if (!$this->isRawDataPending()) { // This is not fatal but should be avoided - // @TODO Add some logging here + self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: No raw (decoded?) data is pending, but ' . __METHOD__ . ' has been called!'); return; } // END - if // Very noisy debug message: - /* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Stacker size is ' . $this->getStackerInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Stacker size is ' . $this->getStackerInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.'); // "Pop" the next entry (the same array again) from the stack $decodedData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING); // Make sure both array elements are there - assert((is_array($decodedData)) && (isset($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA])) && (isset($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE]))); + assert( + (is_array($decodedData)) && + (isset($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA])) && + (isset($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE])) + ); /* * Also make sure the error code is SOCKET_ERROR_UNHANDLED because we * only want to handle unhandled packages here. */ + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: errorCode=' . $decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] . '(' . BaseRawDataHandler::SOCKET_ERROR_UNHANDLED . ')'); assert($decodedData[BaseRawDataHandler::PACKAGE_ERROR_CODE] == BaseRawDataHandler::SOCKET_ERROR_UNHANDLED); - // Remove the last chunk seperator (because it is being added and we don't need it) - if (substr($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPERATOR) { + // Remove the last chunk SEPARATOR (because it is being added and we don't need it) + if (substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) { // It is there and should be removed - $decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA] = substr($decodedData[BaseRawDataHandler::PACKAGE_DECODED_DATA], 0, -1); + $decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA] = substr($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA], 0, -1); } // END - if // This package is "handled" and can be pushed on the next stack + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Pushing ' . strlen($decodedData[BaseRawDataHandler::PACKAGE_RAW_DATA]) . ' bytes to stack ' . self::STACKER_NAME_DECODED_HANDLED . ' ...'); $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $decodedData); } @@ -603,26 +862,26 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva * @param $handlerInstance An instance of a Networkable class * @return void */ - public function addDecodedDataToIncomingStack (Networkable $handlerInstance) { + public function addRawDataToIncomingStack (Networkable $handlerInstance) { /* * Get the decoded data from the handler, this is an array with - * 'decoded_data' and 'error_code' as elements. + * 'raw_data' and 'error_code' as elements. */ - $decodedData = $handlerInstance->getNextDecodedData(); + $decodedData = $handlerInstance->getNextRawData(); // Very noisy debug message: - //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, true)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, TRUE)); // And push it on our stack $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $decodedData); } /** - * Checks wether incoming decoded data is handled. + * Checks whether incoming decoded data is handled. * - * @return $isHandled Wether incoming decoded data is handled + * @return $isHandled Whether incoming decoded data is handled */ - public function isIncomingDecodedDataHandled () { + public function isIncomingRawDataHandled () { // Determine if the stack is not empty $isHandled = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECODED_HANDLED)); @@ -631,22 +890,59 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva } /** - * Assembles incoming decoded data so it will become an abstract network - * package again. + * Checks whether the assembler has pending data left + * + * @return $isHandled Whether the assembler has pending data left + */ + public function ifAssemblerHasPendingDataLeft () { + // Determine if the stack is not empty + $isHandled = (!$this->getAssemblerInstance()->isPendingDataEmpty()); + + // Return it + return $isHandled; + } + + /** + * Handles the attached assemler's pending data queue to be finally + * assembled to the raw package data back. * * @return void */ - public function assembleDecodedDataToPackage () { - $this->partialStub('Please implement this method.'); + public function handleAssemblerPendingData () { + // Handle it + $this->getAssemblerInstance()->handlePendingData(); } /** - * Checks wether a new package has arrived + * Assembles incoming decoded data so it will become an abstract network + * package again. The assembler does later do it's job by an other task, + * not this one to keep best speed possible. * - * @return $hasArrived Wether a new package has arrived for processing + * @return void */ - public function isNewPackageArrived () { - // @TODO Add some content here + public function assembleDecodedDataToPackage () { + // Make sure the raw decoded package data is handled + assert($this->isIncomingRawDataHandled()); + + // Get current package content (an array with two elements; see handleIncomingDecodedData() for details) + $packageContent = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED); + + // Assert on some elements + assert( + (is_array($packageContent)) && + (isset($packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA])) && + (isset($packageContent[BaseRawDataHandler::PACKAGE_ERROR_CODE])) + ); + + // Start assembling the raw package data array by chunking it + $this->getAssemblerInstance()->chunkPackageContent($packageContent); + + // Remove the package from 'handled_decoded' stack ... + $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECODED_HANDLED); + + // ... and push it on the 'chunked' stacker + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Pushing ' . strlen($packageContent[BaseRawDataHandler::PACKAGE_RAW_DATA]) . ' bytes on stack ' . self::STACKER_NAME_DECODED_CHUNKED . ' ...'); + $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageContent); } /** @@ -657,13 +953,13 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva */ public function accept (Visitor $visitorInstance) { // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: ' . $visitorInstance->__toString() . ' has visited - START'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - START'); // Visit the package $visitorInstance->visitNetworkPackage($this); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: ' . $visitorInstance->__toString() . ' has visited - FINISHED'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited - FINISHED'); } /** @@ -672,22 +968,188 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva * @return void */ public function clearAllStacks () { - // Do the cleanup (no flushing) - foreach ( - array( - self::STACKER_NAME_UNDECLARED, - self::STACKER_NAME_DECLARED, - self::STACKER_NAME_OUTGOING, - self::STACKER_NAME_DECODED_INCOMING, - self::STACKER_NAME_DECODED_HANDLED, - self::STACKER_NAME_BACK_BUFFER - ) as $stackerName) { - // Clear this stacker by forcing an init - $this->getStackerInstance()->initStacker($stackerName, true); - } // END - foreach + // Call the init method to force re-initialization + $this->initStacks(TRUE); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: All stacker have been re-initialized.'); + } + + /** + * Removes the first failed outoging package from the stack to continue + * with next one (it will never work until the issue is fixed by you). + * + * @return void + * @throws UnexpectedPackageStatusException If the package status is not 'failed' + * @todo This may be enchanced for outgoing packages? + */ + public function removeFirstFailedPackage () { + // Get the package again + $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED); + + // Is the package status 'failed'? + if ($packageData[self::PACKAGE_DATA_STATUS] != self::PACKAGE_STATUS_FAILED) { + // Not failed! + throw new UnexpectedPackageStatusException(array($this, $packageData, self::PACKAGE_STATUS_FAILED), BaseListener::EXCEPTION_UNEXPECTED_PACKAGE_STATUS); + } // END - if + + // Remove this entry + $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED); + } + + /** + * "Decode" the package content into the same array when it was sent. + * + * @param $rawPackageContent The raw package content to be "decoded" + * @return $decodedData An array with 'sender', 'recipient', 'content' and 'status' elements + */ + public function decodeRawContent ($rawPackageContent) { + // Use the separator '#' to "decode" it + $decodedArray = explode(self::PACKAGE_DATA_SEPARATOR, $rawPackageContent); + + // Assert on count (should be always 3) + assert(count($decodedArray) == self::DECODED_DATA_ARRAY_SIZE); + + // Generate the signature of comparing it + /* + * @todo Unsupported feature of "signed" messages commented out + if (!$this->isPackageSignatureValid($decodedArray)) { + // Is not valid, so throw an exception here + exit(__METHOD__ . ':INVALID SIG! UNDER CONSTRUCTION!' . chr(10)); + } // END - if + */ + + /* + * Create 'decodedData' array with all assoziative array elements, + * except signature. + */ + $decodedData = array( + self::PACKAGE_DATA_SENDER => $decodedArray[self::INDEX_PACKAGE_SENDER], + self::PACKAGE_DATA_RECIPIENT => $decodedArray[self::INDEX_PACKAGE_RECIPIENT], + self::PACKAGE_DATA_CONTENT => $decodedArray[self::INDEX_PACKAGE_CONTENT], + self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_DECODED + ); + + // And return it + return $decodedData; + } + + /** + * Handles decoded data for this node by "decoding" the 'content' part of + * it. Again this method uses explode() for the "decoding" process. + * + * @param $decodedData An array with decoded raw package data + * @return void + * @throws InvalidDataChecksumException If the checksum doesn't match + */ + public function handleRawData (array $decodedData) { + /* + * "Decode" the package's content by a simple explode() call, for + * details of the array elements, see comments for constant + * PACKAGE_MASK. + */ + $decodedContent = explode(self::PACKAGE_MASK_SEPARATOR, $decodedData[self::PACKAGE_DATA_CONTENT]); + + // Assert on array count for a very basic validation + assert(count($decodedContent) == self::PACKAGE_CONTENT_ARRAY_SIZE); + + /* + * Convert the indexed array into an associative array. This is much + * better to remember than plain numbers, isn't it? + */ + $decodedContent = array( + // Compressor's extension used to compress the data + self::PACKAGE_CONTENT_EXTENSION => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION], + // Package data (aka "message") in BASE64-decoded form but still compressed + self::PACKAGE_CONTENT_MESSAGE => base64_decode($decodedContent[self::INDEX_PACKAGE_DATA]), + // Tags as an indexed array for "tagging" the message + self::PACKAGE_CONTENT_TAGS => explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]), + // Checksum of the _decoded_ data + self::PACKAGE_CONTENT_CHECKSUM => $decodedContent[self::INDEX_CHECKSUM] + ); + + // Is the checksum valid? + if (!$this->isChecksumValid($decodedContent, $decodedData)) { + // Is not the same, so throw an exception here + throw new InvalidDataChecksumException(array($this, $decodedContent, $decodedData), BaseListener::EXCEPTION_INVALID_DATA_CHECKSUM); + } // END - if + + /* + * The checksum is the same, then it can be decompressed safely. The + * original message is at this point fully decoded. + */ + $decodedContent[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedContent[self::PACKAGE_CONTENT_MESSAGE]); + + // And push it on the next stack + $this->getStackerInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedContent); + } + + /** + * Checks whether a new message has arrived + * + * @return $hasArrived Whether a new message has arrived for processing + */ + public function isNewMessageArrived () { + // Determine if the stack is not empty + $hasArrived = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_NEW_MESSAGE)); + + // Return it + return $hasArrived; + } + + /** + * Handles newly arrived messages + * + * @return void + * @todo Implement verification of all sent tags here? + */ + public function handleNewlyArrivedMessage () { + // Get it from the stacker, it is the full array with the decoded message + $decodedContent = $this->getStackerInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE); + + // Now get a filter chain back from factory with given tags array + $chainInstance = PackageFilterChainFactory::createChainByTagsArray($decodedContent[self::PACKAGE_CONTENT_TAGS]); + + /* + * Process the message through all filters, note that all other + * elements from $decodedContent are no longer needed. + */ + $chainInstance->processMessage($decodedContent[self::PACKAGE_CONTENT_MESSAGE], $this); + } + + /** + * Checks whether a processed message is pending for "interpretation" + * + * @return $isPending Whether a processed message is pending + */ + public function isProcessedMessagePending () { + // Check it + $isPending = (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE)); + + // Return it + return $isPending; + } + + /** + * Handle processed messages by "interpreting" the 'message_type' element + * + * @return void + */ + public function handleProcessedMessage () { + // Get it from the stacker, it is the full array with the processed message + $messageArray = $this->getStackerInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE); + + // Add type for later easier handling + $messageArray[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_TYPE] = $messageArray[self::MESSAGE_ARRAY_TYPE]; // Debug message - /* DEBUG: */ $this->debugOutput('PACKAGE: All stackers has be re-initialized.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: messageArray=' . print_r($messageArray, TRUE)); + + // Create a handler instance from given message type + $handlerInstance = MessageTypeHandlerFactory::createMessageTypeHandlerInstance($messageArray[self::MESSAGE_ARRAY_TYPE]); + + // Handle message data + $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this); } }