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 parking_lot::RwLock;
use rerun::external::re_log;
use std::error::Error;
use std::result::Result;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf};
use tokio::net::{TcpListener, TcpSocket, TcpStream};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::sync::Mutex;

use super::protocol::Message;

type HandlerFn = Box<dyn Fn(&Message) + Send + Sync + 'static>;

pub struct ControlApp {
    listener: TcpListener,
    handlers: RwLock<Vec<HandlerFn>>,
    clients: Arc<Mutex<Vec<UnboundedSender<Message>>>>,
}

impl ControlApp {
    pub async fn bind(addr: &str) -> tokio::io::Result<ControlApp> {
        let socket = TcpSocket::new_v4()?;
        socket.set_reuseaddr(true)?;
        socket.bind(addr.parse().unwrap())?;

        let listener = socket.listen(1024)?;
        Ok(Self {
            listener,
            handlers: RwLock::new(Vec::new()),
            clients: Arc::new(Mutex::new(Vec::new())),
        })
    }

    pub fn add_handler(&self, handler: HandlerFn) -> Result<(), Box<dyn Error>> {
        let mut handlers = self.handlers.write();
        handlers.push(handler);
        Ok(())
    }

    pub async fn broadcast(&self, message: Message) -> tokio::io::Result<()> {
        let clients = self.clients.lock().await;
        clients.iter().for_each(|client| {
            let _ = client.send(message.clone());
        });

        Ok(())
    }

    pub fn run(self) -> ControlAppHandle {
        re_log::info!(
            "Server running on {:?}",
            self.listener.local_addr().unwrap()
        );

        let app = Arc::new(self);
        let handle = app.clone();

        tokio::spawn(async move {
            loop {
                re_log::info!("Waiting for connection...");
                let app = app.clone();
                match app.listener.accept().await {
                    Ok((socket, addr)) => {
                        re_log::info!("Accepted connection from {:?}", addr);

                        tokio::spawn(async move {
                            app.handle_connection(socket).await;
                        });
                    }
                    Err(err) => {
                        re_log::error!(
                            "Error accepting connection: {}",
                            re_error::format_ref(&err)
                        );
                    }
                }
            }
        });

        ControlAppHandle { app: handle }
    }

    async fn handle_connection(&self, socket: TcpStream) {
        let (read_half, write_half) = tokio::io::split(socket);
        let (tx, rx) = unbounded_channel();

        // Add the client to the list
        {
            self.clients.lock().await.push(tx.clone());
        }

        // Spawn reader and writer tasks
        let reader_task = self.handle_reader(read_half);
        let writer_task = self.handle_writer(write_half, rx);

        let _ = tokio::join!(reader_task, writer_task);

        // Remove the client when the connection ends
        {
            let mut clients = self.clients.lock().await;
            if let Some(pos) = clients.iter().position(|x| x.same_channel(&tx)) {
                clients.remove(pos);
            }
        }
    }

    async fn handle_reader(&self, mut read_half: ReadHalf<TcpStream>) {
        let mut buf = [0; 1024];
        loop {
            match read_half.read(&mut buf).await {
                Ok(0) => {
                    re_log::info!("Connection closed by client");
                    break;
                }
                Ok(n) => match Message::decode(&buf[..n]) {
                    Ok(message) => {
                        re_log::info!("Received message: {:?}", message);
                        let handlers = &self.handlers.read();
                        for handler in handlers.iter() {
                            handler(&message);
                        }
                    }
                    Err(err) => {
                        re_log::error!("Failed to decode message: {}", re_error::format_ref(&err));
                    }
                },
                Err(err) => {
                    re_log::error!(
                        "Error reading from socket: {:?}",
                        re_error::format_ref(&err),
                    );
                    break;
                }
            }
        }
    }

    async fn handle_writer(
        &self,
        mut write_half: WriteHalf<TcpStream>,
        mut rx: UnboundedReceiver<Message>,
    ) {
        while let Some(message) = rx.recv().await {
            if matches!(message, Message::Disconnect) {
                re_log::info!("Received disconnect message, closing connection");
                break;
            }

            // Encode and send response
            if let Ok(data) = message.encode() {
                if write_half.write_all(&data).await.is_err() {
                    re_log::info!("Failed to send response to client");
                    break;
                }
            }
        }
    }
}

#[derive(Clone)]
pub struct ControlAppHandle {
    app: Arc<ControlApp>,
}

impl ControlAppHandle {
    pub fn add_handler<H>(
        &mut self,
        handler: H,
    ) -> std::result::Result<(), Box<dyn std::error::Error>>
    where
        H: Fn(&Message) + Send + Sync + 'static,
    {
        self.app.add_handler(Box::new(handler))
    }

    pub async fn broadcast(&self, message: Message) {
        let clients = self.app.clients.lock().await;

        clients.iter().for_each(|client| {
            let _ = client.send(message.clone());
        });
    }
}