]> git.mxchange.org Git - hub.git/commitdiff
Refactured sending encoded (raw) data to sockets to a stacker-based approach.
authorRoland Haeder <roland@mxchange.org>
Thu, 13 Mar 2014 22:23:10 +0000 (23:23 +0100)
committerRoland Haeder <roland@mxchange.org>
Thu, 13 Mar 2014 22:23:10 +0000 (23:23 +0100)
Signed-off-by: Roland Haeder <roland@mxchange.org>
13 files changed:
application/hub/config.php
application/hub/interfaces/helper/connections/class_ConnectionHelper.php
application/hub/interfaces/package/class_Deliverable.php
application/hub/main/helper/connection/class_BaseConnectionHelper.php
application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php
application/hub/main/nodes/class_BaseHubNode.php
application/hub/main/package/class_NetworkPackage.php
application/hub/main/states/node/active/class_NodeActiveState.php
application/hub/main/states/node/announced/class_NodeAnnouncedState.php [deleted file]
application/hub/main/states/node/announced/class_NodeAnnouncingState.php [new file with mode: 0644]
application/hub/main/states/node/class_BaseNodeState.php
application/hub/main/states/node/seniors/class_NodeReachableState.php
application/hub/main/tasks/network/class_NetworkPackageWriterTask.php

index 34719970b604a2e832c7e4065ab809f5e97dc2cc..f64c592fd3541144ae6c2ea344b313847ed328d4 100644 (file)
@@ -291,6 +291,9 @@ $cfg->setConfigEntry('node_request_node_list_entry_stacker_class', 'FiLoStacker'
 // CFG: DHT-STACKER-CLASS
 $cfg->setConfigEntry('dht_stacker_class', 'FiLoStacker');
 
+// CFG: RAW-DATA-STACKER-CLASS
+$cfg->setConfigEntry('raw_data_stacker_class', 'FiFoStacker');
+
 // CFG: NODE-ANNOUNCEMENT-ANSWER-TEMPLATE-TYPE
 $cfg->setConfigEntry('node_announcement_answer_template_type', 'xml/answer/announcement');
 
@@ -411,6 +414,9 @@ $cfg->setConfigEntry('stacker_node_object_registry_max_size', 100);
 // CFG: STACKER-RAW-DATA-MAX-SIZE
 $cfg->setConfigEntry('stacker_raw_data_max_size', 100);
 
+// CFG: STACKER-OUTGOING-STREAM-MAX-SIZE
+$cfg->setConfigEntry('stacker_outgoing_stream_max_size', 100);
+
 // CFG: STACKER-FINAL-CHUNKS-MAX-SIZE
 $cfg->setConfigEntry('stacker_final_chunks_max_size', 100);
 
@@ -789,8 +795,8 @@ $cfg->setConfigEntry('node_virgin_state_class', 'NodeVirginState');
 // CFG: NODE-ACTIVE-STATE-CLASS
 $cfg->setConfigEntry('node_active_state_class', 'NodeActiveState');
 
-// CFG: NODE-ANNOUNCED-STATE-CLASS
-$cfg->setConfigEntry('node_announced_state_class', 'NodeAnnouncedState');
+// CFG: NODE-ANNOUNCING-STATE-CLASS
+$cfg->setConfigEntry('node_announcing_state_class', 'NodeAnnouncingState');
 
 // CFG: NODE-REACHABLE-STATE-CLASS
 $cfg->setConfigEntry('node_reachable_state_class', 'NodeReachableState');
index 03386ffba1b9d5e8ec9dd33820056fa0ee1ebab3..f6530d87b5c096ebfed0c8263e786edd5b11dd6f 100644 (file)
@@ -27,7 +27,7 @@ interface ConnectionHelper extends Helper {
         * Sends raw package data to the recipient
         *
         * @param       $packageData            Raw package data
-        * @return      $totalSentBytes         Total sent bytes to the peer
+        * @return      void
         * @throws      InvalidSocketException  If we got a problem with this socket
         */
        function sendRawPackageData (array $packageData);
@@ -40,6 +40,44 @@ interface ConnectionHelper extends Helper {
         * @throws      SocketShutdownException         If the current socket could not be shut down
         */
        function doShutdown ();
+
+       /**
+        * Getter for port number to satify ProtocolHandler
+        *
+        * @return      $port   The port number
+        */
+       function getPort ();
+
+       /**
+        * Getter for protocol
+        *
+        * @return      $protocol       Used protocol
+        */
+       function getProtocol ();
+
+       /**
+        * Getter for IP address
+        *
+        * @return      $address        The IP address
+        */
+       function getAddress ();
+
+       /**
+        * Static "getter" for this connection class' name
+        *
+        * @param       $address        IP address
+        * @param       $port           Port number
+        * @param       $className      Original class name
+        * @return      $class          Expanded class name
+        */
+       static function getConnectionClassName ($address, $port, $className);
+
+       /**
+        * Getter for shuttedDown
+        *
+        * @return      $shuttedDown    Whether this connection is shutted down
+        */
+       function isShuttedDown ();
 }
 
 // [EOF]
index b4cb0ad0b600609e68b4626e93abc203015e7a8d..a8f6b791494f363146cb90e6016158793a590c9d 100644 (file)
@@ -54,6 +54,13 @@ interface Deliverable extends FrameworkInterface {
         */
        function isPackageWaitingForDelivery ();
 
+       /**
+        * Checks whether encoded (raw) data is pending
+        *
+        * @return      $isPending              Whether encoded data is pending
+        */
+       function isEncodedDataPending ();
+
        /**
         * Delivers an enqueued package to the stated destination. If a non-session
         * id is provided, recipient resolver is being asked (and instanced once).
@@ -82,6 +89,13 @@ interface Deliverable extends FrameworkInterface {
         */
        function sendWaitingPackage ();
 
+       /**
+        * Sends pending encoded (raw) data
+        *
+        * @return      void
+        */
+       function sendEncodedData ();
+
        /**
         * Clears all stacks
         *
index dd31138a1e76a024525454d745fc1ff35b77e56c..b58638b1adb010a810d85efe661820fa91869ed6 100644 (file)
@@ -60,11 +60,6 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
         */
        private $sentData = 0;
 
-       /**
-        * Difference
-        */
-       private $diff = 0;
-
        /**
         * Whether this connection is initialized
         */
@@ -95,14 +90,20 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                // Call parent constructor
                parent::__construct($className);
 
+               // Init state which sets the state to 'init'
+               $this->initState();
+
                // Initialize output stream
                $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
 
                // And add it to this connection helper
                $this->setOutputStreamInstance($streamInstance);
 
-               // Init state which sets the state to 'init'
-               $this->initState();
+               // Get package instance from factory
+               $packageInstance = NetworkPackageFactory::createNetworkPackageInstance();
+
+               // ... and set it here
+               $this->setPackageInstance($packageInstance);
 
                // Register this connection helper
                Registry::getRegistry()->addInstance('connection', $this);
@@ -374,7 +375,7 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
         * Sends raw package data to the recipient
         *
         * @param       $packageData            Raw package data
-        * @return      $totalSentBytes         Total sent bytes to the peer
+        * @return      void
         * @throws      InvalidSocketException  If we got a problem with this socket
         */
        public function sendRawPackageData (array $packageData) {
@@ -383,6 +384,8 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
 
                // Implode the package data array and fragement the resulting string, returns the final hash
                $finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageData, $this);
+
+               // Is the final hash set?
                if ($finalHash !== TRUE) {
                        // Debug message
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Setting finalHash=' . $finalHash . ',currentFinalHash[' . gettype($this->currentFinalHash) . ']=' . $this->currentFinalHash);
@@ -394,13 +397,9 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                // Reset serial number
                $this->getFragmenterInstance()->resetSerialNumber($this->currentFinalHash);
 
-               // Cache buffer length
-               $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
-
                // Init variables
                $rawData        = '';
                $dataStream     = ' ';
-               $totalSentBytes = 0;
 
                // Fill sending buffer with data
                while (strlen($dataStream) > 0) {
@@ -416,73 +415,30 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Registerable,
                // Nothing to sent is bad news, so assert on it
                assert(strlen($rawData) > 0);
 
+               // Calculate buffer size
+               $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
+
                // Encode the raw data with our output-stream
                $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: rawData()=' . strlen($rawData) . ',encodedData()=' . strlen($encodedData));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('HELPER[' . __METHOD__ . ':' . __LINE__ . ']: socketResource[]=' . gettype($this->getSocketResource()) . PHP_EOL);
+
+               // Init array
+               $encodedDataArray = array(
+                       NetworkPackage::RAW_FINAL_HASH_INDEX   => $this->currentFinalHash,
+                       NetworkPackage::RAW_ENCODED_DATA_INDEX => $encodedData,
+                       NetworkPackage::RAW_SENT_BYTES_INDEX   => 0,
+                       NetworkPackage::RAW_SOCKET_INDEX       => $this->getSocketResource(),
+                       NetworkPackage::RAW_BUFFER_SIZE_INDEX  => $bufferSize,
+                       NetworkPackage::RAW_DIFF_INDEX         => 0
+               );
 
                // Calculate difference
-               $this->diff = $bufferSize - strlen($encodedData);
-
-               // Get socket resource
-               $socketResource = $this->getSocketResource();
-
-               // Init sent bytes
-               $sentBytes = 0;
-
-               // Deliver all data
-               while ($sentBytes !== FALSE) {
-                       // And deliver it
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
-
-                       if ($this->diff >= 0) {
-                               // Send all out (encodedData is smaller than or equal buffer size)
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, ($bufferSize - $this->diff))));
-                               $sentBytes = socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
-                       } else {
-                               // Send buffer size out
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedData, 0, $bufferSize)));
-                               $sentBytes = socket_write($socketResource, $encodedData, $bufferSize);
-                       }
-
-                       // If there was an error, we don't continue here
-                       if ($sentBytes === FALSE) {
-                               // Handle the error with a faked recipientData array
-                               $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
-
-                               // And throw it
-                               throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
-                       } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
-                               // Nothing sent means we are done
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
-                               break;
-                       }
-
-                       // The difference between sent bytes and length of raw data should not go below zero
-                       assert((strlen($encodedData) - $sentBytes) >= 0);
-
-                       // Add total sent bytes
-                       $totalSentBytes += $sentBytes;
-
-                       // Cut out the last unsent bytes
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
-                       $encodedData = substr($encodedData, $sentBytes);
-
-                       // Calculate difference again
-                       $this->diff = $bufferSize - strlen($encodedData);
-
-                       // Can we abort?
-                       if (strlen($encodedData) <= 0) {
-                               // Abort here, all sent!
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
-                               break;
-                       } // END - if
-               } // END - while
+               $diff = $encodedDataArray[NetworkPackage::RAW_BUFFER_SIZE_INDEX] - strlen($encodedDataArray[NetworkPackage::RAW_ENCODED_DATA_INDEX]);
 
-               // Return sent bytes
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: totalSentBytes=' . $totalSentBytes . ',diff=' . $this->diff);
-               return $totalSentBytes;
+               // Push raw data to the package's outgoing stack
+               $this->getPackageInstance()->getStackerInstance()->pushNamed(NetworkPackage::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray);
        }
 
        /**
index 923b0731f604bce849f3308f43ec91400e32045f..4c7a6ab4ee3e6e6c69f4314038d1434f8515a19b 100644 (file)
@@ -77,6 +77,9 @@ class TcpConnectionHelper extends BaseConnectionHelper implements ConnectionHelp
                        throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_SOCKET_CREATION_FAILED);
                } // END - if
 
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Setting socket resource ... (' . gettype($socketResource) . ')');
+
                // Set the resource
                $helperInstance->setSocketResource($socketResource);
 
index 80e31ff2624b698c6b8fad6a121db1f2ea94cc7f..b32c3dc905b5b043415aafbb6f7cc20068acdbbe 100644 (file)
@@ -479,7 +479,7 @@ class BaseHubNode extends BaseHubSystem implements Updateable, AddableCriteria {
                $helperInstance->sendPackage($this);
 
                // Change the state, this should be the last line except debug output
-               $this->getStateInstance()->nodeAnnouncedToUpperHubs();
+               $this->getStateInstance()->nodeAnnouncingToUpperHubs();
 
                // Debug output
                self::createDebugInstance(__CLASS__)->debugOutput('HUB-Announcement: FINISHED');
index 3c8aa6590ad6d8979ce8ca9da251dabe20103c7c..7e599a4166907177aaa5aec6b3f3eec741b81df2 100644 (file)
@@ -208,13 +208,43 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        const STACKER_NAME_PROCESSED_MESSAGE = 'package_processed_message';
 
        /**************************************************************************
-        *                   Stacker for other/internal purposes                  *
+        *                      Stacker for raw data handling                     *
         **************************************************************************/
 
        /**
-        * Stacker name for "back-buffered" packages
+        * Stacker for outgoing data stream
         */
-       const STACKER_NAME_BACK_BUFFER = 'package_backbuffer';
+       const STACKER_NAME_OUTGOING_STREAM = 'outgoing_stream';
+
+       /**
+        * Array index for final hash
+        */
+       const RAW_FINAL_HASH_INDEX = 'hash';
+
+       /**
+        * Array index for encoded data
+        */
+       const RAW_ENCODED_DATA_INDEX = 'data';
+
+       /**
+        * Array index for sent bytes
+        */
+       const RAW_SENT_BYTES_INDEX = 'sent';
+
+       /**
+        * Array index for socket resource
+        */
+       const RAW_SOCKET_INDEX = 'socket';
+
+       /**
+        * Array index for buffer size
+        */
+       const RAW_BUFFER_SIZE_INDEX = 'buffer';
+
+       /**
+        * Array index for diff between buffer and sent bytes
+        */
+       const RAW_DIFF_INDEX = 'diff';
 
        /**************************************************************************
         *                            Protocol names                              *
@@ -291,7 +321,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        self::STACKER_NAME_DECODED_CHUNKED,
                        self::STACKER_NAME_NEW_MESSAGE,
                        self::STACKER_NAME_PROCESSED_MESSAGE,
-                       self::STACKER_NAME_BACK_BUFFER
+                       self::STACKER_NAME_OUTGOING_STREAM
                ), $forceReInit);
        }
 
@@ -487,17 +517,17 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: stateInstance=' . $helperInstance->getStateInstance());
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.');
 
-               // Is it not there?
-               if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) {
-                       // The socket needs to be put in a special registry that can handle such data
-                       $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
+               // The socket needs to be put in a special registry that can handle such data
+               $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
 
-                       // Get the connection helper from registry
-                       $helperInstance = Registry::getRegistry()->getInstance('connection');
+               // Get the connection helper from registry
+               $helperInstance = Registry::getRegistry()->getInstance('connection');
 
-                       // And make sure it is valid
-                       assert($helperInstance instanceof ConnectionHelper);
+               // And make sure it is valid
+               assert($helperInstance instanceof ConnectionHelper);
 
+               // Is it not there?
+               if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) {
                        // Debug message
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Registering socket ' . $socketResource . ' ...');
 
@@ -548,10 +578,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                } // END - if
 
                // Sent out package data
-               $sentBytes = $helperInstance->sendRawPackageData($packageData);
-
-               // Remember unsent raw bytes in back-buffer, if any
-               $this->storeUnsentBytesInBackBuffer($packageData, $sentBytes);
+               $helperInstance->sendRawPackageData($packageData);
        }
 
        /**
@@ -677,6 +704,19 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                return $isWaitingDelivery;
        }
 
+       /**
+        * Checks whether encoded (raw) data is pending
+        *
+        * @return      $isPending              Whether encoded data is pending
+        */
+       public function isEncodedDataPending () {
+               // Check whether the stacker is not empty
+               $isPending = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING_STREAM)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING_STREAM)));
+
+               // Return the result
+               return $isPending;
+       }
+
        /**
         * Delivers an enqueued package to the stated destination. If a non-session
         * id is provided, recipient resolver is being asked (and instanced once).
@@ -748,9 +788,6 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @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
@@ -776,6 +813,73 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                }
        }
 
+       /**
+        * Sends out encoded data to a socket
+        *
+        * @return      void
+        */
+       public function sendEncodedData () {
+               // Make sure there is pending encoded data
+               assert($this->isEncodedDataPending());
+
+               // Pop current data from stack
+               $encodedDataArray = $this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING_STREAM);
+
+               // Init in this round sent bytes
+               $sentBytes = 0;
+
+               // Assert on socket
+               assert(is_resource($encodedDataArray[self::RAW_SOCKET_INDEX]));
+
+               // And deliver it
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sending out ' . strlen($encodedDataArray[self::RAW_ENCODED_DATA_INDEX]) . ' bytes,rawBufferSize=' . $encodedDataArray[self::RAW_BUFFER_SIZE_INDEX] . ',diff=' . $encodedDataArray[self::RAW_DIFF_INDEX]);
+               if ($encodedDataArray[self::RAW_DIFF_INDEX] >= 0) {
+                       // Send all out (encodedData is smaller than or equal buffer size)
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedDataArray[self::RAW_ENCODED_DATA_INDEX], 0, ($encodedDataArray[self::RAW_BUFFER_SIZE_INDEX] - $encodedDataArray[self::RAW_DIFF_INDEX]))));
+                       $sentBytes = socket_write($encodedDataArray[self::RAW_SOCKET_INDEX], $encodedDataArray[self::RAW_ENCODED_DATA_INDEX], ($encodedDataArray[self::RAW_BUFFER_SIZE_INDEX] - $encodedDataArray[self::RAW_DIFF_INDEX]));
+               } else {
+                       // Send buffer size out
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: MD5=' . md5(substr($encodedDataArray[self::RAW_ENCODED_DATA_INDEX], 0, $encodedDataArray[self::RAW_BUFFER_SIZE_INDEX])));
+                       $sentBytes = socket_write($encodedDataArray[self::RAW_SOCKET_INDEX], $encodedDataArray[self::RAW_ENCODED_DATA_INDEX], $encodedDataArray[self::RAW_BUFFER_SIZE_INDEX]);
+               }
+
+               // If there was an error, we don't continue here
+               if ($sentBytes === FALSE) {
+                       // Handle the error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $encodedDataArray[self::RAW_SOCKET_INDEX], array('0.0.0.0', '0'));
+
+                       // And throw it
+                       throw new InvalidSocketException(array($this, $encodedDataArray[self::RAW_SOCKET_INDEX], $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+               } elseif (($sentBytes === 0) && (strlen($encodedDataArray[self::RAW_ENCODED_DATA_INDEX]) > 0)) {
+                       // Nothing sent means we are done
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
+                       return;
+               } else {
+                       // The difference between sent bytes and length of raw data should not go below zero
+                       assert((strlen($encodedDataArray[self::RAW_ENCODED_DATA_INDEX]) - $sentBytes) >= 0);
+
+                       // Add total sent bytes
+                       $encodedDataArray[self::RAW_SENT_BYTES_INDEX] += $sentBytes;
+
+                       // Cut out the last unsent bytes
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: Sent out ' . $sentBytes . ' of ' . strlen($encodedDataArray[self::RAW_ENCODED_DATA_INDEX]) . ' bytes ...');
+                       $encodedDataArray[self::RAW_ENCODED_DATA_INDEX] = substr($encodedDataArray[self::RAW_ENCODED_DATA_INDEX], $sentBytes);
+
+                       // Calculate difference again
+                       $encodedDataArray[self::RAW_DIFF_INDEX] = $encodedDataArray[self::RAW_BUFFER_SIZE_INDEX] - strlen($encodedDataArray[self::RAW_ENCODED_DATA_INDEX]);
+
+                       // Can we abort?
+                       if (strlen($encodedDataArray[self::RAW_ENCODED_DATA_INDEX]) <= 0) {
+                               // Abort here, all sent!
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONNECTION-HELPER[' . __METHOD__ . ':' . __LINE__ . ']: All sent! (LINE=' . __LINE__ . ')');
+                               return;
+                       } // END - if
+               }
+
+               // Push array back in stack
+               $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray);
+       }
+
        ///////////////////////////////////////////////////////////////////////////
        //                   Receiving packages / raw data
        ///////////////////////////////////////////////////////////////////////////
index 3b2cb250a095bdd36aeceeea8c2c98558ec60e09..985f2b0884b9cd70364b67bfaa7c3fd9a4249a53 100644 (file)
@@ -60,9 +60,9 @@ class NodeActiveState extends BaseNodeState implements Stateable {
         *
         * @return      void
         */
-       public function nodeAnnouncedToUpperHubs () {
+       public function nodeAnnouncingToUpperHubs () {
                // Create the new state instance
-               NodeStateFactory::createNodeStateInstanceByName('announced', $this->getNodeInstance());
+               NodeStateFactory::createNodeStateInstanceByName('announcing', $this->getNodeInstance());
        }
 
        /**
diff --git a/application/hub/main/states/node/announced/class_NodeAnnouncedState.php b/application/hub/main/states/node/announced/class_NodeAnnouncedState.php
deleted file mode 100644 (file)
index 89e8e0c..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-<?php
-/**
- * A Announced node state class
- *
- * @author             Roland Haeder <webmaster@shipsimu.org>
- * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
- * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.shipsimu.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 NodeAnnouncedState extends BaseNodeState implements Stateable {
-       /**
-        * Protected constructor
-        *
-        * @return      void
-        */
-       protected function __construct () {
-               // Call parent constructor
-               parent::__construct(__CLASS__);
-
-               // Set state name
-               $this->setStateName('announced');
-       }
-
-       /**
-        * Creates an instance of this class
-        *
-        * @param       $nodeInstance   An instance of a NodeHelper class
-        * @return      $stateInstance  An instance of a Stateable class
-        */
-       public static final function createNodeAnnouncedState (NodeHelper $nodeInstance) {
-               // Get new instance
-               $stateInstance = new NodeAnnouncedState();
-
-               // Set the node instance
-               $stateInstance->setNodeInstance($nodeInstance);
-
-               // Return the prepared instance
-               return $stateInstance;
-       }
-
-       /**
-        * Changes the state if the announcement (to bootstrap node) was
-        * successful.
-        *
-        * @return      void
-        */
-       public function nodeAnnouncementSuccessful () {
-               // The node's announcement was successful
-               NodeStateFactory::createNodeStateInstanceByName('announcement_completed', $this->getNodeInstance());
-       }
-}
-
-// [EOF]
-?>
diff --git a/application/hub/main/states/node/announced/class_NodeAnnouncingState.php b/application/hub/main/states/node/announced/class_NodeAnnouncingState.php
new file mode 100644 (file)
index 0000000..cb8bb08
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/**
+ * A Announcing node state class
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.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 NodeAnnouncingState extends BaseNodeState implements Stateable {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+
+               // Set state name
+               $this->setStateName('announcing');
+       }
+
+       /**
+        * Creates an instance of this class
+        *
+        * @param       $nodeInstance   An instance of a NodeHelper class
+        * @return      $stateInstance  An instance of a Stateable class
+        */
+       public static final function createNodeAnnouncingState (NodeHelper $nodeInstance) {
+               // Get new instance
+               $stateInstance = new NodeAnnouncingState();
+
+               // Set the node instance
+               $stateInstance->setNodeInstance($nodeInstance);
+
+               // Return the prepared instance
+               return $stateInstance;
+       }
+
+       /**
+        * Changes the state if the announcement (to bootstrap node) was
+        * successful.
+        *
+        * @return      void
+        */
+       public function nodeAnnouncementSuccessful () {
+               // The node's announcement was successful
+               NodeStateFactory::createNodeStateInstanceByName('announcement_completed', $this->getNodeInstance());
+       }
+}
+
+// [EOF]
+?>
index 6fd35a734443199265182f66948421079965fff6..3f3974dd6397ed2d861ec3ef6e806e8d317019d9 100644 (file)
@@ -49,11 +49,11 @@ class BaseNodeState extends BaseState {
        }
 
        /**
-        * Validates whether the state is 'active' or 'announced' or throws an
+        * Validates whether the state is 'active' or 'announcing' or throws an
         * exception if it is every other state.
         *
         * @return      void
-        * @throws      InvalidStateException   If the state is not 'active' and not 'announced'
+        * @throws      InvalidStateException   If the state is not 'active' and not 'announcing'
         */
        public function validateNodeStateIsActiveOrAnnounced () {
                // Just compare it...
@@ -83,7 +83,7 @@ class BaseNodeState extends BaseState {
         * exception if it is every other state.
         *
         * @return      void
-        * @throws      InvalidStateException   If the state is not 'active' and not 'announced'
+        * @throws      InvalidStateException   If the state is not 'active' and not 'announcing'
         */
        public function validateNodeStateIsAnnouncementCompleted () {
                // Just compare it...
index 4dadd5d39e6e6885ece0dd125b859af6e25678df..65a4f3e3066417066d8e26950f245ab1d922c378 100644 (file)
@@ -60,9 +60,9 @@ class NodeReachableState extends BaseNodeState implements Stateable {
         *
         * @return      void
         */
-       public function nodeAnnouncedToUpperHubs () {
+       public function nodeAnnouncingToUpperHubs () {
                // Create the new state instance
-               NodeStateFactory::createNodeStateInstanceByName('announced', $this->getNodeInstance());
+               NodeStateFactory::createNodeStateInstanceByName('announcing', $this->getNodeInstance());
        }
 }
 
index bad9c02a9721271f93ebcd71ec10b30d037898f6..49a9d2d845f415d7f7b7a5bcb85b676de239221e 100644 (file)
@@ -73,7 +73,10 @@ class NetworkPackageWriterTask extends BaseTask implements Taskable, Visitable {
                $packageInstance = $this->getPackageInstance();
 
                // Do we have something to deliver?
-               if ($packageInstance->isPackageWaitingForDelivery()) {
+               if ($packageInstance->isEncodedDataPending()) {
+                       // Sent encoded (raw) data
+                       $packageInstance->sendEncodedData();
+               } elseif ($packageInstance->isPackageWaitingForDelivery()) {
                        // Sent it finally out
                        $packageInstance->sendWaitingPackage();
                } elseif ($packageInstance->isPackageDeclared()) {