]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/class_NetworkPackage.php
Removed old-lost comments, moved two class instances around:
[hub.git] / application / hub / main / package / class_NetworkPackage.php
index ddc95fe874eb8336a00ac4f3a535b2c74cdc6933..bdadfa7f8a4f296f04a2dbb4d4d64c58207fd1e0 100644 (file)
@@ -1,9 +1,10 @@
 <?php
 /**
- * A NetworkPackage class. This class implements Deliverable 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
@@ -34,7 +35,7 @@
  * 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:
         * 0: Compressor extension
@@ -50,6 +51,11 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
         */
        const PACKAGE_MASK_SEPERATOR = ':';
 
+       /**
+        * Seperator for checksum
+        */
+       const PACKAGE_CHECKSUM_SEPERATOR = ':';
+
        /**
         * Array indexes for above mask, start with zero
         */
@@ -65,6 +71,13 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
        const INDEX_PACKAGE_RECIPIENT = 1;
        const INDEX_PACKAGE_CONTENT   = 2;
 
+       /**
+        * Named array elements for package data
+        */
+       const PACKAGE_DATA_RECIPIENT = 'recipient';
+       const PACKAGE_DATA_SENDER    = 'sender';
+       const PACKAGE_DATA_CONTENT   = 'content';
+
        /**
         * Tags seperator
         */
@@ -105,6 +118,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
         *
@@ -146,15 +164,16 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
         * @param       $helperInstance         An instance of a BaseHubHelper class
         * @param       $nodeInstance           An instance of a NodeHelper class
         * @return      $hash   Hash for given package content
+        * @todo        $helperInstance is unused
         */
        private function getHashFromContent ($content, BaseHubHelper $helperInstance, NodeHelper $nodeInstance) {
                // Create the hash
-               // @TODO crc32 is not good, but it needs to be fast
+               // @TODO crc32 is not very strong, but it needs to be fast
                $hash = crc32(
                        $content .
-                       ':' .
+                       self::PACKAGE_CHECKSUM_SEPERATOR .
                        $nodeInstance->getSessionId() .
-                       ':' .
+                       self::PACKAGE_CHECKSUM_SEPERATOR .
                        $this->getCompressorInstance()->getCompressorExtension()
                );
 
@@ -162,6 +181,10 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
                return $hash;
        }
 
+       ///////////////////////////////////////////////////////////////////////////
+       //                   Delivering packages / raw data
+       ///////////////////////////////////////////////////////////////////////////
+
        /**
         * Delivers the given raw package data.
         *
@@ -192,7 +215,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
                        $this->debugOutput('PACKAGE: Package declared for recipient ' . $currentRecipient);
 
                        // Set the recipient
-                       $packageData['recipient'] = $currentRecipient;
+                       $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient;
 
                        // And enqueue it to the writer class
                        $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData);
@@ -265,7 +288,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
@@ -301,9 +324,9 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
 
                // Now prepare the temporary array and push it on the 'undeclared' stack
                $this->getStackerInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
-                       'sender'    => $nodeInstance->getSessionId(),
-                       'recipient' => $helperInstance->getRecipientType(),
-                       'content'   => $content,
+                       self::PACKAGE_DATA_SENDER    => $nodeInstance->getSessionId(),
+                       self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(),
+                       self::PACKAGE_DATA_CONTENT   => $content,
                ));
        }
 
@@ -412,7 +435,7 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
                // 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.');
+                       $this->debugOutput('PACKAGE: No package is waiting for delivery, but ' . __METHOD__ . ' was called.');
                        return;
                } // END - if
 
@@ -422,7 +445,6 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
                try {
                        // Now try to send it
                        $this->sendOutgoingRawPackageData($packageData);
-                       die("O!\n");
 
                        // And remove it finally
                        $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
@@ -431,6 +453,30 @@ 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
+        *
+        * @param       $poolInstance   An instance of a PoolableListener class
+        * @return      $hasArrived             Wether new raw package data has arrived for processing
+        */
+       public function isNewRawDataPending (PoolableListener $poolInstance) {
+               // @TODO Add some content here
+               $this->partialStub('Do something here. poolInstance=' . $poolInstance->__toString());
+       }
+
+       /**
+        * 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]