veilid/veilid-core/src/rpc_processor/destination.rs

144 lines
3.9 KiB
Rust
Raw Normal View History

2022-09-03 17:57:25 +00:00
use super::*;
/// Where to send an RPC message
#[derive(Debug, Clone)]
pub enum Destination {
/// Send to node directly
Direct {
/// The node to send to
target: NodeRef,
2022-10-09 18:59:01 +00:00
/// Require safety route or not
2022-10-22 01:27:07 +00:00
safety_selection: SafetySelection,
2022-09-03 17:57:25 +00:00
},
/// Send to node for relay purposes
Relay {
/// The relay to send to
relay: NodeRef,
/// The final destination the relay should send to
target: DHTKey,
2022-10-09 18:59:01 +00:00
/// Require safety route or not
2022-10-22 01:27:07 +00:00
safety_selection: SafetySelection,
2022-09-03 17:57:25 +00:00
},
/// Send to private route (privateroute)
PrivateRoute {
/// A private route to send to
private_route: PrivateRoute,
2022-10-09 18:59:01 +00:00
/// Require safety route or not
2022-10-22 01:27:07 +00:00
safety_selection: SafetySelection,
2022-09-03 17:57:25 +00:00
},
}
impl Destination {
pub fn direct(target: NodeRef) -> Self {
2022-10-31 03:23:12 +00:00
let sequencing = target.sequencing();
2022-09-03 17:57:25 +00:00
Self::Direct {
target,
2022-10-31 03:23:12 +00:00
safety_selection: SafetySelection::Unsafe(sequencing),
2022-09-03 17:57:25 +00:00
}
}
pub fn relay(relay: NodeRef, target: DHTKey) -> Self {
2022-10-31 03:23:12 +00:00
let sequencing = relay.sequencing();
2022-09-03 17:57:25 +00:00
Self::Relay {
relay,
target,
2022-10-31 03:23:12 +00:00
safety_selection: SafetySelection::Unsafe(sequencing),
2022-09-03 17:57:25 +00:00
}
}
2022-10-22 01:27:07 +00:00
pub fn private_route(private_route: PrivateRoute, safety_selection: SafetySelection) -> Self {
2022-09-03 17:57:25 +00:00
Self::PrivateRoute {
private_route,
2022-10-22 01:27:07 +00:00
safety_selection,
2022-09-03 17:57:25 +00:00
}
}
2022-09-04 18:17:28 +00:00
2022-10-22 01:27:07 +00:00
pub fn with_safety(self, safety_selection: SafetySelection) -> Self {
2022-09-03 17:57:25 +00:00
match self {
2022-10-19 01:53:45 +00:00
Destination::Direct {
2022-09-03 17:57:25 +00:00
target,
2022-10-22 01:27:07 +00:00
safety_selection: _,
2022-10-19 01:53:45 +00:00
} => Self::Direct {
target,
2022-10-22 01:27:07 +00:00
safety_selection,
2022-09-03 17:57:25 +00:00
},
Destination::Relay {
relay,
target,
2022-10-22 01:27:07 +00:00
safety_selection: _,
2022-09-03 17:57:25 +00:00
} => Self::Relay {
relay,
target,
2022-10-22 01:27:07 +00:00
safety_selection,
2022-09-03 17:57:25 +00:00
},
Destination::PrivateRoute {
private_route,
2022-10-22 01:27:07 +00:00
safety_selection: _,
2022-09-03 17:57:25 +00:00
} => Self::PrivateRoute {
private_route,
2022-10-22 01:27:07 +00:00
safety_selection,
2022-09-03 17:57:25 +00:00
},
}
2022-10-21 03:11:41 +00:00
}
2022-10-22 01:27:07 +00:00
pub fn get_safety_selection(&self) -> &SafetySelection {
2022-10-21 03:11:41 +00:00
match self {
Destination::Direct {
target: _,
2022-10-22 01:27:07 +00:00
safety_selection,
} => safety_selection,
2022-10-21 03:11:41 +00:00
Destination::Relay {
relay: _,
target: _,
2022-10-22 01:27:07 +00:00
safety_selection,
} => safety_selection,
2022-10-21 03:11:41 +00:00
Destination::PrivateRoute {
private_route: _,
2022-10-22 01:27:07 +00:00
safety_selection,
} => safety_selection,
2022-10-21 03:11:41 +00:00
}
2022-09-03 17:57:25 +00:00
}
}
impl fmt::Display for Destination {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
2022-10-19 01:53:45 +00:00
Destination::Direct {
target,
2022-10-22 01:27:07 +00:00
safety_selection,
2022-10-19 01:53:45 +00:00
} => {
2022-10-22 01:27:07 +00:00
let sr = if matches!(safety_selection, SafetySelection::Safe(_)) {
"+SR"
} else {
""
};
2022-09-03 17:57:25 +00:00
2022-10-09 18:59:01 +00:00
write!(f, "{}{}", target, sr)
2022-09-03 17:57:25 +00:00
}
Destination::Relay {
relay,
target,
2022-10-22 01:27:07 +00:00
safety_selection,
2022-09-03 17:57:25 +00:00
} => {
2022-10-22 01:27:07 +00:00
let sr = if matches!(safety_selection, SafetySelection::Safe(_)) {
"+SR"
} else {
""
};
2022-09-03 17:57:25 +00:00
2022-10-09 18:59:01 +00:00
write!(f, "{}@{}{}", target.encode(), relay, sr)
2022-09-03 17:57:25 +00:00
}
Destination::PrivateRoute {
private_route,
2022-10-22 01:27:07 +00:00
safety_selection,
2022-09-03 17:57:25 +00:00
} => {
2022-10-22 01:27:07 +00:00
let sr = if matches!(safety_selection, SafetySelection::Safe(_)) {
"+SR"
} else {
""
};
2022-09-03 17:57:25 +00:00
2022-10-22 01:27:07 +00:00
write!(f, "{}{}", private_route, sr)
2022-09-03 17:57:25 +00:00
}
}
}
}