]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/class_NetworkPackage.php
Added a new task for listener pools and network package readers (for abstract Network...
[hub.git] / application / hub / main / package / class_NetworkPackage.php
index f47e10b4fad7315264008e6e88b3c3e6cea376f1..9a4ce15df1299ad6ad4c3ec5ec91a52ea0435269 100644 (file)
@@ -1,9 +1,10 @@
 <?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 {
+class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receivable, Registerable {
        /**
-        * Package mask for compressing package data
+        * Package mask for compressing package data:
+        * 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';
+       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;
+
+       /**
+        * Array indexes for raw package array
+        */
+       const INDEX_PACKAGE_SENDER    = 0;
+       const INDEX_PACKAGE_RECIPIENT = 1;
+       const INDEX_PACKAGE_CONTENT   = 2;
+
+       /**
+        * 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';
+
+       /**
+        * 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
@@ -51,7 +122,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable {
 
                // 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);
@@ -63,45 +134,334 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable {
         * @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();
 
-               // 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;
        }
 
        /**
-        * "Queues" raw content into this delivery class by reading the raw content
+        * "Getter" for hash from given content and helper instance
+        *
+        * @param       $content        Raw package content
+        * @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, NodeHelper $nodeInstance) {
+               // Create the hash
+               // @TODO crc32 is not good, but it needs to be fast
+               $hash = crc32(
+                       $content .
+                       ':' .
+                       $nodeInstance->getSessionId() .
+                       ':' .
+                       $this->getCompressorInstance()->getCompressorExtension()
+               );
+
+               // And return it
+               return $hash;
+       }
+
+       ///////////////////////////////////////////////////////////////////////////
+       //                   Delivering packages / raw data
+       ///////////////////////////////////////////////////////////////////////////
+
+       /**
+        * Delivers the given raw package data.
+        *
+        * @param       $packageData    Raw package data in an array
+        * @return      void
+        */
+       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
+                * 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();
+       }
+
+       /**
+        * 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 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
+                * 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 sendOutgoingRawPackageData (array $packageData) {
+               // Get the right connection instance
+               $connectionInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($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       $templateInstance       A CompileableTemplate instance
+        * @param       $helperInstance         An instance of a  BaseHubHelper class
+        * @param       $nodeInstance           An instance of a NodeHelper class
         * @return      void
         */
-       public function queueRawDataFromTemplate (CompileableTemplate $templateInstance) {
-               // Get the raw content and compress it
-               $content = $this->getCompressorInstance()->compressStream($templateInstance->getRawTemplateData());
+       public function enqueueRawDataFromTemplate (BaseHubHelper $helperInstance, NodeHelper $nodeInstance) {
+               // Get the raw content ...
+               $content = $helperInstance->getTemplateInstance()->getRawTemplateData();
+
+               // ... 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,
+                       // 1.) Compressor's extension
                        $this->getCompressorInstance()->getCompressorExtension(),
+                       // 2.) Raw package content, encoded with BASE64
                        base64_encode($content),
-                       crc32($content) // Not so good, but needs to be fast!
+                       // 3.) Tags
+                       implode(self::PACKAGE_TAGS_SEPERATOR, $helperInstance->getPackageTags()),
+                       // 4.) Checksum
+                       $this->getHashFromContent($content, $helperInstance, $nodeInstance)
                );
 
                // Now prepare the temporary array and push it on the 'undeclared' stack
-               $this->getStackerInstance()->pushNamed('undeclared', array(
-                       'sender'    => null,
-                       'recipient' => null,
-                       'content'   => $content
+               $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
+                       'sender'    => $nodeInstance->getSessionId(),
+                       'recipient' => $helperInstance->getRecipientType(),
+                       'content'   => $content,
                ));
        }
+
+       /**
+        * Checks wether a package has been enqueued for delivery.
+        *
+        * @return      $isEnqueued             Wether a package is enqueued
+        */
+       public function isPackageEnqueued () {
+               // Check wether the stacker is not empty
+               $isEnqueued = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED)));
+
+               // Return the result
+               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 isPackageWaitingForDelivery () {
+               // 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).
+        * This allows that a single package is being delivered to multiple targets
+        * without enqueueing it for every target. If no target is provided or it
+        * can't be determined a NoTargetException is being thrown.
+        *
+        * @return      void
+        * @throws      NoTargetException       If no target can't be determined
+        */
+       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
+                       // @TODO Add some logging here
+                       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);
+
+               // Declare the raw package data for delivery
+               $this->declareRawPackageData($packageData);
+
+               // 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->deliverRawPackageData($packageData);
+
+               // And remove it finally
+               $this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
+       }
+
+       /**
+        * Sends waiting packages out for delivery
+        *
+        * @return      void
+        */
+       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->isPackageWaitingForDelivery()) {
+                       // This is not fatal but should be avoided
+                       $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);
+
+               try {
+                       // Now try to send it
+                       $this->sendOutgoingRawPackageData($packageData);
+
+                       // 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]