X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=application%2Fhub%2Fmain%2Fpackage%2Ffragmenter%2Fclass_PackageFragmenter.php;h=2f0add250e1bd070ff6e4cf64d9a765b4783d79b;hb=c13ca7c93ee55b02d1d3320fca5d2d8d6e953768;hp=a254b3d18db7c4575a0d9dcef7a4409c7cf56982;hpb=c4cc97b7c641a57ec0630d995ca8e0d70fdeda2d;p=hub.git diff --git a/application/hub/main/package/fragmenter/class_PackageFragmenter.php b/application/hub/main/package/fragmenter/class_PackageFragmenter.php index a254b3d18..2f0add250 100644 --- a/application/hub/main/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/main/package/fragmenter/class_PackageFragmenter.php @@ -13,7 +13,7 @@ * * @author Roland Haeder * @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 * @@ -30,7 +30,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Registerable { +class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registerable { /** * Cached chunk size in bits */ @@ -165,10 +165,10 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg } /** - * 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 @@ -220,7 +220,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg assert(strlen($finalHash) > 0); // Is the pointer already initialized? - //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: finalHash=' . $finalHash); + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash); assert(isset($this->chunkPointers[$finalHash])); // Return it @@ -237,6 +237,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg assert(isset($this->chunkPointers[$finalHash])); // Count one up + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash); $this->chunkPointers[$finalHash]++; } @@ -271,6 +272,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg * * @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) { /* @@ -283,46 +285,27 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg 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, self::MAX_SERIAL_LENGTH); - - // 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_SEPARATOR . - $chunkHash . self::CHUNK_SEPARATOR; + $this->generateHashFromRawData($lastChunk); - // Also get a hash from it - $eopHash = $this->generateHashFromRawData($rawData); + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': Adding EOP chunk with size of ' . strlen($chunkData) . ',finalHash=' . $finalHash . ' ...'); - // Append it to the eop's data and hash array - $this->chunkHashes[$finalHash][] = $eopHash; - $this->chunks[$finalHash][] = $rawData; + // Add it as regular chunk + $this->addChunkData($finalHash, $chunkData); } /** @@ -340,82 +323,96 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg // Calculate real (data) chunk size $dataChunkSize = $this->getDataChunkSizeFromHash($finalHash); - //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: dataChunkSize=' . $dataChunkSize); + //* NOISY-DEBUG: */ $this->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, '', false); - $this->chunkHashes[$finalHash][] = $chunkHash; + $chunkData = substr($rawData, $idx, $dataChunkSize); - // Prepend the hash to the chunk - $chunk = ( - $chunkHash . self::CHUNK_DATA_HASH_SEPARATOR . - $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPARATOR . - $chunk . self::CHUNK_SEPARATOR - ); - - // 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: */ $this->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: */ $this->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: */ $this->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_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, '', false); - 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_SEPARATOR . - $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPARATOR . - $chunk . self::CHUNK_SEPARATOR - ; + // 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; } /** @@ -451,7 +448,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg $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); @@ -461,7 +458,7 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg } // Return final hash - //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: finalHash[' . gettype($finalHash) . ']=' . $finalHash); + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash); return $finalHash; } @@ -475,6 +472,9 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg * @throws AssertionException If $finalHash was not 'true' */ public function getNextRawDataChunk ($finalHash) { + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash); + try { // Get current chunk index $current = $this->getCurrentChunkPointer($finalHash); @@ -492,9 +492,13 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg // 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: */ $this->debugOutput(__METHOD__. ': finalHash=' . $finalHash . ',current=' . $current . ' - No more entries found!'); return array(); } // END - if + // Debug message + //* NOISY-DEBUG: */ $this->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] @@ -506,6 +510,15 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg // Return the chunk array return $rawDataChunk; } + + /** + * Resets the serial number to zero + * + * @return void + */ + public function resetSerialNumber () { + $this->serialNumber = 0; + } } // [EOF]