From: Roland Häder Date: Thu, 3 Dec 2020 20:50:44 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d2bd97b83dc54e5f93db4e2eb5a62f70ccd79ab1;p=hub.git Continued: - improved some debug messages - thrown more SPL exceptions instead of assert() - updated core framework Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index b35386050..8f74603a0 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -189,15 +189,16 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unl=%s - CALLED!', strtoupper($this->getSocketProtocol()), $unl)); if (empty($unl)) { // Throw again - throw new InvalidArgumentException('unl is empty'); + throw new InvalidArgumentException('Parameter "unl" is empty'); } // Get current package data $packageInstance = $this->getPackageDataInstance(); // So, does both match? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance->recipientUnl=%s', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipientUnl())); //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: unl=' . $unl . ',packageInstance=' . print_r($packageInstance, true)); - $matches = (($packageInstance->getRecipientUnl() !== '') && ($packageInstance->getRecipientUnl() === $unl)); + $matches = (!empty($packageInstance->getRecipientUnl()) && $packageInstance->getRecipientUnl() === $unl); // Return result /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); diff --git a/application/hub/classes/dht/node/class_NodeDhtFacade.php b/application/hub/classes/dht/node/class_NodeDhtFacade.php index 918b68bad..3a51a3a46 100644 --- a/application/hub/classes/dht/node/class_NodeDhtFacade.php +++ b/application/hub/classes/dht/node/class_NodeDhtFacade.php @@ -22,6 +22,7 @@ use Org\Mxchange\CoreFramework\Traits\Database\Frontend\DatabaseFrontendTrait; // Import SPL stuff use \InvalidArgumentException; +use \UnexpectedValueException; /** * A Node DHT facade class @@ -252,6 +253,7 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { * @param $forceUpdate Optionally force update, don't register (default: register if not found) * @return void * @throws NodeSessionIdVerficationException If the node was not found and update is forced + * @throws UnexpectedValueException If $resultInstance does not implement wanted interface */ public function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, bool $forceUpdate = FALSE) { // Get a search criteria class @@ -277,10 +279,11 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { $resultInstance = $this->getFrontendInstance()->doSelectByCriteria($searchInstance); // Make sure the result instance is valid - assert($resultInstance instanceof SearchableResult); - - // Is there already an entry? - if ($resultInstance->valid()) { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: resultInstance[]=%s', gettype($resultInstance))); + if (!($resultInstance instanceof SearchableResult)) { + // Not valid instance + throw new UnexpectedValueException(sprintf('resultInstance[]=%s does not implement SearchableResult', gettype($resultInstance))); + } elseif ($resultInstance->valid()) { // Entry found, so update it $this->getFrontendInstance()->updateNodeByMessageInstance($messageInstance, $handlerInstance, $searchInstance); } elseif ($forceUpdate === FALSE) { @@ -296,6 +299,9 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { // Save last exception $handlerInstance->setLastException($this->getFrontendInstance()->getLastException()); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: EXIT!'); } /** @@ -308,19 +314,18 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { * @param $andKey Array of $separator-separated list of elements which all must match * @param $separator Sepator char (1st parameter for explode() call) * @return $nodeList An array with all found nodes + * @throws UnexpectedValueException If $resultInstance does not implement wanted interface */ public function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, string $excludeKey, string $andKey, string $separator) { // Get a search criteria class /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: messageInstance=%s,handlerInstance=%s,excludeKey=%s,andKey=%s,separator=%s - CALLED!', $messageInstance->__toString(), $handlerInstance->__toString(), $excludeKey, $andKey, $separator)); - /* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: state=%s,messageInstance=%s', $this->getPrintableState(), print_r($messageInstance, TRUE))); + //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: state=%s,messageInstance=%s', $this->getPrintableState(), print_r($messageInstance, TRUE))); $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Add all keys foreach (explode($separator, $messageData[$andKey]) as $criteria) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: andKey=' . $andKey . ',criteria=' . $criteria); - // Add it and leave any 'my-' prefix out + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: andKey=' . $andKey . ',criteria=' . $criteria); $searchInstance->addChoiceCriteria(str_replace('my-', '', $andKey), $criteria); } @@ -334,8 +339,15 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { $resultInstance = $this->getFrontendInstance()->doSelectByCriteria($searchInstance); // Make sure the result instance is valid - assert($resultInstance instanceof SearchableResult); - assert($resultInstance->valid()); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: resultInstance[]=%s', gettype($resultInstance))); + if (!($resultInstance instanceof SearchableResult)) { + // Not valid instance + throw new UnexpectedValueException(sprintf('resultInstance[]=%s does not implement SearchableResult', gettype($resultInstance))); + } elseif (!$resultInstance->valid()) { + // Then skip below loop + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: No results found, returning empty array ... - EXIT!'); + return []; + } // Init array $nodeList = []; @@ -347,12 +359,10 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { assert(is_array($current)); assert(count($current) > 0); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: current(' . count($current) . ')[' . gettype($current) . ']=' . print_r($current, TRUE)); - /* * Remove some keys as they should not be published. */ + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: current(' . count($current) . ')[' . gettype($current) . ']=' . print_r($current, TRUE)); unset($current[$this->getFrontendInstance()->getIndexKey()]); // Add this entry @@ -377,11 +387,10 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { */ public function insertNodeList (array $nodeList) { // If no node is in the list (array), skip the rest of this method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: nodeList()=%d - CALLED!', count($nodeList))); if (count($nodeList) == 0) { - // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: No node record has been returned.'); - // Abort here + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: No node record has been returned.'); return; } @@ -390,6 +399,9 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { // Insert all entries $this->getStackInstance()->pushNamed(self::STACKER_NAME_INSERT_NODE, $nodeData); } + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: EXIT!'); } /** @@ -397,6 +409,7 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { * * @param $packageInstance An instance of a DeliverablePackage class * @return $recipients An indexed array with DHT recipients + * @throws UnexpectedValueException If $resultInstance does not implement wanted interface */ public function findRecipientsByPackageInstance (DeliverablePackage $packageInstance) { // Query get a result instance back from DHT database frontend @@ -404,12 +417,14 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { $resultInstance = $this->getFrontendInstance()->getResultFromExcludedSender($packageInstance); // Make sure the result instance is valid - assert($resultInstance instanceof SearchableResult); - - // No entries found? - if (!$resultInstance->valid()) { + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: resultInstance[]=%s', gettype($resultInstance))); + if (!($resultInstance instanceof SearchableResult)) { + // Not valid instance + throw new UnexpectedValueException(sprintf('resultInstance[]=%s does not implement SearchableResult', gettype($resultInstance))); + } elseif (!$resultInstance->valid()) { // Then skip below loop - return array(); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: No results found, returning empty array ... - EXIT!'); + return []; } // Init array @@ -419,13 +434,14 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { while ($resultInstance->next()) { // Get current entry $current = $resultInstance->current(); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: current=' . print_r($current, TRUE)); // Add instance to recipient list + /* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: current=%s', print_r($current, TRUE))); array_push($recipients, $current); } // Return filled array + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: recipients()=%d - EXIT!', count($recipients))); return $recipients; } @@ -436,21 +452,29 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { * @param $value Value to check on found key * @return $recipiens Array with DHT recipients from given key/value pair * @throws InvalidArgumentException If $key is empty + * @throws UnexpectedValueException If $resultInstance does not implement wanted interface */ public function findRecipientsByKey (string $key, $value) { // Is key parameter valid? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: key=%s,value[%s]=%s - CALLED!', $key, gettype($value), $value)); if (empty($key)) { // Throw exception - throw new InvalidArgumentException('Parameter key is empty'); + throw new InvalidArgumentException('Parameter "key" is empty'); } // Look for all suitable nodes $resultInstance = $this->getFrontendInstance()->getResultFromKeyValue($key, $value); // Make sure the result instance is valid - assert($resultInstance instanceof SearchableResult); - assert($resultInstance->valid()); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: resultInstance[]=%s', gettype($resultInstance))); + if (!($resultInstance instanceof SearchableResult)) { + // Not valid instance + throw new UnexpectedValueException(sprintf('resultInstance[]=%s does not implement SearchableResult', gettype($resultInstance))); + } elseif (!$resultInstance->valid()) { + // Then skip below loop + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: No results found, returning empty array ... - EXIT!'); + return []; + } // Init array $recipients = []; @@ -461,6 +485,7 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { $current = $resultInstance->current(); // Add instance to recipient list + /* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: current=%s', print_r($current, TRUE))); array_push($recipients, $current); } diff --git a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php index 6927e0ce1..193921b14 100644 --- a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php @@ -27,6 +27,9 @@ use Org\Mxchange\CoreFramework\Traits\State\StateableTrait; use Org\Mxchange\CoreFramework\Visitor\Visitable; use Org\Mxchange\CoreFramework\Visitor\Visitor; +// Import SPL stuff +use \LogicException; + /** * A general ConnectionHelper class * @@ -305,6 +308,7 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit * * @param $packageInstance An instance of a DeliverablePackage class * @return void + * @throws LogicException If $rawData was not filled with some raw data */ public function sendRawPackageData (DeliverablePackage $packageInstance) { // The helper's state must be 'connected' @@ -348,8 +352,14 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit $rawData .= $dataStream; } - // Calculate buffer size + // Was some raw data been collected? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONNECTION-HELPER: rawData()=%d - after loop ...', strlen($rawData))); + if (empty($rawData)) { + // That should not happen + throw new LogicException('rawData should not be empty at this point.'); + } + + // Calculate buffer size $bufferSize = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($this->getProtocolName() . '_buffer_length'); // Encode the raw data with our output-stream diff --git a/application/hub/classes/nodes/boot/class_HubBootNode.php b/application/hub/classes/nodes/boot/class_HubBootNode.php index 78a3f6db2..a61697b5e 100644 --- a/application/hub/classes/nodes/boot/class_HubBootNode.php +++ b/application/hub/classes/nodes/boot/class_HubBootNode.php @@ -72,10 +72,8 @@ class HubBootNode extends BaseHubNode implements Node, Registerable { // Get UNL $unl = $this->detectOwnUniversalNodeLocator(); - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-BOOT-NODE: unl=' . $unl); - // Now check if the IP address matches one of the bootstrap nodes + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-BOOT-NODE: unl=' . $unl); if ($this->ifAddressMatchesBootstrapNodes($unl)) { // Get our port from configuration $ourPort = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('node_listen_port'); diff --git a/application/hub/classes/nodes/class_BaseHubNode.php b/application/hub/classes/nodes/class_BaseHubNode.php index 035672ab7..b2e818f00 100644 --- a/application/hub/classes/nodes/class_BaseHubNode.php +++ b/application/hub/classes/nodes/class_BaseHubNode.php @@ -30,6 +30,7 @@ use Org\Mxchange\CoreFramework\Generic\NullPointerException; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Request\Requestable; use Org\Mxchange\CoreFramework\Response\Responseable; +use Org\Mxchange\CoreFramework\Result\Search\SearchableResult; use Org\Mxchange\CoreFramework\Task\Taskable; use Org\Mxchange\CoreFramework\Traits\Crypto\CryptoTrait; use Org\Mxchange\CoreFramework\Traits\Database\Frontend\DatabaseFrontendTrait; @@ -233,7 +234,7 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC * @param $remoteAddr IP address to checkout against our bootstrapping list * @return $isFound Whether the IP is found */ - protected function ifAddressMatchesBootstrapNodes ($remoteAddr) { + protected function ifAddressMatchesBootstrapNodes (string $remoteAddr) { // By default nothing is found $isFound = FALSE; @@ -340,7 +341,8 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC $resultInstance = $this->getFrontendInstance()->findFirstNodeData(); // Is there a node id? - if ($resultInstance->valid()) { + /* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: resultInstance=%s', __METHOD__, __LINE__, print_r($resultInstance, TRUE))); + if ($resultInstance instanceof SearchableResult && $resultInstance->valid()) { // Get current record $nodeData = $resultInstance->current(); @@ -349,7 +351,7 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC $this->setNodeId($resultInstance->getField(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_ID)); // Output message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BOOTSTRAP: Re-using found node-id: ' . $this->getNodeId() . ''); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BOOTSTRAP: Re-using found node-id: %s', $this->getNodeId())); } else { // Get an RNG instance $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class'); @@ -364,7 +366,7 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC $this->getFrontendInstance()->registerNodeId($this); // Output message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BOOTSTRAP: Created new node-id: ' . $this->getNodeId() . ''); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BOOTSTRAP: Created new node-id: %s', $this->getNodeId())); } } diff --git a/core b/core index 69c5071a6..0a198892c 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 69c5071a6d75b58df3c1f4c6057a09a3ac491dda +Subproject commit 0a198892c079422ef72d679321caad9310b417f4