#![deny(rust_2018_idioms)]
#![allow(clippy::type_complexity)]
#[macro_use]
extern crate derive_more;
#[macro_use]
extern crate log;
use ntex::{time::Seconds, util::ByteString};
use ntex_amqp_codec::protocol::{Handle, Milliseconds, Open, OpenInner};
use uuid::Uuid;
mod cell;
pub mod client;
mod connection;
mod control;
mod default;
mod dispatcher;
pub mod error;
pub mod error_code;
mod rcvlink;
mod router;
pub mod server;
mod session;
mod sndlink;
mod state;
pub mod types;
pub use self::connection::Connection;
pub use self::control::{ControlFrame, ControlFrameKind};
pub use self::rcvlink::{ReceiverLink, ReceiverLinkBuilder};
pub use self::session::Session;
pub use self::sndlink::{SenderLink, SenderLinkBuilder};
pub use self::state::State;
pub mod codec {
pub use ntex_amqp_codec::*;
}
#[derive(Debug, Clone)]
pub struct Configuration {
pub max_frame_size: u32,
pub channel_max: u16,
pub idle_time_out: 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: 120_000,
hostname: None,
}
}
pub fn channel_max(&mut self, num: u16) -> &mut Self {
self.channel_max = num;
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) -> u32 {
self.max_frame_size
}
pub fn idle_timeout(&mut self, timeout: u16) -> &mut Self {
self.idle_time_out = (timeout * 1000) 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(Box::new(OpenInner {
container_id: ByteString::from(Uuid::new_v4().simple().to_string()),
hostname: self.hostname.clone(),
max_frame_size: self.max_frame_size,
channel_max: self.channel_max,
idle_time_out: if self.idle_time_out > 0 {
Some(self.idle_time_out)
} else {
None
},
outgoing_locales: None,
incoming_locales: None,
offered_capabilities: None,
desired_capabilities: None,
properties: None,
}))
}
pub(crate) fn timeout_secs(&self) -> Seconds {
if self.idle_time_out > 0 {
Seconds::checked_new((self.idle_time_out / 1000) as usize)
} else {
Seconds::ZERO
}
}
pub(crate) fn timeout_remote_secs(&self) -> Seconds {
if self.idle_time_out > 0 {
Seconds::checked_new(((self.idle_time_out as f32) * 0.75 / 1000.0) as usize)
} else {
Seconds::ZERO
}
}
}
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(),
idle_time_out: open.idle_time_out().unwrap_or(0),
hostname: open.hostname().cloned(),
}
}
}