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
use std::{
    str::FromStr, 
    path::{ Path, PathBuf },
    time::Duration,
};
use clap::{App, SubCommand, Arg};
use cyfs_base::*;
use crate::stack::Stack;

pub fn debug_command_line() -> clap::App<'static, 'static> {
    App::new("bdt-debug")
        .about("bdt stack debug")
        .arg(Arg::with_name("host")
        .short("h")
        .long("host")
        .value_name("host")
        .help("connect remote host")
        .default_value("127.0.0.1"))
        .arg(Arg::with_name("port")
        .short("p")
        .long("port")
        .value_name("port")
        .help("local server port")
        .default_value("12345"))
        .subcommand(SubCommand::with_name("test"))
        .subcommand(SubCommand::with_name("ping")
            .arg(Arg::with_name("remote").required(true))
            .arg(Arg::with_name("count").required(true))
            .arg(Arg::with_name("timeout").required(true))
        )
        .subcommand(SubCommand::with_name("nc")
            .arg(Arg::with_name("remote").required(true))
            .arg(Arg::with_name("port").required(true))
        )
        .subcommand(SubCommand::with_name("get_chunk")
            .arg(Arg::with_name("remotes").required(true))
            .arg(Arg::with_name("timeout").required(true))
            .arg(Arg::with_name("chunk_id").required(true))
            .arg(Arg::with_name("local_path").required(true))
        )
        .subcommand(SubCommand::with_name("get_file")
            .arg(Arg::with_name("remotes").required(true))
            .arg(Arg::with_name("timeout").required(true))
            .arg(Arg::with_name("file_id").required(true))
            .arg(Arg::with_name("local_path").required(true))
        )
        .subcommand(SubCommand::with_name("put_chunk")
            .arg(Arg::with_name("local_path").required(true))
        )
        .subcommand(SubCommand::with_name("put_file")
            .arg(Arg::with_name("local_path").required(true))
        )
        .subcommand(SubCommand::with_name("sn_conn_status")
            .arg(Arg::with_name("timeout").required(true))
        )
        .subcommand(SubCommand::with_name("bench_datagram")
            .arg(Arg::with_name("remote").required(true))
            .arg(Arg::with_name("plaintext").required(true))
            .arg(Arg::with_name("timeout").required(true))
        )
}

pub enum DebugCommand {
    Test(DebugCommandTest),
    Ping(DebugCommandPing), 
    Nc(DebugCommandNc), 
    GetChunk(DebugCommandGetChunk),
    GetFile(DebugCommandGetFile),
    PutChunk(DebugCommandPutChunk),
    PutFile(DebugCommandPutFile),
    SnConnStatus(DebugCommandSnConnStatus),
    BenchDatagram(DebugCommandBenchDatagram),
}

impl DebugCommand {
    pub async fn from_str(stack: &Stack, command: &str) -> Result<Self, String> {
        let params = debug_command_line().get_matches_from_safe(command.split(" "))
            .map_err(|err| err.message)?;
        let subcommand = params.subcommand_name().ok_or_else(|| "no subcommand\r\n".to_string())?;
        match subcommand {
            "test" => {
                let _ = params.subcommand_matches("test").unwrap();
                Ok(Self::Test(DebugCommandTest{}))
            },
            "ping" => {
                let subcommand = params.subcommand_matches("ping").unwrap();
                let remote = remote_device(stack, subcommand.value_of("remote").unwrap()).await
                    .map_err(|err| format!("load remote desc {} failed for {}\r\n", subcommand.value_of("remote").unwrap(), err))?;
                let count = u32::from_str(subcommand.value_of("count").unwrap()).unwrap();
                let timeout = u64::from_str(subcommand.value_of("timeout").unwrap()).unwrap();

                Ok(Self::Ping(DebugCommandPing {
                    remote, 
                    count, 
                    timeout: Duration::from_secs(timeout)
                }))
            },
            "nc" => {
                let subcommand = params.subcommand_matches("nc").unwrap();
                let remote = remote_device(stack, subcommand.value_of("remote").unwrap()).await
                    .map_err(|err| format!("load remote desc {} failed for {}\r\n", subcommand.value_of("remote").unwrap(), err))?;
                let port = u16::from_str(subcommand.value_of("port").unwrap()).unwrap();
                Ok(Self::Nc(DebugCommandNc {
                    remote, 
                    port, 
                    timeout: Duration::from_secs(8)
                }))
            }, 
            "get_chunk" => {
                let subcommand = params.subcommand_matches("get_chunk").unwrap();
                let reomotes_str = String::from_str(subcommand.value_of("remotes").unwrap()).unwrap();
                let remotes_split = reomotes_str.split(",");
                let mut remotes = Vec::new();
                for remote_file in remotes_split {
                    let remote = remote_device(stack, remote_file).await
                    .map_err(|err| format!("load remote desc {} failed for {}\r\n", subcommand.value_of("remote").unwrap(), err))?;
                    remotes.push(remote.desc().device_id());
                }
                let chunk_id_str = String::from_str(subcommand.value_of("chunk_id").unwrap()).unwrap();
                let chunk_id = ChunkId::from_str(&chunk_id_str.as_str()).map_err(|err| format!("load chunk_id {} fail: {}\r\n",
                    chunk_id_str, err))?;
                let local_path_str = String::from_str(subcommand.value_of("local_path").unwrap()).unwrap();
                let local_path = PathBuf::from_str(&local_path_str.as_str()).unwrap();
                let timeout = u32::from_str(subcommand.value_of("timeout").unwrap()).unwrap();

                Ok(Self::GetChunk(DebugCommandGetChunk {
                    remotes, 
                    timeout, 
                    chunk_id, 
                    local_path
                }))
            },
            "get_file" => {
                let subcommand = params.subcommand_matches("get_file").unwrap();
                let reomotes_str = String::from_str(subcommand.value_of("remotes").unwrap()).unwrap();
                let remotes_split = reomotes_str.split(",");
                let mut remotes = Vec::new();
                for remote_file in remotes_split {
                    let remote = remote_device(stack, remote_file).await
                    .map_err(|err| format!("load remote desc {} failed for {}\r\n", subcommand.value_of("remote").unwrap(), err))?;
                    remotes.push(remote.desc().device_id());
                }
                let file_id_str = String::from_str(subcommand.value_of("file_id").unwrap()).unwrap();
                let hex_data = hex::decode(file_id_str.as_bytes()).unwrap();
                let (file_id, _) = File::raw_decode(hex_data.as_slice()).map_err(|err| format!("load file_id {} fail: {}",
                    file_id_str, err))?;
                let local_path_str = String::from_str(subcommand.value_of("local_path").unwrap()).unwrap();
                let local_path = PathBuf::from_str(&local_path_str.as_str()).unwrap();
                let timeout = u32::from_str(subcommand.value_of("timeout").unwrap()).unwrap();

                Ok(Self::GetFile(DebugCommandGetFile {
                    remotes, 
                    timeout, 
                    file_id, 
                    local_path
                }))
            },
            "put_chunk" => {
                let subcommand = params.subcommand_matches("put_chunk").unwrap();
                let local_path_str = String::from_str(subcommand.value_of("local_path").unwrap()).unwrap();
                let local_path = PathBuf::from_str(&local_path_str.as_str()).unwrap();

                Ok(Self::PutChunk(DebugCommandPutChunk {
                    local_path
                }))
            },
            "put_file" => {
                let subcommand = params.subcommand_matches("put_file").unwrap();
                let local_path_str = String::from_str(subcommand.value_of("local_path").unwrap()).unwrap();
                let local_path = PathBuf::from_str(&local_path_str.as_str()).unwrap();

                Ok(Self::PutFile(DebugCommandPutFile {
                    local_path,
                }))
            },
            "sn_conn_status" => {
                let subcommand = params.subcommand_matches("sn_conn_status").unwrap();
                let timeout = u64::from_str(subcommand.value_of("timeout").unwrap()).unwrap();
                Ok(Self::SnConnStatus(DebugCommandSnConnStatus {
                    timeout_sec: timeout,
                }))
            },
            "bench_datagram" => {
                let subcommand = params.subcommand_matches("bench_datagram").unwrap();
                let remote = remote_device(stack, subcommand.value_of("remote").unwrap()).await
                    .map_err(|err| format!("load remote desc {} failed for {}\r\n", subcommand.value_of("remote").unwrap(), err))?;
                let plaintext = u32::from_str(subcommand.value_of("plaintext").unwrap()).unwrap();
                let timeout = u64::from_str(subcommand.value_of("timeout").unwrap()).unwrap();

                Ok(Self::BenchDatagram(DebugCommandBenchDatagram {
                    remote,
                    timeout: Duration::from_secs(timeout),
                    plaintext: plaintext != 0
                }))
            }
            _ => {
                Err(format!("invalid subcommand {}\r\n", subcommand))
            }
        }
    } 
}

pub struct DebugCommandTest {}

pub struct DebugCommandBenchDatagram {
    pub remote: Device, 
    pub timeout: Duration,
    pub plaintext: bool,
}

pub struct DebugCommandPing {
    pub remote: Device, 
    pub count: u32, 
    pub timeout: Duration
}

pub struct DebugCommandNc {
    pub remote: Device, 
    pub port: u16, 
    pub timeout: Duration
}

pub struct DebugCommandGetChunk {
    pub remotes: Vec<DeviceId>, 
    pub timeout: u32,
    pub chunk_id: ChunkId,
    pub local_path: PathBuf,
}

pub struct DebugCommandGetFile {
    pub remotes: Vec<DeviceId>, 
    pub timeout: u32,
    pub file_id: File,
    pub local_path: PathBuf,
}

pub struct DebugCommandPutChunk {
    pub local_path: PathBuf,
}

pub struct DebugCommandPutFile {
    pub local_path: PathBuf,
}

pub struct DebugCommandSnConnStatus {
    pub timeout_sec: u64,
}

async fn remote_device(
    stack: &Stack, 
    str: &str) -> BuckyResult<Device> {
    if let Ok(device_id) = DeviceId::from_str(str) {
        if let Some(device) = stack.device_cache().get(&device_id).await {
            Ok(device)
        } else {
            Err(BuckyError::new(BuckyErrorCode::NotFound, "not found"))
        }
    } else {
        let path = Path::new(str);
        if !path.exists() {
            Err(BuckyError::new(BuckyErrorCode::NotFound, "not found"))
        } else {
            let mut buf = vec![];
            let (device, _) = Device::decode_from_file(&path, &mut buf)?;
            let device_id = device.desc().device_id();
            if stack.device_cache().get(&device_id).await.is_none() {
                stack.device_cache().add(&device_id, &device);
            }
            Ok(device)
        }
    }
}