2021-12-05 19:14:14 -05:00
|
|
|
use crate::close::Close;
|
|
|
|
use crate::error::Result;
|
2021-12-05 21:28:02 -05:00
|
|
|
use crate::event::Event;
|
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-05 19:14:14 -05:00
|
|
|
// subscription identifiers must be reasonably sized.
|
|
|
|
const MAX_SUBSCRIPTION_ID_LEN: usize = 256;
|
|
|
|
|
2021-12-05 17:53:26 -05:00
|
|
|
// state for a client connection
|
|
|
|
pub struct ClientConn {
|
2021-12-05 21:28:02 -05:00
|
|
|
client_id: Uuid,
|
2021-12-05 17:53:26 -05:00
|
|
|
// current set of subscriptions
|
2021-12-05 19:14:14 -05:00
|
|
|
subscriptions: HashMap<String, Subscription>,
|
2021-12-05 17:53:26 -05:00
|
|
|
// websocket
|
|
|
|
//stream: WebSocketStream<TcpStream>,
|
2021-12-05 19:14:14 -05:00
|
|
|
max_subs: usize,
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientConn {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let client_id = Uuid::new_v4();
|
|
|
|
ClientConn {
|
2021-12-05 21:28:02 -05:00
|
|
|
client_id: client_id,
|
2021-12-05 19:14:14 -05:00
|
|
|
subscriptions: HashMap::new(),
|
|
|
|
max_subs: 128,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 21:28:02 -05:00
|
|
|
pub fn get_client_prefix(&self) -> String {
|
|
|
|
self.client_id.to_string().chars().take(8).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the first subscription that matches the event.
|
|
|
|
pub fn get_matching_subscription(&self, e: &Event) -> Option<&str> {
|
|
|
|
for (id, sub) in self.subscriptions.iter() {
|
|
|
|
if sub.interested_in_event(e) {
|
|
|
|
return Some(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
if sub_id_len > MAX_SUBSCRIPTION_ID_LEN {
|
|
|
|
info!("Dropping subscription with huge ({}) length", sub_id_len);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
info!("Replaced existing subscription");
|
|
|
|
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 {
|
|
|
|
info!("Client has reached the maximum number of unique subscriptions");
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// add subscription
|
|
|
|
self.subscriptions.insert(k, s);
|
|
|
|
info!(
|
|
|
|
"Registered new subscription, currently have {} active subs",
|
|
|
|
self.subscriptions.len()
|
|
|
|
);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unsubscribe(&mut self, c: Close) {
|
|
|
|
// TODO: return notice if subscription did not exist.
|
|
|
|
self.subscriptions.remove(&c.get_id());
|
|
|
|
info!(
|
|
|
|
"Removed subscription, currently have {} active subs",
|
|
|
|
self.subscriptions.len()
|
|
|
|
);
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|
|
|
|
}
|