1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! Methods to keep track of receive sessions

use crate::time::Instant;
use crate::OutOfMemoryError;
use alloc::collections::BTreeMap;
use core::fmt::Debug;
use heapless::LinearMap;

/// Something that can keep track of receive sessions associated with other nodes
///
/// This is some kind of map from node ID to session.
///
/// Type parameters:
/// * `I`: A time instant
/// * `N`: A node ID
/// * `T`: A transfer ID
/// * `D`: Additional transport-specific session data
pub trait SessionTracker<I, N, T, D>
where
    I: Instant,
{
    /// Returns a reference to the session for the provided node, if one exists
    fn get(&self, node: N) -> Option<&Session<I, T, D>>;

    /// Returns a mutable reference to the session for the provided node, if one exists
    fn get_mut(&mut self, node: N) -> Option<&mut Session<I, T, D>>;

    //noinspection RsSelfConvention
    /// Returns a mutable reference to the session for the provided node
    ///
    /// If no session exists, this function calls the provided function, inserts the result,
    /// and returns a mutable reference to it.
    fn get_mut_or_insert_with<F>(
        &mut self,
        node: N,
        generator: F,
    ) -> Result<&mut Session<I, T, D>, OutOfMemoryError>
    where
        N: Clone,
        F: FnOnce() -> Session<I, T, D>;

    /// Inserts a session
    ///
    /// If another session with the same node already exists, it is removed.
    fn insert(&mut self, node: N, session: Session<I, T, D>) -> Result<(), OutOfMemoryError>;

    /// Removes all sessions that have expired
    fn remove_expired(&mut self, now: I);
}

/// A session, associated with a port ID and source node ID
///
/// Multiple transfers may be received during the lifetime of a session
pub struct Session<I, T, D>
where
    I: Instant,
{
    /// The time when a frame for this session was last received
    last_activity: I,
    /// The timeout for this session
    ///
    /// This session will be deleted if it has not had any activity for this duration
    timeout: I::Duration,
    /// The ID of the last successfully received transfer, if any
    ///
    /// This is used to eliminate duplicate transfers.
    last_transfer_id: Option<T>,
    /// Additional transport-specific data
    data: D,
}

impl<I, T, D> Session<I, T, D>
where
    I: Instant,
{
    /// Creates a new session
    pub fn new(
        last_activity: I,
        timeout: I::Duration,
        last_transfer_id: Option<T>,
        data: D,
    ) -> Self {
        Session {
            last_activity,
            timeout,
            last_transfer_id,
            data,
        }
    }

    /// Returns true if this session has expired and should be removed
    pub fn is_expired(&self, now: I) -> bool {
        now.duration_since(&self.last_activity) > self.timeout
    }

    /// Returns the time when this session was last active
    pub fn last_activity(&self) -> &I {
        &self.last_activity
    }
    /// Sets the time when this session was last active
    pub fn set_last_activity(&mut self, time: I) {
        self.last_activity = time;
    }
    /// Returns the timeout duration for this session
    pub fn timeout(&self) -> I::Duration {
        self.timeout
    }
    /// Returns the ID of the last received transfer, if any
    pub fn last_transfer_id(&self) -> Option<&T> {
        self.last_transfer_id.as_ref()
    }
    /// Sets the ID of the most recently received transfer
    pub fn set_last_transfer_id(&mut self, id: T) {
        self.last_transfer_id = Some(id);
    }
    /// Returns a reference to the transport-specific data
    pub fn data(&self) -> &D {
        &self.data
    }
    /// Returns a mutable reference to the transport-specific data
    pub fn data_mut(&mut self) -> &mut D {
        &mut self.data
    }
}

/// A fixed-capacity session map that uses linear search to find sessions
///
/// This implementation offers configurable memory use. Its time complexity is `O(C)`.
///
/// Type parameters:
/// * `I`: A time instant
/// * `N`: A node ID
/// * `T`: A transfer ID
/// * `D`: Additional transport-specific session data
/// * `C` (usize): Maximum number of sessions to store simultaneously
pub struct SessionLinearMap<I, N, T, D, const C: usize>
where
    I: Instant,
{
    sessions: LinearMap<N, Session<I, T, D>, C>,
}

impl<I, N, T, D, const C: usize> SessionTracker<I, N, T, D> for SessionLinearMap<I, N, T, D, C>
where
    I: Instant,
    N: Eq,
    N: Clone,
{
    fn get(&self, node: N) -> Option<&Session<I, T, D>> {
        self.sessions.get(&node)
    }

    fn get_mut(&mut self, node: N) -> Option<&mut Session<I, T, D>> {
        self.sessions.get_mut(&node)
    }

    fn get_mut_or_insert_with<F>(
        &mut self,
        node: N,
        generator: F,
    ) -> Result<&mut Session<I, T, D>, OutOfMemoryError>
    where
        N: Clone,
        F: FnOnce() -> Session<I, T, D>,
    {
        if !self.sessions.contains_key(&node) {
            self.sessions
                .insert(node.clone(), generator())
                .map_err(|_| OutOfMemoryError)?;
        }
        Ok(self.sessions.get_mut(&node).unwrap())
    }

    fn insert(&mut self, node: N, session: Session<I, T, D>) -> Result<(), OutOfMemoryError> {
        self.sessions
            .insert(node, session)
            .map(drop)
            .map_err(|_| OutOfMemoryError)
    }

    fn remove_expired(&mut self, now: I) {
        loop {
            let mut expired_node_id: Option<N> = None;
            for (id, session) in &self.sessions {
                if session.is_expired(now) {
                    expired_node_id = Some(id.clone());
                    break;
                }
            }
            match expired_node_id {
                Some(id) => {
                    self.sessions.remove(&id);
                }
                None => break,
            }
        }
    }
}

/// A fixed-capacity array of sessions, with one session slot for each possible node ID
///
/// `C` must be one greater than the maximum node ID value.
///
/// This implementation uses a consistent large amount of memory and operates in constant time.
/// Its `insert` function never fails.
pub struct SessionArray<I, T, D, const C: usize>
where
    I: Instant,
{
    /// A session for every node ID
    sessions: [Option<Session<I, T, D>>; C],
}

impl<I, N, T, D, const C: usize> SessionTracker<I, N, T, D> for SessionArray<I, T, D, C>
where
    I: Instant,
    N: Into<usize>,
{
    fn get(&self, node: N) -> Option<&Session<I, T, D>> {
        self.sessions[node.into()].as_ref()
    }

    fn get_mut(&mut self, node: N) -> Option<&mut Session<I, T, D>> {
        self.sessions[node.into()].as_mut()
    }

    fn get_mut_or_insert_with<F>(
        &mut self,
        node: N,
        generator: F,
    ) -> Result<&mut Session<I, T, D>, OutOfMemoryError>
    where
        N: Clone,
        F: FnOnce() -> Session<I, T, D>,
    {
        let entry = &mut self.sessions[node.into()];
        if entry.is_none() {
            *entry = Some(generator());
        }
        Ok(entry.as_mut().unwrap())
    }

    fn insert(&mut self, node: N, session: Session<I, T, D>) -> Result<(), OutOfMemoryError> {
        self.sessions[node.into()] = Some(session);
        Ok(())
    }

    fn remove_expired(&mut self, now: I) {
        for entry in &mut self.sessions {
            let mut remove = false;
            if let Some(session) = entry {
                if session.is_expired(now) {
                    remove = true;
                }
            }
            if remove {
                *entry = None;
            }
        }
    }
}

/// A session map that uses dynamic memory allocation
///
/// **Caution:** This implementation cannot detect when memory allocation fails, so it may cause
/// the program to abort. Only use it when memory is plentiful.
///
/// This implementation uses variable amounts of memory and takes `O(log(number of sessions))` time.
pub struct SessionDynamicMap<I, N, T, D>
where
    I: Instant,
{
    sessions: BTreeMap<N, Session<I, T, D>>,
}

impl<I, N, T, D> Default for SessionDynamicMap<I, N, T, D>
where
    I: Instant,
    N: Ord,
{
    fn default() -> Self {
        SessionDynamicMap {
            sessions: BTreeMap::default(),
        }
    }
}

impl<I, N, T, D> SessionTracker<I, N, T, D> for SessionDynamicMap<I, N, T, D>
where
    I: Instant,
    N: Ord + Clone + Debug,
{
    fn get(&self, node: N) -> Option<&Session<I, T, D>> {
        self.sessions.get(&node)
    }

    fn get_mut(&mut self, node: N) -> Option<&mut Session<I, T, D>> {
        self.sessions.get_mut(&node)
    }

    fn get_mut_or_insert_with<F>(
        &mut self,
        node: N,
        generator: F,
    ) -> Result<&mut Session<I, T, D>, OutOfMemoryError>
    where
        N: Clone,
        F: FnOnce() -> Session<I, T, D>,
    {
        Ok(self.sessions.entry(node).or_insert_with(generator))
    }

    fn insert(&mut self, node: N, session: Session<I, T, D>) -> Result<(), OutOfMemoryError> {
        let _ = self.sessions.insert(node, session);
        Ok(())
    }

    fn remove_expired(&mut self, now: I) {
        loop {
            let mut expired_node_id: Option<N> = None;
            for (id, session) in &self.sessions {
                if session.is_expired(now) {
                    expired_node_id = Some(id.clone());
                    break;
                }
            }
            match expired_node_id {
                Some(id) => {
                    log::debug!("Removing expired session from node {:?}", id);
                    self.sessions.remove(&id);
                }
                None => break,
            }
        }
    }
}