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
#![warn(rust_2018_idioms, unreachable_pub)]
#![allow(clippy::type_complexity)]
#[macro_use]
extern crate derive_more;
#[macro_use]
extern crate log;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use bytestring::ByteString;
use ntex::channel::oneshot;
use ntex_amqp_codec::protocol::{Disposition, Handle, Milliseconds, Open};
use uuid::Uuid;
mod cell;
pub mod client;
mod connection;
mod errors;
mod hb;
mod rcvlink;
pub mod sasl;
pub mod server;
mod service;
mod session;
mod sndlink;
pub use self::connection::{Connection, ConnectionController};
pub use self::errors::{AmqpTransportError, LinkError};
pub use self::rcvlink::{ReceiverLink, ReceiverLinkBuilder};
pub use self::session::Session;
pub use self::sndlink::{SenderLink, SenderLinkBuilder};
pub mod codec {
pub use ntex_amqp_codec::*;
}
pub enum Delivery {
Resolved(Result<Disposition, AmqpTransportError>),
Pending(oneshot::Receiver<Result<Disposition, AmqpTransportError>>),
Gone,
}
type DeliveryPromise = oneshot::Sender<Result<Disposition, AmqpTransportError>>;
impl Future for Delivery {
type Output = Result<Disposition, AmqpTransportError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Delivery::Pending(ref mut receiver) = *self {
return match Pin::new(receiver).poll(cx) {
Poll::Ready(Ok(r)) => Poll::Ready(r.map(|state| state)),
Poll::Pending => Poll::Pending,
Poll::Ready(Err(e)) => {
trace!("delivery oneshot is gone: {:?}", e);
Poll::Ready(Err(AmqpTransportError::Disconnected))
}
};
}
let old_v = ::std::mem::replace(&mut *self, Delivery::Gone);
if let Delivery::Resolved(r) = old_v {
return match r {
Ok(state) => Poll::Ready(Ok(state)),
Err(e) => Poll::Ready(Err(e)),
};
}
panic!("Polling Delivery after it was polled as ready is an error.");
}
}
#[derive(Debug, Clone)]
pub struct Configuration {
pub max_frame_size: u32,
pub channel_max: usize,
pub idle_time_out: Option<Milliseconds>,
pub hostname: Option<ByteString>,
}
impl Default for Configuration {
fn default() -> Self {
Self::new()
}
}
impl Configuration {
pub fn new() -> Self {
Configuration {
max_frame_size: std::u16::MAX as u32,
channel_max: 1024,
idle_time_out: Some(120_000),
hostname: None,
}
}
pub fn channel_max(&mut self, num: u16) -> &mut Self {
self.channel_max = num as usize;
self
}
pub fn max_frame_size(&mut self, size: u32) -> &mut Self {
self.max_frame_size = size;
self
}
pub fn get_max_frame_size(&self) -> usize {
self.max_frame_size as usize
}
pub fn idle_timeout(&mut self, timeout: u32) -> &mut Self {
self.idle_time_out = Some(timeout as Milliseconds);
self
}
pub fn hostname(&mut self, hostname: &str) -> &mut Self {
self.hostname = Some(ByteString::from(hostname));
self
}
pub fn to_open(&self) -> Open {
Open {
container_id: ByteString::from(Uuid::new_v4().to_simple().to_string()),
hostname: self.hostname.clone(),
max_frame_size: self.max_frame_size,
channel_max: self.channel_max as u16,
idle_time_out: self.idle_time_out,
outgoing_locales: None,
incoming_locales: None,
offered_capabilities: None,
desired_capabilities: None,
properties: None,
}
}
pub(crate) fn timeout(&self) -> Option<Duration> {
self.idle_time_out
.map(|v| Duration::from_millis(((v as f32) * 0.8) as u64))
}
}
impl<'a> From<&'a Open> for Configuration {
fn from(open: &'a Open) -> Self {
Configuration {
max_frame_size: open.max_frame_size,
channel_max: open.channel_max as usize,
idle_time_out: open.idle_time_out,
hostname: open.hostname.clone(),
}
}
}