*
* @param $packageInstance An instance of a DeliverablePackage class
* @return $hash Private key's hash
- * @throws InvalidPrivateKeyHashException If the private key's hash is not valid
- * @throws InvalidArgumentException If $packageInstance does not contain senderId
*/
private function determineSenderPrivateKeyHash (DeliverablePackage $packageInstance) {
- // Is the parameter valid?
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
- if ($packageInstance->getSenderId() == '') {
- // Invalid $packageInstance
- throw new InvalidArgumentException('packageInstance does not contain senderId');
- }
-
// Get DHT instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
$dhtInstance = DhtObjectFactory::createDhtInstance('node');
// Ask DHT for sender's id or address (both session id)
return $senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH];
}
- // Don't accept empty keys
- if (empty($packageInstance->getSenderPrivateKeyHash())) {
- // This needs fixing
- throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in decodedData'), BaseHubSystem::EXCEPTION_INVALID_PRIVATE_KEY_HASH);
- }
-
// There is no DHT entry so, accept the hash from decoded data
/* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Returning packagheInstance->senderPrivateKeyHash=%s - EXIT!', $packageInstance->getSenderPrivateKeyHash()));
return $packageInstance->getSenderPrivateKeyHash();
// This avoids an exception after all packages has failed
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: stackerName=%s is empty - EXIT!', $stackerName));
return;
- } // END - if
+ }
// Pop the entry (it should be it)
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Getting next stack from stackerName=%s ...', $stackerName));
// Skip to next entry
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Package declared for recipient ' . $currentRecipient);
$iteratorInstance->next();
- } // END - while
+ }
/*
* The recipient list can be cleaned up here because the package which
// This connection is shutting down
// @TODO We may want to do somthing more here?
return;
- } // END - if
+ }
// Sent out package data
$helperInstance->sendRawPackageData($packageInstance);
// Feature is not enabled
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Feature "hubcoin_reward" not available, not generating package hash. Returning NULL ...');
return NULL;
- } // END - if
+ }
// Fake package instance
$packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class');
*
* @param $packageInstance An instance of a DeliverablePackage class
* @return $isHashValid Whether the hash is valid
- * @throws InvalidArgumentException If $packageInstance contains no senderId
*/
private function isPackageHashValid (DeliverablePackage $packageInstance) {
// Is the feature enabled?
// Feature is not enabled, so hashes are always valid
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Feature "hubcoin_reward" not available, not checking hash. Returning TRUE ...');
return TRUE;
- } elseif ($packageInstance->getSenderId() == '') {
- // Invalid $packageInstance
- throw new InvalidArgumentException('packageInstance does not contain senderId');
}
// Check validity
// This is not fatal but should be avoided
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: No raw package data waiting declaration, but ' . __METHOD__ . ' has been called!');
return;
- } // END - if
+ }
/*
* Now there are for sure packages to deliver, so start with the first
// This is not fatal but should be avoided
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: No package has been declared, but ' . __METHOD__ . ' has been called!');
return;
- } // END - if
+ }
// Get the package instance
$packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECLARED);
// This is not fatal but should be avoided
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: No package is waiting for delivery, but ' . __METHOD__ . ' was called.');
return;
- } // END - if
+ }
// Get the package
$packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_OUTGOING);
// The UNL should be set but not recipientId
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: recipientUnl(%d)=%s,recipientId(%d)=%s', strlen($packageInstance->getRecipientUnl()), $packageInstance->getRecipientUnl(), strlen($packageInstance->getRecipientId()), $packageInstance->getRecipientId()));
- if ($packageInstance->getRecipientUnl() != '' && $packageInstance->getRecipientId() == '') {
+ if (!empty($packageInstance->getRecipientUnl()) && empty($packageInstance->getRecipientId())) {
// Need to resolve UNL -> session id ("recipient id"), so prepare UNL instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: recipientUnl=%s is being solved into a session id ...', $packageInstance->getRecipientUnl()));
$locatorInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipientUnl());
if (!$socketInstance->writeBufferToSocketByArray($encodedDataArray, $sentBytes)) {
// Something bad happened while writing to socket
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Debug message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: sentBytes[%s]=%d', gettype($sentBytes), $sentBytes));
if (!$this->isRawDataPending()) {
// Should not be invoked anymore
throw new BadMethodCallException('No incoming decoded data on stack but method was called.');
- } // END - if
+ }
// "Pop" the next entry (the same array again) from the stack
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Stacker size is %d entries.', $this->getStackInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING)));
// It is there and should be removed
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Raw data contains CHUNK_SEPARATOR=%s - Removing ...', PackageFragmenter::CHUNK_SEPARATOR));
$packageInstance->setRawData(substr($packageInstance->getRawData(), 0, -1));
- } // END - if
+ }
// This package is "handled" and can be pushed on the next stack
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Pushing %d(%s) bytes to stack %s ...', strlen($packageInstance->getRawData()), $packageInstance->getRawData(), self::STACKER_NAME_DECODED_HANDLED));
if ($packageInstance->getStatus() != self::PACKAGE_STATUS_FAILED) {
// Not failed!
throw new UnexpectedPackageStatusException(array($this, $packageInstance, self::PACKAGE_STATUS_FAILED), self::EXCEPTION_UNEXPECTED_PACKAGE_STATUS);
- } // END - if
+ }
// Remove this entry
$this->getStackInstance()->popNamed(self::STACKER_NAME_DECLARED);
public function handleReceivedPackageInstance (DeliverablePackage $packageInstance) {
// Is the package content set?
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
- if ($packageInstance->getPackageContent() == '') {
+ if (empty($packageInstance->getPackageContent())) {
// Package content is not set
throw new InvalidArgumentException(sprintf('Package with receipientId=%s,recipientType=%s has empty packageContent',
$packageInstance->getRecipientId(),
* Handles newly arrived messages
*
* @return void
+ * @throws BadMethodCallException If no new message has arrived
+ * @throws InvalidArgumentException If $packageInstance contains no senderId
+ * @throws InvalidPrivateKeyHashException If the private key-hash is empty
* @todo Implement verification of all sent tags here?
*/
public function handleNewlyArrivedMessage () {
// Make sure there is at least one message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
- assert($this->isNewMessageArrived());
+ if (!$this->isNewMessageArrived()) {
+ // Bad method call
+ throw new BadMethodCallException('No new message arrived but this method has been called.');
+ }
// Get it from the stacker, it is the full array with the decoded message
$packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
// Generate the hash of comparing it
- if (!$this->isPackageHashValid($packageInstance)) {
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
+ if (empty($packageInstance->getSenderId())) {
+ // Invalid $packageInstance
+ throw new InvalidArgumentException('packageInstance does not contain senderId');
+ } elseif (empty($packageInstance->getSenderPrivateKeyHash())) {
+ // This needs fixing
+ throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in decodedData'), BaseHubSystem::EXCEPTION_INVALID_PRIVATE_KEY_HASH);
+ } elseif (!$this->isPackageHashValid($packageInstance)) {
// Is not valid, so throw an exception here
exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . chr(10));
- } // END - if
+ }
// Now get a filter chain back from factory with given tags array
$chainInstance = PackageFilterChainFactory::createChainByTagsArray($packageInstance->getContentHash());
*may be invalid.
*/
return;
- } // END - if
+ }
// Copy node id
$messageData[self::MESSAGE_ARRAY_DATA_NODE_ID] = $messageData[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_DATA_NODE_ID];
* endless loop of mining.
*/
return;
- } // END - if
+ }
// Also remove tags as the miner don't need this.
unset($messageData[self::MESSAGE_ARRAY_TAGS]);