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 | * Socket for this operation |
||
41 | * |
||
42 | * @var SocketInterface |
||
43 | */ |
||
44 | private $socket; |
||
45 | |||
46 | /** |
||
47 | * Key-value pairs with meta information |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private $metadata; |
||
52 | |||
53 | /** |
||
54 | * Event handler object |
||
55 | * |
||
56 | * @var EventHandlerInterface |
||
57 | */ |
||
58 | private $handlers; |
||
59 | |||
60 | /** |
||
61 | * Flag whether this socket request is running |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | private $isRunning; |
||
66 | |||
67 | /** |
||
68 | * Operation to perform on socket |
||
69 | * |
||
70 | * @var OperationInterface |
||
71 | */ |
||
72 | private $operation; |
||
73 | |||
74 | /** |
||
75 | * Flag if this socket is postponed |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | private $isPostponed = false; |
||
80 | |||
81 | /** |
||
82 | * Set of state flags: RDS_* consts |
||
83 | * |
||
84 | * @var int |
||
85 | */ |
||
86 | private $state = 0; |
||
87 | |||
88 | /** |
||
89 | * RequestDescriptor constructor. |
||
90 | * |
||
91 | * @param SocketInterface $socket Socket object |
||
92 | * @param OperationInterface $operation Operation to perform on socket |
||
93 | * @param array $metadata Metadata |
||
94 | * @param EventHandlerInterface $handlers Handlers for this socket |
||
95 | */ |
||
96 | 158 | public function __construct( |
|
97 | SocketInterface $socket, |
||
98 | OperationInterface $operation, |
||
99 | array $metadata, |
||
100 | EventHandlerInterface $handlers = null |
||
101 | ) { |
||
102 | 158 | $this->socket = $socket; |
|
103 | 158 | $this->operation = $operation; |
|
104 | 158 | $this->metadata = $metadata; |
|
105 | 158 | $this->handlers = $handlers; |
|
106 | 158 | $this->initialize(); |
|
107 | 158 | } |
|
108 | |||
109 | /** |
||
110 | * Return Operation |
||
111 | * |
||
112 | * @return OperationInterface |
||
113 | */ |
||
114 | 99 | public function getOperation() |
|
115 | { |
||
116 | 99 | return $this->operation; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Sets Operation |
||
121 | * |
||
122 | * @param OperationInterface $operation New operation |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | 49 | public function setOperation(OperationInterface $operation) |
|
127 | { |
||
128 | 49 | $this->operation = $operation; |
|
129 | 49 | } |
|
130 | |||
131 | /** |
||
132 | * Initialize data before request |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | 158 | public function initialize() |
|
140 | |||
141 | /** |
||
142 | * Return flag whether request is running |
||
143 | * |
||
144 | * @return boolean |
||
145 | */ |
||
146 | 127 | public function isRunning() |
|
150 | |||
151 | /** |
||
152 | * Sets running flag |
||
153 | * |
||
154 | * @param boolean $isRunning New value for IsRunning |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | 98 | public function setRunning($isRunning) |
|
159 | { |
||
160 | 98 | $this->isRunning = $isRunning; |
|
161 | 98 | } |
|
162 | |||
163 | /** |
||
164 | * Return Socket |
||
165 | * |
||
166 | * @return SocketInterface |
||
167 | */ |
||
168 | 126 | public function getSocket() |
|
169 | { |
||
170 | 126 | return $this->socket; |
|
171 | } |
||
172 | |||
173 | /** {@inheritdoc} */ |
||
174 | 48 | public function getStreamResource() |
|
175 | { |
||
176 | 48 | return $this->socket->getStreamResource(); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Return key-value array with metadata |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | 145 | public function getMetadata() |
|
188 | |||
189 | /** |
||
190 | * Set metadata for given socket |
||
191 | * |
||
192 | * @param string|array $key Either string or key-value array of metadata. If string, then value must be |
||
193 | * passed in third argument, if array, then third argument will be ignored |
||
194 | * @param mixed $value Value for key or null, if $key is array |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | 140 | public function setMetadata($key, $value = null) |
|
209 | |||
210 | /** {@inheritdoc} */ |
||
211 | 125 | public function invokeEvent(Event $event) |
|
217 | |||
218 | /** |
||
219 | * Completes processing this socket in event loop, but keep this socket connection opened. Applicable |
||
220 | * only to persistent sockets, all other socket types are ignored by this method. |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | 6 | public function postpone() |
|
232 | |||
233 | /** |
||
234 | * Return true, if this socket shouldn't be processed by executor engine |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 112 | public function isPostponed() |
|
242 | |||
243 | /** |
||
244 | * Return true if descriptor has given state |
||
245 | * |
||
246 | * @param int $state State to check, set of RDS_* consts |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | 100 | public function hasState($state) |
|
254 | |||
255 | /** |
||
256 | * Sets one state into an object |
||
257 | * |
||
258 | * @param int $state State to set, set of RDS_* consts |
||
259 | * |
||
260 | * @return void |
||
261 | */ |
||
262 | 118 | public function setState($state) |
|
266 | |||
267 | /** |
||
268 | * Clears given state in object |
||
269 | * |
||
270 | * @param int $state State to clear, set of RDS_* consts |
||
271 | * |
||
272 | * @return void |
||
273 | */ |
||
274 | 100 | public function clearState($state) |
|
278 | } |
||
279 |