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
use crate::Connection;
use anyhow::Result;
use libworterbuch::{
    codec::{
        encode_export_message, encode_get_message, encode_import_message, encode_pget_message,
        encode_psubscribe_message, encode_set_message, encode_subscribe_message,
        read_server_message, ClientMessage as CM, Export, Get, Import, PGet, PSubscribe,
        ServerMessage as SM, Set, Subscribe,
    },
    config::Config,
};
use tokio::{
    io::AsyncWriteExt,
    net::TcpStream,
    spawn,
    sync::{
        broadcast,
        mpsc::{self, UnboundedSender},
    },
};

#[derive(Clone)]
pub struct TcpConnection {
    cmd_tx: UnboundedSender<CM>,
    counter: u64,
    ack_tx: broadcast::Sender<u64>,
}

impl Connection for TcpConnection {
    fn set(&mut self, key: &str, value: &str) -> Result<u64> {
        let i = self.counter;
        self.counter += 1;
        self.cmd_tx.send(CM::Set(Set {
            transaction_id: i,
            key: key.to_owned(),
            value: value.to_owned(),
        }))?;
        Ok(i)
    }

    fn get(&mut self, key: &str) -> Result<u64> {
        let i = self.counter;
        self.counter += 1;
        self.cmd_tx.send(CM::Get(Get {
            transaction_id: i,
            key: key.to_owned(),
        }))?;
        Ok(i)
    }

    fn pget(&mut self, key: &str) -> Result<u64> {
        let i = self.counter;
        self.counter += 1;
        self.cmd_tx.send(CM::PGet(PGet {
            transaction_id: i,
            request_pattern: key.to_owned(),
        }))?;
        Ok(i)
    }

    fn subscribe(&mut self, key: &str) -> Result<u64> {
        let i = self.counter;
        self.counter += 1;
        self.cmd_tx.send(CM::Subscribe(Subscribe {
            transaction_id: i,
            key: key.to_owned(),
        }))?;
        Ok(i)
    }

    fn psubscribe(&mut self, request_pattern: &str) -> Result<u64> {
        let i = self.counter;
        self.counter += 1;
        self.cmd_tx.send(CM::PSubscribe(PSubscribe {
            transaction_id: i,
            request_pattern: request_pattern.to_owned(),
        }))?;
        Ok(i)
    }

    fn export(&mut self, path: &str) -> Result<u64> {
        let i = self.counter;
        self.counter += 1;
        self.cmd_tx.send(CM::Export(Export {
            transaction_id: i,
            path: path.to_owned(),
        }))?;
        Ok(i)
    }

    fn import(&mut self, path: &str) -> Result<u64> {
        let i = self.counter;
        self.counter += 1;
        self.cmd_tx.send(CM::Import(Import {
            transaction_id: i,
            path: path.to_owned(),
        }))?;
        Ok(i)
    }

    fn acks(&mut self) -> broadcast::Receiver<u64> {
        self.ack_tx.subscribe()
    }
}

pub async fn connect() -> Result<TcpConnection> {
    let config = Config::new()?;

    let host_addr = config.host_addr;
    let port = config.tcp_port;

    let server = TcpStream::connect(format!("{host_addr}:{port}")).await?;
    let (mut tcp_rx, mut tcp_tx) = server.into_split();

    let (cmd_tx, mut cmd_rx) = mpsc::unbounded_channel();
    let (ack_tx, ack_rx) = broadcast::channel(1_000);
    let ack_tx_rcv = ack_tx.clone();

    spawn(async move {
        while let Some(msg) = cmd_rx.recv().await {
            match encode_message(&msg) {
                Ok(data) => {
                    if let Err(e) = tcp_tx.write_all(&data).await {
                        eprintln!("failed to send tcp message: {e}");
                        break;
                    }
                }
                Err(e) => {
                    eprintln!("error encoding message: {e}");
                }
            }
        }
        // make sure initial rx is not dropped as long as stdin is read
        drop(ack_rx);
    });

    spawn(async move {
        loop {
            match read_server_message(&mut tcp_rx).await {
                Ok(Some(SM::PState(msg))) => {
                    for (key, value) in msg.key_value_pairs {
                        println!("{key} = {value}");
                    }
                    if let Err(e) = ack_tx_rcv.send(msg.transaction_id) {
                        eprintln!("Error forwarding ack: {e}");
                    }
                }
                Ok(Some(SM::State(msg))) => {
                    if let Some((key, value)) = msg.key_value {
                        println!("{} = {}", key, value);
                    } else {
                        println!("No result.");
                    }
                    if let Err(e) = ack_tx_rcv.send(msg.transaction_id) {
                        eprintln!("Error forwarding ack: {e}");
                    }
                }
                Ok(Some(SM::Ack(msg))) => {
                    if let Err(e) = ack_tx_rcv.send(msg.transaction_id) {
                        eprintln!("Error forwarding ack: {e}");
                    }
                }
                Ok(Some(SM::Err(msg))) => {
                    eprintln!("server error {}: {}", msg.error_code, msg.metadata);
                    if let Err(e) = ack_tx_rcv.send(msg.transaction_id) {
                        eprintln!("Error forwarding ack: {e}");
                    }
                }
                Ok(None) => {
                    eprintln!("Connection to server lost.");
                    break;
                }
                Err(e) => {
                    eprintln!("error decoding message: {e}");
                }
            }
        }
    });

    let con = TcpConnection {
        cmd_tx,
        counter: 1,
        ack_tx,
    };

    Ok(con)
}

fn encode_message(msg: &CM) -> Result<Vec<u8>> {
    match msg {
        CM::Get(msg) => Ok(encode_get_message(msg)?),
        CM::PGet(msg) => Ok(encode_pget_message(msg)?),
        CM::Set(msg) => Ok(encode_set_message(msg)?),
        CM::Subscribe(msg) => Ok(encode_subscribe_message(msg)?),
        CM::PSubscribe(msg) => Ok(encode_psubscribe_message(msg)?),
        CM::Export(msg) => Ok(encode_export_message(msg)?),
        CM::Import(msg) => Ok(encode_import_message(msg)?),
    }
}