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
use crate::{
channel_api::Channel,
consumer::ConsumerSignal,
message::ReturnedMessage,
model::ChannelNumber,
processor::{self, ClientRequest, ClientRequestSink, Param, WaitFor},
};
use anyhow::{anyhow, Result};
use metalmq_codec::frame;
use std::collections::HashMap;
use tokio::sync::{mpsc, oneshot};
pub(crate) type ConsumerSink = mpsc::UnboundedSender<ConsumerSignal>;
pub(crate) type ConnectionSink = mpsc::UnboundedSender<EventSignal>;
#[derive(Debug)]
pub enum EventSignal {
BasicAck {
channel: ChannelNumber,
delivery_tag: u64,
multiple: bool,
},
BasicReturn {
channel: ChannelNumber,
message: ReturnedMessage,
},
ChannelClose,
ConnectionClose,
}
pub struct Client {
pub connection_id: String,
pub event_stream: mpsc::UnboundedReceiver<EventSignal>,
request_sink: mpsc::Sender<ClientRequest>,
sync_calls: HashMap<u16, oneshot::Sender<Result<()>>>,
channels: HashMap<u16, Channel>,
}
async fn create_connection(url: &str, conn_sink: ConnectionSink) -> Result<ClientRequestSink> {
use tokio::net::TcpStream;
match TcpStream::connect(url).await {
Ok(socket) => {
let (sender, receiver) = mpsc::channel(1);
tokio::spawn(async move {
if let Err(e) = processor::start_loop_and_output(socket, receiver, conn_sink).await {
eprintln!("error: {:?}", e);
}
});
Ok(sender)
}
Err(e) => Err(anyhow!("Connection error {:?}", e)),
}
}
impl Client {
pub async fn connect(url: &str, username: &str, password: &str) -> Result<Client> {
let (conn_evt_tx, conn_evt_rx) = mpsc::unbounded_channel();
let (connected_tx, connected_rx) = oneshot::channel();
let client_sink = create_connection(url, conn_evt_tx).await?;
client_sink
.send(ClientRequest {
param: Param::Connect {
username: username.to_owned(),
password: password.to_owned(),
virtual_host: "/".to_owned(),
connected: connected_tx,
},
response: WaitFor::Nothing,
})
.await?;
connected_rx.await?;
Ok(Client {
connection_id: "01234".to_owned(),
request_sink: client_sink,
event_stream: conn_evt_rx,
sync_calls: HashMap::new(),
channels: HashMap::new(),
})
}
pub async fn channel_open(&mut self, channel: ChannelNumber) -> Result<Channel> {
processor::call(&self.request_sink, frame::channel_open(channel)).await?;
Ok(Channel::new(channel, self.request_sink.clone()))
}
pub async fn close(&mut self) -> Result<()> {
let fr = frame::connection_close(200, "Normal close", 0);
processor::call(&self.request_sink, fr).await?;
Ok(())
}
pub async fn receive_event(&mut self, timeout: std::time::Duration) -> Option<EventSignal> {
let sleep = tokio::time::sleep(tokio::time::Duration::from(timeout));
tokio::pin!(sleep);
tokio::select! {
signal = self.event_stream.recv() => {
signal
}
_ = &mut sleep => {
return None;
}
}
}
}