1 | <?php |
||
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) |
|
76 | |||
77 | /** {@inheritdoc} */ |
||
78 | 67 | public function socketBag() |
|
82 | |||
83 | /** {@inheritdoc} */ |
||
84 | 51 | public function withEventHandler(EventHandlerInterface $handler) |
|
89 | |||
90 | /** {@inheritdoc} */ |
||
91 | 6 | public function withLimitationSolver(LimitationSolverInterface $solver) |
|
99 | |||
100 | /** {@inheritdoc} */ |
||
101 | 68 | public function isExecuting() |
|
105 | |||
106 | /** {@inheritdoc} */ |
||
107 | 67 | public function executeRequest() |
|
159 | |||
160 | /** {@inheritdoc} */ |
||
161 | 3 | public function stopRequest() |
|
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) |
|
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() |
|
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() |
|
227 | |||
228 | /** {@inheritdoc} */ |
||
229 | 63 | public function invokeEvent(Event $event) |
|
236 | } |
||
237 |