1 | <?php |
||
12 | class Manager extends EventEmitter |
||
13 | { |
||
14 | /** |
||
15 | * @var Connector |
||
16 | */ |
||
17 | private $connector; |
||
18 | |||
19 | /** |
||
20 | * @var Peer[] |
||
21 | */ |
||
22 | private $outPeers = []; |
||
23 | |||
24 | /** |
||
25 | * @var Peer[] |
||
26 | */ |
||
27 | private $inPeers = []; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $nOutPeers = 0; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | private $nInPeers = 0; |
||
38 | |||
39 | /** |
||
40 | * @var NetworkSettings |
||
41 | */ |
||
42 | private $settings; |
||
43 | |||
44 | /** |
||
45 | * Manager constructor. |
||
46 | * @param Connector $connector |
||
47 | * @param NetworkSettings $settings |
||
48 | */ |
||
49 | 6 | public function __construct(Connector $connector, NetworkSettings $settings) |
|
54 | |||
55 | /** |
||
56 | * Store the newly connected peer, and trigger a new connection if they go away. |
||
57 | * |
||
58 | * @param Peer $peer |
||
59 | * @return Peer |
||
60 | */ |
||
61 | 3 | public function registerOutboundPeer(Peer $peer) |
|
73 | |||
74 | /** |
||
75 | * @param Peer $peer |
||
76 | */ |
||
77 | 3 | public function registerInboundPeer(Peer $peer) |
|
86 | |||
87 | /** |
||
88 | * @param Listener $listener |
||
89 | * @return $this |
||
90 | */ |
||
91 | 3 | public function registerListener(Listener $listener) |
|
99 | |||
100 | /** |
||
101 | * @param NetworkAddressInterface $address |
||
102 | * @return \React\Promise\PromiseInterface |
||
103 | * @throws \Exception |
||
104 | */ |
||
105 | 3 | public function connect(NetworkAddressInterface $address) |
|
109 | |||
110 | /** |
||
111 | * @param Locator $locator |
||
112 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
113 | */ |
||
114 | 3 | public function getAnotherPeer(Locator $locator) |
|
134 | |||
135 | /** |
||
136 | * @param Locator $locator |
||
137 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
138 | */ |
||
139 | 3 | public function attemptNextPeer(Locator $locator) |
|
162 | |||
163 | /** |
||
164 | * @param Locator $locator |
||
165 | * @param int $retries |
||
166 | * @return \React\Promise\PromiseInterface |
||
167 | */ |
||
168 | 3 | public function connectNextPeer(Locator $locator, $retries = null) |
|
169 | { |
||
170 | 3 | if ($retries === null) { |
|
171 | 3 | $retries = $this->settings->getMaxConnectRetries(); |
|
172 | 1 | } |
|
173 | |||
174 | 3 | if (!(is_integer($retries) && $retries >= 0)) { |
|
175 | throw new \InvalidArgumentException("Invalid retry count, must be an integer greater than zero"); |
||
176 | } |
||
177 | |||
178 | 3 | $errorBack = function ($error) use ($locator, $retries) { |
|
179 | 1 | $allowContinue = false; |
|
180 | 1 | if ($error instanceof \RuntimeException) { |
|
181 | 1 | if ($error->getMessage() === "Connection refused") { |
|
182 | $allowContinue = true; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | 1 | if ($error instanceof TimeoutException) { |
|
187 | 1 | $allowContinue = true; |
|
188 | } |
||
189 | |||
190 | 1 | if (!$allowContinue) { |
|
191 | throw $error; |
||
192 | } |
||
193 | |||
194 | 1 | if (0 >= $retries) { |
|
195 | throw new \RuntimeException("Connection to peers failed: too many attempts"); |
||
196 | } |
||
197 | |||
198 | 1 | return $this->connectNextPeer($locator, $retries - 1); |
|
199 | 3 | }; |
|
200 | |||
201 | 1 | return $this |
|
202 | 3 | ->attemptNextPeer($locator) |
|
203 | 3 | ->then(null, $errorBack); |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param Locator $locator |
||
208 | * @param int $n |
||
209 | * @return \React\Promise\Promise |
||
210 | */ |
||
211 | 3 | public function connectToPeers(Locator $locator, $n) |
|
220 | } |
||
221 |