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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use crate::{
    error, response::Event, Client, Error, Hooks, InnerClient, RpcRequest, RpcResponse, TaskHooks,
};
use futures::{
    future::BoxFuture,
    prelude::*,
    stream::{SplitSink, SplitStream},
    StreamExt, TryStreamExt,
};
use log::{debug, info};
use serde::{de::DeserializeOwned, Deserialize};
use serde_json::Value;
use snafu::prelude::*;
use std::{
    collections::{hash_map::Entry, HashMap},
    sync::{
        atomic::{AtomicU64, Ordering::SeqCst},
        Arc, Mutex, Weak,
    },
    time::Duration,
};
use tokio::{
    net::TcpStream,
    select, spawn,
    sync::{broadcast, mpsc, oneshot, Notify},
    time::sleep,
};
use tokio_tungstenite::{tungstenite::Message, MaybeTlsStream, WebSocketStream};

use crate::response::Notification;

macro_rules! try_continue {
    ($res:expr) => {
        match $res {
            Ok(v) => v,
            Err(err) => {
                info!("{}", err);
                continue;
            }
        }
    };
}

/// Print error if the result is an Err.
fn print_error<E>(res: Result<(), E>)
where
    E: std::error::Error,
{
    if let Err(err) = res {
        info!("{}", err);
    }
}

/// Checks all stopped tasks to see if some hooks need to be called.
async fn on_reconnect(inner_client: Weak<InnerClient>) -> Result<(), Error> {
    // Response from `custom_tell_stopped` call
    #[allow(non_snake_case)]
    #[derive(Debug, Clone, Deserialize)]
    struct TaskStatus {
        status: String,
        totalLength: String,
        completedLength: String,
        gid: String,
    }

    if let Some(client) = inner_client.upgrade() {
        let mut res: HashMap<String, TaskStatus> = HashMap::new();
        // Convert map to TaskStatus.
        for map in client
            .custom_tell_stopped(
                0,
                1000,
                Some(
                    ["status", "totalLength", "completedLength", "gid"]
                        .into_iter()
                        .map(|x| x.to_string())
                        .collect(),
                    // Convert to Vec<String>
                ),
            )
            .await?
        {
            let r: TaskStatus =
                serde_json::from_value(Value::Object(map)).context(error::JsonSnafu)?;

            res.insert(r.gid.clone(), r);
        }

        let mut lock = client.hooks.lock().unwrap();
        for (gid, hooks) in &mut lock.0 {
            if let Some(status) = res.get(gid) {
                // Check if the task is finished by checking the length.
                if status.totalLength == status.completedLength {
                    if let Some(h) = hooks.on_complete.take() {
                        spawn(h);
                    }
                } else if status.status == "error" {
                    if let Some(h) = hooks.on_error.take() {
                        spawn(h);
                    }
                }
            }
        }
    }
    Ok(())
}

fn take_hook(event: Event, hook: &mut TaskHooks) -> Option<BoxFuture<'static, ()>> {
    use Event::*;
    match event {
        // "aria2.onDownloadStart" => &mut hook.on_start,
        // "aria2.onDownloadPause" => &mut hook.on_pause,
        // "aria2.onDownloadStop" => &mut hook.on_stop,
        Complete => &mut hook.on_complete,
        Error => &mut hook.on_error,
        BtComplete => &mut hook.on_complete,
        _ => return None,
    }
    .take()
}

fn process_nofitications(notification: &Notification, hooks: &Hooks) -> Result<(), Error> {
    use Event::*;
    if !matches!(notification.event, Complete | Error | BtComplete) {
        return Ok(());
    }
    let mut lock = hooks.lock().unwrap();
    if let Some(hook) = lock.0.get_mut(&notification.gid) {
        if let Some(hook) = take_hook(notification.event, hook) {
            spawn(hook);
        }
    } else {
        match lock.1.entry(notification.gid.clone()) {
            Entry::Occupied(mut e) => {
                e.get_mut().insert(notification.event);
            }
            Entry::Vacant(e) => {
                e.insert([notification.event].into_iter().collect());
            }
        }
    }
    Ok(())
}

fn get_gid_from_notifictaion(req: &RpcRequest) -> Option<&str> {
    req.params.get(0)?.get("gid")?.as_str()
}

impl InnerClient {
    fn id(&self) -> u64 {
        self.id.fetch_add(1, SeqCst)
    }

    fn subscribe_id<T>(
        &self,
        id: u64,
        timeout: Option<Duration>,
    ) -> impl Future<Output = Result<T, Error>>
    where
        T: DeserializeOwned + Send,
    {
        let (tx, rx) = oneshot::channel();
        self.subscriptions.lock().unwrap().insert(id, tx);
        let timeout = timeout.unwrap_or(self.default_timeout);
        let subscriptions = self.subscriptions.clone();

        async move {
            let res = if timeout.is_zero() {
                rx.await.context(error::OneshotRecvSnafu)?
            } else {
                match tokio::time::timeout(timeout, rx).await {
                    Ok(r) => r.context(error::OneshotRecvSnafu)?,
                    Err(e) => {
                        subscriptions.lock().unwrap().remove(&id);
                        // Timed out. Clean up.
                        Err(Error::Timeout { source: e })?
                    }
                }
            };

            if let Some(err) = res.error {
                return Err(Error::Aria2 { source: err });
            }

            if let Some(v) = res.result {
                Ok(serde_json::from_value::<T>(v).context(error::JsonSnafu)?)
            } else {
                error::ResultNotFoundSnafu { response: res }.fail()
            }
        }
    }

    async fn call(&self, id: u64, method: &str, mut params: Vec<Value>) -> Result<(), Error> {
        if let Some(ref token) = self.token {
            params.insert(0, Value::String(token.clone()))
        }
        let req = RpcRequest {
            id: Some(id),
            jsonrpc: "2.0".to_string(),
            method: "aria2.".to_string() + method,
            params,
        };
        self.tx_write
            .send(Message::Text(
                serde_json::to_string(&req).context(error::JsonSnafu)?,
            ))
            .await
            .context(error::MpscSendMessageSnafu)?;
        Ok(())
    }

    pub async fn call_and_subscribe<T>(
        &self,
        method: &str,
        params: Vec<Value>,
        timeout: Option<Duration>,
    ) -> Result<T, Error>
    where
        T: DeserializeOwned + Send,
    {
        let id = self.id();
        let fut = self.subscribe_id::<T>(id, timeout);
        // subscribe before calling
        self.call(id, method, params).await?;
        fut.await
    }
}

async fn read_worker(
    mut read: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
    subscriptions: Arc<Mutex<HashMap<u64, oneshot::Sender<RpcResponse>>>>,
    hooks: Hooks,
    tx_not: broadcast::Sender<Notification>,
) -> Result<(), Error> {
    while let Some(Message::Text(s)) = read.try_next().await.context(error::WebsocketSnafu)? {
        print_error((|| -> Result<(), Error> {
            let v: Value = serde_json::from_str(&s).context(error::JsonSnafu)?;
            if let Value::Object(obj) = &v {
                if obj.contains_key("method") {
                    // The message is a notification.
                    // https://aria2.github.io/manual/en/html/aria2c.html#notifications
                    let errf = || error::UnexpectedMessageSnafu { message: s.clone() };
                    let req: RpcRequest = serde_json::from_value(v).context(error::JsonSnafu)?;
                    let gid = get_gid_from_notifictaion(&req).with_context(errf)?;
                    let not = Notification::new(gid.to_string(), &req.method).with_context(errf)?;

                    process_nofitications(&not, &hooks)?;

                    let _ = tx_not.send(not);
                    return Ok(());
                }
            }

            // The message can be a response.
            let res: RpcResponse = serde_json::from_value(v).context(error::JsonSnafu)?;
            if let Some(ref id) = res.id {
                let tx = subscriptions.lock().unwrap().remove(id);
                if let Some(tx) = tx {
                    let _ = tx.send(res);
                }
            }
            Ok(())
        })());
    }
    Ok(())
}

async fn write_worker(
    mut write: SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>,
    mut rx_write: mpsc::Receiver<Message>,
    exit: Arc<Notify>,
) -> mpsc::Receiver<Message> {
    loop {
        select! {
            msg = rx_write.recv() => {
                if let Some(msg) = msg {
                    try_continue!(write.send(msg).await);
                } else {
                    // The sender is closed, which means the client is dropped.
                    return rx_write;
                }
            },
            _ = exit.notified() => {
                return rx_write;
            }
        }
    }
}

impl Client {
    /// Create a new `Client` that connects to the given url.
    ///
    /// # Example
    ///
    /// ```
    /// use aria2_ws::Client;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let client = Client::connect("ws://127.0.0.1:6800/jsonrpc", None)
    ///         .await
    ///         .unwrap();
    ///     let gid = client
    ///         .add_uri(
    ///             vec!["https://go.dev/dl/go1.17.6.windows-amd64.msi".to_string()],
    ///             None,
    ///             None,
    ///             None,
    ///         )
    ///         .await
    ///         .unwrap();
    ///     client.force_remove(gid).await.unwrap();
    /// }
    /// ```
    pub async fn connect(url: &str, token: Option<&str>) -> Result<Self, Error> {
        let url = url.to_string();
        let (tx_write, mut rx_write) = mpsc::channel::<Message>(4);
        // channel for sending messages to the aria2 server.
        let subscriptions: Arc<Mutex<HashMap<u64, oneshot::Sender<RpcResponse>>>> =
            Arc::new(Mutex::new(HashMap::new()));
        // Used for storing subscriptions.
        // On receiving a message, subscription with the same id to the message id will be removed and processed.
        let hooks: Hooks = Arc::new(Mutex::new((HashMap::new(), HashMap::new())));
        // The first hashmap stores the hooks, and the second hashmap stores the penging events for extra hooks run check.
        let shutdown = Arc::new(Notify::new());
        // sync all spawned tasks to shutdown
        let (tx_not, _) = broadcast::channel(16);
        // Broadcast notifications to all subscribers.
        // The receiver is dropped cause no one subscribes for now.
        // The notifications can be received again by calling tx_not.subscribe().

        let inner = Arc::new(InnerClient {
            tx_write,
            id: AtomicU64::new(0),
            token: token.map(|t| "token:".to_string() + t),
            subscriptions: subscriptions.clone(),
            shutdown: shutdown.clone(),
            hooks: hooks.clone(),
            default_timeout: Duration::from_secs(10),
            extended_timeout: Duration::from_secs(120),
            tx_not: tx_not.clone(),
        });

        let inner_weak = Arc::downgrade(&inner);
        // The following spawned task following will only hold a weak reference to the inner client.
        spawn(async move {
            let mut reconnect = false;
            loop {
                let connected = select! {
                    r = tokio_tungstenite::connect_async(&url) => r,
                    _ = shutdown.notified() => return,
                };
                match connected {
                    Ok((ws, _)) => {
                        let (write, read) = ws.split();
                        let read_fut =
                            read_worker(read, subscriptions.clone(), hooks.clone(), tx_not.clone());
                        // `read_fut` will be dropped if current task is stopped.

                        let exit = Arc::new(Notify::new());
                        let write_fut = spawn(write_worker(write, rx_write, exit.clone()));

                        if reconnect {
                            spawn(on_reconnect(inner_weak.clone()));
                        } else {
                            reconnect = true;
                            // run `on_reconnect` task next time.
                        }

                        select! {
                            result = read_fut => {
                                debug!("aria2 disconnected: {:?}", result);
                                exit.notify_waiters();
                                // notify write_worker to exit.
                                rx_write = write_fut.await.unwrap();
                                // re-initialize rx_write for the next connection.
                                sleep(Duration::from_secs(1)).await;
                            },
                            _ = shutdown.notified() => {
                                debug!("aria2 client is exiting");
                                exit.notify_waiters();
                                return;
                            }
                        }
                    }
                    Err(err) => {
                        info!("aria2: connect failed: {}", err);
                        sleep(Duration::from_secs(3)).await;
                    }
                }
            }
        });

        Ok(Self(inner))
    }

    // Call aria2 without waiting for the response.
    pub async fn call(&self, id: u64, method: &str, params: Vec<Value>) -> Result<(), Error> {
        self.0.call(id, method, params).await
    }

    /// Call aria2 with the given method and params,
    /// and return the result in timeout.
    ///
    /// If the timeout is `None`, the default timeout will be used.
    ///
    /// If the timeout is zero, there will be no timeout.
    pub async fn call_and_subscribe<T: DeserializeOwned + Send>(
        &self,
        method: &str,
        params: Vec<Value>,
        timeout: Option<Duration>,
    ) -> Result<T, Error> {
        self.0.call_and_subscribe(method, params, timeout).await
    }

    /// Set hook for task with given gid.
    pub async fn set_hooks(&self, gid: &str, hooks: Option<TaskHooks>) {
        if let Some(mut hooks) = hooks {
            if !hooks.is_some() {
                return;
            }
            let mut lock = self.0.hooks.lock().unwrap();
            if let Some(set) = lock.1.remove(gid) {
                for event in set {
                    if let Some(h) = take_hook(event, &mut hooks) {
                        spawn(h);
                    }
                }
            }
            if hooks.is_some() {
                lock.0.insert(gid.to_string(), hooks);
            }
        }
    }

    pub fn subscribe_notifications(&self) -> broadcast::Receiver<Notification> {
        self.0.tx_not.subscribe()
    }
}