1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Networking; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
6
|
|
|
use BitWasp\Bitcoin\Crypto\Random\Random; |
7
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
8
|
|
|
use BitWasp\Bitcoin\Networking\Ip\IpInterface; |
9
|
|
|
use BitWasp\Bitcoin\Networking\Peer\ConnectionParams; |
10
|
|
|
use BitWasp\Bitcoin\Networking\Peer\Connector; |
11
|
|
|
use BitWasp\Bitcoin\Networking\Peer\Listener; |
12
|
|
|
use BitWasp\Bitcoin\Networking\Peer\Locator; |
13
|
|
|
use BitWasp\Bitcoin\Networking\Peer\Manager; |
14
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress; |
15
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface; |
16
|
|
|
use React\EventLoop\LoopInterface; |
17
|
|
|
|
18
|
|
|
class Factory |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var NetworkInterface |
23
|
|
|
*/ |
24
|
|
|
private $network; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var LoopInterface |
28
|
|
|
*/ |
29
|
18 |
|
private $loop; |
30
|
|
|
|
31
|
18 |
|
private $messages = null; |
32
|
18 |
|
|
33
|
18 |
|
/** |
34
|
|
|
* @param NetworkInterface $network |
35
|
|
|
* @param LoopInterface $loop |
36
|
|
|
*/ |
37
|
|
|
public function __construct(LoopInterface $loop, NetworkInterface $network = null) |
38
|
|
|
{ |
39
|
18 |
|
$this->loop = $loop; |
40
|
|
|
$this->network = $network ?: Bitcoin::getNetwork(); |
41
|
18 |
|
$this->settings = new Settings\MainnetSettings(); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Dns\Resolver |
46
|
|
|
*/ |
47
|
|
|
public function getDns() |
48
|
12 |
|
{ |
49
|
|
|
return (new Dns\Factory())->create($this->settings->getDnsServer(), $this->loop); |
50
|
12 |
|
} |
51
|
12 |
|
|
52
|
12 |
|
/** |
53
|
8 |
|
* @param Random|null $random |
54
|
|
|
* @return Messages\Factory |
55
|
|
|
*/ |
56
|
|
|
public function getMessages(Random $random = null) |
57
|
|
|
{ |
58
|
|
|
if (null === $this->messages) { |
59
|
|
|
$this->messages = new Messages\Factory( |
60
|
|
|
$this->network, |
61
|
|
|
$random ?: new Random() |
62
|
6 |
|
); |
63
|
|
|
} |
64
|
6 |
|
return $this->messages; |
65
|
4 |
|
} |
66
|
4 |
|
|
67
|
|
|
/** |
68
|
4 |
|
* @param Settings\NetworkSettings $settings |
69
|
|
|
*/ |
70
|
|
|
public function setSettings(Settings\NetworkSettings $settings) |
71
|
|
|
{ |
72
|
|
|
$this->settings = $settings; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Settings\NetworkSettings |
77
|
|
|
*/ |
78
|
|
|
public function getSettings() |
79
|
|
|
{ |
80
|
|
|
return $this->settings; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Peer\ConnectionParams $params |
85
|
|
|
* @return Peer\Connector |
86
|
|
|
*/ |
87
|
|
|
public function getConnector(Peer\ConnectionParams $params) |
88
|
|
|
{ |
89
|
|
|
return new Connector($this->getMessages(), $params, $this->loop, $this->settings->getSocketParams()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Peer\Connector $connector |
94
|
|
|
* @return Peer\Manager |
95
|
|
|
*/ |
96
|
|
|
public function getManager(Peer\Connector $connector) |
97
|
|
|
{ |
98
|
|
|
return new Manager($connector, $this->settings); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return Peer\Locator |
103
|
|
|
*/ |
104
|
|
|
public function getLocator() |
105
|
|
|
{ |
106
|
|
|
return new Locator($this->getDns(), $this->settings); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param ConnectionParams $params |
111
|
|
|
* @param NetworkAddressInterface $serverAddress |
112
|
|
|
* @return Listener |
113
|
|
|
*/ |
114
|
|
|
public function getListener(Peer\ConnectionParams $params, NetworkAddressInterface $serverAddress) |
115
|
|
|
{ |
116
|
|
|
return new Listener($params, $this->getMessages(), $serverAddress, $this->loop); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param IpInterface $ipAddress |
121
|
|
|
* @param int $port |
122
|
|
|
* @param int $services |
123
|
|
|
* @return NetworkAddress |
124
|
|
|
*/ |
125
|
|
|
public function getAddress(IpInterface $ipAddress, $port = 8333, $services = Services::NONE) |
126
|
|
|
{ |
127
|
|
|
return new NetworkAddress( |
128
|
|
|
$services, |
129
|
|
|
$ipAddress, |
130
|
|
|
$port |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: