// Import SPL stuff
use \InvalidArgumentException;
+use \UnexpectedValueException;
/**
* A Node DHT facade class
* @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
$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) {
// Save last exception
$handlerInstance->setLastException($this->getFrontendInstance()->getLastException());
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-DHT-FACADE: EXIT!');
}
/**
* @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);
}
$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 = [];
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
*/
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;
}
// 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!');
}
/**
*
* @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
$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
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;
}
* @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 = [];
$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);
}