1 | <?php |
||
25 | class Connection implements ConnectionInterface, WireSubscriberInterface |
||
26 | { |
||
27 | const STATUS_CLOSED = 'closed'; |
||
28 | const STATUS_READY = 'ready'; |
||
29 | const STATUS_BLOCKED = 'blocked'; |
||
30 | |||
31 | /** |
||
32 | * @var Url |
||
33 | */ |
||
34 | private $url; |
||
35 | |||
36 | /** |
||
37 | * @var WireInterface |
||
38 | */ |
||
39 | private $wire; |
||
40 | |||
41 | /** |
||
42 | * @var AuthenticatorInterface |
||
43 | */ |
||
44 | private $authenticator; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $status; |
||
50 | |||
51 | /** |
||
52 | * @var Channel[] |
||
53 | */ |
||
54 | private $channels = []; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $capabilities = []; |
||
60 | |||
61 | /** |
||
62 | * @param Url|string $url |
||
63 | * @param WireInterface $wire |
||
64 | * @param AuthenticatorInterface $authenticator |
||
65 | */ |
||
66 | 31 | public function __construct(Url $url, WireInterface $wire, AuthenticatorInterface $authenticator = null) |
|
72 | |||
73 | /** |
||
74 | * Connection status. See STATUS_* constants for possible values. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 4 | public function getStatus() |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 19 | public function open() |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | 19 | public function channel($id = null) |
|
123 | |||
124 | /** |
||
125 | * @return int |
||
126 | */ |
||
127 | 18 | private function allocateChannelNumber() |
|
131 | |||
132 | /** |
||
133 | * @param int $id |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | 19 | private function isChannelNumberValid($id) |
|
141 | |||
142 | /** |
||
143 | * @param int $id |
||
144 | * |
||
145 | * @return Channel |
||
146 | */ |
||
147 | 18 | private function openChannel($id) |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 7 | public function close($code = 0, $reason = '') |
|
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | 1 | public function isSupported($capability) |
|
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | 10 | public function serve($blocking = true) |
|
188 | |||
189 | /** |
||
190 | * Sends frame to the service channel (#0). |
||
191 | * |
||
192 | * @param Frame $frame |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | 26 | private function send(Frame $frame) |
|
202 | |||
203 | /** |
||
204 | * Wait for a frame in the service channel (#0). |
||
205 | * |
||
206 | * @param string|array $type |
||
207 | * |
||
208 | * @return Frame |
||
209 | */ |
||
210 | 20 | private function wait($type) |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | 26 | public function dispatch(Frame $frame) |
|
232 | |||
233 | /** |
||
234 | * This frame is the first frame received from server. |
||
235 | * It provides server details and requests client credentials. |
||
236 | * |
||
237 | * @param ConnectionStart $frame |
||
238 | */ |
||
239 | 20 | private function onConnectionStart(ConnectionStart $frame) |
|
271 | |||
272 | /** |
||
273 | * This frame is received to setup connection preferences, like max frame size, |
||
274 | * max number of channel and heartbeat delay. |
||
275 | * |
||
276 | * Values in the request can be lowered by client. |
||
277 | * |
||
278 | * @param ConnectionTune $frame |
||
279 | */ |
||
280 | private function onConnectionTune(ConnectionTune $frame) |
||
295 | |||
296 | /** |
||
297 | * This frame is received once server decide to close connection, normally because an unrecoverable error occur. |
||
298 | * |
||
299 | * @param ConnectionClose $frame |
||
300 | * |
||
301 | * @throws AMQPException |
||
302 | */ |
||
303 | 3 | private function onConnectionClose(ConnectionClose $frame) |
|
314 | |||
315 | /** |
||
316 | * This frame is received once server decide to suspend connection, for example because server |
||
317 | * run out of memory and can not provide service for the connection. When this happen consumer |
||
318 | * suppose to suspend all activities until connection.unblocked is received. |
||
319 | * |
||
320 | * @param ConnectionBlocked $frame |
||
321 | */ |
||
322 | 1 | private function onConnectionBlocked(ConnectionBlocked $frame) |
|
326 | |||
327 | /** |
||
328 | * This frame is received once connection returns back to normal state after being suspended. |
||
329 | * See onConnectionBlocked above. |
||
330 | * |
||
331 | * @param ConnectionUnblocked $frame |
||
332 | */ |
||
333 | 1 | private function onConnectionUnblocked(ConnectionUnblocked $frame) |
|
337 | } |
||
338 |