59
59
//! The overall program logic thus is the following:
60
60
//!
61
61
//! In the process starting from `main()`:
62
- //! - Parse cli arguments into a config. There is no config file, since the
63
- //! daemon can be started only from another control process (`lnpd`) or by
64
- //! forking itself.
65
- //! - If `--listen` argument is present, start a listening version as described
66
- //! above and open TCP port in listening mode; wait for incoming connections
62
+ //! - Parse cli arguments into a config. There is no config file, since the daemon can be started
63
+ //! only from another control process (`lnpd`) or by forking itself.
64
+ //! - If `--listen` argument is present, start a listening version as described above and open TCP
65
+ //! port in listening mode; wait for incoming connections
67
66
//! - If `--connect` argument is present, connect to the remote TCP peer
68
67
//!
69
68
//! In forked/spawned version:
73
72
//! launched from the control process:
74
73
//! - Split TCP socket and related transcoders into reading and writing parts
75
74
//! - Create bridge ZMQ PAIR socket
76
- //! - Put both TCP socket reading ZMQ bridge write PAIR parts into a thread
77
- //! ("bridge")
75
+ //! - Put both TCP socket reading ZMQ bridge write PAIR parts into a thread ("bridge")
78
76
//! - Open control interface socket
79
77
//! - Create run loop in the main thread for polling three ZMQ sockets:
80
78
//! * control interface
@@ -93,21 +91,18 @@ extern crate log;
93
91
#[ macro_use]
94
92
extern crate amplify;
95
93
96
- use clap:: Parser ;
97
- use internet2:: addr:: InetSocketAddr ;
98
- use nix:: unistd:: { fork, ForkResult } ;
99
94
use std:: convert:: TryFrom ;
100
- use std:: net:: TcpListener ;
101
- use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
95
+ use std:: net:: { IpAddr , Ipv4Addr , SocketAddr , TcpListener } ;
102
96
use std:: time:: Duration ;
103
97
104
98
use bitcoin:: secp256k1:: PublicKey ;
105
- use internet2 :: {
106
- session , FramingProtocol , NodeAddr , RemoteNodeAddr , RemoteSocketAddr ,
107
- } ;
99
+ use clap :: Parser ;
100
+ use internet2 :: addr :: InetSocketAddr ;
101
+ use internet2 :: { session , FramingProtocol , NodeAddr , RemoteNodeAddr , RemoteSocketAddr } ;
108
102
use lnp_node:: peerd:: { self , Opts } ;
109
103
use lnp_node:: { Config , LogStyle } ;
110
104
use microservices:: peer:: PeerConnection ;
105
+ use nix:: unistd:: { fork, ForkResult } ;
111
106
112
107
/*
113
108
mod internal {
@@ -142,21 +137,15 @@ impl From<Opts> for PeerSocket {
142
137
Self :: Connect ( peer_addr)
143
138
} else if let Some ( bind_addr) = opts. listen {
144
139
Self :: Listen ( match opts. overlay {
145
- FramingProtocol :: FramedRaw => {
146
- RemoteSocketAddr :: Ftcp ( InetSocketAddr {
147
- address : bind_addr
148
- . unwrap_or ( IpAddr :: V4 ( Ipv4Addr :: UNSPECIFIED ) )
149
- . into ( ) ,
150
- port : opts. port ,
151
- } )
152
- }
140
+ FramingProtocol :: FramedRaw => RemoteSocketAddr :: Ftcp ( InetSocketAddr {
141
+ address : bind_addr. unwrap_or ( IpAddr :: V4 ( Ipv4Addr :: UNSPECIFIED ) ) . into ( ) ,
142
+ port : opts. port ,
143
+ } ) ,
153
144
// TODO: (v2) implement overlay protocols
154
145
_ => unimplemented ! ( ) ,
155
146
} )
156
147
} else {
157
- unreachable ! (
158
- "Either `connect` or `listen` must be present due to Clap configuration"
159
- )
148
+ unreachable ! ( "Either `connect` or `listen` must be present due to Clap configuration" )
160
149
}
161
150
}
162
151
}
@@ -207,29 +196,24 @@ fn main() {
207
196
208
197
debug ! ( "Binding TCP socket {}" , inet_addr) ;
209
198
let listener = TcpListener :: bind (
210
- SocketAddr :: try_from ( inet_addr)
211
- . expect ( "Tor is not yet supported" ) ,
199
+ SocketAddr :: try_from ( inet_addr) . expect ( "Tor is not yet supported" ) ,
212
200
)
213
201
. expect ( "Unable to bind to Lightning network peer socket" ) ;
214
202
215
203
debug ! ( "Running TCP listener event loop" ) ;
216
204
loop {
217
205
debug ! ( "Awaiting for incoming connections..." ) ;
218
- let ( stream, remote_socket_addr) = listener
219
- . accept ( )
220
- . expect ( "Error accepting incpming peer connection" ) ;
206
+ let ( stream, remote_socket_addr) =
207
+ listener. accept ( ) . expect ( "Error accepting incpming peer connection" ) ;
221
208
debug ! ( "New connection from {}" , remote_socket_addr) ;
222
209
223
210
remote_socket = remote_socket_addr. into ( ) ;
224
211
225
212
// TODO: Support multithread mode
226
213
debug ! ( "Forking child process" ) ;
227
- if let ForkResult :: Child =
228
- unsafe { fork ( ) . expect ( "Unable to fork child process" ) }
214
+ if let ForkResult :: Child = unsafe { fork ( ) . expect ( "Unable to fork child process" ) }
229
215
{
230
- trace ! (
231
- "Child forked; returning into main listener event loop"
232
- ) ;
216
+ trace ! ( "Child forked; returning into main listener event loop" ) ;
233
217
continue ;
234
218
}
235
219
@@ -238,11 +222,8 @@ fn main() {
238
222
. expect ( "Unable to set up timeout for TCP connection" ) ;
239
223
240
224
debug ! ( "Establishing session with the remote" ) ;
241
- let session =
242
- session:: Raw :: with_ftcp_unencrypted ( stream, inet_addr)
243
- . expect (
244
- "Unable to establish session with the remote peer" ,
245
- ) ;
225
+ let session = session:: Raw :: with_ftcp_unencrypted ( stream, inet_addr)
226
+ . expect ( "Unable to establish session with the remote peer" ) ;
246
227
247
228
debug ! ( "Session successfully established" ) ;
248
229
break PeerConnection :: with ( session) ;
@@ -264,17 +245,8 @@ fn main() {
264
245
} ;
265
246
266
247
debug ! ( "Starting runtime ..." ) ;
267
- peerd:: run (
268
- config,
269
- connection,
270
- id,
271
- local_id,
272
- remote_id,
273
- local_socket,
274
- remote_socket,
275
- connect,
276
- )
277
- . expect ( "Error running peerd runtime" ) ;
248
+ peerd:: run ( config, connection, id, local_id, remote_id, local_socket, remote_socket, connect)
249
+ . expect ( "Error running peerd runtime" ) ;
278
250
279
251
unreachable ! ( )
280
252
}
0 commit comments