use std::any::Any;
use super::{ConnectionId, PeerId};
pub struct MessageContext<Source, MT> {
source_id: Source,
message_type: MT,
message_bytes: Vec<u8>,
parent_context: Option<Box<dyn Any + Send>>,
}
impl<Source, MT> MessageContext<Source, MT> {
pub(super) fn new(message_type: MT, message_bytes: Vec<u8>, source_id: Source) -> Self {
Self {
message_type,
message_bytes,
source_id,
parent_context: None,
}
}
pub fn message_type(&self) -> &MT {
&self.message_type
}
pub fn message_bytes(&self) -> &[u8] {
&self.message_bytes
}
pub fn source_id(&self) -> &Source {
&self.source_id
}
pub(super) fn set_parent_context(&mut self, parent_context: Box<dyn Any + Send>) {
self.parent_context = Some(parent_context);
}
pub fn get_parent_context<T: 'static>(&self) -> Option<&T> {
self.parent_context
.as_ref()
.and_then(|boxed| boxed.downcast_ref())
}
}
impl<MT> MessageContext<PeerId, MT> {
pub fn source_peer_id(&self) -> &PeerId {
&self.source_id
}
}
impl<MT> MessageContext<ConnectionId, MT> {
pub fn source_connection_id(&self) -> &str {
&self.source_id
}
}