1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phloppy\Stream; |
4
|
|
|
|
5
|
|
|
use Phloppy\Exception\ConnectException; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Psr\Log\NullLogger; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Phloppy Node Pool. |
11
|
|
|
*/ |
12
|
|
|
class Pool implements StreamInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $nodeUrls; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var LoggerInterface |
22
|
|
|
*/ |
23
|
|
|
protected $log; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var StreamInterface |
27
|
|
|
*/ |
28
|
|
|
protected $connected; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $nodeUrls |
33
|
|
|
* @param LoggerInterface|null $log |
34
|
|
|
* |
35
|
|
|
* @throws ConnectException |
36
|
|
|
*/ |
37
|
28 |
|
public function __construct(array $nodeUrls = array(), LoggerInterface $log = null) |
38
|
|
|
{ |
39
|
28 |
|
$this->nodeUrls = $nodeUrls; |
40
|
|
|
|
41
|
28 |
|
if (!$log) { |
42
|
4 |
|
$log = new NullLogger(); |
43
|
4 |
|
} |
44
|
|
|
|
45
|
28 |
|
$this->log = $log; |
46
|
28 |
|
$this->connected = $this->connect(); |
47
|
27 |
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
1 |
|
public function getNodeUrls() |
54
|
|
|
{ |
55
|
1 |
|
return $this->nodeUrls; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return StreamInterface |
61
|
|
|
*/ |
62
|
1 |
|
public function getActiveNode() |
63
|
|
|
{ |
64
|
1 |
|
return $this->connected; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
* @throws ConnectException |
71
|
|
|
*/ |
72
|
1 |
|
public function reconnect() |
|
|
|
|
73
|
|
|
{ |
74
|
1 |
|
$this->close(); |
75
|
1 |
|
$this->connected = $this->connect(); |
76
|
|
|
|
77
|
1 |
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Connect to a random node in the node list. |
83
|
|
|
* |
84
|
|
|
* @return DefaultStream Stream to a connected node. |
85
|
|
|
* |
86
|
|
|
* @throws ConnectException |
87
|
|
|
*/ |
88
|
28 |
|
public function connect() |
89
|
|
|
{ |
90
|
28 |
|
$nodes = $this->nodeUrls; |
91
|
|
|
|
92
|
28 |
|
while (count($nodes)) { |
93
|
|
|
// pick random server |
94
|
28 |
|
$idx = rand(0, count($nodes) - 1); |
95
|
|
|
|
96
|
|
|
try { |
97
|
28 |
|
$stream = new DefaultStream($nodes[$idx], $this->log); |
98
|
28 |
|
$stream->connect(); |
99
|
|
|
|
100
|
27 |
|
return $stream; |
101
|
1 |
|
} catch (ConnectException $e) { |
102
|
1 |
|
$this->log->warning($e->getMessage()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// remove the selected server from the list |
106
|
1 |
|
array_splice($nodes, $idx, 1); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
1 |
|
throw new ConnectException('unable to connect to any of ['.implode(',', $this->nodeUrls).']'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Read a line from the stream |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
23 |
|
public function readLine() |
119
|
|
|
{ |
120
|
23 |
|
return $this->connected->readLine(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Read bytes off from the stream. |
126
|
|
|
* |
127
|
|
|
* @param int|null $maxlen |
128
|
|
|
* |
129
|
|
|
* @return string The response. |
130
|
|
|
*/ |
131
|
13 |
|
public function readBytes($maxlen = null) |
132
|
|
|
{ |
133
|
13 |
|
return $this->connected->readBytes($maxlen); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $msg |
139
|
|
|
* @param int|null $len |
140
|
|
|
* |
141
|
|
|
* @return StreamInterface the Stream instance. |
|
|
|
|
142
|
|
|
*/ |
143
|
23 |
|
public function write($msg, $len = null) |
144
|
|
|
{ |
145
|
23 |
|
$this->connected->write($msg, $len); |
146
|
|
|
|
147
|
23 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Close the stream. |
153
|
|
|
* |
154
|
|
|
* @return bool True on success. |
155
|
|
|
*/ |
156
|
26 |
|
public function close() |
157
|
|
|
{ |
158
|
26 |
|
return $this->connected->close(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Check if the stream is connected. |
164
|
|
|
* |
165
|
|
|
* @return boolean True if the stream is connected. |
166
|
|
|
*/ |
167
|
2 |
|
public function isConnected() |
168
|
|
|
{ |
169
|
2 |
|
return $this->connected->isConnected(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* return the internal stream url. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
2 |
|
public function getNodeUrl() |
179
|
|
|
{ |
180
|
2 |
|
return $this->connected->getNodeUrl(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.