1 | <?php |
||
24 | abstract class AbstractRequestExecutor implements RequestExecutorInterface, EventHandlerInterface |
||
25 | { |
||
26 | /** |
||
27 | * Decider for request limitation |
||
28 | * |
||
29 | * @var LimitationSolverInterface |
||
30 | */ |
||
31 | protected $solver; |
||
32 | |||
33 | /** |
||
34 | * EventHandlerInterface |
||
35 | * |
||
36 | * @var EventHandlerInterface[] |
||
37 | */ |
||
38 | protected $eventHandlers = []; |
||
39 | |||
40 | /** |
||
41 | * Socket bag |
||
42 | * |
||
43 | * @var SocketBag |
||
44 | */ |
||
45 | protected $socketBag; |
||
46 | |||
47 | /** |
||
48 | * Flag, indicating stopping request |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $isRequestStopped = false; |
||
53 | |||
54 | /** |
||
55 | * Flag, indicating stopping request is in progress |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $isRequestStopInProgress = false; |
||
60 | |||
61 | /** |
||
62 | * Flag whether request is executing |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | private $isExecuting = false; |
||
67 | |||
68 | /** |
||
69 | * AbstractRequestExecutor constructor. |
||
70 | * |
||
71 | * @param Configuration $configuration Configuration for executor |
||
72 | */ |
||
73 | 139 | public function __construct(Configuration $configuration) |
|
77 | |||
78 | /** {@inheritdoc} */ |
||
79 | 135 | public function socketBag() |
|
83 | |||
84 | /** {@inheritdoc} */ |
||
85 | 102 | public function withEventHandler(EventHandlerInterface $handler) |
|
90 | |||
91 | /** {@inheritdoc} */ |
||
92 | 12 | public function withLimitationSolver(LimitationSolverInterface $solver) |
|
100 | |||
101 | /** {@inheritdoc} */ |
||
102 | 137 | public function isExecuting() |
|
106 | |||
107 | /** {@inheritdoc} */ |
||
108 | 135 | public function executeRequest(ExecutionContext $context = null) |
|
164 | |||
165 | /** {@inheritdoc} */ |
||
166 | 6 | public function stopRequest() |
|
174 | |||
175 | /** |
||
176 | * Prepare executor for request |
||
177 | * |
||
178 | * @param EventCaller $eventCaller Event caller |
||
179 | * @param ExecutionContext $executionContext Execution context |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | 135 | protected function initializeRequest(EventCaller $eventCaller, ExecutionContext $executionContext) |
|
187 | |||
188 | /** |
||
189 | * Execute network request |
||
190 | * |
||
191 | * @param EventCaller $eventCaller Event caller object |
||
192 | * @param ExecutionContext $executionContext Execution context |
||
193 | * |
||
194 | * @return void |
||
195 | */ |
||
196 | abstract protected function doExecuteRequest(EventCaller $eventCaller, ExecutionContext $executionContext); |
||
197 | |||
198 | /** |
||
199 | * Terminate request in executor |
||
200 | * |
||
201 | * @param ExecutionContext $executionContext Execution context |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | 135 | protected function terminateRequest(ExecutionContext $executionContext) |
|
209 | |||
210 | /** |
||
211 | * Disconnect given sockets |
||
212 | * |
||
213 | * @param RequestDescriptor[] $items Sockets' operations to disconnect |
||
214 | * |
||
215 | * @return mixed |
||
216 | */ |
||
217 | abstract protected function disconnectItems(array $items); |
||
218 | |||
219 | /** |
||
220 | * Shutdown all sockets in case of unhandled exception |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | 54 | private function emergencyShutdown() |
|
236 | |||
237 | /** {@inheritdoc} */ |
||
238 | 126 | public function invokeEvent( |
|
249 | } |
||
250 |