/**
* Checks whether the checksum (sometimes called "hash") is the same
*
- * @param $decodedContent Package raw content
* @param $packageInstance An instance of a DeliverablePackage class
* @return $isChecksumValid Whether the checksum is the same
*/
- private function isChecksumValid (array $decodedContent, DeliverablePackage $packageInstance) {
+ private function isChecksumValid (DeliverablePackage $packageInstance) {
// Get checksum
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedContent()=%d,packageInstance=%s - CALLED!', count($decodedContent), $packageInstance->__toString()));
- $checksum = $this->getHashFromContentSessionId($decodedContent, $packageInstance->getSenderAddress());
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
+ $checksum = $this->getHashFromPackageSessionId($packageInstance);
// Is it the same?
- $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]);
+ $isChecksumValid = ($checksum == $packageInstance->getContentChecksum());
// Return it
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: isChecksumValid=%d - EXIT!', intval($isChecksumValid)));
/**
* "Getter" for hash from given content and sender's session id
*
- * @param $decodedContent Raw package content
- * @param $sessionId Session id of the sender
+ * @param $packageInstance An instance of a DeliverablePackage class in an array
* @return $hash Hash for given package content
*/
- public function getHashFromContentSessionId (array $decodedContent, $sessionId) {
+ public function getHashFromPackageSessionId (DeliverablePackage $packageInstance) {
// Create the hash
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedContent()=%d,sessionId=%s - CALLED!', count($decodedContent), $sessionId));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
// @TODO md5() is very weak, but it needs to be fast
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_COMPRESSOR]);
$hash = md5(sprintf(self::CONTENT_CHECKSUM_MASK,
- $decodedContent[self::PACKAGE_CONTENT_MESSAGE],
- $sessionId,
- $decodedContent[self::PACKAGE_CONTENT_COMPRESSOR]
+ $packageInstance->getContentMessage(),
+ $packageInstance->getSessionId(),
+ $packageInstance->getCompressorExtension()
));
// And return it
* Checks whether the hash of given package data is 'valid', here that
* means it is the same or not.
*
- * @param $decodedArray An array with 'decoded' (explode() was mostly called) data
+ * @param $packageInstance An instance of a DeliverablePackage class
* @return $isHashValid Whether the hash is valid
* @todo Unfinished area, hashes are currently NOT fully supported
*/
- private function isPackageHashValid (array $decodedArray) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: decodedArray=' . print_r($decodedArray, TRUE) . ' - CALLED!');
-
- // Make sure the required array elements are there
- assert(isset($decodedArray[self::PACKAGE_CONTENT_SENDER]));
- assert(isset($decodedArray[self::PACKAGE_CONTENT_MESSAGE]));
- assert(isset($decodedArray[self::PACKAGE_CONTENT_HASH]));
- assert(isset($decodedArray[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH]));
-
+ private function isPackageHashValid (DeliverablePackage $packageInstance) {
// Is the feature enabled?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) {
// 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 ...');
} // END - if
// Check validity
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: senderId=' . $decodedArray[self::PACKAGE_CONTENT_SENDER] . ',message()=' . strlen($decodedArray[self::PACKAGE_CONTENT_MESSAGE]) . ',hash=' . $decodedArray[self::PACKAGE_CONTENT_HASH]);
- //* DEBUG-DIE: */ die(__METHOD__ . ': decodedArray=' . print_r($decodedArray, TRUE));
- $isHashValid = FrameworkFeature::callFeature('hubcoin_reward', 'checkHash', array($decodedArray[self::PACKAGE_CONTENT_SENDER] . ':' . $decodedArray[self::PACKAGE_CONTENT_MESSAGE] . ':' . $this->determineSenderPrivateKeyHash($decodedArray), $decodedArray[self::PACKAGE_CONTENT_HASH]));
+ $isHashValid = FrameworkFeature::callFeature('hubcoin_reward', 'checkHash', array(
+ sprintf('%s:%s:%s',
+ $packageInstance->getSenderId(),
+ $packageInstance->getPackageContent(),
+ $this->determineSenderPrivateKeyHash($packageInstance)
+ ),
+ $packageInstance->getContentHash(),
+ ));
// Return it
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: isHashValid=' . intval($isHashValid) . ' - EXIT!');
// Assert on count (should be always 6)
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedArray()=%d', count($decodedArray)));
- /* DEBUG-DIE: */ die(sprintf('[%s:%d]: decodedArray=%s', __METHOD__, __LINE__, print_r($decodedArray, TRUE)));
+ //* DEBUG-DIE: */ die(sprintf('[%s:%d]: decodedArray=%s', __METHOD__, __LINE__, print_r($decodedArray, TRUE)));
if (count($decodedArray) != self::DECODED_DATA_ARRAY_SIZE) {
// Count of array elements not expected
throw new InvalidArgumentException(sprintf('decodedArray()=%d does not match expected size %d. rawPackageContent=%s',
));
}
- /*
- * Convert the indexed array into an associative array. This is much
- * better to remember than plain numbers, isn't it?
- */
- $decodedMessage = array(
- // Compressor's extension used to compress the data
- self::PACKAGE_CONTENT_COMPRESSOR => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION],
- // Package data (aka "message") in BASE64-decoded form but still compressed
- self::PACKAGE_CONTENT_MESSAGE => base64_decode($decodedContent[self::INDEX_PACKAGE_DATA]),
- // Tags as an indexed array for "tagging" the message
- self::PACKAGE_CONTENT_TAGS => explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]),
- // Checksum of the _decoded_ data
- self::PACKAGE_CONTENT_CHECKSUM => $decodedContent[self::INDEX_CHECKSUM],
- // Sender's id
- self::PACKAGE_CONTENT_SENDER => $packageInstance->getSenderAddress(),
- // Hash from decoded raw data
- self::PACKAGE_CONTENT_HASH => $packageInstance->getHash(),
- // Hash of private key
- self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $packageInstance->getSenderPrivateKeyHash()
- );
+ // Copy data to package instance
+ $packageInstance->setCompressorExtension($decodedContent[self::INDEX_COMPRESSOR_EXTENSION]);
+ // Package data (aka "message") in BASE64-decoded form but still compressed
+ $packageInstance->setContentMessage(base64_decode($decodedContent[self::INDEX_PACKAGE_DATA]));
+ // Tags as an indexed array for "tagging" the message
+ $packageInstance->setContentHash(explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]));
+ // Checksum of the _decoded_ data
+ $packageInstance->setContentChecksum($decodedContent[self::INDEX_CHECKSUM]);
// Is the checksum valid?
- if (!$this->isChecksumValid($decodedMessage, $packageInstance)) {
+ if (!$this->isChecksumValid($packageInstance)) {
// Is not the same, so throw an exception here
- throw new InvalidDataChecksumException(array($this, $decodedMessage, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM);
+ throw new InvalidDataChecksumException(array($this, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM);
} // END - if
/*
* The checksum is the same, then it can be decompressed safely. The
* original message is at this point fully decoded.
*/
- $decodedMessage[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedMessage[self::PACKAGE_CONTENT_MESSAGE]);
+ $packageInstance->setContentMessage($this->getCompressorInstance()->decompressStream($packageInstance->getContentMessage()));
// And push it on the next stack
- $this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedMessage);
+ $this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $packageInstance);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
}
/**
*/
public function isNewMessageArrived () {
// Determine if the stack is not empty
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
$hasArrived = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_NEW_MESSAGE));
// Return it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: hasArrived=%d - EXIT!', intval($hasArrived)));
return $hasArrived;
}
*/
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());
// Get it from the stacker, it is the full array with the decoded message
- $decodedMessage = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
+ $packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
// Generate the hash of comparing it
- if (!$this->isPackageHashValid($decodedMessage)) {
+ if (!$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($decodedMessage[self::PACKAGE_CONTENT_TAGS]);
+ $chainInstance = PackageFilterChainFactory::createChainByTagsArray($packageInstance->getContentHash());
/*
* Process the message through all filters, note that all other
- * elements from $decodedMessage are no longer needed.
+ * elements from $packageInstance are no longer needed.
*/
- $chainInstance->processMessage($decodedMessage, $this);
+ $chainInstance->processMessage($packageInstance, $this);
/*
* Post-processing of message data (this won't remove the message from
* the stack).
*/
$chainInstance->postProcessMessage($this);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
}
/**
*/
public function isProcessedMessagePending () {
// Check it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
$isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE));
// Return it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: isPending=%d - EXIT!', intval($isPending)));
return $isPending;
}
*/
public function handleProcessedMessage () {
// Get it from the stacker, it is the full array with the processed message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
$messageArray = $this->getStackInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE);
// Add type for later easier handling
// Post-handling of message data
$handlerInstance->postHandleMessageData($messageArray, $this);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
}
/**