// Import framework stuff
use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
-use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
use Org\Mxchange\CoreFramework\Registry\Registerable;
use Org\Mxchange\CoreFramework\Traits\Stream\Output\OutputStreamTrait;
* @return $chunkData Raw data chunk
*/
private function getRawDataFromPackageArray (DeliverablePackage $packageInstance) {
- // Make sure the final hash is set
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: currentFinalHash=' . $this->currentFinalHash);
- assert((is_string($this->currentFinalHash)) && (!empty($this->currentFinalHash)));
-
// Get the next raw data chunk from the fragmenter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: packageInstance=%s,this->currentFinalHash=%s - CALLED!', $packageInstance->__toString(), $this->currentFinalHash));
$rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash);
+ // At least one entry should be there?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawDataChunk()=%d', count($rawDataChunk)));
+ if (count($rawDataChunk) == 0) {
+ // No, then all has been added
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: Returning empty string ... - EXIT!');
+ return '';
+ }
+
// Get chunk hashes and chunk data
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: rawDataChunk=' . print_r($rawDataChunk, TRUE));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawDataChunk()=%d', count($rawDataChunk)));
+ //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawDataChunk=%s', print_r($rawDataChunk, TRUE)));
$chunkHashes = array_keys($rawDataChunk);
$chunkData = array_values($rawDataChunk);
$rawData = '';
// Is the required data there?
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: chunkHashes()=' . count($chunkHashes) . ',chunkData()=' . count($chunkData));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: chunkHashes()=%s,chunkData()=%s', count($chunkHashes), count($chunkData)));
//* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('chunkData='.print_r($chunkData, TRUE));
if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
// Remember this chunk as queued
$this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
// Return the raw data
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: rawData()=' . strlen($chunkData[0]) . ' bytes.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawData()=%d bytes.', strlen($chunkData[0])));
$rawData = $chunkData[0];
}
$nodeData = $resultInstance->current();
// Get the node id from result and set it
- /* DEBUG-DIE: */ die(sprintf('[%s:%d]: nodeData=%s', __METHOD__, __LINE__, print_r($nodeData, TRUE)));
+ /* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: nodeData=%s', __METHOD__, __LINE__, print_r($nodeData, TRUE)));
$this->setNodeId($resultInstance->getField(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_ID));
// Output message
use Org\Shipsimu\Hub\Generic\BaseHubSystem;
use Org\Shipsimu\Hub\Handler\Network\PackageCode\UnsupportedPackageCodeHandlerException;
use Org\Shipsimu\Hub\Handler\Network\RawData\HandleableRawData;
-use Org\Shipsimu\Hub\Network\Delivery\Deliverable;
use Org\Shipsimu\Hub\Network\Package\DeliverablePackage;
use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler;
use Org\Shipsimu\Hub\Network\Package\Delivery\Fragment\PackageFragmenter;
use Org\Shipsimu\Hub\Network\Receive\Receivable;
+use Org\Shipsimu\Hub\Traits\Network\Package\Delivery\DeliverableTrait;
// Import framework stuff
+use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
use Org\Mxchange\CoreFramework\Registry\Registerable;
use Org\Mxchange\CoreFramework\Traits\Handler\HandleableTrait;
*/
class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, Visitable {
// Load traits
+ use DeliverableTrait;
use HandleableTrait;
use InputStreamTrait;
use StackableTrait;
*/
private $callbacks = [];
- /**
- * A network package handler instance
- */
- private $packageHandlerInstance = NULL;
-
/**
* Protected constructor
*
$assemblerInstance->setStackInstance($stackInstance);
// Return the prepared instance
- //* DEBUG-DIE: */ die(sprintf('[%s:%d]: assemblerInstance=%s', __METHOD__, __LINE__, print_r($assemblerInstance, TRUE)));
+ //* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: assemblerInstance=%s', __METHOD__, __LINE__, print_r($assemblerInstance, TRUE)));
return $assemblerInstance;
}
*/
public function clearPendingData () {
// Clear it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: CALLED!');
$this->pendingData = '';
}
- /**
- * Setter for network package handler instance
- *
- * @param $packageHandlerInstance The network package instance we shall set
- * @return void
- */
- protected final function setPackageHandlerInstance (Deliverable $packageHandlerInstance) {
- $this->packageHandlerInstance = $packageHandlerInstance;
- }
-
- /**
- * Getter for network package handler instance
- *
- * @return $packageHandlerInstance The network package handler instance we shall set
- */
- protected final function getPackageHandlerInstance () {
- return $this->packageHandlerInstance;
- }
-
/**
* Checks whether the input buffer (stacker to be more preceise) is empty.
*
*/
private function ifInputBufferIsEmpty () {
// Check it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: CALLED!');
$isInputBufferEmpty = $this->getPackageHandlerInstance()->getStackInstance()->isStackEmpty(NetworkPackageHandler::STACKER_NAME_DECODED_HANDLED);
// Return it
use Org\Mxchange\CoreFramework\Traits\Crypto\CryptoTrait;
use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
+// Import SPL stuff
+use \InvalidArgumentException;
+
/**
* A PackageFragmenter class to fragment package data into smaller chunks for
* delivery. This class calculates a final hash on the raw input data and
* @param $finalHash Final hash to initialize pointer for
* @return void
*/
- private function initPointer ($finalHash) {
+ private function initPointer (string $finalHash) {
// Init counter
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s - CALLED!', gettype($finalHash), $finalHash));
$this->chunkPointers[$finalHash] = 0;
*
* @param $finalHash Final hash to get current pointer for
*/
- private function getCurrentChunkPointer ($finalHash) {
+ private function getCurrentChunkPointer (string $finalHash) {
// Is the final hash valid?
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s - CALLED!', gettype($finalHash), $finalHash));
- assert(strlen($finalHash) > 0);
+ assert(!empty($finalHash));
// Is the pointer already initialized?
assert(isset($this->chunkPointers[$finalHash]));
*
* @param $finalHash Final hash to advance the pointer for
*/
- private function nextChunkPointer ($finalHash) {
+ private function nextChunkPointer (string $finalHash) {
// Is the pointer already initialized?
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s - CALLED!', gettype($finalHash), $finalHash));
assert(isset($this->chunkPointers[$finalHash]));
* @param $hash Hash to substract it's length
* @return $dataChunkSize The chunk size
*/
- private function getDataChunkSizeFromHash ($hash) {
+ private function getDataChunkSizeFromHash (string $hash) {
// Calculate real (data) chunk size
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: hash=%s - CALLED!', $hash));
$dataChunkSize = (
* @return $hash Hash from the raw data
* @todo Implement a way to send non-announcement packages with extra-salt
*/
- private function generateHashFromRawData ($rawData) {
+ private function generateHashFromRawData (string $rawData) {
/*
* Get the crypto instance and hash the data with no extra salt because
* the other peer doesn't have *this* peer's salt.
* @param $finalHash Final hash for raw (unencoded) data
* @return void
*/
- private function appendEndOfPackageChunk ($lastChunk, $finalHash) {
+ private function appendEndOfPackageChunk (string $lastChunk, string $finalHash) {
// Generate end-of-package marker
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: lastChunk=%s,finalHash[%s]=%s - CALLED!', $lastChunk, gettype($finalHash), $finalHash));
$chunkData =
* @param $finalHash Final hash from the raw data
* @return void
*/
- private function splitEncodedDataIntoChunks ($rawData, $finalHash) {
+ private function splitEncodedDataIntoChunks (string $rawData, string $finalHash) {
// Make sure final hashes with at least 32 bytes can pass
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: rawData=%s,finalHash[%s]=%s - CALLED!', $rawData, gettype($finalHash), $finalHash));
assert(strlen($finalHash) >= 32);
* @param $prepend Whether append (default) or prepend the chunk
* @return void
*/
- private function addChunkData ($finalHash, $chunkData, $prepend = FALSE) {
+ private function addChunkData (string $finalHash, string $chunkData, bool $prepend = FALSE) {
// Hash it
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s,chunkData=%s,prepend=%d - CALLED!', gettype($finalHash), $finalHash, $chunkData, intval($prepend)));
$rawDataHash = $this->getCryptoInstance()->hashString($chunkData, '', FALSE);
* @param $finalHash Final hash from the raw data
* @return void
*/
- private function prependHashChunk ($finalHash) {
+ private function prependHashChunk (string $finalHash) {
// "Implode" the whole array of hashes into one string
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s - CALLED!', gettype($finalHash), $finalHash));
$rawData = self::HASH_CHUNK_IDENTIFIER . implode(self::CHUNK_HASH_SEPARATOR, $this->chunkHashes[$finalHash]);
* @param $finalHash Final hash
* @return $encodedSerialNumber The next hexadecimal-encoded serial number
*/
- public function getNextHexSerialNumber ($finalHash) {
+ public function getNextHexSerialNumber (string $finalHash) {
// Assert on maximum serial number length
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s - CALLED!', gettype($finalHash), $finalHash));
assert(isset($this->serialNumber[$finalHash]));
* @param $finalHash Final hash for faster lookup
* @return $rawDataChunk An instance of a DeliverablePackage class chunk
* @throws AssertionException If $finalHash was not 'TRUE'
- */
- public function getNextRawDataChunk ($finalHash) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s - CALLED!', gettype($finalHash), $finalHash));
+ * @throws InvalidArgumentException If a parameter is invalid
+ */
+ public function getNextRawDataChunk (string $finalHash) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash(%d)=%s - CALLED!', strlen($finalHash), $finalHash));
+ if (empty($finalHash)) {
+ // Should not be empty
+ throw new InvalidArgumentException('Parameter "finalHash" is empty');
+ }
try {
// Get current chunk index
// If there is no entry left, return an empty array
if ((!isset($this->chunkHashes[$finalHash][$current])) || (!isset($this->chunks[$finalHash][$current]))) {
// No more entries found
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: finalHash=' . $finalHash . ',current=' . $current . ' - No more entries found!');
- return array();
- } // END - if
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: finalHash=' . $finalHash . ',current=' . $current . ',chunkHashes()=' . count($this->chunkHashes[$finalHash]) .' - Entry choosen ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash=%s,current=%s - No more entries found!', $finalHash, $current));
+ return [];
+ }
// Generate the array
- $rawDataChunk = array(
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash=%s,current=%s,chunkHashes()=%d - Entry choosen ...', $finalHash, $current, count($this->chunkHashes[$finalHash])));
+ $rawDataChunk = [
$this->chunkHashes[$finalHash][$current] => $this->chunks[$finalHash][$current]
- );
+ ];
// Count one index up
$this->nextChunkPointer($finalHash);
// Return the chunk array
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: Returning rawDataChunk ... - EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: Returning rawDataChunk ... - EXIT!');
return $rawDataChunk;
}
* @param $finalHash Final hash to reset counter for
* @return void
*/
- public function resetSerialNumber ($finalHash) {
- // Final hash must be set
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s - CALLED!', gettype($finalHash), $finalHash));
- assert((is_string($finalHash)) && (!empty($finalHash)));
+ public function resetSerialNumber (string $finalHash) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash(%d)=%s - CALLED!', strlen($finalHash), $finalHash));
+ if (empty($finalHash)) {
+ // Should not be empty
+ throw new InvalidArgumentException('Parameter "finalHash" is empty');
+ }
+
// Reset/set serial number
$this->serialNumber[$finalHash] = 0;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
interface Fragmentable extends HubInterface {
+ /**
+ * "Getter" for the next hexadecimal-encoded serial number
+ *
+ * @param $finalHash Final hash
+ * @return $encodedSerialNumber The next hexadecimal-encoded serial number
+ */
+ function getNextHexSerialNumber (string $finalHash);
+
/**
* This method does "implode" the given package data array into one long
* string, splits it into small chunks, adds a serial number and checksum
- * to all chunks and prepends a final hashsum chunk. It will return the
- * final hash for faster processing of packages.
+ * to all chunks and prepends a chunk with all hashes only in it. It will
+ * return the final hash for faster processing of packages.
*
- * @param $packageInstance An instance of a DeliverablePackage instance
+ * @param $packageInstance An instance of a DeliverablePackage class array
* @param $helperInstance An instance of a ConnectionHelper class
* @return $finalHash Final hash for faster processing
+ * @todo $helperInstance is unused
*/
function fragmentPackageArray (DeliverablePackage $packageInstance, ConnectionHelper $helperInstance);
*
* @param $finalHash Final hash for faster lookup
* @return $rawDataChunk An instance of a DeliverablePackage class chunk
+ * @throws AssertionException If $finalHash was not 'TRUE'
+ * @throws InvalidArgumentException If a parameter is invalid
+ */
+ function getNextRawDataChunk (string $finalHash);
+
+ /**
+ * Resets the serial number to zero for given final hash
+ *
+ * @param $finalHash Final hash to reset counter for
+ * @return void
*/
- function getNextRawDataChunk ($finalHash);
+ function resetSerialNumber (string $finalHash);
}
--- /dev/null
+<?php
+// Own namespace
+namespace Org\Shipsimu\Hub\Traits\Network\Package\Delivery;
+
+// Import application-specific stuff
+use Org\Shipsimu\Hub\Network\Delivery\Deliverable;
+
+/**
+ * A trait for package delivery
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
+ * @license GNU GPL 3.0 or any newer version
+ * @link http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+trait DeliverableTrait {
+ /**
+ * A network package handler instance
+ */
+ private $packageHandlerInstance = NULL;
+
+ /**
+ * Setter for network package handler instance
+ *
+ * @param $packageHandlerInstance The network package instance we shall set
+ * @return void
+ */
+ protected final function setPackageHandlerInstance (Deliverable $packageHandlerInstance) {
+ $this->packageHandlerInstance = $packageHandlerInstance;
+ }
+
+ /**
+ * Getter for network package handler instance
+ *
+ * @return $packageHandlerInstance The network package handler instance we shall set
+ */
+ protected final function getPackageHandlerInstance () {
+ return $this->packageHandlerInstance;
+ }
+
+}