1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use structopt::StructOpt;

use async_std::stream;
use async_std::sync::channel;
use async_std::task;

use futures::prelude::*;
use futures::select;

use tracing::{span, Level};

use bytes::Bytes;

use dsf_core::net::Message as DsfMessage;
use dsf_core::service::{Publisher, ServiceBuilder};
use dsf_core::types::{Address, Id};

use dsf_rpc::{Request as RpcRequest, Response as RpcResponse};

use kad::Config as DhtConfig;

use crate::daemon::*;
use crate::error::Error;
use crate::io::*;
use crate::store::*;

use crate::daemon::Options as DaemonOptions;

pub struct Engine {
    dsf: Dsf<WireConnector>,

    unix: Unix,
    wire: Option<Wire>,
    net: Option<Net>,

    options: Options,
}

pub const DEFAULT_UNIX_SOCKET: &str = "/tmp/dsf.sock";
pub const DEFAULT_DATABASE_FILE: &str = "/tmp/dsf.db";
pub const DEFAULT_SERVICE: &str = "/tmp/dsf.svc";

#[derive(StructOpt, Debug, Clone, PartialEq)]
pub struct Options {
    #[structopt(short = "a", long = "bind-address", default_value = "0.0.0.0:10100")]
    /// Interface(s) to bind DSF daemon
    /// These may be reconfigured at runtime
    pub bind_addresses: Vec<SocketAddr>,

    #[structopt(
        long = "database-file",
        default_value = "/var/dsfd/dsf.db",
        env = "DSF_DB_FILE"
    )]
    /// Database file for storage by the daemon
    pub database_file: String,

    #[structopt(
        short = "s",
        long = "daemon-socket",
        default_value = "/var/run/dsfd/dsf.sock",
        env = "DSF_SOCK"
    )]
    /// Unix socket for communication with the daemon
    pub daemon_socket: String,

    #[structopt(long = "no-bootstrap")]
    /// Disable automatic bootstrapping
    pub no_bootstrap: bool,

    #[structopt(flatten)]
    pub daemon_options: DaemonOptions,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            bind_addresses: vec![SocketAddr::new(
                IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
                10100,
            )],
            daemon_socket: DEFAULT_UNIX_SOCKET.to_string(),
            database_file: DEFAULT_DATABASE_FILE.to_string(),
            no_bootstrap: false,
            daemon_options: DaemonOptions {
                dht: DhtConfig::default(),
            },
        }
    }
}

impl Options {
    /// Helper constructor to run multiple instances alongside each other
    pub fn with_suffix(&self, suffix: usize) -> Self {
        let bind_addresses = self
            .bind_addresses
            .iter()
            .map(|a| {
                let mut a = a.clone();
                a.set_port(a.port() + suffix as u16);
                a
            })
            .collect();

        Self {
            bind_addresses,
            daemon_socket: format!("{}.{}", self.daemon_socket, suffix),
            database_file: format!("{}.{}", self.database_file, suffix),
            no_bootstrap: self.no_bootstrap,
            daemon_options: DaemonOptions {
                dht: self.daemon_options.dht.clone(),
            },
        }
    }
}

impl Engine {
    /// Create a new daemon instance
    pub async fn new(options: Options) -> Result<Self, Error> {
        // Create new local data store
        let store = Store::new(&options.database_file)?;

        // Fetch or create new peer service
        let mut service = match store.load_peer_service()? {
            Some(s) => {
                info!("Loaded existing peer service: {}", s.id());
                s
            }
            None => {
                let s = ServiceBuilder::peer().build().unwrap();
                info!("Created new peer service: {}", s.id());
                s
            }
        };

        // Generate updated peer page
        let mut buff = vec![0u8; 1025];
        let (n, mut page) = service.publish_primary(&mut buff)?;
        page.raw = Some(buff[..n].to_vec());

        // Store service and page
        store.set_peer_service(&service, &page)?;

        let span = span!(Level::DEBUG, "engine", "{}", service.id());
        let _enter = span.enter();

        info!("Creating new engine");

        // Create new network connector
        info!(
            "Creating network connector on addresses: {:?}",
            options.bind_addresses
        );
        let mut net = Net::new();
        for addr in &options.bind_addresses {
            if let Err(e) = net.bind(NetKind::Udp, *addr).await {
                error!("Error binding interface: {:?}", addr);
                return Err(e.into());
            }
        }

        // Create new unix socket connector
        info!("Creating unix socket: {}", options.daemon_socket);
        let unix = match Unix::new(&options.daemon_socket).await {
            Ok(u) => u,
            Err(e) => {
                error!("Error binding unix socket: {}", options.daemon_socket);
                return Err(e.into());
            }
        };

        // Create new wire adaptor
        let wire = Wire::new(service.private_key().unwrap());

        // Create new DSF instance
        let dsf = Dsf::new(
            options.daemon_options.clone(),
            service,
            Arc::new(Mutex::new(store)),
            wire.connector(),
        )?;

        debug!("Engine created!");

        Ok(Self {
            dsf,
            wire: Some(wire),
            net: Some(net),
            unix,
            options,
        })
    }

    pub fn id(&self) -> Id {
        self.dsf.id()
    }

    // Run the DSF daemon
    pub async fn run(&mut self, running: Arc<AtomicBool>) -> Result<(), Error> {
        let span = span!(Level::DEBUG, "engine", "{}", self.dsf.id());
        let _enter = span.enter();

        if !self.options.no_bootstrap {
            let mut d = self.dsf.clone();
            // Create future bootstrap event
            task::spawn(async move {
                task::sleep(Duration::from_secs(2)).await;
                let _ = d.bootstrap().await;
            });
        }

        // Create periodic timer
        let mut update_timer = stream::interval(Duration::from_secs(30));
        let mut tick_timer = stream::interval(Duration::from_secs(1));

        let (net_in_tx, mut net_in_rx) = channel(1000);
        let (net_out_tx, mut net_out_rx) = channel(1000);

        // Setup network IO task

        let (mut wire, mut net) = (self.wire.take().unwrap(), self.net.take().unwrap());
        let r = running.clone();
        let net_dsf = self.dsf.clone();

        let _net_handle = task::spawn(async move {
            while r.load(Ordering::SeqCst) {
                select! {
                    // Incoming network messages
                    net_rx = net.next().fuse() => {
                        trace!("engine::net_rx");

                        if let Some(m) = net_rx {
                            let address = m.address.clone();

                            // Decode message via wire module
                            let message = match wire.handle(m, |id| net_dsf.find_public_key(id)).await {
                                // Incoming request
                                Ok(Some(v)) => v,
                                // Incoming response, routed internally
                                Ok(None) => continue,
                                // Decoding error
                                Err(e) => {
                                    error!("error decoding network message from: {:?}", address);
                                    continue;
                                }
                            };

                            // Forward to DSF for execution
                            net_in_tx.send((address, message)).await;
                        }
                    },
                    net_tx = net_out_rx.next().fuse() => {
                        trace!("engine::net_tx");

                        if let Some((address, message)) = net_tx {
                            let net_tx = match wire.handle_outgoing(Address::from(address), message) {
                                Ok(v) => v,
                                Err(e) => {
                                    error!("error encoding network message: {:?}", e);
                                    continue;
                                }
                            };

                            if let Err(e) = net.send(net_tx).await {
                                error!("error sending network message: {:?}", e);
                            }
                        }
                    }
                    // Outgoing network messages
                    net_tx = wire.next().fuse() => {
                        trace!("engine::wire_tx");

                        if let Some(m) = net_tx {
                            net.send(m).await.unwrap();
                        }
                    }
                }
            }

            warn!("Exiting network handler");
        });

        while running.load(Ordering::SeqCst) {
            #[cfg(feature = "profile")]
            let _fg = ::flame::start_guard("engine::tick");

            select! {
                // Incoming network _requests_
                net_rx = net_in_rx.next().fuse() => {
                    if let Some((address, req)) = net_rx {
                        let mut dsf = self.dsf.clone();

                        // Handle request via DSF
                        let resp = match dsf.handle(address, req).await {
                            Ok(v) => v,
                            Err(e) => {
                                error!("error handling DSF request: {:?}", e);
                                continue;
                            }
                        };
                        // Return response
                        net_out_tx.send((address, DsfMessage::Response(resp))).await;
                    }
                },
                // Incoming RPC messages, response is inline
                rpc_rx = self.unix.next().fuse() => {
                    trace!("engine::unix_rx");

                    if let Some(m) = rpc_rx {
                        let mut dsf = self.dsf.clone();
                        //let mut unix = self.unix.clone();

                        // RPC tasks can take some time and thus must be independent threads
                        // To avoid blocking network operations
                        task::spawn(async move {
                            Self::handle_rpc(&mut dsf, m).await.unwrap();
                        });
                    }
                },
                // TODO: periodic update
                interval = update_timer.next().fuse() => {
                    trace!("engine::tick");

                    if let Some(_i) = interval {

                    }
                },
                // Tick timer for process reactivity
                tick = tick_timer.next().fuse() => {},
            }
        }

        // TODO: join on net handle

        Ok(())
    }

    async fn handle_rpc(dsf: &mut Dsf<WireConnector>, unix_req: UnixMessage) -> Result<(), Error> {
        // Parse out message
        let req: RpcRequest = serde_json::from_slice(&unix_req.data).unwrap();

        let span = span!(Level::TRACE, "rpc", id = req.req_id());
        let _enter = span.enter();

        // Handle RPC request
        let resp_kind = dsf.exec(req.kind()).await?;

        // Generate response
        let resp = RpcResponse::new(req.req_id(), resp_kind);

        // Encode response
        let enc = serde_json::to_vec(&resp).unwrap();

        // Generate response with required socket info
        let unix_resp = unix_req.response(Bytes::from(enc));

        // Send response
        if let Err(e) = unix_resp.send().await {
            error!("Error sending RPC response: {:?}", e);
        }

        Ok(())
    }
}