Skip to main content

remote_shell/slave/
slave_entrance.rs

1use crate::common::order::*;
2use crate::common::utils::*;
3use std::io::Write;
4use std::io::stdout;
5use crate::common::utils::*;
6use crate::common::cmd::Cmd;
7use super::rec::Rec;
8use std::sync::mpsc::{Sender, Receiver};
9use std::sync::mpsc;
10use std::time::Duration;
11use udp_hole_punching as hole;
12
13
14pub fn dispatch(id: &str, swap_server: &str, db_path: &str) {
15    let mut conf = hole::Conf::default();
16    conf.swap_server = swap_server.to_string();
17    conf.db_path = db_path.to_string();
18    conf.id = id.to_string();
19    conf.set();
20
21    hole::init_udp();
22    std::thread::spawn(|| {
23        hole::listen();
24    });
25
26
27    print!("******************slave begin to work\n");
28    loop {
29        match _dispatch() {
30            Ok(_) => {}
31            Err(e) => {}
32        }
33    }
34}
35
36pub fn _dispatch() -> anyhow::Result<()> {
37    let (peer, mut ord) = Order::read_order_from_cache();
38
39    if ord.cmd == "".to_string() {
40        return Ok(());
41    }
42    dbg!("receive ord");
43    dbg!(&peer);
44    let rec = Rec::new(peer, &ord);
45
46    let cmd = Cmd::from_str(&ord.cmd);
47    // 检查ord时候可以parse
48    dbg!(&cmd);
49
50    let start = Order::start();
51    start.send(peer);
52
53    let res = {
54        match cmd {
55            Cmd::Restart => rec.restart(),
56            Cmd::Cd => rec.cd(),
57            Cmd::Send => rec.send(),
58            Cmd::Rec => rec.rec(),
59            _ => rec.others(),
60        }
61    };
62    if let Err(e) = res {
63        let res = format!("错误:{}", e);
64        ord.data = res.as_bytes().to_vec();
65        ord.send(peer)?;
66    }
67
68    let finish = Order::finish();
69    finish.send(peer);
70
71    Ok(())
72}
73