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
use core::fmt::Debug;

use embedded_time::Clock;
use kwap_common::prelude::*;
use kwap_msg::{Id, Opt, OptNumber, Token};
use no_std_net::SocketAddr;
#[cfg(feature = "alloc")]
use std_alloc::{collections::BTreeMap, vec::Vec};

use crate::net::Socket;
use crate::time::Stamped;

/// kwap configuration trait
pub trait Platform: Sized + 'static + core::fmt::Debug {
  /// What type should we use to store the message payloads?
  type MessagePayload: Array<Item = u8> + Clone + Debug;
  /// What type should we use to store the option values?
  type MessageOptionBytes: Array<Item = u8> + 'static + Clone + Debug;
  /// What type should we use to store the options?
  type MessageOptions: Array<Item = Opt<Self::MessageOptionBytes>> + Clone + Debug;

  /// What type should we use to keep track of message IDs we've seen with a remote socket?
  type MessageIdHistory: Array<Item = Stamped<Self::Clock, Id>> + Clone + Debug;
  /// How do we track socket <> id histories?
  type MessageIdHistoryBySocket: Map<SocketAddr, Self::MessageIdHistory> + Clone + Debug;

  /// What type should we use to keep track of message Tokens we've seen with a remote socket?
  type MessageTokenHistory: Array<Item = Stamped<Self::Clock, Token>> + Clone + Debug;
  /// How do we track socket <> token histories?
  type MessageTokenHistoryBySocket: Map<SocketAddr, Self::MessageTokenHistory> + Clone + Debug;

  /// What type should we use to keep track of options before serializing?
  type NumberedOptions: Array<Item = (OptNumber, Opt<Self::MessageOptionBytes>)> + Clone + Debug;

  /// What should we use to keep track of time?
  type Clock: Clock<T = u64>;

  /// What should we use for networking?
  type Socket: Socket;
}

/// Used to associate a value with a RetryTimer.
///
/// The value is usually used as the basis for some
/// fallible IO, e.g. `T` may be an outbound `Req` -
/// `Retryable` allows us to keep track of how many times
/// we've attempted to send this request and whether we
/// should consider it poisoned.
#[derive(Debug, Clone, Copy)]
pub struct Retryable<P: Platform, T>(pub T, pub crate::retry::RetryTimer<P::Clock>);

impl<P: Platform, T> Retryable<P, T> {
  /// Gets the data, discarding the retry timer
  pub fn unwrap(self) -> T {
    self.0
  }
}

/// Configures `kwap` to use `Vec` for collections,
/// `UdpSocket` for networking,
/// and [`crate::std::Clock`] for timing
///
/// ```
/// use kwap::platform::Std;
/// use kwap::req::Req;
///
/// Req::<Std>::get("192.168.0.1:5683".parse().unwrap(), "/hello");
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[derive(Copy)]
pub struct Alloc<Clk, Sock>(core::marker::PhantomData<(Clk, Sock)>)
  where Clk: Clock<T = u64> + 'static,
        Sock: Socket + 'static;

#[cfg(feature = "alloc")]
impl<Clk: Clock<T = u64> + 'static, Sock: Socket + 'static> core::fmt::Debug for Alloc<Clk, Sock> {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(f, "Alloc::<_, _>(_)")
  }
}

#[cfg(feature = "alloc")]
impl<Clk: Clock<T = u64> + 'static, Sock: Socket + 'static> Clone for Alloc<Clk, Sock> {
  fn clone(&self) -> Self {
    Self(Default::default())
  }
}

#[cfg(feature = "alloc")]
impl<Clk: Clock<T = u64> + Debug + 'static, Sock: Socket + 'static> Platform for Alloc<Clk, Sock> {
  type MessagePayload = Vec<u8>;
  type MessageOptionBytes = Vec<u8>;
  type MessageOptions = Vec<Opt<Vec<u8>>>;
  type MessageIdHistory = Vec<Stamped<Self::Clock, Id>>;
  type MessageTokenHistory = Vec<Stamped<Self::Clock, Token>>;
  type MessageIdHistoryBySocket = BTreeMap<SocketAddr, Self::MessageIdHistory>;
  type MessageTokenHistoryBySocket = BTreeMap<SocketAddr, Self::MessageTokenHistory>;
  type NumberedOptions = Vec<(OptNumber, Opt<Vec<u8>>)>;
  type Clock = Clk;
  type Socket = Sock;
}

/// Configures `kwap` to use `Vec` for collections,
/// `UdpSocket` for networking,
/// and [`crate::std::Clock`] for timing
///
/// ```
/// use kwap::platform::Std;
/// use kwap::req::Req;
///
/// Req::<Std>::get("192.168.0.1:5683".parse().unwrap(), "/hello");
/// ```
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub type Std = Alloc<crate::std::Clock, std::net::UdpSocket>;

/// Type alias using Config instead of explicit type parameters for [`kwap_msg::Message`]
pub type Message<P> = kwap_msg::Message<<P as Platform>::MessagePayload,
                                        <P as Platform>::MessageOptionBytes,
                                        <P as Platform>::MessageOptions>;