*/
const STACKER_NAME_CHUNKS_WITH_FINAL_EOP = 'final_chunks';
+ /**
+ * The final array for assembling the original package back together
+ */
+ private $finalPackageChunks = array(
+ // Array for package content
+ 'content' => array(),
+ // ... and for the hashes
+ 'hashes' => array()
+ );
+
/**
* Protected constructor
*
return $isValid;
}
+ /**
+ * Adds the chunk to the final array which will be used for the final step
+ * which will be to assemble all chunks back to the original package content
+ * and for the final hash check.
+ *
+ * This method may throw an exception if a chunk with the same serial number
+ * has already been added to avoid mixing chunks from different packages.
+ *
+ * @param $chunkSplits An array from a splitted chunk
+ * @return void
+ */
+ private function addChunkToFinalArray (array $chunkSplits) {
+ // Is the serial number (index 1) already been added?
+ if (isset($this->finalPackageChunks[$chunkSplits[1])) {
+ // Then throw an exception
+ throw new ChunkAlreadyAssembledException(array($this, $chunkSplits), self::EXCEPTION_CHUNK_ALREADY_ASSEMBLED);
+ } // END - if
+
+ // Add the chunk data (index 2) to the final array and use the serial number as index
+ $this->finalPackageChunks['content'][$chunkSplits[1]] = $chunkSplits[2];
+
+ // ... and the hash as well
+ $this->finalPackageChunks['hashes'][$chunkSplits[1]] = $chunkSplits[0];
+ }
+
/**
* Adds all chunks if the last one verifies as a 'final chunk'.
*
// Is the serial number valid (chars 0-9, length equals PackageFragmenter::MAX_SERIAL_LENGTH)?
if (!$this->isSerialNumberValid($chunkSplits[1])) {
// Do some logging
- $this->debugOutput('CHUNK-HANDLER: Chunk serial number is invalid.');
+ $this->debugOutput('CHUNK-HANDLER: Chunk serial number for hash ' . $chunkSplits[0] . ' is invalid.');
// Re-request this chunk
$this->rerequestChunkBySplitsArray($chunkSplits);
* peer.
*
* And also the serial number is valid (basicly) at this point.
+ *
+ * Now the chunk can be added to the final array
*/
- die('chunk=' . $chunk . chr(10));
+ $this->addChunkToFinalArray($chunkSplits);
}
}