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) |
|
108 | { |
||
109 | 19 | if ($id === null) { |
|
110 | 18 | $id = $this->allocateChannelNumber(); |
|
111 | 18 | } |
|
112 | |||
113 | 19 | if (!$this->isChannelNumberValid($id)) { |
|
114 | 1 | throw new InvalidChannelNumberException('Channel ID should be positive integer'); |
|
115 | } |
||
116 | |||
117 | 18 | if (!isset($this->channels[$id])) { |
|
118 | 18 | $this->channels[$id] = $this->openChannel($id); |
|
119 | 18 | } |
|
120 | |||
121 | 18 | return $this->channels[$id]; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return int |
||
126 | */ |
||
127 | 18 | private function allocateChannelNumber() |
|
128 | { |
||
129 | 18 | return count($this->channels) == 0 ? 1 : max(array_keys($this->channels)) + 1; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param int $id |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | 19 | private function isChannelNumberValid($id) |
|
138 | { |
||
139 | 19 | return is_integer($id) && $id > 0; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param int $id |
||
144 | * |
||
145 | * @return Channel |
||
146 | */ |
||
147 | 18 | private function openChannel($id) |
|
148 | { |
||
149 | 18 | $channel = new Channel($this->wire, $id); |
|
150 | 18 | $channel->open(); |
|
151 | |||
152 | 18 | return $channel; |
|
153 | } |
||
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) |
|
259 | |||
260 | /** |
||
261 | * @return array |
||
262 | */ |
||
263 | 20 | private function getClientProperties() |
|
279 | |||
280 | /** |
||
281 | * This frame is received to setup connection preferences, like max frame size, |
||
282 | * max number of channel and heartbeat delay. |
||
283 | * |
||
284 | * Values in the request can be lowered by client. |
||
285 | * |
||
286 | * @param ConnectionTune $frame |
||
287 | */ |
||
288 | private function onConnectionTune(ConnectionTune $frame) |
||
303 | |||
304 | /** |
||
305 | * This frame is received once server decide to close connection, normally because an unrecoverable error occur. |
||
306 | * |
||
307 | * @param ConnectionClose $frame |
||
308 | * |
||
309 | * @throws AMQPException |
||
310 | */ |
||
311 | 3 | private function onConnectionClose(ConnectionClose $frame) |
|
322 | |||
323 | /** |
||
324 | * This frame is received once server decide to suspend connection, for example because server |
||
325 | * run out of memory and can not provide service for the connection. When this happen consumer |
||
326 | * suppose to suspend all activities until connection.unblocked is received. |
||
327 | * |
||
328 | * @param ConnectionBlocked $frame |
||
329 | */ |
||
330 | 1 | private function onConnectionBlocked(ConnectionBlocked $frame) |
|
334 | |||
335 | /** |
||
336 | * This frame is received once connection returns back to normal state after being suspended. |
||
337 | * See onConnectionBlocked above. |
||
338 | * |
||
339 | * @param ConnectionUnblocked $frame |
||
340 | */ |
||
341 | 1 | private function onConnectionUnblocked(ConnectionUnblocked $frame) |
|
345 | } |
||
346 |