use std::sync::Arc;
use super::{ServiceBus, ServiceChannel, ServiceLoadEntry};
impl ServiceChannel {
pub fn push_request(&self, id: u64, req: Box<dyn std::any::Any + Send>) {
self.requests
.lock()
.expect("service req lock poisoned")
.push_back((id, req));
}
pub fn pop_request(&self) -> Option<(u64, Box<dyn std::any::Any + Send>)> {
self.requests
.lock()
.expect("service req lock poisoned")
.pop_front()
}
pub fn push_response(&self, id: u64, res: Box<dyn std::any::Any + Send>) {
self.responses
.lock()
.expect("service res lock poisoned")
.insert(id, res);
}
pub fn pop_response(&self, id: u64) -> Option<Box<dyn std::any::Any + Send>> {
self.responses
.lock()
.expect("service res lock poisoned")
.remove(&id)
}
pub fn pending_request_count(&self) -> usize {
self.requests
.lock()
.expect("service req lock poisoned")
.len()
}
pub fn pending_response_count(&self) -> usize {
self.responses
.lock()
.expect("service res lock poisoned")
.len()
}
}
impl ServiceBus {
pub fn get_or_create(&mut self, service: &str) -> Arc<ServiceChannel> {
self.channels
.entry(service.to_string())
.or_insert_with(|| Arc::new(ServiceChannel::default()))
.clone()
}
pub fn load_entries(&self) -> Vec<ServiceLoadEntry> {
let mut out = self
.channels
.iter()
.map(|(service, ch)| ServiceLoadEntry {
service: service.clone(),
pending_requests: ch.pending_request_count(),
pending_responses: ch.pending_response_count(),
})
.collect::<Vec<_>>();
out.sort_by(|a, b| a.service.cmp(&b.service));
out
}
}