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
#![feature(const_btree_new)]
use async_std::sync::Mutex;
use async_std::task;
use futures_util::stream::StreamExt;
use log::*;
use redis::{AsyncCommands, IntoConnectionInfo};
use std::collections::BTreeMap;
use std::sync::Arc;
use unmp::net;
use unmp_link::{Driver, ErrorKind, Link, ResultFuture};
static LINKS: Mutex<BTreeMap<String, Link>> = Mutex::new(BTreeMap::new());
pub struct ClientDriver {
identifier: String,
client: redis::Client,
topic: String,
}
impl Driver for ClientDriver {
fn name(&self) -> &str {
&self.identifier
}
fn send(self: Arc<Self>, buf: &[u8]) -> ResultFuture {
trace!("redis send: {:02X?}.", buf);
let (result_future, sender) = ResultFuture::new();
let buf = Vec::from(buf);
task::spawn(async move {
let mut tx = self.client.get_async_connection().await.unwrap();
match tx.publish::<_, _, u8>(&self.topic, buf.as_slice()).await {
Ok(_) => {
let _err = sender.send(Ok(()));
}
Err(_) => {
warn!("redis send error.");
let _err = sender.send(Err(ErrorKind::TimedOut));
}
};
});
return result_future;
}
}
pub async fn start(url: &str, topic: &str) -> Result<Link, ()> {
let url = {
if let Ok(url) = url.into_connection_info() {
url
} else {
return Err(());
}
};
let identifier = format!("{}/{}", url.addr, topic);
if let Some(_) = LINKS.lock().await.get(&identifier) {
return Err(());
}
let client = redis::Client::open(url).unwrap();
let driver = Arc::new(ClientDriver {
identifier: identifier.clone(),
client: client,
topic: String::from(topic),
});
let link_driver = Arc::downgrade(&driver);
let link = Link::new(link_driver);
info!("redis new {:?}.", link);
LINKS.lock().await.insert(identifier.clone(), link.clone());
let link_tmp = link.clone();
task::spawn(async move {
let link = link_tmp;
let rx = driver.client.get_async_connection().await.unwrap();
let mut pubsub = rx.into_pubsub();
pubsub.subscribe(&driver.topic).await.unwrap();
while let Some(msg) = pubsub.on_message().next().await {
let buf = msg.get_payload_bytes();
trace!("redis recv: {:02X?}.", buf);
net::when_recv(&link, &buf);
}
warn!("redis {:?} recv error.", link);
LINKS.lock().await.remove(&identifier);
link.destroy();
});
return Ok(link);
}