X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fpackage%2Fclass_NetworkPackage.php;h=9a4ce15df1299ad6ad4c3ec5ec91a52ea0435269;hb=d391ecd222ae868265735539d68a2c148711b5e3;hp=165e74ecc248e9551cc7564b00f36ca47468b742;hpb=f33fd598a1a77034720172ea80353e4d4c0f9662;p=hub.git diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 165e74ecc..9a4ce15df 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -1,9 +1,10 @@ * @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 @@ -34,13 +35,14 @@ * 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, 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'; @@ -104,6 +106,11 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe */ const NETWORK_TARGET_SELF = 'self'; + /** + * TCP package size in bytes + */ + const TCP_PACKAGE_SIZE = 512; + /** * Protected constructor * @@ -115,7 +122,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe // 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); @@ -142,16 +149,17 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe * "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() ); @@ -160,13 +168,17 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe 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 @@ -204,12 +216,15 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe } /** - * 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 @@ -249,7 +264,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe * @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); @@ -260,7 +275,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe return; } // END - if - // Sent it away (we catch exceptions one method above + // Sent it away (we catch exceptions one method above) $sentBytes = $connectionInstance->sendRawPackageData($packageData); // Remember unsent raw bytes in back-buffer, if any @@ -271,10 +286,11 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe * "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(); @@ -290,12 +306,12 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe // 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(), + 'sender' => $nodeInstance->getSessionId(), 'recipient' => $helperInstance->getRecipientType(), 'content' => $content, )); @@ -332,7 +348,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe * * @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))); @@ -362,8 +378,8 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe // 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); @@ -388,7 +404,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe $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); @@ -400,13 +416,13 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe * @return void */ public function sendWaitingPackage () { - // Sent any waiting bytes in the back-buffer + // 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 @@ -415,8 +431,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe try { // Now try to send it - $this->sendOutgoingPackage($packageData); - die("O!\n"); + $this->sendOutgoingRawPackageData($packageData); // And remove it finally $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING); @@ -425,6 +440,28 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe $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]