|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Async sockets |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace AsyncSockets\RequestExecutor\Pipeline; |
|
12
|
|
|
|
|
13
|
|
|
use AsyncSockets\Operation\OperationInterface; |
|
14
|
|
|
use AsyncSockets\Operation\ReadWriteOperation; |
|
15
|
|
|
use AsyncSockets\RequestExecutor\EventHandlerInterface; |
|
16
|
|
|
use AsyncSockets\RequestExecutor\ExecutionContext; |
|
17
|
|
|
use AsyncSockets\RequestExecutor\IoHandlerInterface; |
|
18
|
|
|
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor; |
|
19
|
|
|
use AsyncSockets\RequestExecutor\RequestExecutorInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class ReadWriteIoHandler |
|
23
|
|
|
*/ |
|
24
|
|
|
class ReadWriteIoHandler implements IoHandlerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Io handler |
|
28
|
|
|
* |
|
29
|
|
|
* @var IoHandlerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $handler; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Initialize I/O handler |
|
35
|
|
|
* |
|
36
|
|
|
* @param IoHandlerInterface $handler New value for Handler |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
139 |
|
public function setHandler(IoHandlerInterface $handler) |
|
41
|
|
|
{ |
|
42
|
139 |
|
$this->handler = $handler; |
|
43
|
139 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function supports(OperationInterface $operation) |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $operation instanceof ReadWriteOperation; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritDoc |
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function handle( |
|
57
|
|
|
OperationInterface $operation, |
|
58
|
|
|
RequestDescriptor $descriptor, |
|
59
|
|
|
RequestExecutorInterface $executor, |
|
60
|
|
|
EventHandlerInterface $eventHandler, |
|
61
|
|
|
ExecutionContext $executionContext |
|
62
|
|
|
) { |
|
63
|
|
|
/** @var ReadWriteOperation $operation */ |
|
64
|
4 |
|
$sequence = $operation->isReadFirst() ? |
|
65
|
4 |
|
[$operation->getReadOperation(), $operation->getWriteOperation()] : |
|
66
|
4 |
|
[$operation->getWriteOperation(), $operation->getReadOperation()]; |
|
67
|
4 |
|
$sequence = array_filter($sequence); |
|
68
|
|
|
|
|
69
|
4 |
|
foreach ($sequence as $op) { |
|
70
|
4 |
|
$this->handleOperation( |
|
71
|
4 |
|
$op, |
|
72
|
4 |
|
$operation, |
|
73
|
4 |
|
$descriptor, |
|
74
|
4 |
|
$executor, |
|
75
|
4 |
|
$eventHandler, |
|
76
|
|
|
$executionContext |
|
77
|
4 |
|
); |
|
78
|
4 |
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
return $operation->getReadOperation() !== null || $operation->getWriteOperation() !== null ? |
|
81
|
4 |
|
$operation : |
|
82
|
4 |
|
null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Handle read operation |
|
87
|
|
|
* |
|
88
|
|
|
* @param OperationInterface $nestedOperation Nested I/O operation |
|
89
|
|
|
* @param ReadWriteOperation $operation Operation to process |
|
90
|
|
|
* @param RequestDescriptor $descriptor Request descriptor |
|
91
|
|
|
* @param RequestExecutorInterface $executor Executor, processing operation |
|
92
|
|
|
* @param EventHandlerInterface $eventHandler Event handler for this operation |
|
93
|
|
|
* @param ExecutionContext $executionContext Execution context |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
4 |
|
private function handleOperation( |
|
98
|
|
|
OperationInterface $nestedOperation = null, |
|
99
|
|
|
ReadWriteOperation $operation, |
|
100
|
|
|
RequestDescriptor $descriptor, |
|
101
|
|
|
RequestExecutorInterface $executor, |
|
102
|
|
|
EventHandlerInterface $eventHandler, |
|
103
|
|
|
ExecutionContext $executionContext |
|
104
|
|
|
) { |
|
105
|
4 |
|
if (!$nestedOperation) { |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
4 |
|
$next = $this->handler->handle($nestedOperation, $descriptor, $executor, $eventHandler, $executionContext); |
|
110
|
4 |
|
if ($next !== $nestedOperation) { |
|
111
|
4 |
|
$operation->markCompleted($nestedOperation); |
|
112
|
4 |
|
} |
|
113
|
|
|
|
|
114
|
4 |
|
if (!$next) { |
|
115
|
2 |
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
$operation->scheduleOperation($next); |
|
119
|
2 |
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|