*
* @author Roland Haeder <webmaster@ship-simu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Registerable {
+class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registerable {
/**
* Cached chunk size in bits
*/
private $serialNumber = 0x00000000;
/**
- * Length of largest possible serial number
+ * Maximum possible serial number
*/
- private $maxSerialLength = 8;
+ private $maxSerialNumber = 0;
/**
- * Maximum possible serial number
+ * Length of largest possible serial number
*/
- private $maxSerialNumber = 0;
+ const MAX_SERIAL_LENGTH = 8;
/**
- * Seperator between chunk data, serial number and chunk hash
+ * Separator between chunk data, serial number and chunk hash
*/
- const CHUNK_DATA_HASH_SEPERATOR = '@';
+ const CHUNK_DATA_HASH_SEPARATOR = '@';
/**
- * Seperator for all chunk hashes
+ * SEPARATOR for all chunk hashes
*/
- const CHUNK_HASH_SEPERATOR = ';';
+ const CHUNK_HASH_SEPARATOR = ';';
/**
- * Seperator between two chunks
+ * SEPARATOR between two chunks
*/
- const CHUNK_SEPERATOR = '|';
+ const CHUNK_SEPARATOR = '|';
/**
* Identifier for hash chunk
$this->chunkSize = $this->getConfigInstance()->getConfigEntry('package_chunk_size');
// - Maximum serial number
- $this->maxSerialNumber = $this->hex2dec(str_repeat('f', $this->maxSerialLength));
+ $this->maxSerialNumber = $this->hex2dec(str_repeat('f', self::MAX_SERIAL_LENGTH));
}
/**
*/
private function getProcessedPackagesIndex (array $packageData) {
return (
- $packageData['sender'] . NetworkPackage::PACKAGE_DATA_SEPERATOR .
- $packageData['recipient'] . NetworkPackage::PACKAGE_DATA_SEPERATOR .
- $packageData['content'] . NetworkPackage::PACKAGE_DATA_SEPERATOR
+ $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . NetworkPackage::PACKAGE_DATA_SEPARATOR .
+ $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . NetworkPackage::PACKAGE_DATA_SEPARATOR .
+ $packageData[NetworkPackage::PACKAGE_DATA_CONTENT] . NetworkPackage::PACKAGE_DATA_SEPARATOR
);
}
/**
- * Checks wether the given package data is already processed by this fragmenter
+ * Checks whether the given package data is already processed by this fragmenter
*
* @param $packageData Raw package data array
- * @return $isProcessed Wether the package has been fragmented
+ * @return $isProcessed Whether the package has been fragmented
*/
private function isPackageProcessed (array $packageData) {
// Get array index
// Is the array index there?
$isProcessed = (
- (isset($this->processedPackages[$index])) &&
+ (isset($this->processedPackages[$index]))
+ &&
($this->processedPackages[$index] === true)
);
assert(strlen($finalHash) > 0);
// Is the pointer already initialized?
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: finalHash=' . $finalHash);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash);
assert(isset($this->chunkPointers[$finalHash]));
// Return it
assert(isset($this->chunkPointers[$finalHash]));
// Count one up
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash);
$this->chunkPointers[$finalHash]++;
}
// Hash size
strlen($hash) -
// Length of sperators
- (strlen(self::CHUNK_DATA_HASH_SEPERATOR) * 2) -
+ (strlen(self::CHUNK_DATA_HASH_SEPARATOR) * 2) -
// Length of max serial number
- $this->maxSerialLength
+ self::MAX_SERIAL_LENGTH
);
// This should be larger than zero bytes
*
* @param $rawData Raw data bytes to hash
* @return $hash Hash from the raw data
+ * @todo Implement a way to send non-announcement packages with extra-salt
*/
private function generateHashFromRawData ($rawData) {
- // Get the crypto instance and hash the data
- $hash = $this->getCryptoInstance()->hashString($rawData);
+ /*
+ * Get the crypto instance and hash the data with no extra salt because
+ * the other peer doesn't have *this* peer's salt.
+ */
+ $hash = $this->getCryptoInstance()->hashString($rawData, '', false);
// Return it
return $hash;
}
- /**
- * "Getter" for the next hexadecimal-encoded serial number
- *
- * @return $encodedSerialNumber The next hexadecimal-encoded serial number
- */
- private function getNextHexSerialNumber () {
- // Assert on maximum serial number length
- assert($this->serialNumber <= $this->maxSerialNumber);
-
- // Encode the current serial number
- $encodedSerialNumber = $this->dec2Hex($this->serialNumber, $this->maxSerialLength);
-
- // Count one up
- $this->serialNumber++;
-
- // Return the encoded serial number
- return $encodedSerialNumber;
- }
-
/**
* Appends an end-of-package chunk to the chunk list for given chunk and
- * final hash.
+ * final hash. As of 23-March-2012 the format of this chunk will be as any
+ * regular one to keep things easy (KISS) in ChunkHandler class.
*
- * @param $chunkHash Last chunk's hash
+ * @param $lastChunk Last chunk raw data
* @param $finalHash Final hash for raw (unencoded) data
* @return void
*/
- private function appendEndOfPackageChunk ($chunkHash, $finalHash) {
+ private function appendEndOfPackageChunk ($lastChunk, $finalHash) {
// Generate end-of-package marker
- $rawData =
+ $chunkData =
self::END_OF_PACKAGE_IDENTIFIER .
- $finalHash . self::CHUNK_HASH_SEPERATOR .
- $chunkHash . self::CHUNK_SEPERATOR;
+ $finalHash . self::CHUNK_HASH_SEPARATOR .
+ $this->generateHashFromRawData($lastChunk);
- // Also get a hash from it
- $chunkHash = $this->generateHashFromRawData($rawData);
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': Adding EOP chunk with size of ' . strlen($chunkData) . ',finalHash=' . $finalHash . ' ...');
- // Append it to the chunk's data and hash array
- $this->chunkHashes[$finalHash][] = $chunkHash;
- $this->chunks[$finalHash][] = $rawData;
+ // Add it as regular chunk
+ $this->addChunkData($finalHash, $chunkData);
}
/**
* Splits the given encoded data into smaller chunks, the size of the final
- * and the seperator is being subtracted from chunk size to fit it into a
+ * and the SEPARATOR is being subtracted from chunk size to fit it into a
* TCP package (512 bytes).
*
* @param $rawData Raw data string
// Calculate real (data) chunk size
$dataChunkSize = $this->getDataChunkSizeFromHash($finalHash);
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: dataChunkSize=' . $dataChunkSize);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': dataChunkSize=' . $dataChunkSize);
// Init variables
$chunkHash = '';
+ $chunkData = '';
// Now split it up
for ($idx = 0; $idx < strlen($rawData); $idx += $dataChunkSize) {
// Get the next chunk
- $chunk = substr($rawData, $idx, $dataChunkSize);
-
- // Hash it and remember it in seperate array
- $chunkHash = $this->getCryptoInstance()->hashString($chunk);
- $this->chunkHashes[$finalHash][] = $chunkHash;
-
- // Prepend the hash to the chunk
- $chunk =
- $chunkHash . self::CHUNK_DATA_HASH_SEPERATOR .
- $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPERATOR .
- $chunk . self::CHUNK_SEPERATOR
- ;
+ $chunkData = substr($rawData, $idx, $dataChunkSize);
- // Make sure the chunk is not larger than a TCP package can hold
- assert(strlen($chunk) <= NetworkPackage::TCP_PACKAGE_SIZE);
-
- // Add it to the array
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Adding ' . strlen($chunk) . ' bytes of a chunk.');
- $this->chunks[$finalHash][] = $chunk;
+ // Add the chunk to the propper array and do all the stuff there
+ $this->addChunkData($finalHash, $chunkData);
} // END - for
// Debug output
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Raw data of ' . strlen($rawData) . ' bytes has been fragmented into ' . count($this->chunks[$finalHash]) . ' chunk(s).');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': Raw data of ' . strlen($rawData) . ' bytes has been fragmented into ' . count($this->chunks[$finalHash]) . ' chunk(s).');
// Add end-of-package chunk
- $this->appendEndOfPackageChunk($chunkHash, $finalHash);
+ $this->appendEndOfPackageChunk($chunkData, $finalHash);
+ }
+
+ /**
+ * Adds the given chunk (raw data) to the proper array and hashes it for
+ * later verfication.
+ *
+ * @param $finalHash Final hash for faster processing
+ * @param $chunkData Raw chunk data
+ * @param $prepend Whether append (default) or prepend the chunk
+ * @return void
+ */
+ private function addChunkData ($finalHash, $chunkData, $prepend = false) {
+ // Hash it
+ $rawDataHash = $this->getCryptoInstance()->hashString($chunkData, '', false);
+
+ // Prepend the hash to the chunk
+ $rawData = (
+ $rawDataHash . self::CHUNK_DATA_HASH_SEPARATOR .
+ $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPARATOR .
+ $chunkData . self::CHUNK_SEPARATOR
+ );
+
+ // Make sure the chunk is not larger than a TCP package can hold
+ assert(strlen($rawData) <= NetworkPackage::TCP_PACKAGE_SIZE);
+
+ // Add it to the array
+ if ($prepend === true) {
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': Prepending ' . strlen($rawData) . ' bytes of a chunk, finalHash=' . $finalHash . ' ...');
+ array_unshift($this->chunkHashes[$finalHash], $rawDataHash);
+ array_unshift($this->chunks[$finalHash] , $rawData);
+ } else {
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': Appending ' . strlen($rawData) . ' bytes of a chunk, finalHash=' . $finalHash . ' ...');
+ $this->chunks[$finalHash][] = $rawData;
+ $this->chunkHashes[$finalHash][] = $rawDataHash;
+ }
}
/**
* Prepends a chunk (or more) with all hashes from all chunks + final chunk.
*
- * @param $rawData Raw data string
* @param $finalHash Final hash from the raw data
* @return void
*/
- private function prependHashChunk ($rawData, $finalHash) {
+ private function prependHashChunk ($finalHash) {
// "Implode" the whole array of hashes into one string
- $rawData = self::HASH_CHUNK_IDENTIFIER . implode(self::CHUNK_HASH_SEPERATOR, $this->chunkHashes[$finalHash]);
+ $rawData = self::HASH_CHUNK_IDENTIFIER . implode(self::CHUNK_HASH_SEPARATOR, $this->chunkHashes[$finalHash]);
- // Also get a hash from it
- $chunkHash = $this->generateHashFromRawData($rawData);
-
- // Calulcate chunk size
- $dataChunkSize = $this->getDataChunkSizeFromHash($chunkHash);
-
- // Now array_unshift() it to the two chunk arrays
- for ($idx = 0; $idx < strlen($rawData); $idx += $dataChunkSize) {
- // Get the next chunk
- $chunk = substr($rawData, $idx, $dataChunkSize);
+ // Prepend chunk
+ $this->addChunkData($finalHash, $rawData, true);
+ }
- // Hash it and remember it in seperate array
- $chunkHash = $this->getCryptoInstance()->hashString($chunk);
- array_unshift($this->chunkHashes[$finalHash], $chunkHash);
+ /**
+ * "Getter" for the next hexadecimal-encoded serial number
+ *
+ * @return $encodedSerialNumber The next hexadecimal-encoded serial number
+ */
+ public function getNextHexSerialNumber () {
+ // Assert on maximum serial number length
+ assert($this->serialNumber <= $this->maxSerialNumber);
- // Prepend the hash to the chunk
- $chunk =
- $chunkHash . self::CHUNK_DATA_HASH_SEPERATOR .
- $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPERATOR .
- $chunk . self::CHUNK_SEPERATOR
- ;
+ // Encode the current serial number
+ $encodedSerialNumber = $this->dec2Hex($this->serialNumber, self::MAX_SERIAL_LENGTH);
- // Make sure the chunk is not larger than a TCP package can hold
- assert(strlen($chunk) <= NetworkPackage::TCP_PACKAGE_SIZE);
+ // Count one up
+ $this->serialNumber++;
- // Add it to the array
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Adding ' . strlen($chunk) . ' bytes of a chunk.');
- array_unshift($this->chunks[$finalHash], $chunk);
- } // END - for
+ // Return the encoded serial number
+ return $encodedSerialNumber;
}
/**
* to all chunks and prepends a chunk with all hashes only in it. It will
* return the final hash for faster processing of packages.
*
- * @param $packageData Raw package data array
- * @param $connectionInstance A helper instance for connections
- * @return $finalHash Final hash for faster processing
- * @todo $connectionInstance is unused
+ * @param $packageData Raw package data array
+ * @param $helperInstance An instance of a ConnectionHelper class
+ * @return $finalHash Final hash for faster processing
+ * @todo $helperInstance is unused
*/
- public function fragmentPackageArray (array $packageData, BaseConnectionHelper $connectionInstance) {
+ public function fragmentPackageArray (array $packageData, ConnectionHelper $helperInstance) {
// Is this package already fragmented?
if (!$this->isPackageProcessed($packageData)) {
+ // Remove package status and protocol, the recipient doesn't need this
+ unset($packageData[NetworkPackage::PACKAGE_DATA_STATUS]);
+ unset($packageData[NetworkPackage::PACKAGE_DATA_PROTOCOL]);
+
// First we need to "implode" the array
- $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPERATOR, $packageData);
+ $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPARATOR, $packageData);
// Generate the final hash from the raw data (not encoded!)
$finalHash = $this->generateHashFromRawData($rawData);
$this->splitEncodedDataIntoChunks($rawData, $finalHash);
// Prepend a chunk with all hashes together
- $this->prependHashChunk($rawData, $finalHash);
+ $this->prependHashChunk($finalHash);
// Mark the package as fragmented
$this->markPackageDataProcessed($packageData);
}
// Return final hash
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: finalHash[' . gettype($finalHash) . ']=' . $finalHash);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash);
return $finalHash;
}
*
* @param $finalHash Final hash for faster lookup
* @return $rawDataChunk Raw package data chunk
+ * @throws AssertionException If $finalHash was not 'true'
*/
public function getNextRawDataChunk ($finalHash) {
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash);
+
try {
// Get current chunk index
$current = $this->getCurrentChunkPointer($finalHash);
// This may happen when the final hash is true
if ($finalHash === true) {
// Set current to null
- $current = null;
+ $current = NULL;
} else {
// Throw the exception
throw $e;
// If there is no entry left, return an empty array
if ((!isset($this->chunkHashes[$finalHash][$current])) || (!isset($this->chunks[$finalHash][$current]))) {
// No more entries found
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__. ': finalHash=' . $finalHash . ',current=' . $current . ' - No more entries found!');
return array();
} // END - if
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__. ': finalHash=' . $finalHash . ',current=' . $current . ',chunkHashes()=' . count($this->chunkHashes[$finalHash]) .' - Entry choosen ...');
+
// Generate the array
$rawDataChunk = array(
$this->chunkHashes[$finalHash][$current] => $this->chunks[$finalHash][$current]
// Return the chunk array
return $rawDataChunk;
}
+
+ /**
+ * Resets the serial number to zero
+ *
+ * @return void
+ */
+ public function resetSerialNumber () {
+ // Debug message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('FRAGMENTER: Resetting serial number, previous=' . $this->serialNumber);
+
+ // Reset serial number
+ $this->serialNumber = 0;
+ }
}
// [EOF]