1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Networking\Settings; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Networking\DnsSeeds\DnsSeedList; |
6
|
|
|
|
7
|
|
|
abstract class NetworkSettings |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var int |
11
|
|
|
*/ |
12
|
|
|
private $defaultP2PPort = 8333; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
private $connectionTimeout = 10; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $dnsServer = '8.8.8.8'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var DnsSeedList |
26
|
|
|
*/ |
27
|
|
|
private $dnsSeeds = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $maxRetries = 5; |
33
|
|
|
|
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->setup(); |
37
|
|
|
$this->validateSettings(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Setup of network goes here: |
42
|
|
|
*/ |
43
|
|
|
abstract protected function setup(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
*/ |
48
|
|
|
protected function validateSettings() |
49
|
|
|
{ |
50
|
|
|
if (!$this->dnsSeeds instanceof DnsSeedList) { |
51
|
|
|
throw new \RuntimeException("Invalid DNS Seeds"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!(is_integer($this->maxRetries) && $this->maxRetries >= 0)) { |
55
|
|
|
throw new \RuntimeException("Max retries must be a positive integer"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!(is_integer($this->connectionTimeout) && $this->connectionTimeout >= 0)) { |
59
|
|
|
throw new \RuntimeException("Connection timeout must be a positive integer"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!(is_integer($this->defaultP2PPort) && $this->defaultP2PPort >= 0)) { |
63
|
|
|
throw new \RuntimeException("Default P2P must be a positive integer"); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return DnsSeedList |
69
|
|
|
*/ |
70
|
|
|
public function getDnsSeedList() |
71
|
|
|
{ |
72
|
|
|
return $this->dnsSeeds; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param DnsSeedList $list |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function setDnsSeeds(DnsSeedList $list) |
80
|
|
|
{ |
81
|
|
|
$this->dnsSeeds = $list; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getDnsServer() |
89
|
|
|
{ |
90
|
|
|
return $this->dnsServer; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $server |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setDnsServer($server) |
98
|
|
|
{ |
99
|
|
|
$this->dnsServer = $server; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
|
|
public function getDefaultP2PPort() |
107
|
|
|
{ |
108
|
|
|
return $this->defaultP2PPort; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int $port |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function setDefaultP2PPort($port) |
116
|
|
|
{ |
117
|
|
|
$this->defaultP2PPort = $port; |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
|
|
public function getConnectionTimeout() |
125
|
|
|
{ |
126
|
|
|
return $this->connectionTimeout; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param int $timeout |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function setConnectionTimeout($timeout) |
134
|
|
|
{ |
135
|
|
|
$this->connectionTimeout = $timeout; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int $maxRetries |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setMaxConnectRetries($maxRetries) |
144
|
|
|
{ |
145
|
|
|
$this->maxRetries = $maxRetries; |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
|
|
public function getMaxConnectRetries() |
153
|
|
|
{ |
154
|
|
|
return $this->maxRetries; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
public function getSocketParams() |
161
|
|
|
{ |
162
|
|
|
return [ |
163
|
|
|
'timeout' => $this->getConnectionTimeout(), |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|