2021-12-11 22:43:41 -05:00
|
|
|
//! Client connection state
|
2021-12-05 19:14:14 -05:00
|
|
|
use crate::close::Close;
|
2021-12-12 11:50:16 -05:00
|
|
|
use crate::error::Error;
|
2021-12-05 19:14:14 -05:00
|
|
|
use crate::error::Result;
|
2021-12-05 21:28:02 -05:00
|
|
|
use crate::event::Event;
|
2021-12-12 11:50:16 -05:00
|
|
|
|
2021-12-05 19:14:14 -05:00
|
|
|
use crate::subscription::Subscription;
|
|
|
|
use log::*;
|
|
|
|
use std::collections::HashMap;
|
2021-12-05 17:53:26 -05:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-12-11 22:43:41 -05:00
|
|
|
/// A subscription identifier has a maximum length
|
2021-12-05 19:14:14 -05:00
|
|
|
const MAX_SUBSCRIPTION_ID_LEN: usize = 256;
|
|
|
|
|
2021-12-11 22:43:41 -05:00
|
|
|
/// State for a client connection
|
2021-12-05 17:53:26 -05:00
|
|
|
pub struct ClientConn {
|
2021-12-11 22:43:41 -05:00
|
|
|
/// Unique client identifier generated at connection time
|
2021-12-05 21:28:02 -05:00
|
|
|
client_id: Uuid,
|
2021-12-11 22:43:41 -05:00
|
|
|
/// The current set of active client subscriptions
|
2021-12-05 19:14:14 -05:00
|
|
|
subscriptions: HashMap<String, Subscription>,
|
2021-12-11 22:43:41 -05:00
|
|
|
/// Per-connection maximum concurrent subscriptions
|
2021-12-05 19:14:14 -05:00
|
|
|
max_subs: usize,
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
|
|
|
|
2021-12-11 22:56:52 -05:00
|
|
|
impl Default for ClientConn {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:53:26 -05:00
|
|
|
impl ClientConn {
|
2021-12-11 22:43:41 -05:00
|
|
|
/// Create a new, empty connection state.
|
2021-12-05 17:53:26 -05:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let client_id = Uuid::new_v4();
|
|
|
|
ClientConn {
|
2021-12-11 22:43:41 -05:00
|
|
|
client_id,
|
2021-12-05 19:14:14 -05:00
|
|
|
subscriptions: HashMap::new(),
|
2021-12-12 11:50:16 -05:00
|
|
|
max_subs: 32,
|
2021-12-05 19:14:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 22:43:41 -05:00
|
|
|
/// Get a short prefix of the client's unique identifier, suitable
|
|
|
|
/// for logging.
|
2021-12-05 21:28:02 -05:00
|
|
|
pub fn get_client_prefix(&self) -> String {
|
|
|
|
self.client_id.to_string().chars().take(8).collect()
|
|
|
|
}
|
|
|
|
|
2021-12-14 22:38:26 -05:00
|
|
|
/// Find all matching subscriptions.
|
|
|
|
pub fn get_matching_subscriptions(&self, e: &Event) -> Vec<&str> {
|
|
|
|
let mut v: Vec<&str> = vec![];
|
2021-12-05 21:28:02 -05:00
|
|
|
for (id, sub) in self.subscriptions.iter() {
|
|
|
|
if sub.interested_in_event(e) {
|
2021-12-14 22:38:26 -05:00
|
|
|
v.push(id);
|
2021-12-05 21:28:02 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-19 17:26:32 -05:00
|
|
|
v
|
2021-12-05 21:28:02 -05:00
|
|
|
}
|
|
|
|
|
2021-12-11 22:43:41 -05:00
|
|
|
/// Add a new subscription for this connection.
|
2021-12-05 19:14:14 -05:00
|
|
|
pub fn subscribe(&mut self, s: Subscription) -> Result<()> {
|
|
|
|
let k = s.get_id();
|
|
|
|
let sub_id_len = k.len();
|
2021-12-11 22:43:41 -05:00
|
|
|
// prevent arbitrarily long subscription identifiers from
|
|
|
|
// being used.
|
2021-12-05 19:14:14 -05:00
|
|
|
if sub_id_len > MAX_SUBSCRIPTION_ID_LEN {
|
2021-12-12 11:03:28 -05:00
|
|
|
info!(
|
|
|
|
"ignoring sub request with excessive length: ({})",
|
|
|
|
sub_id_len
|
|
|
|
);
|
2021-12-12 11:50:16 -05:00
|
|
|
return Err(Error::SubIdMaxLengthError);
|
2021-12-05 19:14:14 -05:00
|
|
|
}
|
|
|
|
// check if an existing subscription exists, and replace if so
|
|
|
|
if self.subscriptions.contains_key(&k) {
|
|
|
|
self.subscriptions.remove(&k);
|
|
|
|
self.subscriptions.insert(k, s);
|
2021-12-12 11:03:28 -05:00
|
|
|
debug!("replaced existing subscription");
|
2021-12-05 19:14:14 -05:00
|
|
|
return Ok(());
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
2021-12-05 19:14:14 -05:00
|
|
|
|
|
|
|
// check if there is room for another subscription.
|
|
|
|
if self.subscriptions.len() >= self.max_subs {
|
2021-12-12 11:50:16 -05:00
|
|
|
return Err(Error::SubMaxExceededError);
|
2021-12-05 19:14:14 -05:00
|
|
|
}
|
|
|
|
// add subscription
|
|
|
|
self.subscriptions.insert(k, s);
|
2021-12-12 11:03:28 -05:00
|
|
|
debug!(
|
|
|
|
"registered new subscription, currently have {} active subs",
|
2021-12-05 19:14:14 -05:00
|
|
|
self.subscriptions.len()
|
|
|
|
);
|
2021-12-11 22:56:52 -05:00
|
|
|
Ok(())
|
2021-12-05 19:14:14 -05:00
|
|
|
}
|
|
|
|
|
2021-12-11 22:43:41 -05:00
|
|
|
/// Remove the subscription for this connection.
|
2021-12-05 19:14:14 -05:00
|
|
|
pub fn unsubscribe(&mut self, c: Close) {
|
|
|
|
// TODO: return notice if subscription did not exist.
|
2021-12-11 22:43:41 -05:00
|
|
|
self.subscriptions.remove(&c.id);
|
2021-12-12 11:03:28 -05:00
|
|
|
debug!(
|
|
|
|
"removed subscription, currently have {} active subs",
|
2021-12-05 19:14:14 -05:00
|
|
|
self.subscriptions.len()
|
|
|
|
);
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
|
|
|
}
|