*/
class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registerable {
/**
- * Package mask for compressing package data
+ * Package mask for compressing package data:
+ * 1.) Compressor extension
+ * 2.) Raw package data
+ * 3.) Tags, seperated by semicolons, no semicolon is required if only one tag is needed
+ * 4.) Checksum
*/
- const PACKAGE_MASK = '%s:%s:%s';
+ const PACKAGE_MASK = '%s:%s:%s:%s';
+
+ /**
+ * Seperator for the above mask
+ */
+ const PACKAGE_MASK_SEPERATOR = ':';
+
+ /**
+ * Array indexes for above mask, start with zero
+ */
+ const INDEX_COMPRESSOR_EXTENSION = 0;
+ const INDEX_PACKAGE_DATA = 1;
+ const INDEX_TAGS = 2;
+ const INDEX_CHECKSUM = 3;
+
+ /**
+ * Tags seperator
+ */
+ const PACKAGE_TAGS_SEPERATOR = ';';
+
+ /**
+ * Raw package data seperator
+ */
+ const PACKAGE_DATA_SEPERATOR = '|';
/**
* Stacker name for "undeclared" packages
*/
const STACKER_NAME_UNDECLARED = 'undeclared';
+ /**
+ * Stacker name for "declared" packages (which are ready to send out)
+ */
+ const STACKER_NAME_DECLARED = 'declared';
+
+ /**
+ * Stacker name for "out-going" packages
+ */
+ const STACKER_NAME_OUTGOING = 'outgoing';
+
/**
* Network target (alias): 'upper hubs'
*/
// Get new instance
$packageInstance = new NetworkPackage();
- // Now set the compressor instance if set
- if ($compressorInstance instanceof Compressor) {
- // Okay, set it
- $packageInstance->setCompressorInstance($compressorInstance);
- } // END - if
+ // Now set the compressor instance
+ $packageInstance->setCompressorInstance($compressorInstance);
// Return the prepared instance
return $packageInstance;
}
+ /**
+ * "Getter" for hash from given content and helper instance
+ *
+ * @param $content Raw package content
+ * @param $helperInstance A BaseHubHelper instance
+ * @return $hash Hash for given package content
+ */
+ private function getHashFromContent ($content, BaseHubHelper $helperInstance) {
+ // Create the hash
+ // @TODO crc32 is not good, but it needs to be fast
+ $hash = crc32(
+ $content .
+ ':' .
+ $helperInstance->getNodeInstance()->getSessionId() .
+ ':' .
+ $this->getCompressorInstance()->getCompressorExtension()
+ );
+
+ // And return it
+ return $hash;
+ }
+
+ /**
+ * Delivers the given raw package data.
+ *
+ * @param $packageData Raw package data in an array
+ * @return void
+ */
+ private function deliverPackage (array $packageData) {
+ /*
+ * 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
+ * target because it causes an overload on the network and may be
+ * abused for attacking the network with large packages.
+ */
+ $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
+
+ // Discover all recipients, this may throw an exception
+ $discoveryInstance->discoverRecipients($packageData);
+
+ // Now get an iterator
+ $iteratorInstance = $discoveryInstance->getIterator();
+
+ // ... and begin iteration
+ while ($iteratorInstance->valid()) {
+ // Get current entry
+ $currentRecipient = $iteratorInstance->current();
+
+ // Debug message
+ $this->debugOutput('PACKAGE: Package declared for recipient ' . $currentRecipient);
+
+ // Set the recipient
+ $packageData['recipient'] = $currentRecipient;
+
+ // And enqueue it to the writer class
+ $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData);
+
+ // Skip to next entry
+ $iteratorInstance->next();
+ } // END - while
+
+ // Clean-up the list
+ $discoveryInstance->clearRecipients();
+ }
+
+ /**
+ * Sends a raw package out
+ *
+ * @param $packageData Raw package data in an array
+ * @return void
+ */
+ private function sendRawPackage (array $packageData) {
+ /*
+ * This package may become big, depending on the shared object size or
+ * delivered message size which shouldn't be so long (to save
+ * bandwidth). Because of the nature of the used protocol (TCP) we need
+ * to split it up into smaller pieces to fit it into a TCP frame.
+ *
+ * So first we need (again) a discovery class but now a protocol
+ * discovery to choose the right socket resource. The discovery class
+ * should take a look at the raw package data itself and then decide
+ * which (configurable!) protocol should be used for that type of
+ * package.
+ */
+ $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance();
+
+ // Now discover the right protocol
+ $socketResource = $discoveryInstance->discoverSocket($packageData);
+
+ // We have to put this socket in our registry, so get an instance
+ $registryInstance = SocketRegistry::createSocketRegistry();
+
+ // Get the listener from registry
+ $connectionInstance = Registry::getRegistry()->getInstance('connection');
+
+ // Is it not there?
+ if (!$registryInstance->isSocketRegistered($connectionInstance, $socketResource)) {
+ // Then register it
+ $registryInstance->registerSocket($connectionInstance, $socketResource, $packageData);
+ } // END - if
+
+ // We enqueue it again, but now in the out-going queue
+ $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData);
+ }
+
+ /**
+ * Sends waiting packages
+ *
+ * @param $packageData Raw package data
+ * @return void
+ */
+ private function sendOutgoingPackage (array $packageData) {
+ // Get the right connection instance
+ $connectionInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($packageData);
+
+ // Sent it away (we catch exceptions one method above
+ $connectionInstance->sendRawPackageData($packageData);
+ }
+
/**
* "Enqueues" raw content into this delivery class by reading the raw content
* from given template instance and pushing it on the 'undeclared' stack.
// Add magic in front of it and hash behind it, including BASE64 encoding
$content = sprintf(self::PACKAGE_MASK,
+ // 1.) Compressor's extension
$this->getCompressorInstance()->getCompressorExtension(),
+ // 2.) Raw package content, encoded with BASE64
base64_encode($content),
- crc32($content) // @TODO Not so good, but needs to be fast!
+ // 3.) Tags
+ implode(self::PACKAGE_TAGS_SEPERATOR, $helperInstance->getPackageTags()),
+ // 4.) Checksum
+ $this->getHashFromContent($content, $helperInstance)
);
// Now prepare the temporary array and push it on the 'undeclared' stack
return $isEnqueued;
}
+ /**
+ * Checks wether a package has been declared
+ *
+ * @return $isDeclared Wether a package is declared
+ */
+ public function isPackageDeclared () {
+ // Check wether the stacker is not empty
+ $isDeclared = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_DECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_DECLARED)));
+
+ // Return the result
+ return $isDeclared;
+ }
+
+ /**
+ * Checks wether a package should be sent out
+ *
+ * @return $isWaitingDelivery Wether a package is waiting for delivery
+ */
+ public function isPackageWaitingDelivery () {
+ // Check wether the stacker is not empty
+ $isWaitingDelivery = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)));
+
+ // Return the result
+ return $isWaitingDelivery;
+ }
+
/**
* Delivers an enqueued package to the stated destination. If a non-session
* id is provided, recipient resolver is being asked (and instanced once).
* @return void
* @throws NoTargetException If no target can't be determined
*/
- public function deliverEnqueuedPackage () {
+ public function declareEnqueuedPackage () {
// Make sure this method isn't working if there is no package enqueued
if (!$this->isPackageEnqueued()) {
// This is not fatal but should be avoided
// And remove it finally
$this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
}
+
+ /**
+ * Delivers the next declared package. Only one package per time will be sent
+ * because this may take time and slows down the whole delivery
+ * infrastructure.
+ *
+ * @return void
+ */
+ public function deliverDeclaredPackage () {
+ // Sanity check if we have packages declared
+ if (!$this->isPackageDeclared()) {
+ // This is not fatal but should be avoided
+ // @TODO Add some logging here
+ return;
+ } // END - if
+
+ // Get the package again
+ $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
+
+ // And send it
+ $this->sendRawPackage($packageData);
+
+ // And remove it finally
+ $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
+ }
+
+ /**
+ * Sends waiting packages out for delivery
+ *
+ * @return void
+ */
+ public function sentWaitingPackage () {
+ // Sanity check if we have packages waiting for delivery
+ if (!$this->isPackageWaitingDelivery()) {
+ // This is not fatal but should be avoided
+ // @TODO Add some logging here
+ return;
+ } // END - if
+
+ // Get the package again
+ $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_OUTGOING);
+
+ // Now try to send it
+ try {
+ $this->sendOutgoingPackage($packageData);
+
+ // And remove it finally when it has been fully delivered
+ $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
+ } catch (InvalidSocketException $e) {
+ // Output exception message
+ $this->debugOutput('PACKAGE: Package was not delivered: ' . $e->getMessage());
+ }
+ }
}
// [EOF]