]> 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 c7e37ab8c4c6a49b573f033dcf05d4360cd17b8d..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
@@ -11,7 +12,7 @@
  * 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>
  * 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';
 
@@ -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
         *
@@ -161,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
@@ -205,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
@@ -250,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);
 
@@ -261,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
@@ -334,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)));
 
@@ -364,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);
@@ -390,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);
@@ -402,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
 
@@ -417,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);
@@ -427,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]