use std::{
any::Any,
collections::HashMap,
mem,
sync::{atomic::AtomicU32, Arc},
};
use token::{token_new_face, undeclare_simple_token};
use zenoh_config::{unwrap_or_default, ModeDependent, WhatAmI};
use zenoh_protocol::{
common::ZExtBody,
network::{
declare::{
ext::{NodeIdType, QoSType},
queryable::ext::QueryableInfoType,
QueryableId, SubscriberId, TokenId,
},
interest::{InterestId, InterestOptions},
oam::id::OAM_LINKSTATE,
Declare, DeclareBody, DeclareFinal, Oam,
},
};
use zenoh_result::ZResult;
use zenoh_sync::get_mut_unchecked;
use zenoh_transport::unicast::TransportUnicast;
use self::{
gossip::Network,
interests::interests_new_face,
pubsub::{pubsub_new_face, undeclare_simple_subscription},
queries::{queries_new_face, undeclare_simple_queryable},
};
use super::{
super::dispatcher::{
face::FaceState,
tables::{NodeId, Resource, RoutingExpr, Tables, TablesLock},
},
HatBaseTrait, HatTrait, SendDeclare,
};
use crate::net::{
codec::Zenoh080Routing,
protocol::linkstate::LinkStateList,
routing::{
dispatcher::{
face::{Face, InterestState},
interests::RemoteInterest,
},
RoutingContext,
},
runtime::Runtime,
};
mod gossip;
mod interests;
mod pubsub;
mod queries;
mod token;
macro_rules! hat_mut {
($t:expr) => {
$t.hat.downcast_mut::<HatTables>().unwrap()
};
}
use hat_mut;
macro_rules! face_hat {
($f:expr) => {
$f.hat.downcast_ref::<HatFace>().unwrap()
};
}
use face_hat;
macro_rules! face_hat_mut {
($f:expr) => {
get_mut_unchecked($f).hat.downcast_mut::<HatFace>().unwrap()
};
}
use face_hat_mut;
use crate::net::common::AutoConnect;
struct HatTables {
gossip: Option<Network>,
}
impl HatTables {
fn new() -> Self {
Self { gossip: None }
}
}
pub(crate) struct HatCode {}
impl HatBaseTrait for HatCode {
fn init(&self, tables: &mut Tables, runtime: Runtime) -> ZResult<()> {
let config_guard = runtime.config().lock();
let config = &config_guard.0;
let whatami = tables.whatami;
let gossip = unwrap_or_default!(config.scouting().gossip().enabled());
let gossip_multihop = unwrap_or_default!(config.scouting().gossip().multihop());
let gossip_target = *unwrap_or_default!(config.scouting().gossip().target().get(whatami));
if gossip_target.matches(WhatAmI::Client) {
bail!("\"client\" is not allowed as gossip target")
}
let autoconnect = if gossip {
AutoConnect::gossip(config, whatami, runtime.zid().into())
} else {
AutoConnect::disabled()
};
let wait_declares = unwrap_or_default!(config.open().return_conditions().declares());
let router_peers_failover_brokering =
unwrap_or_default!(config.routing().router().peers_failover_brokering());
drop(config_guard);
if gossip {
hat_mut!(tables).gossip = Some(Network::new(
"[Gossip]".to_string(),
tables.zid,
runtime,
router_peers_failover_brokering,
gossip,
gossip_multihop,
gossip_target,
autoconnect,
wait_declares,
));
}
Ok(())
}
fn new_tables(&self, _router_peers_failover_brokering: bool) -> Box<dyn Any + Send + Sync> {
Box::new(HatTables::new())
}
fn new_face(&self) -> Box<dyn Any + Send + Sync> {
Box::new(HatFace::new())
}
fn new_resource(&self) -> Box<dyn Any + Send + Sync> {
Box::new(HatContext::new())
}
fn new_local_face(
&self,
tables: &mut Tables,
_tables_ref: &Arc<TablesLock>,
face: &mut Face,
send_declare: &mut SendDeclare,
) -> ZResult<()> {
interests_new_face(tables, &mut face.state);
pubsub_new_face(tables, &mut face.state, send_declare);
queries_new_face(tables, &mut face.state, send_declare);
token_new_face(tables, &mut face.state, send_declare);
tables.disable_all_routes();
Ok(())
}
fn new_transport_unicast_face(
&self,
tables: &mut Tables,
_tables_ref: &Arc<TablesLock>,
face: &mut Face,
transport: &TransportUnicast,
send_declare: &mut SendDeclare,
) -> ZResult<()> {
if face.state.whatami != WhatAmI::Client {
if let Some(net) = hat_mut!(tables).gossip.as_mut() {
net.add_link(transport.clone());
}
}
if face.state.whatami == WhatAmI::Peer {
let face_id = face.state.id;
get_mut_unchecked(&mut face.state).local_interests.insert(
INITIAL_INTEREST_ID,
InterestState::new(face_id, InterestOptions::ALL, None, false),
);
}
interests_new_face(tables, &mut face.state);
pubsub_new_face(tables, &mut face.state, send_declare);
queries_new_face(tables, &mut face.state, send_declare);
token_new_face(tables, &mut face.state, send_declare);
tables.disable_all_routes();
if face.state.whatami == WhatAmI::Peer {
send_declare(
&face.state.primitives,
RoutingContext::new(Declare {
interest_id: Some(0),
ext_qos: QoSType::default(),
ext_tstamp: None,
ext_nodeid: NodeIdType::default(),
body: DeclareBody::DeclareFinal(DeclareFinal),
}),
);
}
Ok(())
}
fn close_face(
&self,
tables: &TablesLock,
_tables_ref: &Arc<TablesLock>,
face: &mut Arc<FaceState>,
send_declare: &mut SendDeclare,
) {
let mut wtables = zwrite!(tables.tables);
let mut face_clone = face.clone();
let face = get_mut_unchecked(face);
let hat_face = match face.hat.downcast_mut::<HatFace>() {
Some(hate_face) => hate_face,
None => {
tracing::error!("Error downcasting face hat in close_face!");
return;
}
};
hat_face.remote_interests.clear();
hat_face.local_subs.clear();
hat_face.local_qabls.clear();
hat_face.local_tokens.clear();
for res in face.remote_mappings.values_mut() {
get_mut_unchecked(res).session_ctxs.remove(&face.id);
Resource::clean(res);
}
face.remote_mappings.clear();
for res in face.local_mappings.values_mut() {
get_mut_unchecked(res).session_ctxs.remove(&face.id);
Resource::clean(res);
}
face.local_mappings.clear();
let mut subs_matches = vec![];
for (_id, mut res) in hat_face.remote_subs.drain() {
get_mut_unchecked(&mut res).session_ctxs.remove(&face.id);
undeclare_simple_subscription(&mut wtables, &mut face_clone, &mut res, send_declare);
if res.context.is_some() {
for match_ in &res.context().matches {
let mut match_ = match_.upgrade().unwrap();
if !Arc::ptr_eq(&match_, &res) {
get_mut_unchecked(&mut match_)
.context_mut()
.disable_data_routes();
subs_matches.push(match_);
}
}
get_mut_unchecked(&mut res)
.context_mut()
.disable_data_routes();
subs_matches.push(res);
}
}
let mut qabls_matches = vec![];
for (_id, (mut res, _)) in hat_face.remote_qabls.drain() {
get_mut_unchecked(&mut res).session_ctxs.remove(&face.id);
undeclare_simple_queryable(&mut wtables, &mut face_clone, &mut res, send_declare);
if res.context.is_some() {
for match_ in &res.context().matches {
let mut match_ = match_.upgrade().unwrap();
if !Arc::ptr_eq(&match_, &res) {
get_mut_unchecked(&mut match_)
.context_mut()
.disable_query_routes();
qabls_matches.push(match_);
}
}
get_mut_unchecked(&mut res)
.context_mut()
.disable_query_routes();
qabls_matches.push(res);
}
}
for (_id, mut res) in hat_face.remote_tokens.drain() {
get_mut_unchecked(&mut res).session_ctxs.remove(&face.id);
undeclare_simple_token(&mut wtables, &mut face_clone, &mut res, send_declare);
}
for mut res in subs_matches {
get_mut_unchecked(&mut res)
.context_mut()
.disable_data_routes();
Resource::clean(&mut res);
}
for mut res in qabls_matches {
get_mut_unchecked(&mut res)
.context_mut()
.disable_query_routes();
Resource::clean(&mut res);
}
wtables.faces.remove(&face.id);
if face.whatami != WhatAmI::Client {
if let Some(net) = hat_mut!(wtables).gossip.as_mut() {
net.remove_link(&face.zid);
}
};
drop(wtables);
}
fn handle_oam(
&self,
tables: &mut Tables,
_tables_ref: &Arc<TablesLock>,
oam: &mut Oam,
transport: &TransportUnicast,
_send_declare: &mut SendDeclare,
) -> ZResult<()> {
if oam.id == OAM_LINKSTATE {
if let ZExtBody::ZBuf(buf) = mem::take(&mut oam.body) {
if let Ok(zid) = transport.get_zid() {
let whatami = transport.get_whatami()?;
if whatami != WhatAmI::Client {
if let Some(net) = hat_mut!(tables).gossip.as_mut() {
use zenoh_buffers::reader::HasReader;
use zenoh_codec::RCodec;
let codec = Zenoh080Routing::new();
let mut reader = buf.reader();
let Ok(list): Result<LinkStateList, _> = codec.read(&mut reader) else {
bail!("failed to decode link state");
};
net.link_states(list.link_states, zid, whatami);
}
};
}
}
}
Ok(())
}
#[inline]
fn map_routing_context(
&self,
_tables: &Tables,
_face: &FaceState,
_routing_context: NodeId,
) -> NodeId {
0
}
#[inline]
fn ingress_filter(&self, _tables: &Tables, _face: &FaceState, _expr: &RoutingExpr) -> bool {
true
}
#[inline]
fn egress_filter(
&self,
_tables: &Tables,
src_face: &FaceState,
out_face: &Arc<FaceState>,
_expr: &RoutingExpr,
) -> bool {
src_face.id != out_face.id
&& (out_face.mcast_group.is_none()
|| (src_face.whatami == WhatAmI::Client && src_face.mcast_group.is_none()))
}
fn info(&self, _tables: &Tables, _kind: WhatAmI) -> String {
"graph {}".to_string()
}
}
struct HatContext {}
impl HatContext {
fn new() -> Self {
Self {}
}
}
struct HatFace {
next_id: AtomicU32, remote_interests: HashMap<InterestId, RemoteInterest>,
local_subs: HashMap<Arc<Resource>, SubscriberId>,
remote_subs: HashMap<SubscriberId, Arc<Resource>>,
local_tokens: HashMap<Arc<Resource>, TokenId>,
remote_tokens: HashMap<TokenId, Arc<Resource>>,
local_qabls: HashMap<Arc<Resource>, (QueryableId, QueryableInfoType)>,
remote_qabls: HashMap<QueryableId, (Arc<Resource>, QueryableInfoType)>,
}
impl HatFace {
fn new() -> Self {
Self {
next_id: AtomicU32::new(1), remote_interests: HashMap::new(),
local_subs: HashMap::new(),
remote_subs: HashMap::new(),
local_tokens: HashMap::new(),
remote_tokens: HashMap::new(),
local_qabls: HashMap::new(),
remote_qabls: HashMap::new(),
}
}
}
impl HatTrait for HatCode {}
const INITIAL_INTEREST_ID: u32 = 0;
#[inline]
fn initial_interest(face: &FaceState) -> Option<&InterestState> {
face.local_interests.get(&INITIAL_INTEREST_ID)
}
#[inline]
pub(super) fn push_declaration_profile(face: &FaceState) -> bool {
face.whatami != WhatAmI::Client
}