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
use std::{
    collections::HashMap,
    fmt::Debug,
    net::{SocketAddr, TcpStream},
};

use std::io;
use std::sync::{Arc, Mutex};

use io::{BufReader, Write};
use protocol::RequestHeader;
use serde::de::DeserializeOwned;

const MAX_IPC_VERSION: u32 = 1;

mod coordinates;
mod members;
mod request;
mod stream;

pub mod protocol;

pub use request::RPCRequest;
pub use stream::RPCStream;

/// A wrapper allowing reading a Seq response.
///
/// This is an internal implementation detail, but public because it is exposed in traits.
#[doc(hidden)]
pub struct SeqRead<'a>(&'a mut BufReader<TcpStream>);
impl<'a> SeqRead<'a> {
    fn read_msg<T: DeserializeOwned + Debug>(mut self) -> T {
        // annoyingly, we pretty much have to panic, because otherwise the reader is left in an invalid state
        rmp_serde::from_read(&mut self.0).unwrap()
    }
}

trait SeqHandler: 'static + Send + Sync {
    fn handle(&self, res: RPCResult<SeqRead>);
    /// are we expecting more than one response?
    fn streaming(&self) -> bool {
        false
    }
}

type RPCResult<T = ()> = Result<T, String>;

#[derive(Clone)]
pub struct Client {
    dispatch: Arc<Mutex<DispatchMap>>,
    tx: std::sync::mpsc::Sender<Vec<u8>>,
}

struct DispatchMap {
    map: HashMap<u64, Arc<dyn SeqHandler>>,
    next_seq: u64,
}

impl Client {
    /// Connect to hub.
    ///
    /// Waits for handshake, and optionally for authentication if an auth key is provided.
    pub async fn connect(rpc_addr: SocketAddr, auth_key: Option<&str>) -> RPCResult<Self> {
        let (tx, rx) = std::sync::mpsc::channel();

        let dispatch = Arc::new(Mutex::new(DispatchMap {
            map: HashMap::new(),
            next_seq: 0,
        }));

        let client = Client { dispatch, tx };

        let dispatch = Arc::downgrade(&client.dispatch);

        std::thread::spawn(move || {
            let mut stream = TcpStream::connect(rpc_addr).unwrap();

            // clone the stream to create a reader
            let mut reader = BufReader::new(stream.try_clone().unwrap());

            // write loop
            std::thread::spawn(move || {
                while let Ok(buf) = rx.recv() {
                    stream.write_all(&buf).unwrap();
                }
            });

            // read loop
            while let Some(dispatch) = dispatch.upgrade() {
                let protocol::ResponseHeader { seq, error } =
                    rmp_serde::from_read(&mut reader).unwrap();

                let seq_handler = {
                    let mut dispatch = dispatch.lock().unwrap();
                    match dispatch.map.get(&seq) {
                        Some(v) => {
                            if v.streaming() {
                                v.clone()
                            } else {
                                dispatch.map.remove(&seq).unwrap()
                            }
                        }
                        None => {
                            // response with no handler, ignore
                            continue;
                        }
                    }
                };

                let res = if error.is_empty() {
                    Ok(SeqRead(&mut reader))
                } else {
                    Err(error)
                };

                seq_handler.handle(res);
            }
        });

        client.handshake(MAX_IPC_VERSION).await?;

        if let Some(auth_key) = auth_key {
            client.auth(auth_key).await?;
        }

        return Ok(client);
    }

    fn deregister_seq_handler(&self, seq: u64) -> Option<Arc<dyn SeqHandler>> {
        self.dispatch.lock().unwrap().map.remove(&seq)
    }

    /// Send a command, optionally registering a handler for responses.
    ///
    /// Returns the sequence number.
    fn send_command(&self, cmd: SerializedCommand, handler: Option<Arc<dyn SeqHandler>>) -> u64 {
        let seq = {
            let mut dispatch = self.dispatch.lock().unwrap();

            let seq = dispatch.next_seq;
            dispatch.next_seq += 1;

            if let Some(handler) = handler {
                dispatch.map.insert(seq, handler);
            }

            seq
        };

        let mut buf = rmp_serde::encode::to_vec_named(&RequestHeader {
            command: cmd.name,
            seq,
        })
        .unwrap();
        buf.extend_from_slice(&cmd.body);

        self.tx.send(buf).unwrap();

        seq
    }

    pub async fn current_node_name(&self) -> RPCResult<String> {
        Ok(self.stats().await?.agent.name)
    }
}

struct SerializedCommand {
    name: &'static str,
    body: Vec<u8>,
}

/// A trait for types that can be deserialized as the response to a command
///
/// This is an internal implementation detail, but public because it is exposed in traits.
#[doc(hidden)]
pub trait RPCResponse: Sized + Send + 'static {
    fn read_from(read: SeqRead<'_>) -> RPCResult<Self>;
}

impl RPCResponse for () {
    fn read_from(_: SeqRead<'_>) -> RPCResult<Self> {
        Ok(())
    }
}