|
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; |
|
11
|
|
|
|
|
12
|
|
|
use AsyncSockets\Configuration\Configuration; |
|
13
|
|
|
use AsyncSockets\Event\Event; |
|
14
|
|
|
use AsyncSockets\Exception\SocketException; |
|
15
|
|
|
use AsyncSockets\Exception\StopRequestExecuteException; |
|
16
|
|
|
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor; |
|
17
|
|
|
use AsyncSockets\RequestExecutor\Metadata\SocketBag; |
|
18
|
|
|
use AsyncSockets\RequestExecutor\Pipeline\EventCaller; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class AbstractRequestExecutor |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractRequestExecutor implements RequestExecutorInterface, EventHandlerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Decider for request limitation |
|
27
|
|
|
* |
|
28
|
|
|
* @var LimitationSolverInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $solver; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* EventHandlerInterface |
|
34
|
|
|
* |
|
35
|
|
|
* @var EventHandlerInterface[] |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $eventHandlers = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Socket bag |
|
41
|
|
|
* |
|
42
|
|
|
* @var SocketBag |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $socketBag; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Flag, indicating stopping request |
|
48
|
|
|
* |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
private $isRequestStopped = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Flag, indicating stopping request is in progress |
|
55
|
|
|
* |
|
56
|
|
|
* @var bool |
|
57
|
|
|
*/ |
|
58
|
|
|
private $isRequestStopInProgress = false; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Flag whether request is executing |
|
62
|
|
|
* |
|
63
|
|
|
* @var bool |
|
64
|
|
|
*/ |
|
65
|
|
|
private $isExecuting = false; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* AbstractRequestExecutor constructor. |
|
69
|
|
|
* |
|
70
|
|
|
* @param Configuration $configuration Configuration for executor |
|
71
|
|
|
*/ |
|
72
|
69 |
|
public function __construct(Configuration $configuration) |
|
73
|
|
|
{ |
|
74
|
69 |
|
$this->socketBag = new SocketBag($this, $configuration->getConnectTimeout(), $configuration->getIoTimeout()); |
|
75
|
69 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** {@inheritdoc} */ |
|
78
|
67 |
|
public function socketBag() |
|
79
|
|
|
{ |
|
80
|
67 |
|
return $this->socketBag; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** {@inheritdoc} */ |
|
84
|
51 |
|
public function withEventHandler(EventHandlerInterface $handler) |
|
85
|
|
|
{ |
|
86
|
51 |
|
$key = spl_object_hash($handler); |
|
87
|
51 |
|
$this->eventHandlers[$key] = $handler; |
|
88
|
51 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** {@inheritdoc} */ |
|
91
|
6 |
|
public function withLimitationSolver(LimitationSolverInterface $solver) |
|
92
|
|
|
{ |
|
93
|
6 |
|
if ($this->isExecuting()) { |
|
94
|
2 |
|
throw new \BadMethodCallException('Can not change limitation solver during request processing'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
$this->solver = $solver; |
|
98
|
4 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** {@inheritdoc} */ |
|
101
|
68 |
|
public function isExecuting() |
|
102
|
|
|
{ |
|
103
|
68 |
|
return $this->isExecuting; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** {@inheritdoc} */ |
|
107
|
67 |
|
public function executeRequest() |
|
108
|
|
|
{ |
|
109
|
67 |
|
if ($this->isExecuting()) { |
|
110
|
2 |
|
throw new \BadMethodCallException('Request is already in progress'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
67 |
|
$this->isRequestStopped = false; |
|
114
|
67 |
|
$this->solver = $this->solver ?: new NoLimitationSolver(); |
|
115
|
|
|
|
|
116
|
67 |
|
$this->isExecuting = true; |
|
117
|
|
|
|
|
118
|
67 |
|
$eventCaller = new EventCaller($this); |
|
119
|
|
|
try { |
|
120
|
67 |
|
foreach ($this->eventHandlers as $handler) { |
|
121
|
51 |
|
$eventCaller->addHandler($handler); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
67 |
|
if ($this->solver instanceof EventHandlerInterface) { |
|
125
|
2 |
|
$eventCaller->addHandler($this->solver); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
67 |
|
$this->initializeRequest($eventCaller); |
|
129
|
|
|
|
|
130
|
67 |
|
$eventCaller->addHandler($this); |
|
131
|
|
|
|
|
132
|
67 |
|
$this->solver->initialize($this); |
|
133
|
67 |
|
$this->doExecuteRequest($eventCaller); |
|
134
|
29 |
|
$this->solver->finalize($this); |
|
135
|
|
|
|
|
136
|
29 |
|
$this->terminateRequest(); |
|
137
|
38 |
|
} catch (StopRequestExecuteException $e) { |
|
138
|
2 |
|
$this->isRequestStopInProgress = true; |
|
139
|
2 |
|
$this->disconnectItems($this->socketBag->getItems()); |
|
140
|
36 |
|
} catch (SocketException $e) { |
|
141
|
9 |
|
foreach ($this->socketBag->getItems() as $item) { |
|
142
|
9 |
|
$eventCaller->setCurrentOperation($item); |
|
143
|
9 |
|
$eventCaller->callExceptionSubscribers($item, $e); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
9 |
|
$this->disconnectItems($this->socketBag->getItems()); |
|
147
|
27 |
|
} catch (\Exception $e) { |
|
148
|
27 |
|
$this->isExecuting = false; |
|
149
|
27 |
|
$this->emergencyShutdown(); |
|
150
|
27 |
|
$this->solver->finalize($this); |
|
151
|
27 |
|
$this->terminateRequest(); |
|
152
|
27 |
|
throw $e; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
40 |
|
$this->solver->finalize($this); |
|
156
|
40 |
|
$this->terminateRequest(); |
|
157
|
40 |
|
$this->isExecuting = false; |
|
158
|
40 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** {@inheritdoc} */ |
|
161
|
3 |
|
public function stopRequest() |
|
162
|
|
|
{ |
|
163
|
3 |
|
if (!$this->isExecuting()) { |
|
164
|
1 |
|
throw new \BadMethodCallException('Can not stop inactive request'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
2 |
|
$this->isRequestStopped = true; |
|
168
|
2 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Prepare executor for request |
|
172
|
|
|
* |
|
173
|
|
|
* @param EventCaller $eventCaller Event caller |
|
174
|
|
|
* |
|
175
|
|
|
* @return void |
|
176
|
|
|
*/ |
|
177
|
67 |
|
protected function initializeRequest(EventCaller $eventCaller) |
|
178
|
|
|
{ |
|
179
|
|
|
// empty body |
|
180
|
67 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Execute network request |
|
184
|
|
|
* |
|
185
|
|
|
* @param EventCaller $eventCaller Event caller object |
|
186
|
|
|
* |
|
187
|
|
|
* @return void |
|
188
|
|
|
*/ |
|
189
|
|
|
abstract protected function doExecuteRequest(EventCaller $eventCaller); |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Terminate request in executor |
|
193
|
|
|
* |
|
194
|
|
|
* @return void |
|
195
|
|
|
*/ |
|
196
|
67 |
|
protected function terminateRequest() |
|
197
|
|
|
{ |
|
198
|
|
|
// empty body |
|
199
|
67 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Disconnect given sockets |
|
203
|
|
|
* |
|
204
|
|
|
* @param RequestDescriptor[] $items Sockets' operations to disconnect |
|
205
|
|
|
* |
|
206
|
|
|
* @return mixed |
|
207
|
|
|
*/ |
|
208
|
|
|
abstract protected function disconnectItems(array $items); |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Shutdown all sockets in case of unhandled exception |
|
212
|
|
|
* |
|
213
|
|
|
* @return void |
|
214
|
|
|
*/ |
|
215
|
27 |
|
private function emergencyShutdown() |
|
216
|
|
|
{ |
|
217
|
27 |
|
foreach ($this->socketBag->getItems() as $item) { |
|
218
|
|
|
try { |
|
219
|
27 |
|
$item->getSocket()->close(); |
|
220
|
|
|
} catch (\Exception $e) { |
|
221
|
|
|
// nothing required |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
27 |
|
$item->setMetadata(self::META_REQUEST_COMPLETE, true); |
|
225
|
|
|
} |
|
226
|
27 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** {@inheritdoc} */ |
|
229
|
63 |
|
public function invokeEvent(Event $event) |
|
230
|
|
|
{ |
|
231
|
63 |
|
if ($this->isRequestStopped && !$this->isRequestStopInProgress) { |
|
232
|
2 |
|
$this->isRequestStopInProgress = true; |
|
233
|
2 |
|
throw new StopRequestExecuteException(); |
|
234
|
|
|
} |
|
235
|
63 |
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|