s2_kit/connect/server/
store.rs1use alloc::collections::BTreeMap;
4use alloc::string::String;
5use alloc::sync::Arc;
6use alloc::vec::Vec;
7
8use crate::connect::proto::{
9 AccessToken, ConnectionDetails, NodeDescription, NodeId, NodeIdAlias, PairingToken, TokenStore,
10};
11
12#[derive(Debug, Clone)]
14pub struct PairedNode {
15 pub local: NodeId,
17 pub remote: NodeDescription,
19 pub tokens: TokenStore,
21 pub details: Option<ConnectionDetails>,
23}
24
25pub trait Store: Send + Sync + 'static {
31 fn pairing_token(&self, node: NodeId) -> Option<PairingToken>;
37
38 fn resolve_alias(&self, alias: &NodeIdAlias) -> Option<NodeId>;
40
41 fn sole_node(&self) -> Option<NodeId>;
43
44 fn node(&self, node: NodeId) -> Option<NodeDescription>;
46
47 fn nodes(&self) -> Vec<(NodeDescription, Option<NodeIdAlias>)>;
49
50 fn pair(&self, paired: PairedNode);
52
53 fn paired(&self, remote: NodeId) -> Option<PairedNode>;
55
56 fn paired_with(&self, local: NodeId) -> Vec<NodeId>;
64
65 fn set_tokens(&self, remote: NodeId, tokens: TokenStore);
67
68 fn unpair(&self, remote: NodeId);
70
71 fn node_for_token(&self, token: &AccessToken) -> Option<NodeId>;
73}
74
75#[derive(Debug, Default)]
80pub struct Memory {
81 inner: std::sync::RwLock<Inner>,
82}
83
84#[derive(Debug, Default)]
85struct Inner {
86 nodes: BTreeMap<NodeId, NodeDescription>,
88 aliases: BTreeMap<String, NodeId>,
90 tokens: BTreeMap<NodeId, PairingToken>,
92 pairings: BTreeMap<NodeId, PairedNode>,
94}
95
96impl Memory {
97 #[must_use]
99 pub fn new() -> Arc<Self> {
100 Arc::new(Self::default())
101 }
102
103 pub fn add_node(&self, node: NodeDescription, alias: Option<NodeIdAlias>) {
105 let Ok(mut inner) = self.inner.write() else {
106 return;
107 };
108 if let Some(alias) = alias {
109 inner.aliases.insert(alias.0, node.id);
110 }
111 inner.nodes.insert(node.id, node);
112 }
113
114 pub fn open_for_pairing(&self, node: NodeId, token: PairingToken) {
118 if let Ok(mut inner) = self.inner.write() {
119 inner.tokens.insert(node, token);
120 }
121 }
122
123 pub fn close_pairing(&self, node: NodeId) {
125 if let Ok(mut inner) = self.inner.write() {
126 inner.tokens.remove(&node);
127 }
128 }
129}
130
131impl Store for Memory {
132 fn pairing_token(&self, node: NodeId) -> Option<PairingToken> {
133 self.inner.read().ok()?.tokens.get(&node).cloned()
134 }
135
136 fn resolve_alias(&self, alias: &NodeIdAlias) -> Option<NodeId> {
137 self.inner.read().ok()?.aliases.get(&alias.0).copied()
138 }
139
140 fn sole_node(&self) -> Option<NodeId> {
141 let inner = self.inner.read().ok()?;
142 if inner.nodes.len() == 1 {
146 inner.nodes.keys().next().copied()
147 } else {
148 None
149 }
150 }
151
152 fn node(&self, node: NodeId) -> Option<NodeDescription> {
153 self.inner.read().ok()?.nodes.get(&node).cloned()
154 }
155
156 fn nodes(&self) -> Vec<(NodeDescription, Option<NodeIdAlias>)> {
157 let Ok(inner) = self.inner.read() else {
158 return Vec::new();
159 };
160 inner
161 .nodes
162 .values()
163 .map(|node| {
164 let alias = inner
165 .aliases
166 .iter()
167 .find(|(_, id)| **id == node.id)
168 .map(|(alias, _)| NodeIdAlias(alias.clone()));
169 (node.clone(), alias)
170 })
171 .collect()
172 }
173
174 fn pair(&self, paired: PairedNode) {
175 if let Ok(mut inner) = self.inner.write() {
176 inner.tokens.remove(&paired.local);
179 inner.pairings.insert(paired.remote.id, paired);
180 }
181 }
182
183 fn paired(&self, remote: NodeId) -> Option<PairedNode> {
184 self.inner.read().ok()?.pairings.get(&remote).cloned()
185 }
186
187 fn paired_with(&self, local: NodeId) -> Vec<NodeId> {
188 let Ok(inner) = self.inner.read() else {
189 return Vec::new();
190 };
191 inner
192 .pairings
193 .values()
194 .filter(|paired| paired.local == local)
195 .map(|paired| paired.remote.id)
196 .collect()
197 }
198
199 fn set_tokens(&self, remote: NodeId, tokens: TokenStore) {
200 if let Ok(mut inner) = self.inner.write()
201 && let Some(paired) = inner.pairings.get_mut(&remote)
202 {
203 paired.tokens = tokens;
204 }
205 }
206
207 fn unpair(&self, remote: NodeId) {
208 if let Ok(mut inner) = self.inner.write() {
209 inner.pairings.remove(&remote);
210 }
211 }
212
213 fn node_for_token(&self, token: &AccessToken) -> Option<NodeId> {
214 let inner = self.inner.read().ok()?;
215 inner
216 .pairings
217 .values()
218 .find(|p| p.tokens.accepts(token))
219 .map(|p| p.remote.id)
220 }
221}