From: Roland Haeder Date: Thu, 13 Mar 2014 22:23:10 +0000 (+0100) Subject: Refactured sending encoded (raw) data to sockets to a stacker-based approach. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3029690e46509a98df2a2ce0ba73daadf7d6d787;p=hub.git Refactured sending encoded (raw) data to sockets to a stacker-based approach. Signed-off-by: Roland Haeder --- diff --git a/application/hub/config.php b/application/hub/config.php index 34719970b..f64c592fd 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -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'); diff --git a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php index 03386ffba..f6530d87b 100644 --- a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php +++ b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php @@ -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] diff --git a/application/hub/interfaces/package/class_Deliverable.php b/application/hub/interfaces/package/class_Deliverable.php index b4cb0ad0b..a8f6b7914 100644 --- a/application/hub/interfaces/package/class_Deliverable.php +++ b/application/hub/interfaces/package/class_Deliverable.php @@ -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 * diff --git a/application/hub/main/helper/connection/class_BaseConnectionHelper.php b/application/hub/main/helper/connection/class_BaseConnectionHelper.php index dd31138a1..b58638b1a 100644 --- a/application/hub/main/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/main/helper/connection/class_BaseConnectionHelper.php @@ -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); } /** diff --git a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php index 923b0731f..4c7a6ab4e 100644 --- a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php +++ b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php @@ -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); diff --git a/application/hub/main/nodes/class_BaseHubNode.php b/application/hub/main/nodes/class_BaseHubNode.php index 80e31ff26..b32c3dc90 100644 --- a/application/hub/main/nodes/class_BaseHubNode.php +++ b/application/hub/main/nodes/class_BaseHubNode.php @@ -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'); diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 3c8aa6590..7e599a416 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -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 /////////////////////////////////////////////////////////////////////////// diff --git a/application/hub/main/states/node/active/class_NodeActiveState.php b/application/hub/main/states/node/active/class_NodeActiveState.php index 3b2cb250a..985f2b088 100644 --- a/application/hub/main/states/node/active/class_NodeActiveState.php +++ b/application/hub/main/states/node/active/class_NodeActiveState.php @@ -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 index 89e8e0c9d..000000000 --- a/application/hub/main/states/node/announced/class_NodeAnnouncedState.php +++ /dev/null @@ -1,68 +0,0 @@ - - * @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 . - */ -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 index 000000000..cb8bb0828 --- /dev/null +++ b/application/hub/main/states/node/announced/class_NodeAnnouncingState.php @@ -0,0 +1,68 @@ + + * @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 . + */ +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] +?> diff --git a/application/hub/main/states/node/class_BaseNodeState.php b/application/hub/main/states/node/class_BaseNodeState.php index 6fd35a734..3f3974dd6 100644 --- a/application/hub/main/states/node/class_BaseNodeState.php +++ b/application/hub/main/states/node/class_BaseNodeState.php @@ -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... diff --git a/application/hub/main/states/node/seniors/class_NodeReachableState.php b/application/hub/main/states/node/seniors/class_NodeReachableState.php index 4dadd5d39..65a4f3e30 100644 --- a/application/hub/main/states/node/seniors/class_NodeReachableState.php +++ b/application/hub/main/states/node/seniors/class_NodeReachableState.php @@ -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()); } } diff --git a/application/hub/main/tasks/network/class_NetworkPackageWriterTask.php b/application/hub/main/tasks/network/class_NetworkPackageWriterTask.php index bad9c02a9..49a9d2d84 100644 --- a/application/hub/main/tasks/network/class_NetworkPackageWriterTask.php +++ b/application/hub/main/tasks/network/class_NetworkPackageWriterTask.php @@ -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()) {