]> git.mxchange.org Git - hub.git/commitdiff
Introduced package fragmenter class to fragment network packages into smaller chunks
authorRoland Häder <roland@mxchange.org>
Sun, 13 Feb 2011 15:19:06 +0000 (15:19 +0000)
committerRoland Häder <roland@mxchange.org>
Sun, 13 Feb 2011 15:19:06 +0000 (15:19 +0000)
.gitattributes
application/hub/config.php
application/hub/exceptions.php
application/hub/interfaces/package/class_Deliverable.php
application/hub/interfaces/package/fragmenter/.htaccess [new file with mode: 0644]
application/hub/interfaces/package/fragmenter/class_Fragmentable.php [new file with mode: 0644]
application/hub/main/helper/connection/class_BaseConnectionHelper.php
application/hub/main/package/class_NetworkPackage.php
application/hub/main/package/fragmenter/.htaccess [new file with mode: 0644]
application/hub/main/package/fragmenter/class_PackageFragmenter.php [new file with mode: 0644]
application/hub/main/tasks/network/class_NetworkPackageWriterTask.php

index 35087cf2c6cddb53c386fa74529015cb5608a4c8..94b2b6a807ad711ad77e0667a819f70f1cb29106 100644 (file)
@@ -54,6 +54,8 @@ application/hub/interfaces/nodes/.htaccess -text
 application/hub/interfaces/nodes/class_NodeHelper.php -text svneol=unset#text/plain
 application/hub/interfaces/package/.htaccess -text
 application/hub/interfaces/package/class_Deliverable.php -text svneol=unset#text/plain
+application/hub/interfaces/package/fragmenter/.htaccess -text
+application/hub/interfaces/package/fragmenter/class_Fragmentable.php -text
 application/hub/interfaces/pool/.htaccess -text
 application/hub/interfaces/pool/class_Poolable.php -text svneol=unset#text/plain
 application/hub/interfaces/pool/listener/.htaccess -text
@@ -284,6 +286,8 @@ application/hub/main/nodes/regular/.htaccess -text
 application/hub/main/nodes/regular/class_HubRegularNode.php -text svneol=unset#text/plain
 application/hub/main/package/.htaccess -text
 application/hub/main/package/class_NetworkPackage.php -text svneol=unset#text/plain
+application/hub/main/package/fragmenter/.htaccess -text
+application/hub/main/package/fragmenter/class_PackageFragmenter.php -text
 application/hub/main/pools/.htaccess -text
 application/hub/main/pools/class_ -text svneol=unset#text/plain
 application/hub/main/pools/class_BasePool.php -text svneol=unset#text/plain
index 68a1b48905b8a8bf331dcc9b222d5bbe78bed86b..369f43e9b74e5442968c60445e12f021786af8b8 100644 (file)
@@ -456,5 +456,8 @@ $cfg->setConfigEntry('session_id', '');
 // CFG: EXTERNAL-IP
 $cfg->setConfigEntry('external_ip', 'free-search.homelinux.org');
 
+// CFG: PACKAGE-FRAGMENTER-CLASS
+$cfg->setConfigEntry('package_fragmenter_class', 'PackageFragmenter');
+
 // [EOF]
 ?>
index 3e621fa94200ef54a237c33305758ab592507d29..62fb49cb9c3fde1e46fdefaa2f325a62afa224ef 100644 (file)
@@ -94,8 +94,48 @@ Backtrace:
        }
 }
 
+// Error handler
+function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) {
+       // Construct the message
+       $message = sprintf("File: %s, Line: %s, Code: %s, Message: %s",
+               basename($errfile),
+               $errline,
+               $errno,
+               $errstr
+       );
+
+       // Throw an exception here
+       throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
+} // END - function
+
+// Assertion handler
+function __assertHandler ($file, $line, $code) {
+       // Empty code?
+       if ($code === '') $code = '<em>Unknown</em>';
+
+       // Create message
+       $message = sprintf("File: %s, Line: %s, Code: %s",
+               basename($file),
+               $line,
+               $code
+       );
+
+       // Throw an exception here
+       throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
+} // END - function
+
+// Set error handler
+//set_error_handler('__errorHandler');
+
 // Set the new handler
 set_exception_handler('hub_exception_handler');
 
+// Init assert handling
+assert_options(ASSERT_ACTIVE,     1);
+assert_options(ASSERT_WARNING,    0);
+assert_options(ASSERT_BAIL,       0);
+assert_options(ASSERT_QUIET_EVAL, 0);
+assert_options(ASSERT_CALLBACK,   '__assertHandler');
+
 // [EOF]
 ?>
index cdf483346ba0c625717f8d08ab3e8620f61585a0..65c70c4aea1b28784c18be396e67e051059ea76a 100644 (file)
@@ -78,7 +78,7 @@ interface Deliverable extends FrameworkInterface {
         *
         * @return      void
         */
-       function sentWaitingPackage ();
+       function sendWaitingPackage ();
 }
 
 // [EOF]
diff --git a/application/hub/interfaces/package/fragmenter/.htaccess b/application/hub/interfaces/package/fragmenter/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/application/hub/interfaces/package/fragmenter/class_Fragmentable.php b/application/hub/interfaces/package/fragmenter/class_Fragmentable.php
new file mode 100644 (file)
index 0000000..2b72b3a
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An interface for a package fragmenter
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+interface Fragmentable extends FrameworkInterface {
+}
+
+// [EOF]
+?>
index b2b1d8121556854e84f2f3528c54b112c5069fb7..a5d427bde97e3579ac6fa5cf8356d5ec75559cdd 100644 (file)
@@ -131,7 +131,7 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
        /**
         * "Accept" a visitor by simply calling it back
         *
-        * @param       $visitorInstance        A Visitable instance
+        * @param       $visitorInstance        A Visitor instance
         * @return      void
         */
        protected final function accept (Visitor $visitorInstance) {
@@ -139,25 +139,41 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                $visitorInstance->visitConnectionHelper($this);
        }
 
+       /**
+        * "Getter" for raw package data from a package array. This method does
+        * honor any unsend bytes in the back-buffer and the sending buffer size.
+        *
+        * @param       $packageData    Raw package data array
+        * @return      $rawData                Raw package data bytes
+        */
+       private function getRawDataFromPackageArray (array $packageData) {
+               // Get the fragmenter instance
+               $fragmenterInstance = ObjectFactory::createObjectByConfiguredName('package_fragmenter_class');
+               $fragmenterInstance->debugInstance();
+
+               // Return it
+               return $rawData;
+       }
+
        /**
         * Sends raw package data to the recipient
         *
         * @param       $packageData    Raw package data
-        * @return      void
-        *@ throws InvalidSocketException       If we got a problem with this socket
+        * @return      $sentBytes              Actual sent bytes to the peer
+        * @throws      InvalidSocketException  If we got a problem with this socket
         */
        public function sendRawPackageData (array $packageData) {
                // We need to "package" all data. This is done by a implode()
-               $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPERATOR, $packageData);
+               $rawData = $this->getRawDataFromPackageArray($packageData);
 
                // Get socket resource
                $socketResource = $this->getSocketResource();
 
                // And deliver it
-               $numBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
+               $sentBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
 
                // If there was an error, we don't continue here
-               if ($numBytes === false) {
+               if ($sentBytes === false) {
                        // Get socket error code for verification
                        $socketError = socket_last_error($socketResource);
 
@@ -171,8 +187,11 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc
                        throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
-               // How much has been sent?
-               die('num=' . $numBytes . "\n");
+               // The difference between sent bytes and length of raw data should not be below zero
+               assert((strlen($rawData) - $sentBytes) >= 0);
+
+               // Return sent bytes
+               return $sentBytes;
        }
 
        /**
index dd634f3adfe84492f6467779c14da3ee32713cc9..323dba060d5854601be8f64adf85748f561245a0 100644 (file)
@@ -89,6 +89,11 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
         */
        const STACKER_NAME_OUTGOING = 'outgoing';
 
+       /**
+        * Stacker name for "back-buffered" packages
+        */
+       const STACKER_NAME_BACK_BUFFER = 'backbuffer';
+
        /**
         * Network target (alias): 'upper hubs'
         */
@@ -256,7 +261,10 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
                } // END - if
 
                // Sent it away (we catch exceptions one method above
-               $connectionInstance->sendRawPackageData($packageData);
+               $sentBytes = $connectionInstance->sendRawPackageData($packageData);
+
+               // Remember unsent raw bytes in back-buffer, if any
+               $this->storeUnsentBytesInBackBuffer($packageData, $sentBytes);
        }
 
        /**
@@ -391,7 +399,10 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
         *
         * @return      void
         */
-       public function sentWaitingPackage () {
+       public function sendWaitingPackage () {
+               // Sent any waiting bytes in the back-buffer
+               $this->sendBackBufferBytes();
+
                // Sanity check if we have packages waiting for delivery
                if (!$this->isPackageWaitingDelivery()) {
                        // This is not fatal but should be avoided
@@ -402,11 +413,12 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registe
                // Get the package again
                $packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_OUTGOING);
 
-               // Now try to send it
                try {
+                       // Now try to send it
                        $this->sendOutgoingPackage($packageData);
+                       die("O!\n");
 
-                       // 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
diff --git a/application/hub/main/package/fragmenter/.htaccess b/application/hub/main/package/fragmenter/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/application/hub/main/package/fragmenter/class_PackageFragmenter.php b/application/hub/main/package/fragmenter/class_PackageFragmenter.php
new file mode 100644 (file)
index 0000000..f19ac25
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+/**
+ * A PackageFragmenter class to fragment package data into smaller chunks for
+ * delivery. This class does add a serial number to it and in the first data
+ * submission chunk it will add a sumerization of all fragements and their
+ * serial numbers.
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * 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 PackageFragmenter extends BaseFrameworkSystem implements Fragmentable {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this class
+        *
+        * @return      $fragmenterInstance             An instance of a Fragmentable class
+        */
+       public final static function createPackageFragmenter () {
+               // Get new instance
+               $fragmenterInstance = new PackageFragmenter();
+
+               // Return the prepared instance
+               return $fragmenterInstance;
+       }
+}
+
+// [EOF]
+?>
index 15b974f967d8b848b9e2aa6c78025c11468480c0..946cf77bd42fe375366de0015ef7e73241514f57 100644 (file)
@@ -74,7 +74,7 @@ class NetworkPackageWriterTask extends BaseTask implements Taskable, Visitable {
                        $packageInstance->deliverDeclaredPackage();
                } elseif ($packageInstance->isPackageWaitingDelivery()) {
                        // Sent it finally out
-                       $packageInstance->sentWaitingPackage();
+                       $packageInstance->sendWaitingPackage();
                }
        }
 }