From 8b8734674eb2f842c227a42e9dd7a3264bf6e82f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 3 Dec 2020 16:31:58 +0100 Subject: [PATCH] Continued: - renamed NodeInformationDatabaseFrontend->ifNodeDataIsFound() to findFirstNodeData() and also changed return type from bool to $resultInstance (SearchableResult) - changed type-hint from BaseHubNode (abstract class) to Node (interface) - improved some debug messages MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../class_NodeInformationDatabaseFrontend.php | 22 +++---- .../handler/chunks/class_ChunkHandler.php | 64 ++++++++++++------- .../connection/class_BaseConnectionHelper.php | 18 +++--- .../hub/classes/nodes/class_BaseHubNode.php | 22 +++++-- .../assembler/class_PackageAssembler.php | 4 +- .../fragmenter/class_PackageFragmenter.php | 24 +++++-- .../output/class_RawDataOutputStream.php | 10 ++- .../class_NodeInformationFrontend.php | 10 +-- 8 files changed, 108 insertions(+), 66 deletions(-) diff --git a/application/hub/classes/database/frontend/node_information/class_NodeInformationDatabaseFrontend.php b/application/hub/classes/database/frontend/node_information/class_NodeInformationDatabaseFrontend.php index dbba15fd3..9b971a687 100644 --- a/application/hub/classes/database/frontend/node_information/class_NodeInformationDatabaseFrontend.php +++ b/application/hub/classes/database/frontend/node_information/class_NodeInformationDatabaseFrontend.php @@ -5,7 +5,6 @@ namespace Org\Shipsimu\Hub\Database\Frontend\Node\Information; // Import application-specific stuff use Org\Shipsimu\Hub\Database\Frontend\BaseHubDatabaseFrontend; use Org\Shipsimu\Hub\Database\Frontend\Node\NodeInformationFrontend; -use Org\Shipsimu\Hub\Node\BaseHubNode; use Org\Shipsimu\Hub\Node\Node; // Import framework stuff @@ -77,12 +76,11 @@ class NodeInformationDatabaseFrontend extends BaseHubDatabaseFrontend implements } /** - * Checks whether there is an entry for given node instance + * Tries to find a node instance by it's nodeId field * - * @param $nodeInstance An instance of a Node class - * @return $isFound Whether a node id has been found for this node + * @return $resultInstance An instance of a SearchableResult class */ - public function ifNodeDataIsFound (Node $nodeInstance) { + public function findFirstNodeData () { // Is there cache? if (!isset($GLOBALS[__METHOD__])) { // Now get a search criteria instance @@ -97,7 +95,7 @@ class NodeInformationDatabaseFrontend extends BaseHubDatabaseFrontend implements $resultInstance = $this->doSelectByCriteria($searchInstance); // Is it valid? - $GLOBALS[__METHOD__] = $resultInstance->next(); + $GLOBALS[__METHOD__] = ($resultInstance->next() ? $resultInstance : NULL); } // Return it @@ -108,10 +106,10 @@ class NodeInformationDatabaseFrontend extends BaseHubDatabaseFrontend implements * 'Registers' a new node id along with data provided in the node instance. * This may sound confusing but avoids double code very nicely... * - * @param $nodeInstance A node instance + * @param $nodeInstance An instance of a Node class * @return void */ - public function registerNodeId (BaseHubNode $nodeInstance) { + public function registerNodeId (Node $nodeInstance) { // Get a dataset instance $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION)); @@ -129,11 +127,11 @@ class NodeInformationDatabaseFrontend extends BaseHubDatabaseFrontend implements * 'Registers' a new session id along with data provided in the node instance. * This may sound confusing but avoids double code very nicely... * - * @param $nodeInstance An instance of a BaseHubNode class + * @param $nodeInstance An instance of a Node class * @param $searchInstance An instance of a LocalSearchCriteria class * @return void */ - public function registerSessionId (BaseHubNode $nodeInstance, LocalSearchCriteria $searchInstance) { + public function registerSessionId (Node $nodeInstance, LocalSearchCriteria $searchInstance) { // Get a dataset instance $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION)); @@ -154,11 +152,11 @@ class NodeInformationDatabaseFrontend extends BaseHubDatabaseFrontend implements * 'Registers' a private key along with data provided in the node instance. * This may sound confusing but avoids double code very nicely... * - * @param $nodeInstance An instance of a BaseHubNode class + * @param $nodeInstance An instance of a Node class * @param $searchInstance An instance of a LocalSearchCriteria class * @return void */ - public function registerPrivateKey (BaseHubNode $nodeInstance, LocalSearchCriteria $searchInstance) { + public function registerPrivateKey (Node $nodeInstance, LocalSearchCriteria $searchInstance) { // Get a dataset instance $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION)); diff --git a/application/hub/classes/handler/chunks/class_ChunkHandler.php b/application/hub/classes/handler/chunks/class_ChunkHandler.php index e2d016914..de5cac418 100644 --- a/application/hub/classes/handler/chunks/class_ChunkHandler.php +++ b/application/hub/classes/handler/chunks/class_ChunkHandler.php @@ -16,6 +16,9 @@ use Org\Mxchange\CoreFramework\Registry\Registerable; use Org\Mxchange\CoreFramework\Utils\String\StringUtils; use Org\Mxchange\CoreFramework\Traits\Crypto\CryptoTrait; +// Import SPL stuff +use \UnexpectedValueException; + /** * A Chunk handler * @@ -152,25 +155,25 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera private function initHandler () { // Init finalPackageChunks /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: Initializing handler - CALLED!'); - $this->finalPackageChunks = array( + $this->finalPackageChunks = [ // Array for package content - 'content' => array(), + 'content' => [], // ... and for the hashes - 'hashes' => array(), + 'hashes' => [], // ... marker for that the final array is complete for assembling all chunks 'is_complete' => FALSE, // ... steps done to assemble all chunks 'assemble_steps' => 0, - ); + ]; // ... chunkHashes: $this->chunkHashes = []; // ... eopChunk: - $this->eopChunk = array( + $this->eopChunk = [ 0 => 'INVALID', 1 => 'INVALID', - ); + ]; // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: EXIT!'); @@ -186,8 +189,10 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera // Assert on some elements /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: chunkSplits()=%d - CALLED!', count($chunkSplits))); //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: chunkSplits=' . print_r($chunkSplits, TRUE)); - assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA])); - assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH])); + if (!isset($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA]) || !isset($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH])) { + // Missing array element + ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: chunkSplits=%s', __METHOD__, __LINE__, print_r($chunkSplits, TRUE))); + } // Now hash the raw data again $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA], $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH], FALSE); @@ -234,7 +239,7 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera if (isset($this->finalPackageChunks[$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]])) { // Then throw an exception throw new ChunkAlreadyAssembledException(array($this, $chunkSplits), self::EXCEPTION_CHUNK_ALREADY_ASSEMBLED); - } // END - if + } // Add the chunk data (index 2) to the final array and use the serial number as index //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: serialNumber=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ',hash=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]); @@ -351,8 +356,8 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera // And re-request it with valid serial number (and hash chunk) /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: serialNumber=%s does not match nextSerial=%s, re-questing it ...', $serialNumber, $nextSerial)); $this->rerequestChunkBySerialNumber($nextSerial); - } // END - if - } // END - foreach + } + } // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: EXIT!'); @@ -383,10 +388,10 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera // Is this chunk valid? This should be the case //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: serialNumber=' . $serialNumber . ',hashes=' . $this->finalPackageChunks['hashes'][$serialNumber] . ' - validating ...'); //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('finalPackageChunks=' . print_r($this->finalPackageChunks, TRUE) . 'chunkHashes=' . print_r($this->chunkHashes, TRUE)); - assert($this->isChunkHashValid(array( + assert($this->isChunkHashValid([ self::CHUNK_SPLITS_INDEX_HASH => $this->finalPackageChunks['hashes'][$serialNumber], self::CHUNK_SPLITS_INDEX_RAW_DATA => $content - ))); + ])); // ... and is also in the hash chunk? assert(in_array($this->finalPackageChunks['hashes'][$serialNumber], $this->chunkHashes)); @@ -394,7 +399,7 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera // Verification okay, add it to the raw data /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: Adding %d bytes as raw package data ...', strlen($content))); $this->rawPackageData .= $content; - } // END - foreach + } // The last chunk hash must match with the one from eopChunk[1] /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: eopChunk[1]=%s,index=%d,chunkHashes()=%d', $this->eopChunk[1], (count($this->chunkHashes) - 2), count($this->chunkHashes))); @@ -452,18 +457,26 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera * * @param $chunks An array with chunks and (hopefully) a valid final chunk * @return $isValid Whether the final (last) chunk is valid + * @throws UnexpectedValueException If some unexpected value was found */ private function isValidFinalChunk (array $chunks) { // Default is all fine /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: chunks()=%d - CALLED!', count($chunks))); $isValid = TRUE; + // Get EOP chunk + $chunk = $chunks[count($chunks) - 1]; + // Split the (possible) EOP chunk - $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunks[count($chunks) - 1]); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: chunk=%s', $chunk)); + $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunk); // Make sure chunks with only 3 elements are parsed (for details see ChunkHandler) /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: eopChunk=%s,chunkSplits=%s', $chunks[count($chunks) - 1], print_r($chunkSplits, TRUE))); - assert(count($chunkSplits) == 3); + if (count($chunkSplits) != 3) { + // Must be 3 elements exact + throw new UnexpectedValueException(sprintf('chunk=%s has %d splits, must be exact 3.', $chunk, count($chunkSplits))); + } // Validate final chunk if (substr($chunkSplits[ChunkHandler::CHUNK_SPLITS_INDEX_RAW_DATA], 0, strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)) != PackageFragmenter::END_OF_PACKAGE_IDENTIFIER) { @@ -502,14 +515,14 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera // Then get it first and add it before the EOP chunks /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: Popping entry from stack %s ...', self::STACKER_NAME_CHUNKS_WITHOUT_FINAL)); array_unshift($chunks, $this->getStackInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL)); - } // END - while + } // Add all chunks to the FIFO stacker foreach ($chunks as $chunk) { // Add the chunk /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: chunk=%s', $chunk)); $this->getStackInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP, $chunk); - } // END - foreach + } // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: EXIT!'); @@ -528,7 +541,7 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera // Add the chunk /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: chunk=%s', $chunk)); $this->getStackInstance()->pushNamed(self::STACKER_NAME_CHUNKS_WITHOUT_FINAL, $chunk); - } // END - foreach + } // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: EXIT!'); @@ -555,15 +568,18 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera * iterator. * * @return void + * @throws UnexpectedValueException If some unexpected value was found */ public function handleAvailableChunksWithFinal () { // First check if there are undhandled chunks available + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: CALLED!'); assert($this->ifUnhandledChunksWithFinalAvailable()); // Get an entry from the stacker $chunk = $this->getStackInstance()->popNamed(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP); // Split the string with proper separator character + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: chunk=%s', $chunk)); $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunk); /* @@ -572,7 +588,11 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera * 1 = Serial number * 2 = Raw data */ - assert(count($chunkSplits) == 3); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CHUNK-HANDLER: chunkSplits()=%d', count($chunkSplits))); + if (count($chunkSplits) != 3) { + // Must be 3 elements exact + throw new UnexpectedValueException(sprintf('chunk=%s has %d splits, must be exact 3.', $chunk, count($chunkSplits))); + } // Is the generated hash from data same ("valid") as given hash? if (!$this->isChunkHashValid($chunkSplits)) { @@ -593,7 +613,7 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera // Don't process this chunk return; - } // END - if + } /* * It is now known that (as long as the hash algorithm has no @@ -609,7 +629,7 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera if ($this->getStackInstance()->isStackEmpty(self::STACKER_NAME_CHUNKS_WITH_FINAL_EOP)) { // Then mark the final array as complete $this->markFinalArrayAsCompleted(); - } // END - if + } // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CHUNK-HANDLER: EXIT!'); diff --git a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php index ec409c796..7cb897007 100644 --- a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php @@ -270,7 +270,7 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit // Return the raw data /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: rawData()=' . strlen($chunkData[0]) . ' bytes.'); $rawData = $chunkData[0]; - } // END - if + } // Return raw data /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawData(%d)=%s - EXIT!', strlen($rawData), $rawData)); @@ -301,10 +301,8 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit * @return void */ public function sendRawPackageData (DeliverablePackage $packageInstance) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: packageInstance=%s - CALLED!', $packageInstance->__toString())); - // The helper's state must be 'connected' + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: packageInstance=%s - CALLED!', $packageInstance->__toString())); $this->getStateInstance()->validatePeerStateConnected(); // Implode the package data array and fragement the resulting string, returns the final hash @@ -312,11 +310,11 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit // Is the final hash set? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: finalHash[%s]=%s', gettype($finalHash), $finalHash)); - if ($finalHash !== TRUE) { + if (is_string($finalHash) && !empty($finalHash)) { // Set final hash /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: Setting finalHash=%s,currentFinalHash[%s]=%s', $finalHash, gettype($this->currentFinalHash), $this->currentFinalHash)); $this->currentFinalHash = $finalHash; - } // END - if + } // Reset serial number $this->getFragmenterInstance()->resetSerialNumber($this->currentFinalHash); @@ -337,12 +335,12 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit // Abort here /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CONNECTION-HELPER: dataStream is now empty, exiting loop ...'); break; - } // END - if + } // Add raw data /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: Adding %d bytes to the sending buffer ...', strlen($dataStream))); $rawData .= $dataStream; - } // END - while + } // Calculate buffer size /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawData()=%d - after loop ...', strlen($rawData))); @@ -354,14 +352,14 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit // Init array /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: socketResource[%s]=%s', gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource())); - $encodedDataArray = array( + $encodedDataArray = [ NetworkPackageHandler::RAW_INDEX_FINAL_HASH => $this->currentFinalHash, NetworkPackageHandler::RAW_INDEX_ENCODED_DATA => $encodedData, NetworkPackageHandler::RAW_INDEX_SENT_BYTES => 0, NetworkPackageHandler::RAW_INDEX_SOCKET_INSTANCE => $this->getSocketInstance(), NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE => $bufferSize, NetworkPackageHandler::RAW_INDEX_DIFF => ($bufferSize - strlen($encodedData)), - ); + ]; // Push raw data to the package's outgoing stack $this->getPackageHandlerInstance()->getStackInstance()->pushNamed(NetworkPackageHandler::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray); diff --git a/application/hub/classes/nodes/class_BaseHubNode.php b/application/hub/classes/nodes/class_BaseHubNode.php index fe2cfc915..8d58abffe 100644 --- a/application/hub/classes/nodes/class_BaseHubNode.php +++ b/application/hub/classes/nodes/class_BaseHubNode.php @@ -336,10 +336,17 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC * @return void */ public function bootstrapAcquireNodeId (Requestable $requestInstance, Responseable $responseInstance) { + // Find node by id + $resultInstance = $this->getFrontendInstance()->findFirstNodeData(); + // Is there a node id? - if ($this->getFrontendInstance()->ifNodeDataIsFound($this)) { + if ($resultInstance->valid()) { + // Get current record + $nodeData = $resultInstance->current(); + // Get the node id from result and set it - $this->setNodeId($this->getField(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_ID)); + /* DEBUG-DIE: */ die(sprintf('[%s:%d]: nodeData=%s', __METHOD__, __LINE__, print_r($nodeData, TRUE))); + $this->setNodeId($resultInstance->getField(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_ID)); // Output message self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BOOTSTRAP: Re-using found node-id: ' . $this->getNodeId() . ''); @@ -401,10 +408,13 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC * @return void */ public function bootstrapGeneratePrivateKey () { + // Find node by id + $resultInstance = $this->getFrontendInstance()->findFirstNodeData(); + // Is it valid? - if ($this->getFrontendInstance()->ifNodeDataIsFound($this)) { + if ($resultInstance->valid()) { // Is the element set? - if (is_null($this->getField(NodeInformationDatabaseFrontend::DB_COLUMN_PRIVATE_KEY))) { + if (is_null($this->getFrontendInstance()->getField(NodeInformationDatabaseFrontend::DB_COLUMN_PRIVATE_KEY))) { /* * Auto-generate the private key for e.g. out-dated database * "tables". This allows a smooth update for the underlaying @@ -413,8 +423,8 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC $this->generatePrivateKeyAndHash($this->getSearchInstance()); } else { // Get the node id from result and set it - $this->setPrivateKey(base64_decode($this->getField(NodeInformationDatabaseFrontend::DB_COLUMN_PRIVATE_KEY))); - $this->setNodePrivateKeyHash($this->getField(NodeInformationDatabaseFrontend::DB_COLUMN_PRIVATE_KEY_HASH)); + $this->setPrivateKey(base64_decode($this->getFrontendInstance()->getField(NodeInformationDatabaseFrontend::DB_COLUMN_PRIVATE_KEY))); + $this->setNodePrivateKeyHash($this->getFrontendInstance()->getField(NodeInformationDatabaseFrontend::DB_COLUMN_PRIVATE_KEY_HASH)); // Output message self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BOOTSTRAP: Re-using found private key hash: ' . $this->getNodePrivateKeyHash() . ''); diff --git a/application/hub/classes/package/assembler/class_PackageAssembler.php b/application/hub/classes/package/assembler/class_PackageAssembler.php index c27fcb807..7274760bd 100644 --- a/application/hub/classes/package/assembler/class_PackageAssembler.php +++ b/application/hub/classes/package/assembler/class_PackageAssembler.php @@ -399,10 +399,8 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, * @return $isset Whether start/end marker are set */ private function ifStartEndMarkersSet (string $data) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-ASSEMBLER: data()=%d - CALLED!', strlen($data))); - // Determine it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-ASSEMBLER: data()=%d - CALLED!', strlen($data))); $isset = ( ( substr($data, 0, strlen(HandleableRawData::STREAM_START_MARKER)) == HandleableRawData::STREAM_START_MARKER diff --git a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php index fb99c473d..0b180dd57 100644 --- a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php @@ -77,6 +77,12 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera */ private $processedPackages = []; + /** + * Array for markers (TRUE) of processed packages, same index position as + * above array. + */ + private $isProcessedPackage = []; + /** * Serial numbers (array key is final hash) */ @@ -212,9 +218,9 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera // Is the array index there? $isProcessed = ( ( - isset($this->processedPackages[$index]) + isset($this->isProcessedPackage[$index]) ) && ( - $this->processedPackages[$index] === TRUE + $this->isProcessedPackage[$index] === TRUE ) ); @@ -232,7 +238,7 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera private function markPackageDataProcessed (DeliverablePackage $packageInstance) { // Remember it (until we may remove it) //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: packageInstance=%s - CALLED!', $packageInstance->__toString())); - $this->processedPackages[$this->getProcessedPackagesIndex($packageInstance)] = TRUE; + $this->isProcessedPackage[$this->getProcessedPackagesIndex($packageInstance)] = TRUE; } /** @@ -243,11 +249,15 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera */ private function getFinalHashFromPackageInstance (DeliverablePackage $packageInstance) { // Make sure it is there - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: packageInstance=%s - CALLED!', $packageInstance->__toString())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: packageInstance=%s - CALLED!', $packageInstance->__toString())); assert(isset($this->processedPackages[$this->getProcessedPackagesIndex($packageInstance)])); // Return it - return $this->processedPackages[$this->getProcessedPackagesIndex($packageInstance)]; + $finalHash = $this->processedPackages[$this->getProcessedPackagesIndex($packageInstance)]; + + // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s - EXIT!', gettype($finalHash), $finalHash)); + return $finalHash; } /** @@ -533,7 +543,7 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera $finalHash = $this->generateHashFromRawData($rawData); // Remember it - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash=%s', $finalHash)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s', gettype($finalHash), $finalHash)); $this->processedPackages[$this->getProcessedPackagesIndex($packageInstance)] = $finalHash; // Init pointer and reset serial number @@ -552,7 +562,7 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera // Get the final hash from the package data /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: Package is already processed, getting final hash ...'); $finalHash = $this->getFinalHashFromPackageInstance($packageInstance); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash=%s', $finalHash)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-FRAGMENTER: finalHash[%s]=%s', gettype($finalHash), $finalHash)); } // Return final hash diff --git a/application/hub/classes/streams/raw_data/output/class_RawDataOutputStream.php b/application/hub/classes/streams/raw_data/output/class_RawDataOutputStream.php index 68a4243c9..91870c868 100644 --- a/application/hub/classes/streams/raw_data/output/class_RawDataOutputStream.php +++ b/application/hub/classes/streams/raw_data/output/class_RawDataOutputStream.php @@ -9,6 +9,9 @@ use Org\Shipsimu\Hub\Handler\Network\RawData\HandleableRawData; use Org\Mxchange\CoreFramework\Stream\BaseStream; use Org\Mxchange\CoreFramework\Stream\Output\OutputStream; +// Import SPL stuff +use \InvalidArgumentException; + /** * A RawDataOutputStream class * @@ -60,10 +63,15 @@ class RawDataOutputStream extends BaseStream implements OutputStream { * * @param $data The data (string mostly) to "stream" * @return $data The data (string mostly) to "stream" + * @throws InvalidArgumentException If the parameter is invalid */ public function streamData (string $data) { - // Trace message + // Is it not empty? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-OUTPUT-STREAM: data()=%d - CALLED!', strlen($data))); + if (empty($data)) { + // Invalid parameter + throw new InvalidArgumentException('Parameter "data" is empty'); + } /* * Encode the data with BASE64 encoding and put it in a "frame": diff --git a/application/hub/interfaces/database/frontend/node_information/class_NodeInformationFrontend.php b/application/hub/interfaces/database/frontend/node_information/class_NodeInformationFrontend.php index b03d17188..cdcd5a15a 100644 --- a/application/hub/interfaces/database/frontend/node_information/class_NodeInformationFrontend.php +++ b/application/hub/interfaces/database/frontend/node_information/class_NodeInformationFrontend.php @@ -3,7 +3,7 @@ namespace Org\Shipsimu\Hub\Database\Frontend\Node; // Import application-specific stuff -use Org\Shipsimu\Hub\Node\BaseHubNode; +use Org\Shipsimu\Hub\Node\Node; // Import framework stuff use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria; @@ -36,10 +36,10 @@ interface NodeInformationFrontend extends DatabaseFrontend { * 'Registers' a new node id along with data provided in the node instance. * This may sound confusing but avoids double code very nicely... * - * @param $nodeInstance A node instance + * @param $nodeInstance An instance of a Node class * @return void */ - function registerNodeId (BaseHubNode $nodeInstance); + function registerNodeId (Node $nodeInstance); /** * 'Registers' a new session id along with data provided in the node instance. @@ -49,7 +49,7 @@ interface NodeInformationFrontend extends DatabaseFrontend { * @param $searchInstance An instance of a LocalSearchCriteria class * @return void */ - function registerSessionId (BaseHubNode $nodeInstance, LocalSearchCriteria $searchInstance); + function registerSessionId (Node $nodeInstance, LocalSearchCriteria $searchInstance); /** * 'Registers' a private key along with data provided in the node instance. @@ -60,6 +60,6 @@ interface NodeInformationFrontend extends DatabaseFrontend { * @param $searchInstance An instance of a LocalSearchCriteria class * @return void */ - function registerPrivateKey (BaseHubNode $nodeInstance, LocalSearchCriteria $searchInstance); + function registerPrivateKey (Node $nodeInstance, LocalSearchCriteria $searchInstance); } -- 2.39.5