<?php
/**
- * A NetworkPackage class. This class implements the Deliverable class because
- * all network packages should be deliverable to other nodes. It further
- * provides methods for reading raw content from template engines and feeding it
- * to the stacker for undeclared packages.
+ * A NetworkPackage class. This class implements Deliverable and Receivable
+ * because all network packages should be deliverable to other nodes and
+ * receivable from other nodes. It further provides methods for reading raw
+ * content from template engines and feeding it to the stacker for undeclared
+ * packages.
*
* The factory method requires you to provide a compressor class (which must
* implement the Compressor interface). If you don't want any compression (not
* class and encode it with BASE64 for a more error-free transfer over the
* Internet.
*
- * For performance reasons, this class should only be instantiated once and then
+ * For performance reasons, this class should only be instanciated once and then
* used as a "pipe-through" class.
*
* @author Roland Haeder <webmaster@ship-simu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
* @todo Needs to add functionality for handling the object's type
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registerable {
+class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receivable, Registerable {
/**
* 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
+ * 0: Compressor extension
+ * 1: Raw package data
+ * 2: Tags, seperated by semicolons, no semicolon is required if only one tag is needed
+ * 3: Checksum
+ * 0 1 2 3
*/
const PACKAGE_MASK = '%s:%s:%s:%s';
const INDEX_TAGS = 2;
const INDEX_CHECKSUM = 3;
+ /**
+ * Array indexes for raw package array
+ */
+ const INDEX_PACKAGE_SENDER = 0;
+ const INDEX_PACKAGE_RECIPIENT = 1;
+ const INDEX_PACKAGE_CONTENT = 2;
+
/**
* Tags seperator
*/
*/
const STACKER_NAME_OUTGOING = 'outgoing';
+ /**
+ * Stacker name for "back-buffered" packages
+ */
+ const STACKER_NAME_BACK_BUFFER = 'backbuffer';
+
/**
* Network target (alias): 'upper hubs'
*/
const NETWORK_TARGET_UPPER_HUBS = 'upper';
+ /**
+ * Network target (alias): 'self'
+ */
+ const NETWORK_TARGET_SELF = 'self';
+
+ /**
+ * TCP package size in bytes
+ */
+ const TCP_PACKAGE_SIZE = 512;
+
/**
* Protected constructor
*
// We need to initialize a stack here for our packages even those
// which have no recipient address and stamp... ;-)
- $stackerInstance = ObjectFactory::createObjectByConfiguredName('package_stacker_class');
+ $stackerInstance = ObjectFactory::createObjectByConfiguredName('network_package_stacker_class');
// At last, set it in this class
$this->setStackerInstance($stackerInstance);
* @param $compressorInstance A Compressor instance for compressing the content
* @return $packageInstance An instance of a Deliverable class
*/
- public final static function createNetworkPackage (Compressor $compressorInstance) {
+ public static final function createNetworkPackage (Compressor $compressorInstance) {
// Get new instance
$packageInstance = new NetworkPackage();
* "Getter" for hash from given content and helper instance
*
* @param $content Raw package content
- * @param $helperInstance A BaseHubHelper instance
+ * @param $helperInstance An instance of a BaseHubHelper class
+ * @param $nodeInstance An instance of a NodeHelper class
* @return $hash Hash for given package content
*/
- private function getHashFromContent ($content, BaseHubHelper $helperInstance) {
+ private function getHashFromContent ($content, BaseHubHelper $helperInstance, NodeHelper $nodeInstance) {
// Create the hash
// @TODO crc32 is not good, but it needs to be fast
$hash = crc32(
$content .
':' .
- $helperInstance->getNodeInstance()->getSessionId() .
+ $nodeInstance->getSessionId() .
':' .
$this->getCompressorInstance()->getCompressorExtension()
);
return $hash;
}
+ ///////////////////////////////////////////////////////////////////////////
+ // Delivering packages / raw data
+ ///////////////////////////////////////////////////////////////////////////
+
/**
* Delivers the given raw package data.
*
* @param $packageData Raw package data in an array
* @return void
*/
- private function deliverPackage (array $packageData) {
+ private function declareRawPackageData (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
}
/**
- * Sends a raw package out
+ * Delivers raw package data. In short, this will discover the raw socket
+ * resource through a discovery class (which will analyse the receipient of
+ * the package), register the socket with the connection (handler/helper?)
+ * instance and finally push the raw data on our outgoing queue.
*
* @param $packageData Raw package data in an array
* @return void
*/
- private function sendRawPackage (array $packageData) {
+ private function deliverRawPackageData (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
* @param $packageData Raw package data
* @return void
*/
- private function sendOutgoingPackage (array $packageData) {
+ private function sendOutgoingRawPackageData (array $packageData) {
// Get the right connection instance
$connectionInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($packageData);
- // Sent it away (we catch exceptions one method above
- $connectionInstance->sendRawPackageData($packageData);
+ // Is this connection still alive?
+ if ($connectionInstance->isShuttedDown()) {
+ // This connection is shutting down
+ // @TODO We may want to do somthing more here?
+ return;
+ } // END - if
+
+ // Sent it away (we catch exceptions one method above)
+ $sentBytes = $connectionInstance->sendRawPackageData($packageData);
+
+ // Remember unsent raw bytes in back-buffer, if any
+ $this->storeUnsentBytesInBackBuffer($packageData, $sentBytes);
}
/**
* "Enqueues" raw content into this delivery class by reading the raw content
* from given template instance and pushing it on the 'undeclared' stack.
*
- * @param $helperInstance A BaseHubHelper instance
+ * @param $helperInstance An instance of a BaseHubHelper class
+ * @param $nodeInstance An instance of a NodeHelper class
* @return void
*/
- public function enqueueRawDataFromTemplate (BaseHubHelper $helperInstance) {
+ public function enqueueRawDataFromTemplate (BaseHubHelper $helperInstance, NodeHelper $nodeInstance) {
// Get the raw content ...
$content = $helperInstance->getTemplateInstance()->getRawTemplateData();
// 3.) Tags
implode(self::PACKAGE_TAGS_SEPERATOR, $helperInstance->getPackageTags()),
// 4.) Checksum
- $this->getHashFromContent($content, $helperInstance)
+ $this->getHashFromContent($content, $helperInstance, $nodeInstance)
);
// Now prepare the temporary array and push it on the 'undeclared' stack
$this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
- 'sender' => $helperInstance->getNodeInstance()->getSessionId(),
- 'recipient' => self::NETWORK_TARGET_UPPER_HUBS,
+ 'sender' => $nodeInstance->getSessionId(),
+ 'recipient' => $helperInstance->getRecipientType(),
'content' => $content,
));
}
*
* @return $isWaitingDelivery Wether a package is waiting for delivery
*/
- public function isPackageWaitingDelivery () {
+ public function isPackageWaitingForDelivery () {
// Check wether the stacker is not empty
$isWaitingDelivery = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)));
// with the first one.
$packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_UNDECLARED);
- // Finally, deliver the package
- $this->deliverPackage($packageData);
+ // Declare the raw package data for delivery
+ $this->declareRawPackageData($packageData);
// And remove it finally
$this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
$packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
// And send it
- $this->sendRawPackage($packageData);
+ $this->deliverRawPackageData($packageData);
// And remove it finally
$this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
*
* @return void
*/
- public function sentWaitingPackage () {
+ public function sendWaitingPackage () {
+ // Send any waiting bytes in the back-buffer before sending a new package
+ $this->sendBackBufferBytes();
+
// Sanity check if we have packages waiting for delivery
- if (!$this->isPackageWaitingDelivery()) {
+ if (!$this->isPackageWaitingForDelivery()) {
// This is not fatal but should be avoided
- // @TODO Add some logging here
+ $this->debugOutput('PACKAGE: No package is waiting for delivery, but ' . __FUNCTION__ . ' was called.');
return;
} // END - if
// Get the package again
$packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_OUTGOING);
- // Now try to send it
try {
- $this->sendOutgoingPackage($packageData);
+ // Now try to send it
+ $this->sendOutgoingRawPackageData($packageData);
- // And remove it finally when it has been fully delivered
+ // And remove it finally
$this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
} catch (InvalidSocketException $e) {
// Output exception message
$this->debugOutput('PACKAGE: Package was not delivered: ' . $e->getMessage());
}
}
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Receiving packages / raw data
+ ///////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Checks wether new raw package data has arrived at a socket
+ *
+ * @return $hasArrived Wether new raw package data has arrived for processing
+ */
+ public function isNewRawDataPending () {
+ // @TODO Add some content here
+ }
+
+ /**
+ * Checks wether a new package has arrived
+ *
+ * @return $hasArrived Wether a new package has arrived for processing
+ */
+ public function isNewPackageArrived () {
+ // @TODO Add some content here
+ }
}
// [EOF]