|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Workers\Chunks; |
|
4
|
|
|
|
|
5
|
|
|
use Puzzle\AMQP\Workers\Worker; |
|
6
|
|
|
use Puzzle\AMQP\ReadableMessage; |
|
7
|
|
|
use Psr\Log\NullLogger; |
|
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
9
|
|
|
use Puzzle\AMQP\Messages\Chunks\ChunkedMessageMetadata; |
|
10
|
|
|
use Puzzle\AMQP\Messages\Chunks\ChunkMetadata; |
|
11
|
|
|
|
|
12
|
|
|
class ChunkAssembler implements Worker |
|
13
|
|
|
{ |
|
14
|
|
|
use LoggerAwareTrait; |
|
15
|
|
|
|
|
16
|
|
|
private |
|
17
|
|
|
$tracker, |
|
18
|
|
|
$storage; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(ChunkAssemblyTracker $tracker, ChunkAssemblyStorage $storage) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->tracker = $tracker; |
|
23
|
|
|
$this->storage = $storage; |
|
24
|
|
|
$this->logger = new NullLogger(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function process(ReadableMessage $message) |
|
28
|
|
|
{ |
|
29
|
|
|
$headers = $message->getHeaders(); |
|
30
|
|
|
|
|
31
|
|
|
$chunkMetadata = $this->retrieveChunkMetadata($headers); |
|
32
|
|
|
$chunkedMessageMetadata = $this->retrieveChunkedMessageMetadata($headers); |
|
33
|
|
|
$uuid = $chunkedMessageMetadata->uuid(); |
|
34
|
|
|
|
|
35
|
|
|
if($this->tracker->hasBeenStarted($uuid) === false) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->tracker->start($chunkedMessageMetadata); |
|
38
|
|
|
$this->storage->start($chunkedMessageMetadata); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->tracker->markAsReceived($uuid, $chunkMetadata->playhead()); |
|
42
|
|
|
$this->storage->store($uuid, $chunkMetadata, $message->getBodyAsTransported()); |
|
43
|
|
|
|
|
44
|
|
|
if($this->tracker->isAllHasBeenReceived($uuid)) |
|
45
|
|
|
{ |
|
46
|
|
|
var_dump("OK for $uuid"); |
|
|
|
|
|
|
47
|
|
|
// check hash |
|
48
|
|
|
$this->storage->finish($chunkedMessageMetadata, $headers); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function retrieveChunkedMessageMetadata(array $headers) |
|
53
|
|
|
{ |
|
54
|
|
|
if(! isset($headers['chunkedMessage'])) |
|
55
|
|
|
{ |
|
56
|
|
|
throw new \InvalidArgumentException("Missing chunkedMessage header"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return ChunkedMessageMetadata::buildFromHeaders($headers['chunkedMessage']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function retrieveChunkMetadata(array $headers) |
|
63
|
|
|
{ |
|
64
|
|
|
if(! isset($headers['chunk'])) |
|
65
|
|
|
{ |
|
66
|
|
|
throw new \InvalidArgumentException("Missing chunk header"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return ChunkMetadata::buildFromHeaders($headers['chunk']); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|