use crate::{
crypto::{SigningPrivateKey, StaticPrivateKey},
events::EventHandle,
primitives::RouterId,
profile::ProfileStorage,
runtime::Runtime,
tunnel::NoiseContext,
};
use bytes::Bytes;
#[cfg(feature = "std")]
use parking_lot::RwLock;
#[cfg(feature = "no_std")]
use spin::rwlock::RwLock;
use alloc::sync::Arc;
struct InnerRouterContext<R: Runtime> {
metrics_handle: R::MetricsHandle,
net_id: u8,
#[allow(unused)]
noise: NoiseContext,
router_id: RouterId,
router_info: Arc<RwLock<Bytes>>,
#[allow(unused)]
signing_key: SigningPrivateKey,
#[allow(unused)]
static_key: StaticPrivateKey,
}
#[derive(Clone)]
pub struct RouterContext<R: Runtime> {
inner: Arc<InnerRouterContext<R>>,
profile_storage: ProfileStorage<R>,
event_handle: EventHandle<R>,
}
impl<R: Runtime> RouterContext<R> {
pub(crate) fn new(
metrics_handle: R::MetricsHandle,
profile_storage: ProfileStorage<R>,
router_id: RouterId,
router_info: Bytes,
static_key: StaticPrivateKey,
signing_key: SigningPrivateKey,
net_id: u8,
event_handle: EventHandle<R>,
) -> Self {
Self {
event_handle,
inner: Arc::new(InnerRouterContext {
metrics_handle,
net_id,
noise: NoiseContext::new(static_key.clone(), Bytes::from(router_id.to_vec())),
router_id,
router_info: Arc::new(RwLock::new(router_info)),
signing_key,
static_key,
}),
profile_storage,
}
}
pub fn router_info(&self) -> Bytes {
self.inner.router_info.read().clone()
}
pub fn router_id(&self) -> &RouterId {
&self.inner.router_id
}
pub fn net_id(&self) -> u8 {
self.inner.net_id
}
pub fn set_router_info(&self, router_info: Bytes) {
let mut inner = self.inner.router_info.write();
*inner = router_info;
}
pub fn metrics_handle(&self) -> &R::MetricsHandle {
&self.inner.metrics_handle
}
pub fn profile_storage(&self) -> &ProfileStorage<R> {
&self.profile_storage
}
pub fn noise(&self) -> &NoiseContext {
&self.inner.noise
}
pub fn signing_key(&self) -> &SigningPrivateKey {
&self.inner.signing_key
}
pub(crate) fn event_handle(&self) -> &EventHandle<R> {
&self.event_handle
}
}