From 69e7b3de7c369cd0ddc74c75d319055cbf4a3a04 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 18 Apr 2011 18:25:40 +0000 Subject: [PATCH] New singleton-factories introduced: - Two new registry-based singleton-factories introduced: + The FragmenterFactory produces singleton fragmenter instances + The ProducerFactory produces singleton producer instance of different types - Rewritten the code that Base64 encoding is now transparently implemented around the socket_write/read() functions. So all data is now being transparently transfered to other peers in Base64 encoding. - Further renames of the package streamer classes to raw-data streamers - Minor improvements here and there - TODOs.txt updated --- .gitattributes | 12 +++ application/hub/config.php | 16 +-- .../handler/network/class_Networkable.php | 4 +- .../interfaces/helper/connections/.htaccess | 1 + .../connections/class_ConnectionHelper.php | 52 +++++++++ .../hub/main/factories/fragmenter/.htaccess | 1 + .../fragmenter/class_FragmenterFactory.php | 62 +++++++++++ .../hub/main/factories/producer/.htaccess | 1 + .../producer/class_ProducerFactory.php | 63 +++++++++++ .../class_BaseNetworkPackageHandler.php | 31 ++++-- .../tcp/class_TcpNetworkPackageHandler.php | 36 +++---- .../udp/class_UdpNetworkPackageHandler.php | 6 +- application/hub/main/helper/connection/class_ | 51 +++++++++ .../connection/class_BaseConnectionHelper.php | 44 ++++---- .../tcp/class_TcpConnectionHelper.php | 36 +++---- .../udp/class_UdpConnectionHelper.php | 15 ++- .../main/listener/tcp/class_TcpListener.php | 2 +- .../hub/main/package/class_NetworkPackage.php | 12 ++- .../fragmenter/class_PackageFragmenter.php | 102 ++++++++++++------ .../input/class_PackageInputStream.php | 64 +---------- .../output/class_PackageOutputStream.php | 64 +---------- .../hub/main/streams/raw_data/.htaccess | 1 + .../hub/main/streams/raw_data/input/.htaccess | 1 + .../input/class_RawDataInputStream.php | 80 ++++++++++++++ .../main/streams/raw_data/output/.htaccess | 1 + .../output/class_RawDataOutputStream.php | 65 +++++++++++ .../class_CruncherKeyProducerTask.php | 13 +-- .../class_CruncherTestUnitProducerTask.php | 13 +-- docs/TODOs.txt | 35 ++++-- 29 files changed, 609 insertions(+), 275 deletions(-) create mode 100644 application/hub/interfaces/helper/connections/.htaccess create mode 100644 application/hub/interfaces/helper/connections/class_ConnectionHelper.php create mode 100644 application/hub/main/factories/fragmenter/.htaccess create mode 100644 application/hub/main/factories/fragmenter/class_FragmenterFactory.php create mode 100644 application/hub/main/factories/producer/.htaccess create mode 100644 application/hub/main/factories/producer/class_ProducerFactory.php create mode 100644 application/hub/main/helper/connection/class_ create mode 100644 application/hub/main/streams/raw_data/.htaccess create mode 100644 application/hub/main/streams/raw_data/input/.htaccess create mode 100644 application/hub/main/streams/raw_data/input/class_RawDataInputStream.php create mode 100644 application/hub/main/streams/raw_data/output/.htaccess create mode 100644 application/hub/main/streams/raw_data/output/class_RawDataOutputStream.php diff --git a/.gitattributes b/.gitattributes index f94e47e86..7d10afbd6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -46,6 +46,8 @@ application/hub/interfaces/handler/network/class_Networkable.php -text svneol=un application/hub/interfaces/handler/task/.htaccess -text svneol=unset#text/plain application/hub/interfaces/handler/task/class_HandleableTask.php -text svneol=unset#text/plain application/hub/interfaces/helper/.htaccess -text svneol=unset#text/plain +application/hub/interfaces/helper/connections/.htaccess -text +application/hub/interfaces/helper/connections/class_ConnectionHelper.php -text application/hub/interfaces/helper/hub/.htaccess -text svneol=unset#text/plain application/hub/interfaces/helper/hub/class_HelpableHub.php svneol=native#text/plain application/hub/interfaces/helper/messages/.htaccess svneol=native#text/plain @@ -184,10 +186,14 @@ application/hub/main/factories/.htaccess -text svneol=unset#text/plain application/hub/main/factories/discovery/.htaccess -text svneol=unset#text/plain application/hub/main/factories/discovery/class_PackageDiscoveryFactory.php svneol=native#text/plain application/hub/main/factories/discovery/class_SocketDiscoveryFactory.php svneol=native#text/plain +application/hub/main/factories/fragmenter/.htaccess -text +application/hub/main/factories/fragmenter/class_FragmenterFactory.php -text application/hub/main/factories/lists/.htaccess -text svneol=unset#text/plain application/hub/main/factories/lists/class_RecipientListFactory.php svneol=native#text/plain application/hub/main/factories/package/.htaccess -text svneol=unset#text/plain application/hub/main/factories/package/class_NetworkPackageFactory.php svneol=native#text/plain +application/hub/main/factories/producer/.htaccess -text +application/hub/main/factories/producer/class_ProducerFactory.php -text application/hub/main/factories/socket/.htaccess -text svneol=unset#text/plain application/hub/main/factories/socket/class_SocketFactory.php svneol=native#text/plain application/hub/main/factories/source/.htaccess svneol=native#text/plain @@ -286,6 +292,7 @@ application/hub/main/handler/tasks/.htaccess -text svneol=unset#text/plain application/hub/main/handler/tasks/class_TaskHandler.php -text svneol=unset#text/plain application/hub/main/helper/.htaccess -text svneol=unset#text/plain application/hub/main/helper/connection/.htaccess -text svneol=unset#text/plain +application/hub/main/helper/connection/class_ -text application/hub/main/helper/connection/class_BaseConnectionHelper.php svneol=native#text/plain application/hub/main/helper/connection/tcp/.htaccess -text svneol=unset#text/plain application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php svneol=native#text/plain @@ -466,6 +473,11 @@ application/hub/main/streams/package/input/.htaccess svneol=native#text/plain application/hub/main/streams/package/input/class_PackageInputStream.php -text application/hub/main/streams/package/output/.htaccess svneol=native#text/plain application/hub/main/streams/package/output/class_PackageOutputStream.php svneol=native#text/plain +application/hub/main/streams/raw_data/.htaccess svneol=native#text/plain +application/hub/main/streams/raw_data/input/.htaccess svneol=native#text/plain +application/hub/main/streams/raw_data/input/class_RawDataInputStream.php -text +application/hub/main/streams/raw_data/output/.htaccess svneol=native#text/plain +application/hub/main/streams/raw_data/output/class_RawDataOutputStream.php svneol=native#text/plain application/hub/main/tags/.htaccess -text svneol=unset#text/plain application/hub/main/tags/class_ -text svneol=unset#text/plain application/hub/main/tags/class_BaseTags.php svneol=native#text/plain diff --git a/application/hub/config.php b/application/hub/config.php index 56d04545b..456bfedf6 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -567,15 +567,19 @@ $cfg->setConfigEntry('external_ip', ''); // CFG: PACKAGE-FRAGMENTER-CLASS $cfg->setConfigEntry('package_fragmenter_class', 'PackageFragmenter'); -// CFG: NODE-RAW-PACKAGE-OUTPUT-STREAM -$cfg->setConfigEntry('node_raw_package_output_stream', 'PackageOutputStream'); - -// CFG: NODE-RAW-PACKAGE-INPUT-STREAM -$cfg->setConfigEntry('node_raw_package_input_stream', 'PackageInputStream'); - // CFG: PACKAGE-CHUNK-SIZE $cfg->setConfigEntry('package_chunk_size', 256*8); +// CFG: NODE-RAW-DATA-OUTPUT-STREAM-CLASS +$cfg->setConfigEntry('node_raw_data_output_stream_class', 'RawDataOutputStream'); + +// CFG: NODE-RAW-DATA-INPUT-STREAM-CLASS +$cfg->setConfigEntry('node_raw_data_input_stream_class', 'RawDataInputStream'); + +/////////////////////////////////////////////////////////////////////////////// +// Cruncher Configuration +/////////////////////////////////////////////////////////////////////////////// + // CFG: CRUNCHER-TEST-UNITS-ENABLED $cfg->setConfigEntry('cruncher_test_units_enabled', 'Y'); diff --git a/application/hub/interfaces/handler/network/class_Networkable.php b/application/hub/interfaces/handler/network/class_Networkable.php index f3611a069..12b1ad2ad 100644 --- a/application/hub/interfaces/handler/network/class_Networkable.php +++ b/application/hub/interfaces/handler/network/class_Networkable.php @@ -23,14 +23,14 @@ */ interface Networkable extends Handleable { /** - * Processes a package from given resource. This is mostly useful for TCP + * Processes raw data from given resource. This is mostly useful for TCP * package handling and is implemented in the TcpListener class * * @param $resource A valid resource identifier * @return void * @throws InvalidResourceException If the given resource is invalid */ - function processResourcePackage ($resource); + function processResourceRawData ($resource); } // [EOF] diff --git a/application/hub/interfaces/helper/connections/.htaccess b/application/hub/interfaces/helper/connections/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/interfaces/helper/connections/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php new file mode 100644 index 000000000..947899c80 --- /dev/null +++ b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php @@ -0,0 +1,52 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 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 . + */ +interface ConnectionHelper extends Helper { + /** + * Sends raw package data to the recipient + * + * @param $packageData Raw package data + * @return $totalSentBytes Total sent bytes to the peer + * @throws InvalidSocketException If we got a problem with this socket + */ + function sendRawPackageData (array $packageData); + + /** + * Checks wether the connect retry is exhausted + * + * @return $isExhaused Wether connect retry is exchausted + */ + function isConnectRetryExhausted (); + + /** + * Do the shutdown sequence for this connection helper + * + * @todo We may want to implement a filter for ease notification of other objects like our pool + * @return void + * @throws SocketShutdownException If the current socket could not be shut down + */ + function doShutdown (); +} + +// [EOF] +?> diff --git a/application/hub/main/factories/fragmenter/.htaccess b/application/hub/main/factories/fragmenter/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/factories/fragmenter/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/factories/fragmenter/class_FragmenterFactory.php b/application/hub/main/factories/fragmenter/class_FragmenterFactory.php new file mode 100644 index 000000000..7e1a0152f --- /dev/null +++ b/application/hub/main/factories/fragmenter/class_FragmenterFactory.php @@ -0,0 +1,62 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 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 . + */ +class FragmenterFactory extends ObjectFactory { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Returns a singleton fragmenter instance. If no instance is found, it fill + * be generated and stored in registry, else the fragmenter from the + * registry will be returned. + * + * @param $configEntry A configuration entry naming the real class' name + * @return $fragmenterInstance A fragmenter instance + */ + public static final function createFragmenterInstance ($configEntry) { + // If there is no fragmenter? + if (Registry::getRegistry()->instanceExists('fragmenter')) { + // Get fragmenter from registry + $fragmenterInstance = Registry::getRegistry()->getInstance('fragmenter'); + } else { + // Get the fragmenter instance + $fragmenterInstance = ObjectFactory::createObjectByConfiguredName($configEntry); + + // Add it to the registry + Registry::getRegistry()->addInstance('fragmenter', $fragmenterInstance); + } + + // Return the instance + return $fragmenterInstance; + } +} + +// [EOF] +?> diff --git a/application/hub/main/factories/producer/.htaccess b/application/hub/main/factories/producer/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/factories/producer/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/factories/producer/class_ProducerFactory.php b/application/hub/main/factories/producer/class_ProducerFactory.php new file mode 100644 index 000000000..d952b25e0 --- /dev/null +++ b/application/hub/main/factories/producer/class_ProducerFactory.php @@ -0,0 +1,63 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 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 . + */ +class FragmenterFactory extends ObjectFactory { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Returns a singleton producer instance. If no instance is found, it fill + * be generated and stored in registry, else the producer from the + * registry will be returned. + * + * @param $configEntry A configuration entry naming the real class' name + * @parasm $producerType Type of the producer, can be 'key', 'test_unit', etc. + * @return $producerInstance A producer instance + */ + public static final function createProducerInstance ($configEntry, $producerType) { + // If there is no producer? + if (Registry::getRegistry()->instanceExists($producerType . '_producer')) { + // Get producer from registry + $producerInstance = Registry::getRegistry()->getInstance($producerType . '_producer'); + } else { + // Get the producer instance + $producerInstance = ObjectFactory::createObjectByConfiguredName($configEntry); + + // Add it to the registry + Registry::getRegistry()->addInstance($producerType . '_producer', $producerInstance); + } + + // Return the instance + return $producerInstance; + } +} + +// [EOF] +?> diff --git a/application/hub/main/handler/network/class_BaseNetworkPackageHandler.php b/application/hub/main/handler/network/class_BaseNetworkPackageHandler.php index 6f910f276..5599cbd38 100644 --- a/application/hub/main/handler/network/class_BaseNetworkPackageHandler.php +++ b/application/hub/main/handler/network/class_BaseNetworkPackageHandler.php @@ -22,16 +22,21 @@ * along with this program. If not, see . */ class BaseNetworkPackageHandler extends BaseHandler { - // Error codes - const SOCKET_ERROR_UNKNOWN = 'unknown_error'; - const SOCKET_ERROR_TRANSPORT_ENDPOINT = 'transport_endpoint'; - const SOCKET_ERROR_UNHANDLED = 'unhandled_package'; - const SOCKET_ERROR_EMPTY_DATA = 'empty_data'; - const PACKAGE_ERROR_INVALID_DATA = 'invalid_data'; - const PACKAGE_ERROR_INCOMPLETE_DATA = 'incomplete_data'; - const PACKAGE_ERROR_INVALID_CONTENT = 'invalid_content'; - const PACKAGE_ERROR_RECIPIENT_MISMATCH = 'recipient_error'; - const PACKAGE_LEVEL_CHECK_OKAY = 'checked_package'; + // Error codes: + // - Socket raw data stream errors + const SOCKET_ERROR_UNKNOWN = 'unknown_error'; // Unknown error (should not happen) + const SOCKET_ERROR_TRANSPORT_ENDPOINT = 'transport_endpoint'; // Transport endpoint has closed + const SOCKET_ERROR_EMPTY_DATA = 'empty_data'; // Other peer has sent nothing + const SOCKET_ERROR_INVALID_BASE64_MODULO = 'base64_modulo'; // Length is not modulo 4 + const SOCKET_ERROR_INVALID_BASE64_MESSAGE = 'base64_message'; // Raw data is not Base64-encoded + const SOCKET_ERROR_UNHANDLED = 'unhandled_package'; // Unhandled raw data (not bad) + + // - Package errors + const PACKAGE_ERROR_INVALID_DATA = 'invalid_data'; // Invalid data in package found + const PACKAGE_ERROR_INCOMPLETE_DATA = 'incomplete_data'; // Incomplete data sent (e.g. field is missing) + const PACKAGE_ERROR_INVALID_CONTENT = 'invalid_content'; // Content is invalid (e.g. not well-formed) + const PACKAGE_ERROR_RECIPIENT_MISMATCH = 'recipient_error'; // Recipient is not us + const PACKAGE_LEVEL_CHECK_OKAY = 'checked_package'; // Package is fine /** * Error code from socket @@ -47,6 +52,12 @@ class BaseNetworkPackageHandler extends BaseHandler { protected function __construct ($className) { // Call parent constructor parent::__construct($className); + + // Get an input stream instance + $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_input_stream_class', array($this)); + + // Set it in this network-package handler + $this->setInputStreamInstance($streamInstance); } /** diff --git a/application/hub/main/handler/network/tcp/class_TcpNetworkPackageHandler.php b/application/hub/main/handler/network/tcp/class_TcpNetworkPackageHandler.php index 61a9a941e..7b644f83b 100644 --- a/application/hub/main/handler/network/tcp/class_TcpNetworkPackageHandler.php +++ b/application/hub/main/handler/network/tcp/class_TcpNetworkPackageHandler.php @@ -44,45 +44,39 @@ class TcpNetworkPackageHandler extends BaseNetworkPackageHandler implements Netw // Get new instance $handlerInstance = new TcpNetworkPackageHandler(); - // Initialize new resolver instance - $resolverInstance = ObjectFactory::createObjectByConfiguredName('network_state_resolver_class'); - - // Set it in this class - $handlerInstance->setResolverInstance($resolverInstance); - // Return the prepared instance return $handlerInstance; } /** - * Processes a package from given resource. This is mostly useful for TCP + * Processes raw data from given resource. This is mostly useful for TCP * package handling and is implemented in the TcpListener class * * @param $resource A valid resource identifier * @return void - * @todo We need to handle the raw data over to the NetworkPackage class if the state if fine */ - public function processResourcePackage ($resource) { + public function processResourceRawData ($resource) { // Check the resource if (!is_resource($resource)) { // Throw an exception throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); } // END - if - // Init package data array - $packageData = array(); - // Reset error code to unhandled $this->setErrorCode(self::SOCKET_ERROR_UNHANDLED); // Debug message - $this->debugOutput('HANDLER: Handling TCP package from peer ' . $resource); + //* NOISY-DEBUG: */ $this->debugOutput('HANDLER: Handling TCP package from peer ' . $resource); - // Read the raw data from socket + /* + * Read the raw data from socket. If you change PHP_BINARY_READ to + * PHP_NORMAL_READ, this line will endless block. We only have + * simultanous threads and no real threads. + */ $rawData = socket_read($resource, $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ); // Debug output of read data length - $this->debugOutput('rawData[]=' . strlen($rawData)); + //* NOISY-DEBUG: */ $this->debugOutput('LISTENER: rawData[]=' . strlen($rawData)); // Is it valid? if (($rawData === false) || (socket_last_error($resource) > 0)) { @@ -91,11 +85,15 @@ class TcpNetworkPackageHandler extends BaseNetworkPackageHandler implements Netw } elseif (empty($rawData)) { // The peer did send nothing to us $this->setErrorCode(self::SOCKET_ERROR_EMPTY_DATA); - } + } else { + // Low-level checks of the raw data went all fine, we can now decode the raw data + $decodedData = $this->getInputStreamInstance()->streamData($rawData); - // Get a state from the resolver for this package data array - $stateInstance = $this->getResolverInstance()->resolveStateByPackage($this, $packageData, $resource); - die('UNFINISHED:'.$stateInstance->__toString()."\n"); + // Is the decoded data false, then streamData() could not validate it + if ($decodedData !== false) { + die('decodedData(' . strlen($decodedData) . ')=' . $decodedData . "\n"); + } // END - if + } } } diff --git a/application/hub/main/handler/network/udp/class_UdpNetworkPackageHandler.php b/application/hub/main/handler/network/udp/class_UdpNetworkPackageHandler.php index 60f6fbe47..84e98fcb6 100644 --- a/application/hub/main/handler/network/udp/class_UdpNetworkPackageHandler.php +++ b/application/hub/main/handler/network/udp/class_UdpNetworkPackageHandler.php @@ -49,7 +49,7 @@ class UdpNetworkPackageHandler extends BaseNetworkPackageHandler implements Netw } /** - * Processes a package from given resource. This is mostly useful for TCP + * Processes raw data from given resource. This is mostly useful for TCP * package handling and is implemented in the TcpListener class * * @param $resource A valid resource identifier @@ -57,7 +57,7 @@ class UdpNetworkPackageHandler extends BaseNetworkPackageHandler implements Netw * @throws InvalidResourceException If the given resource is invalid * @todo 0% */ - public function processResourcePackage ($resource) { + public function processResourceRawData ($resource) { // Check the resource if (!is_resource($resource)) { // Throw an exception @@ -65,7 +65,7 @@ class UdpNetworkPackageHandler extends BaseNetworkPackageHandler implements Netw } // END - if // Implement processing here - $this->partialStub('Please implement this method.'); + $this->partialStub('Please implement this method. resource=' . $resource); } } diff --git a/application/hub/main/helper/connection/class_ b/application/hub/main/helper/connection/class_ new file mode 100644 index 000000000..3ef3ad9e1 --- /dev/null +++ b/application/hub/main/helper/connection/class_ @@ -0,0 +1,51 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * @todo Find an interface for hub helper + * + * 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 ???ConnectionHelper extends BaseConnectionHelper implements ConnectionHelper { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set protocol + $this->setProtocol('!!!'); + } + + /** + * Creates a socket resource ("connection") for given recipient in package data + * + * @param $packageData Raw package data + * @return $socketResource Socket resource + */ + public static function createConnectionFromPackageData (array $packageData) { + die(); + } +} + +// [EOF] +?> diff --git a/application/hub/main/helper/connection/class_BaseConnectionHelper.php b/application/hub/main/helper/connection/class_BaseConnectionHelper.php index c88e89e53..c1a21b6ca 100644 --- a/application/hub/main/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/main/helper/connection/class_BaseConnectionHelper.php @@ -79,6 +79,12 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc // Register this connection helper Registry::getRegistry()->addInstance('connection', $this); + + // Initialize output stream + $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class'); + + // And add it to this connection helper + $this->setOutputStreamInstance($streamInstance); } /** @@ -154,17 +160,8 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc * @return $chunkData Raw data chunk */ private function getRawDataFromPackageArray (array $packageData) { - // If there is no fragmenter? - if (!Registry::getRegistry()->instanceExists('package_fragmenter')) { - // Get the fragmenter instance - $fragmenterInstance = ObjectFactory::createObjectByConfiguredName('package_fragmenter_class'); - - // Add it to the registry - Registry::getRegistry()->addInstance('package_fragmenter', $fragmenterInstance); - } else { - // Get fragmenter from registry - $fragmenterInstance = Registry::getRegistry()->getInstance('package_fragmenter'); - } + // Get the fragmenter instance + $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package_fragmenter_class'); // Implode the package data array and fragement the resulting string, returns the final hash $finalHash = $fragmenterInstance->fragmentPackageArray($packageData, $this); @@ -219,8 +216,8 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length'); // Init variables - $rawData = ''; - $dataStream = ' '; + $rawData = ''; + $dataStream = ' '; $totalSentBytes = 0; // Fill sending buffer with data @@ -234,8 +231,11 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc // Nothing to sent is bad news! assert(strlen($rawData) > 0); + // Encode the raw data with our output-stream + $encodedData = $this->getOutputStreamInstance()->streamData($rawData); + // Calculate difference - $this->diff = $bufferSize - strlen($rawData); + $this->diff = $bufferSize - strlen($encodedData); // Get socket resource $socketResource = $this->getSocketResource(); @@ -246,8 +246,8 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc // Deliver all data while ($sentBytes !== false) { // And deliver it - //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($rawData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff); - $sentBytes = @socket_write($socketResource, $rawData, ($bufferSize - $this->diff)); + //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff); + $sentBytes = @socket_write($socketResource, $encodedData, ($bufferSize - $this->diff)); // If there was an error, we don't continue here if ($sentBytes === false) { @@ -262,27 +262,27 @@ class BaseConnectionHelper extends BaseHubHelper implements Registerable, Protoc // And throw it throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); - } elseif (($sentBytes == 0) && (strlen($rawData) > 0)) { + } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) { // Nothing sent means we are done //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')'); break; } // The difference between sent bytes and length of raw data should not be below zero - assert((strlen($rawData) - $sentBytes) >= 0); + assert((strlen($encodedData) - $sentBytes) >= 0); // Add total sent bytes $totalSentBytes += $sentBytes; // Cut out the last unsent bytes - //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($rawData) . ' bytes ...'); - $rawData = substr($rawData, $sentBytes); + //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...'); + $encodedData = substr($encodedData, $sentBytes); // Calculate difference again - $this->diff = $bufferSize - strlen($rawData); + $this->diff = $bufferSize - strlen($encodedData); // Can we abort? - if (strlen($rawData) <= 0) { + if (strlen($encodedData) <= 0) { // Abort here, all sent! //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')'); break; diff --git a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php index 9d6b55015..947daad35 100644 --- a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php +++ b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php @@ -22,7 +22,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class TcpConnectionHelper extends BaseConnectionHelper implements Helper { +class TcpConnectionHelper extends BaseConnectionHelper implements ConnectionHelper { /** * Protected constructor * @@ -80,21 +80,6 @@ class TcpConnectionHelper extends BaseConnectionHelper implements Helper { throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if - // Now, we want non-blocking mode - if (!socket_set_nonblock($socketResource)) { - // Get socket error code for verification - $socketError = socket_last_error($socketResource); - - // Get error message - $errorMessage = socket_strerror($socketError); - - // Shutdown this socket - $helperInstance->shutdownSocket($socketResource); - - // And throw again - throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); - } // END - if - // Set the resource $helperInstance->setSocketResource($socketResource); @@ -129,16 +114,31 @@ class TcpConnectionHelper extends BaseConnectionHelper implements Helper { } } // END - if + // Now, we want non-blocking mode + if (!socket_set_nonblock($socketResource)) { + // Get socket error code for verification + $socketError = socket_last_error($socketResource); + + // Get error message + $errorMessage = socket_strerror($socketError); + + // Shutdown this socket + $helperInstance->shutdownSocket($socketResource); + + // And throw again + throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); + } // END - if + // Okay, that should be it. So return it... return $socketResource; } /** - * Do the shutdown sequence for TCP connections + * Do the shutdown sequence for this connection helper * - * @todo We may want to implement a filter for ease notification of other objects like our pool * @return void * @throws SocketShutdownException If the current socket could not be shut down + * @todo We may want to implement a filter for ease notification of other objects like our pool */ public function doShutdown () { // Clear any previous errors diff --git a/application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php b/application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php index 01761f96a..db230642b 100644 --- a/application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php +++ b/application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php @@ -22,7 +22,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class UdpConnectionHelper extends BaseConnectionHelper implements Helper { +class UdpConnectionHelper extends BaseConnectionHelper implements ConnectionHelper { /** * Protected constructor * @@ -43,7 +43,18 @@ class UdpConnectionHelper extends BaseConnectionHelper implements Helper { * @return $socketResource Socket resource */ public static function createConnectionFromPackageData (array $packageData) { - die(); + die(__METHOD__ . ': Unfinished method, packageData[]=' . count($packageData) . "\n"); + } + + /** + * Do the shutdown sequence for this connection helper + * + * @return void + * @throws SocketShutdownException If the current socket could not be shut down + * @todo We may want to implement a filter for ease notification of other objects like our pool + */ + public function doShutdown () { + $this->partialStub('Please implement this method.'); } } diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index 54842ca6a..c972c1f5f 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -236,7 +236,7 @@ class TcpListener extends BaseListener implements Listenable { if ($currentSocket != $this->getSocketResource()) { // ... or else it will raise warnings like 'Transport endpoint is not connected' //* NOISY-DEBUG: */ $this->debugOutput('LISTENER: currentSocket=' . $currentSocket); - $this->getPackageInstance()->processResourcePackage($currentSocket); + $this->getPackageInstance()->processResourceRawData($currentSocket); } // END - if // Advance to next entry. This should be the last line diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 9a4ce15df..192403e65 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -51,6 +51,11 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva */ const PACKAGE_MASK_SEPERATOR = ':'; + /** + * Seperator for checksum + */ + const PACKAGE_CHECKSUM_SEPERATOR = ':'; + /** * Array indexes for above mask, start with zero */ @@ -152,15 +157,16 @@ class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receiva * @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() ); diff --git a/application/hub/main/package/fragmenter/class_PackageFragmenter.php b/application/hub/main/package/fragmenter/class_PackageFragmenter.php index 1cf6db7ea..ca073ae07 100644 --- a/application/hub/main/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/main/package/fragmenter/class_PackageFragmenter.php @@ -1,9 +1,15 @@ * @version 0.0.0 @@ -64,6 +70,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg * Maximum possible serial number */ private $maxSerialNumber = 0; + /** * Seperator between chunk data, serial number and chunk hash */ @@ -74,11 +81,21 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg */ const CHUNK_HASH_SEPERATOR = ';'; + /** + * Seperator between two chunks + */ + const CHUNK_SEPERATOR = '|'; + /** * Identifier for hash chunk */ const HASH_CHUNK_IDENTIFIER = 'HASH-CHUNK:'; + /** + * Identifier for end-of-package marker + */ + const END_OF_PACKAGE_IDENTIFIER = 'EOP:'; + /** * Protected constructor * @@ -101,12 +118,6 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg // Get new instance $fragmenterInstance = new PackageFragmenter(); - // Get an output stream for all out-going packages - $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_package_output_stream'); - - // And set it in this fragmenter - $fragmenterInstance->setOutputStreamInstance($streamInstance); - // And also a crypto instance (for our encrypted messages) $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class'); $fragmenterInstance->setCryptoInstance($cryptoInstance); @@ -287,16 +298,39 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg return $encodedSerialNumber; } + /** + * Appends an end-of-package chunk to the chunk list for given chunk and + * final hash. + * + * @param $chunkHash Last chunk's hash + * @param $finalHash Final hash for raw (unencoded) data + * @return void + */ + private function appendEndOfPackageChunk ($chunkHash, $finalHash) { + // Generate end-of-package marker + $rawData = + self::END_OF_PACKAGE_IDENTIFIER . + $finalHash . self::CHUNK_HASH_SEPERATOR . + $chunkHash . self::CHUNK_SEPERATOR; + + // Also get a hash from it + $chunkHash = $this->generateHashFromRawData($rawData); + + // Append it to the chunk's data and hash array + $this->chunkHashes[$finalHash][] = $chunkHash; + $this->chunks[$finalHash][] = $rawData; + } + /** * Splits the given encoded data into smaller chunks, the size of the final * and the seperator is being subtracted from chunk size to fit it into a * TCP package (512 bytes). * - * @param $encodedData Encoded data string - * @param $finalHash Final hash from the raw data + * @param $rawData Raw data string + * @param $finalHash Final hash from the raw data * @return void */ - private function splitEncodedDataIntoChunks ($encodedData, $finalHash) { + private function splitEncodedDataIntoChunks ($rawData, $finalHash) { // Make sure final hashes with at least 32 bytes can pass assert(strlen($finalHash) >= 32); @@ -304,17 +338,24 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg $dataChunkSize = $this->getDataChunkSizeFromHash($finalHash); //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: dataChunkSize=' . $dataChunkSize); + // Init variables + $chunkHash = ''; + // Now split it up - for ($idx = 0; $idx < strlen($encodedData); $idx += $dataChunkSize) { + for ($idx = 0; $idx < strlen($rawData); $idx += $dataChunkSize) { // Get the next chunk - $chunk = substr($encodedData, $idx, $dataChunkSize); + $chunk = substr($rawData, $idx, $dataChunkSize); // Hash it and remember it in seperate array $chunkHash = $this->getCryptoInstance()->hashString($chunk); $this->chunkHashes[$finalHash][] = $chunkHash; // Prepend the hash to the chunk - $chunk = $chunkHash . self::CHUNK_DATA_HASH_SEPERATOR . $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPERATOR . $chunk; + $chunk = + $chunkHash . self::CHUNK_DATA_HASH_SEPERATOR . + $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPERATOR . + $chunk . self::CHUNK_SEPERATOR + ; // Make sure the chunk is not larger than a TCP package can hold assert(strlen($chunk) <= NetworkPackage::TCP_PACKAGE_SIZE); @@ -325,40 +366,44 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg } // END - for // Debug output - $this->debugOutput('FRAGMENTER: Encoded data of ' . strlen($encodedData) . ' bytes has been fragmented into ' . count($this->chunks[$finalHash]) . ' chunk(s).'); + //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Raw data of ' . strlen($rawData) . ' bytes has been fragmented into ' . count($this->chunks[$finalHash]) . ' chunk(s).'); + + // Add end-of-package chunk + $this->appendEndOfPackageChunk($chunkHash, $finalHash); } /** * Prepends a chunk (or more) with all hashes from all chunks + final chunk. * - * @param $encodedData Encoded data string - * @param $finalHash Final hash from the raw data + * @param $rawData Raw data string + * @param $finalHash Final hash from the raw data * @return void */ - private function prependHashChunk ($encodedData, $finalHash) { + private function prependHashChunk ($rawData, $finalHash) { // "Implode" the whole array of hashes into one string $rawData = self::HASH_CHUNK_IDENTIFIER . implode(self::CHUNK_HASH_SEPERATOR, $this->chunkHashes[$finalHash]); // Also get a hash from it $chunkHash = $this->generateHashFromRawData($rawData); - // Also encode this one - $encodedData = $this->getOutputStreamInstance()->streamData($rawData); - // Calulcate chunk size $dataChunkSize = $this->getDataChunkSizeFromHash($chunkHash); // Now array_unshift() it to the two chunk arrays - for ($idx = 0; $idx < strlen($encodedData); $idx += $dataChunkSize) { + for ($idx = 0; $idx < strlen($rawData); $idx += $dataChunkSize) { // Get the next chunk - $chunk = substr($encodedData, $idx, $dataChunkSize); + $chunk = substr($rawData, $idx, $dataChunkSize); // Hash it and remember it in seperate array $chunkHash = $this->getCryptoInstance()->hashString($chunk); array_unshift($this->chunkHashes[$finalHash], $chunkHash); // Prepend the hash to the chunk - $chunk = $chunkHash . self::CHUNK_DATA_HASH_SEPERATOR . $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPERATOR . $chunk; + $chunk = + $chunkHash . self::CHUNK_DATA_HASH_SEPERATOR . + $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPERATOR . + $chunk . self::CHUNK_SEPERATOR + ; // Make sure the chunk is not larger than a TCP package can hold assert(strlen($chunk) <= NetworkPackage::TCP_PACKAGE_SIZE); @@ -395,14 +440,11 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg // Init pointer $this->initPointer($finalHash); - // Encode the package for delivery - $encodedData = $this->getOutputStreamInstance()->streamData($rawData); - // Split the encoded data into smaller chunks - $this->splitEncodedDataIntoChunks($encodedData, $finalHash); + $this->splitEncodedDataIntoChunks($rawData, $finalHash); // Prepend a chunk with all hashes together - $this->prependHashChunk($encodedData, $finalHash); + $this->prependHashChunk($rawData, $finalHash); // Mark the package as fragmented $this->markPackageDataProcessed($packageData); diff --git a/application/hub/main/streams/package/input/class_PackageInputStream.php b/application/hub/main/streams/package/input/class_PackageInputStream.php index aa9548f61..f551ef47b 100644 --- a/application/hub/main/streams/package/input/class_PackageInputStream.php +++ b/application/hub/main/streams/package/input/class_PackageInputStream.php @@ -1,65 +1,3 @@ - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 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 . - */ -class PackageInputStream extends BaseStream implements InputStreamable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Creates an instance of this node class - * - * @return $streamInstance An instance of this node class - */ - public final static function createPackageInputStream () { - // Get a new instance - $streamInstance = new PackageInputStream(); - - // Return the instance - return $streamInstance; - } - - /** - * Streams the data and maybe does something to it - * - * @param $data The data (string mostly) to "stream" - * @return $data The data (string mostly) to "stream" - * @todo Do we need to do something more here? - */ - public function streamData ($data) { - // Encode the data with BASE64 encoding - $data = base64_decode($data); - - // Return it - return $data; - } -} - -// [EOF] +// @DEPRECATED ?> diff --git a/application/hub/main/streams/package/output/class_PackageOutputStream.php b/application/hub/main/streams/package/output/class_PackageOutputStream.php index 35b382ae3..f551ef47b 100644 --- a/application/hub/main/streams/package/output/class_PackageOutputStream.php +++ b/application/hub/main/streams/package/output/class_PackageOutputStream.php @@ -1,65 +1,3 @@ - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 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 . - */ -class PackageOutputStream extends BaseStream implements OutputStreamable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Creates an instance of this node class - * - * @return $streamInstance An instance of this node class - */ - public final static function createPackageOutputStream () { - // Get a new instance - $streamInstance = new PackageOutputStream(); - - // Return the instance - return $streamInstance; - } - - /** - * Streams the data and maybe does something to it - * - * @param $data The data (string mostly) to "stream" - * @return $data The data (string mostly) to "stream" - * @todo Do we need to do something more here? - */ - public function streamData ($data) { - // Encode the data with BASE64 encoding - $data = base64_encode($data); - - // Return it - return $data; - } -} - -// [EOF] +// @DEPRECATED ?> diff --git a/application/hub/main/streams/raw_data/.htaccess b/application/hub/main/streams/raw_data/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/streams/raw_data/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/streams/raw_data/input/.htaccess b/application/hub/main/streams/raw_data/input/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/streams/raw_data/input/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php b/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php new file mode 100644 index 000000000..c6b199ec1 --- /dev/null +++ b/application/hub/main/streams/raw_data/input/class_RawDataInputStream.php @@ -0,0 +1,80 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 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 . + */ +class RawDataInputStream extends BaseStream implements InputStreamable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this node class + * + * @param $handlerInstance An instance of a Networkable class + * @return $streamInstance An instance of this node class + */ + public final static function createRawDataInputStream (Networkable $handlerInstance) { + // Get a new instance + $streamInstance = new RawDataInputStream(); + + // Set the handler instance + $streamInstance->setHandlerInstance($handlerInstance); + + // Return the instance + return $streamInstance; + } + + /** + * Streams the data and maybe does something to it + * + * @param $data The data (string mostly) to "stream" + * @return $data The data (string mostly) to "stream" + * @todo Do we need to do something more here? + */ + public function streamData ($data) { + // Can it be validated? + if ((strlen($data) % 4) != 0) { + // Length modulo 4 must be zero, else it is an invalid Base64 message + $this->getHandlerInstance()->setErrorCode(BaseNetworkPackageHandler::SOCKET_ERROR_INVALID_BASE64_MODULO); + $data = false; + } elseif (!$this->isBase64Encoded($data)) { + // Is not a valid Base64-encoded message + $this->getHandlerInstance()->setErrorCode(BaseNetworkPackageHandler::SOCKET_ERROR_INVALID_BASE64_MESSAGE); + $data = false; + } else { + // Decode the data with BASE64-encoding + $data = base64_decode($data); + } + + // Return it + return $data; + } +} + +// [EOF] +?> diff --git a/application/hub/main/streams/raw_data/output/.htaccess b/application/hub/main/streams/raw_data/output/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/streams/raw_data/output/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/streams/raw_data/output/class_RawDataOutputStream.php b/application/hub/main/streams/raw_data/output/class_RawDataOutputStream.php new file mode 100644 index 000000000..f9c61f455 --- /dev/null +++ b/application/hub/main/streams/raw_data/output/class_RawDataOutputStream.php @@ -0,0 +1,65 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 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 . + */ +class RawDataOutputStream extends BaseStream implements OutputStreamable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this node class + * + * @return $streamInstance An instance of this node class + */ + public final static function createRawDataOutputStream () { + // Get a new instance + $streamInstance = new RawDataOutputStream(); + + // Return the instance + return $streamInstance; + } + + /** + * Streams the data and maybe does something to it + * + * @param $data The data (string mostly) to "stream" + * @return $data The data (string mostly) to "stream" + * @todo Do we need to do something more here? + */ + public function streamData ($data) { + // Encode the data with BASE64 encoding + $data = base64_encode($data); + + // Return it + return $data; + } +} + +// [EOF] +?> diff --git a/application/hub/main/tasks/cruncher/class_CruncherKeyProducerTask.php b/application/hub/main/tasks/cruncher/class_CruncherKeyProducerTask.php index 43673a23b..379011d14 100644 --- a/application/hub/main/tasks/cruncher/class_CruncherKeyProducerTask.php +++ b/application/hub/main/tasks/cruncher/class_CruncherKeyProducerTask.php @@ -63,17 +63,8 @@ class CruncherKeyProducerTask extends BaseTask implements Taskable, Visitable { * @return void */ public function executeTask () { - // First we check if we already have an instance (which will happen at any later cyclus) - if (!Registry::getRegistry()->instanceExists('key_producer')) { - // No instance found, so we need to create a producer instance - $producerInstance = ObjectFactory::createObjectByConfiguredName('cruncher_key_producer_class'); - - // ... and add it to the registry - Registry::getRegistry()->addInstance('key_producer', $producerInstance); - } else { - // Get the producer instance from registry - $producerInstance = Registry::getRegistry()->getInstance('key_producer'); - } + // Get the instance + $producerInstance = ProducerFactory::createProducerInstance('cruncher_key_producer_class', 'key'); // Get the current cruncher state from registry $stateInstance = Registry::getRegistry()->getInstance('cruncher')->getStateInstance(); diff --git a/application/hub/main/tasks/cruncher/class_CruncherTestUnitProducerTask.php b/application/hub/main/tasks/cruncher/class_CruncherTestUnitProducerTask.php index f47f633e3..d3aa891d0 100644 --- a/application/hub/main/tasks/cruncher/class_CruncherTestUnitProducerTask.php +++ b/application/hub/main/tasks/cruncher/class_CruncherTestUnitProducerTask.php @@ -63,17 +63,8 @@ class CruncherTestUnitProducerTask extends BaseTask implements Taskable, Visitab * @return void */ public function executeTask () { - // First we check if we already have an instance (which will happen at any later cyclus) - if (!Registry::getRegistry()->instanceExists('test_unit_producer')) { - // No instance found, so we need to create a producer instance - $producerInstance = ObjectFactory::createObjectByConfiguredName('cruncher_test_unit_producer_class'); - - // ... and add it to the registry - Registry::getRegistry()->addInstance('test_unit_producer', $producerInstance); - } else { - // Get the producer instance from registry - $producerInstance = Registry::getRegistry()->getInstance('test_unit_producer'); - } + // Get the producer instance + $producerInstance = ProducerFactory::createProducerInstance('cruncher_test_unit_producer_class', 'test_unit'); // Get the current cruncher state from registry $stateInstance = Registry::getRegistry()->getInstance('cruncher')->getStateInstance(); diff --git a/docs/TODOs.txt b/docs/TODOs.txt index 2a05e280b..6b47584a5 100644 --- a/docs/TODOs.txt +++ b/docs/TODOs.txt @@ -1,6 +1,7 @@ ### WARNING: THIS FILE IS AUTO-GENERATED BY ./todo-builder.sh ### ### DO NOT EDIT THIS FILE. ### ./application/hub/interfaces/cruncher/class_CruncherHelper.php:10: * @todo We need to find a better name for this interface +./application/hub/interfaces/helper/connections/class_ConnectionHelper.php:44: * @todo We may want to implement a filter for ease notification of other objects like our pool ./application/hub/interfaces/nodes/class_NodeHelper.php:10: * @todo We need to find a better name for this interface ./application/hub/main/commands/console/class_HubConsoleCruncherCommand.php:104: * @todo Should we add some more filters? ./application/hub/main/commands/console/class_HubConsoleCruncherCommand.php:58: * @todo Try to create a CruncherActivationTask or so @@ -30,12 +31,12 @@ ./application/hub/main/filter/shutdown/node/class_NodeShutdownTaskHandlerFilter.php:55: * @todo 0% done ./application/hub/main/filter/task/cruncher/class_CruncherTaskHandlerInitializerFilter.php:55: * @todo 5% done ./application/hub/main/filter/task/node/class_NodeTaskHandlerInitializerFilter.php:55: * @todo Maybe some more tasks needs to be added? -./application/hub/main/handler/network/tcp/class_TcpNetworkPackageHandler.php:96: * @todo ~10% done ./application/hub/main/handler/network/udp/class_UdpNetworkPackageHandler.php:58: * @todo 0% ./application/hub/main/handler/tasks/class_TaskHandler.php:140: // @TODO Messurement can be added around this call ./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:10: * @todo Find an interface for hub helper -./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:139: * @todo We may want to implement a filter for ease notification of other objects like our pool +./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:141: * @todo We may want to implement a filter for ease notification of other objects like our pool ./application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php:10: * @todo Find an interface for hub helper +./application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php:54: * @todo We may want to implement a filter for ease notification of other objects like our pool ./application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php:10: * @todo Find an interface for hub helper ./application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php:61: * @todo Rewrite the ->renderXmlContent() call to no arguments ./application/hub/main/helper/hub/connection/class_HubSelfConnectHelper.php:10: * @todo Find an interface for hub helper @@ -64,11 +65,15 @@ ./application/hub/main/nodes/regular/class_HubRegularNode.php:58: * @todo Implement this method ./application/hub/main/nodes/regular/class_HubRegularNode.php:68: * @todo Unfinished method ./application/hub/main/nodes/regular/class_HubRegularNode.php:91: // @TODO Add some filters here -./application/hub/main/package/class_NetworkPackage.php:152: // @TODO crc32 is not good, but it needs to be fast -./application/hub/main/package/class_NetworkPackage.php:22: * @todo Needs to add functionality for handling the object's type -./application/hub/main/package/class_NetworkPackage.php:264: // @TODO We may want to do somthing more here? -./application/hub/main/package/class_NetworkPackage.php:363: // @TODO Add some logging here -./application/hub/main/package/class_NetworkPackage.php:389: // @TODO Add some logging here +./application/hub/main/package/class_NetworkPackage.php:160: * @todo $helperInstance is unused +./application/hub/main/package/class_NetworkPackage.php:164: // @TODO crc32 is not very strong, but it needs to be fast +./application/hub/main/package/class_NetworkPackage.php:23: * @todo Needs to add functionality for handling the object's type +./application/hub/main/package/class_NetworkPackage.php:280: // @TODO We may want to do somthing more here? +./application/hub/main/package/class_NetworkPackage.php:379: // @TODO Add some logging here +./application/hub/main/package/class_NetworkPackage.php:405: // @TODO Add some logging here +./application/hub/main/package/class_NetworkPackage.php:460: // @TODO Add some content here +./application/hub/main/package/class_NetworkPackage.php:469: // @TODO Add some content here +./application/hub/main/package/fragmenter/class_PackageFragmenter.php:426: * @todo $connectionInstance is unused ./application/hub/main/pools/peer/class_DefaultPeerPool.php:148: // @TODO Check for IP ./application/hub/main/producer/cruncher/keys/class_CruncherKeyProducer.php:106: // @TODO Do something with it ./application/hub/main/producer/cruncher/keys/class_CruncherKeyProducer.php:62: * @todo Find something for init phase of this key producer @@ -78,11 +83,15 @@ ./application/hub/main/resolver/state/network/class_NetworkStateResolver.php:68: * @todo ~30% done ./application/hub/main/resolver/state/network/class_NetworkStateResolver.php:80: // @TODO On some systems it is 134, on some 107? ./application/hub/main/states/node/init/class_NodeInitState.php:63: * @todo We might want to move some calls to this method to fill it with life +./application/hub/main/streams/raw_data/input/class_RawDataInputStream.php:57: * @todo Do we need to do something more here? +./application/hub/main/streams/raw_data/output/class_RawDataOutputStream.php:53: * @todo Do we need to do something more here? ./application/hub/main/tasks/cruncher/class_CruncherKeyProducerTask.php:53: * @todo Maybe visit some sub-objects ./application/hub/main/tasks/cruncher/class_CruncherTestUnitProducerTask.php:53: * @todo Maybe visit some sub-objects ./application/hub/main/tasks/cruncher/class_CruncherWorkUnitFetcherTask.php:54: * @todo Maybe visit some sub-objects ./application/hub/main/tasks/hub/announcement/class_HubSelfAnnouncementTask.php:53: * @todo 0% ./application/hub/main/tasks/hub/class_HubSelfConnectTask.php:53: * @todo 0% +./application/hub/main/tasks/hub/class_HubSocketListenerTask.php:62: // @TODO Do we need to visit this task? $visitorInstance->visitTask($this); +./application/hub/main/tasks/hub/class_HubSocketListenerTask.php:69: * @todo 0% done ./application/hub/main/tasks/hub/ping/class_HubPingTask.php:73: * @todo 0% done ./application/hub/main/tasks/hub/update/class_HubUpdateCheckTask.php:53: * @todo 0% ./application/hub/main/template/announcement/class_XmlAnnouncementTemplateEngine.php:10: * @todo This template engine does not make use of setTemplateType() @@ -101,10 +110,10 @@ ./inc/classes/exceptions/main/class_MissingMethodException.php:13: * @todo Try to rewrite user/guest login classes and mark this exception as deprecated ./inc/classes/exceptions/main/class_NoConfigEntryException.php:10: * @todo Rename this class to NoFoundEntryException ./inc/classes/interfaces/class_FrameworkInterface.php:11: * @todo Find a better name for this interface -./inc/classes/main/class_BaseFrameworkSystem.php:1237: * @todo Write a logging mechanism for productive mode -./inc/classes/main/class_BaseFrameworkSystem.php:1251: // @TODO Finish this part! -./inc/classes/main/class_BaseFrameworkSystem.php:169: // @todo Try to clean these constants up -./inc/classes/main/class_BaseFrameworkSystem.php:408: * @todo SearchableResult and UpdateableResult shall have a super interface to use here +./inc/classes/main/class_BaseFrameworkSystem.php:1253: * @todo Write a logging mechanism for productive mode +./inc/classes/main/class_BaseFrameworkSystem.php:1267: // @TODO Finish this part! +./inc/classes/main/class_BaseFrameworkSystem.php:185: // @todo Try to clean these constants up +./inc/classes/main/class_BaseFrameworkSystem.php:424: * @todo SearchableResult and UpdateableResult shall have a super interface to use here ./inc/classes/main/commands/web/class_WebLoginAreaCommand.php:64: * @todo Add some stuff here: Some personal data, app/game related data ./inc/classes/main/commands/web/class_WebProblemCommand.php:58: * @todo 0% done ./inc/classes/main/commands/web/class_WebStatusCommand.php:58: * @todo 0% done @@ -232,6 +241,8 @@ ./application/hub/main/helper/messages/crypto/class_CryptoRandomMessageHelper.php:2:// @DEPRECATED ./application/hub/main/iterator/producer/class_TestUnitKeyProducerIterator.php:2:// @DEPRECATED ./application/hub/main/nodes/class_BaseHubNode.php:58: * @deprecated +./application/hub/main/streams/package/input/class_PackageInputStream.php:2:// @DEPRECATED +./application/hub/main/streams/package/output/class_PackageOutputStream.php:2:// @DEPRECATED ./application/hub/main/template/announcement/class_AnnouncementTemplateEngine.php:2:// @DEPRECATED ./application/hub/main/template/connect/class_SelfConnectTemplateEngine.php:2:// @DEPRECATED ./application/hub/main/template/objects/class_ObjectRegistryTemplateEngine.php:2:// @DEPRECATED @@ -245,6 +256,8 @@ ./inc/classes/exceptions/main/class_MissingMethodException.php:14: * @deprecated Please do no longer use this exception ./inc/classes/interfaces/helper/class_HelpableLogin.php:2:// @DEPRECATED ./inc/classes/interfaces/helper/class_HelpableTemplate.php:2:// @DEPRECATED +./inc/classes/main/streams/input/class_BaseInputStream.php:2:// @DEPRECATED +./inc/classes/main/streams/output/class_BaseOutputStream.php:2:// @DEPRECATED ./inc/classes.php:9: * @deprecated ./inc/database.php:10: * @deprecated ./inc/file_io.php:2:// @DEPRECATED -- 2.39.2