1 | <?php |
||
22 | class RequestDescriptor implements StreamResourceInterface, EventHandlerInterface |
||
23 | { |
||
24 | /** |
||
25 | * This descriptor is ready for reading |
||
26 | */ |
||
27 | const RDS_READ = 0x0001; |
||
28 | |||
29 | /** |
||
30 | * This descriptor is ready for writing |
||
31 | */ |
||
32 | const RDS_WRITE = 0x0002; |
||
33 | |||
34 | /** |
||
35 | * This descriptor is has OOB data |
||
36 | */ |
||
37 | const RDS_OOB = 0x0004; |
||
38 | |||
39 | /** |
||
40 | * Minimum transfer rate counter |
||
41 | */ |
||
42 | const COUNTER_TRANSFER_MIN_RATE = 'speed_rate_counter'; |
||
43 | |||
44 | /** |
||
45 | * Socket for this operation |
||
46 | * |
||
47 | * @var SocketInterface |
||
48 | */ |
||
49 | private $socket; |
||
50 | |||
51 | /** |
||
52 | * Key-value pairs with meta information |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | private $metadata; |
||
57 | |||
58 | /** |
||
59 | * Event handler object |
||
60 | * |
||
61 | * @var EventHandlerInterface |
||
62 | */ |
||
63 | private $handlers; |
||
64 | |||
65 | /** |
||
66 | * Flag whether this socket request is running |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $isRunning; |
||
71 | |||
72 | /** |
||
73 | * Operation to perform on socket |
||
74 | * |
||
75 | * @var OperationInterface |
||
76 | */ |
||
77 | private $operation; |
||
78 | |||
79 | /** |
||
80 | * Flag if this socket is postponed |
||
81 | * |
||
82 | * @var bool |
||
83 | */ |
||
84 | private $isPostponed = false; |
||
85 | |||
86 | /** |
||
87 | * Set of state flags: RDS_* consts |
||
88 | * |
||
89 | * @var int |
||
90 | */ |
||
91 | private $state = 0; |
||
92 | |||
93 | /** |
||
94 | * Array of counters |
||
95 | * |
||
96 | * @var SpeedRateCounter[] |
||
97 | */ |
||
98 | private $counters; |
||
99 | |||
100 | /** |
||
101 | * RequestDescriptor constructor. |
||
102 | * |
||
103 | * @param SocketInterface $socket Socket object |
||
104 | * @param OperationInterface $operation Operation to perform on socket |
||
105 | * @param array $metadata Metadata |
||
106 | * @param EventHandlerInterface $handlers Handlers for this socket |
||
107 | */ |
||
108 | 106 | public function __construct( |
|
121 | |||
122 | /** |
||
123 | * Initialize data before request |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | 106 | public function initialize() |
|
131 | |||
132 | /** |
||
133 | * Return Operation |
||
134 | * |
||
135 | * @return OperationInterface |
||
136 | */ |
||
137 | 55 | public function getOperation() |
|
141 | |||
142 | /** |
||
143 | * Sets Operation |
||
144 | * |
||
145 | * @param OperationInterface $operation New operation |
||
146 | * |
||
147 | * @return void |
||
148 | */ |
||
149 | 26 | public function setOperation(OperationInterface $operation) |
|
153 | |||
154 | /** |
||
155 | * Return flag whether request is running |
||
156 | * |
||
157 | * @return boolean |
||
158 | */ |
||
159 | 70 | public function isRunning() |
|
163 | |||
164 | /** |
||
165 | * Sets running flag |
||
166 | * |
||
167 | * @param boolean $isRunning New value for IsRunning |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 55 | public function setRunning($isRunning) |
|
175 | |||
176 | /** |
||
177 | * Return Socket |
||
178 | * |
||
179 | * @return SocketInterface |
||
180 | */ |
||
181 | 68 | public function getSocket() |
|
185 | |||
186 | /** {@inheritdoc} */ |
||
187 | 53 | public function getStreamResource() |
|
191 | |||
192 | /** |
||
193 | * Return key-value array with metadata |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | 92 | public function getMetadata() |
|
201 | |||
202 | /** |
||
203 | * Set metadata for given socket |
||
204 | * |
||
205 | * @param string|array $key Either string or key-value array of metadata. If string, then value must be |
||
206 | * passed in third argument, if array, then third argument will be ignored |
||
207 | * @param mixed $value Value for key or null, if $key is array |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | 89 | public function setMetadata($key, $value = null) |
|
222 | |||
223 | /** {@inheritdoc} */ |
||
224 | 68 | public function invokeEvent(Event $event) |
|
230 | |||
231 | /** |
||
232 | * Completes processing this socket in event loop, but keep this socket connection opened. Applicable |
||
233 | * only to persistent sockets, all other socket types are ignored by this method. |
||
234 | * |
||
235 | * @return void |
||
236 | */ |
||
237 | 6 | public function postpone() |
|
245 | |||
246 | /** |
||
247 | * Return true, if this socket shouldn't be processed by executor engine |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | 69 | public function isPostponed() |
|
255 | |||
256 | /** |
||
257 | * Return true if descriptor has given state |
||
258 | * |
||
259 | * @param int $state State to check, set of RDS_* consts |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | 70 | public function hasState($state) |
|
267 | |||
268 | /** |
||
269 | * Sets one state into an object |
||
270 | * |
||
271 | * @param int $state State to set, set of RDS_* consts |
||
272 | * |
||
273 | * @return void |
||
274 | */ |
||
275 | 76 | public function setState($state) |
|
279 | |||
280 | /** |
||
281 | * Clears given state in object |
||
282 | * |
||
283 | * @param int $state State to clear, set of RDS_* consts |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | 70 | public function clearState($state) |
|
291 | |||
292 | /** |
||
293 | * Registers counter in this descriptor |
||
294 | * |
||
295 | * @param string $name SpeedRateCounter name for retrieving |
||
296 | * @param SpeedRateCounter $counter A counter object |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | 30 | public function registerCounter($name, SpeedRateCounter $counter) |
|
306 | |||
307 | /** |
||
308 | * Return counter by given name |
||
309 | * |
||
310 | * @param string $name SpeedRateCounter name |
||
311 | * |
||
312 | * @return SpeedRateCounter|null |
||
313 | */ |
||
314 | 30 | public function getCounter($name) |
|
318 | } |
||
319 |