|
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
|
|
|
namespace AsyncSockets\RequestExecutor\Pipeline; |
|
11
|
|
|
|
|
12
|
|
|
use AsyncSockets\Event\Event; |
|
13
|
|
|
use AsyncSockets\Event\SocketExceptionEvent; |
|
14
|
|
|
use AsyncSockets\Exception\SocketException; |
|
15
|
|
|
use AsyncSockets\Exception\StopSocketOperationException; |
|
16
|
|
|
use AsyncSockets\RequestExecutor\EventHandlerInterface; |
|
17
|
|
|
use AsyncSockets\RequestExecutor\ExecutionContext; |
|
18
|
|
|
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor; |
|
19
|
|
|
use AsyncSockets\RequestExecutor\RequestExecutorInterface; |
|
20
|
|
|
use AsyncSockets\Socket\SocketInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class EventCaller |
|
24
|
|
|
*/ |
|
25
|
|
|
class EventCaller implements EventHandlerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* List of handlers |
|
29
|
|
|
* |
|
30
|
|
|
* @var EventHandlerInterface[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $handlers = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Request executor |
|
36
|
|
|
* |
|
37
|
|
|
* @var RequestExecutorInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $executor; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* RequestDescriptor |
|
43
|
|
|
* |
|
44
|
|
|
* @var RequestDescriptor |
|
45
|
|
|
*/ |
|
46
|
|
|
private $currentOperation; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* EventCaller constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param RequestExecutorInterface $executor Request executor |
|
52
|
|
|
*/ |
|
53
|
204 |
|
public function __construct(RequestExecutorInterface $executor) |
|
54
|
|
|
{ |
|
55
|
204 |
|
$this->executor = $executor; |
|
56
|
204 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Add given handler to list |
|
60
|
|
|
* |
|
61
|
|
|
* @param EventHandlerInterface $handler |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
135 |
|
public function addHandler(EventHandlerInterface $handler) |
|
66
|
|
|
{ |
|
67
|
135 |
|
$this->handlers[] = $handler; |
|
68
|
135 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Sets CurrentOperation |
|
72
|
|
|
* |
|
73
|
|
|
* @param RequestDescriptor $currentOperation New value for CurrentOperation |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
168 |
|
public function setCurrentOperation(RequestDescriptor $currentOperation) |
|
78
|
|
|
{ |
|
79
|
168 |
|
$this->currentOperation = $currentOperation; |
|
80
|
168 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Clear current operation object |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
168 |
|
public function clearCurrentOperation() |
|
88
|
|
|
{ |
|
89
|
168 |
|
$this->currentOperation = null; |
|
90
|
168 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** {@inheritdoc} */ |
|
93
|
81 |
|
public function invokeEvent( |
|
94
|
|
|
Event $event, |
|
95
|
|
|
RequestExecutorInterface $executor, |
|
96
|
|
|
SocketInterface $socket, |
|
97
|
|
|
ExecutionContext $context |
|
98
|
|
|
) { |
|
99
|
81 |
|
$this->callSocketSubscribers($this->currentOperation, $event, $executor, $context); |
|
100
|
49 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Notify handlers about given event |
|
104
|
|
|
* |
|
105
|
|
|
* @param RequestDescriptor $requestDescriptor Request descriptor |
|
106
|
|
|
* @param Event $event Event object |
|
107
|
|
|
* @param RequestExecutorInterface $executor Request executor |
|
108
|
|
|
* @param ExecutionContext $context Execution context |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
134 |
|
public function callSocketSubscribers( |
|
113
|
|
|
RequestDescriptor $requestDescriptor, |
|
114
|
|
|
Event $event, |
|
115
|
|
|
RequestExecutorInterface $executor, |
|
116
|
|
|
ExecutionContext $context |
|
117
|
|
|
) { |
|
118
|
134 |
|
$requestDescriptor->invokeEvent($event, $executor, $requestDescriptor->getSocket(), $context); |
|
119
|
|
|
|
|
120
|
130 |
|
foreach ($this->handlers as $handler) { |
|
121
|
130 |
|
$handler->invokeEvent($event, $executor, $requestDescriptor->getSocket(), $context); |
|
122
|
126 |
|
} |
|
123
|
|
|
|
|
124
|
126 |
|
if ($event->isOperationCancelled()) { |
|
125
|
4 |
|
throw new StopSocketOperationException(); |
|
126
|
|
|
} |
|
127
|
126 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Notify handlers about exception |
|
131
|
|
|
* |
|
132
|
|
|
* @param RequestDescriptor $requestDescriptor Socket operation object |
|
133
|
|
|
* @param SocketException $exception Thrown exception |
|
134
|
|
|
* @param RequestExecutorInterface $executor Request executor |
|
135
|
|
|
* @param ExecutionContext $context Execution context |
|
136
|
|
|
* |
|
137
|
|
|
* @return void |
|
138
|
|
|
*/ |
|
139
|
58 |
|
public function callExceptionSubscribers( |
|
140
|
|
|
RequestDescriptor $requestDescriptor, |
|
141
|
|
|
SocketException $exception, |
|
142
|
|
|
RequestExecutorInterface $executor, |
|
143
|
|
|
ExecutionContext $context |
|
144
|
|
|
) { |
|
145
|
58 |
|
if ($exception instanceof StopSocketOperationException) { |
|
146
|
4 |
|
return; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
54 |
|
$meta = $requestDescriptor->getMetadata(); |
|
150
|
54 |
|
$exceptionEvent = new SocketExceptionEvent( |
|
151
|
54 |
|
$exception, |
|
152
|
54 |
|
$this->executor, |
|
153
|
54 |
|
$requestDescriptor->getSocket(), |
|
154
|
54 |
|
$meta[RequestExecutorInterface::META_USER_CONTEXT] |
|
155
|
54 |
|
); |
|
156
|
54 |
|
$this->callSocketSubscribers($requestDescriptor, $exceptionEvent, $executor, $context); |
|
157
|
50 |
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|