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
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # qauld - qaul Daemon
//!
//! qaul daemon is running headless in the background.
//! It can be used to run on an embedded device, such as a raspberry Pi,
//! or as a static node on a server in the Internet.

use clap::{App, Arg};
use std::collections::BTreeMap;
use std::{thread, time::Duration};

use libqaul;

/// get command line arguments
pub fn get_argument(pattern: &str) -> Option<String> {
    let matches = App::new("")
        .arg(
            Arg::with_name("name")
                .short('n')
                .long("name")
                .takes_value(true)
                .help("user name"),
        )
        .arg(
            Arg::with_name("port")
                .short('p')
                .long("port")
                .takes_value(true)
                .help("port number"),
        )
        .get_matches();

    if let Some(v) = matches.value_of(pattern) {
        Some(v.to_string())
    } else {
        None
    }
}

/// create a default user account for zero configuration Community Node startups
/// without providing a user name
pub fn create_default_named() -> String {
    let mut user_name: String;
    user_name = "Community Node ".to_string();
    user_name.push_str(
        libqaul::utilities::timestamp::Timestamp::get_timestamp()
            .to_string()
            .as_str(),
    );
    user_name
}

#[async_std::main]
async fn main() {
    // get current working directory
    let path = std::env::current_dir().unwrap();
    let storage_path = path.as_path().to_str().unwrap().to_string();

    // parse parameters and create default config
    let mut def_config: BTreeMap<String, String> = BTreeMap::new();
    let possible_arguments = vec!["name", "port"];
    for s in possible_arguments {
        if let Some(v) = get_argument(&s) {
            def_config.insert(s.to_string(), v.clone());
        }
    }

    // start libqaul in new thread and save configuration file to current working path
    libqaul::api::start_with_config(storage_path, Some(def_config.clone()));

    // wait until libqaul finished initializing
    while libqaul::api::initialization_finished() == false {
        // wait a little while
        std::thread::sleep(Duration::from_millis(10));
    }

    // if no account, creating new accounts
    if libqaul::node::user_accounts::UserAccounts::len() == 0 {
        let user_name: String;
        if let Some(usr_name) = get_argument("name") {
            user_name = usr_name.clone();
        } else {
            user_name = create_default_named();
        }
        libqaul::node::user_accounts::UserAccounts::create(user_name.clone());
    }

    // loop
    loop {
        thread::sleep(Duration::from_millis(10));
    }
}