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
/*
Copyright (c) 2020 Todd Stellanova
LICENSE: BSD3 (see LICENSE file)
*/

#![cfg_attr(not(test), no_std)]

use array_macro::array;
use core::marker::PhantomData;
use core::sync::atomic::{AtomicU32, Ordering};

use core::fmt::Debug;
/// The foco system should
/// - allow publishers to broadcast messages on particular topics
/// - allow subscribers to subscribe to topics
/// - allow multiple publishers on a single topic (with unique publisher instance ids )
/// - allow subscribers to filter on any publisher, or specific set of publishers
///
/// The foco system comprises:
/// - One message queue per topic, per publisher (SPMC queue)
/// - One concrete rust type per topic
/// - Multiple readers may poll the queue for messages (subscribe)

/// We know the list of topics+types at compile time
/// We could define a message type as a compound type of the topic and "primitive" type
/// We could tune the queue size at compile time, per type
/// We know the max number of publishers at compile time (esp if nobody ever unpublishes)
/// But what if this is a library? We would not know all the publishers
/// Allow the library to be built with message/topic definition files
///
use spms_ring::{ReadToken, SpmsRing};

pub type PublisherId = u32;
pub const ANY_PUBLISHER: PublisherId = 0;
pub const MAX_PUBLISHERS_PER_TOPIC: u32 = 4;

/// Defines meta information for a topic
pub trait TopicMeta {
    /// The concrete message type published on a topic
    type MsgType;
    /// A string topic label
    const TOPIC: &'static str;
}

/// Permission token for a publisher to publish messages of a given type (on a given topic)
#[derive(Copy, Clone, Hash, Eq, PartialEq, PartialOrd)]
pub struct Advertisement<M>
where
    M: TopicMeta + Copy,
{
    /// The unique publisher id for this topic
    pub advertiser_id: PublisherId,
    /// This just marks the msg type of the advertisement
    meta: PhantomData<*const M>,
}

impl<M> Advertisement<M>
where
    M: TopicMeta + Copy,
{
    fn new(instance: PublisherId) -> Self {
        Self {
            advertiser_id: instance,
            meta: PhantomData,
        }
    }
}

/// Permission token for a subscriber to read messages from a particular
/// topic and publisher.
pub struct Subscription<M>
where
    M: TopicMeta + Copy,
{
    pub(crate) advert: Advertisement<M>,
    pub(crate) read_token: ReadToken,
}

/// The default number of messages stored in a publisher's queue
type DefaultQueueSize = generic_array::typenum::U16;

/// One message Broker per topic-constrained type:
/// Each Broker provides a map from topic to message queue
pub struct Broker<M>
where
    M: TopicMeta + Default + Copy,
{
    advertiser_count: AtomicU32,
    topic_queues:
        [SpmsRing<<M as TopicMeta>::MsgType, DefaultQueueSize>; MAX_PUBLISHERS_PER_TOPIC as usize],
}

impl<M> Broker<M>
where
    M: TopicMeta + Default + Copy,
    <M as TopicMeta>::MsgType: Default + Copy + Debug,
{
    pub fn new() -> Self {
        Self {
            advertiser_count: AtomicU32::new(0),
            topic_queues: array![SpmsRing::default(); MAX_PUBLISHERS_PER_TOPIC as usize],
        }
    }

    /// Subscribe to a topic (and specific instance if desired)
    /// Use `ANY_PUBLISHER` to subscribe to the first available publisher
    pub fn subscribe(&mut self, instance: PublisherId) -> Option<Subscription<M>> {
        if instance != ANY_PUBLISHER && instance >= self.advertiser_count.load(Ordering::SeqCst) {
            //requested a specific advertiser that doesn't exist
            return None;
        }

        let advert = Advertisement::new(instance);

        Some(Subscription {
            advert,
            read_token: Default::default(),
        })
    }

    /// Advertise on a topic.  This provides a publisher a route to publication
    pub fn advertise(&mut self) -> Option<Advertisement<M>> {
        //optimistically try to advertise this publisher
        let publisher_id = self.advertiser_count.fetch_add(1, Ordering::SeqCst);
        if publisher_id < MAX_PUBLISHERS_PER_TOPIC {
            let advert = Advertisement::new(publisher_id);
            Some(advert)
        } else {
            // exceeded number of allowed publishers on this topic
            self.advertiser_count.fetch_sub(1, Ordering::SeqCst);
            None
        }
    }

    /// Publish a new message on the topic
    pub fn publish(&mut self, advert: &Advertisement<M>, msg: &M::MsgType) {
        if advert.advertiser_id < self.advertiser_count.load(Ordering::Relaxed) {
            self.topic_queues[advert.advertiser_id as usize].publish(msg)
        }
    }

    /// Has this publisher already advertised?
    /// This merely tells the caller whether we have n >= instance
    /// number of registered (advertised) publishers.
    pub fn pub_registered(&self, instance: PublisherId) -> bool {
        instance < self.advertiser_count.load(Ordering::Relaxed)
    }

    /// How many publishers have advertised on this topic?
    pub fn group_count(&self) -> u32 {
        self.advertiser_count.load(Ordering::Relaxed)
    }

    /// Read the next message from the topic
    /// Returns `nb::Error::WouldBlock` if the read would block
    pub fn poll(&self, sub: &mut Subscription<M>) -> nb::Result<M::MsgType, ()> {
        if sub.advert.advertiser_id < self.advertiser_count.load(Ordering::Relaxed) {
            return self.topic_queues[sub.advert.advertiser_id as usize]
                .read_next(&mut sub.read_token);
        }
        Err(nb::Error::Other(()))
    }
}

#[cfg(test)]
mod tests {
    use super::{Broker, TopicMeta, ANY_PUBLISHER};

    #[derive(Default, Copy, Clone, Debug)]
    struct Point {
        x: u32,
        y: u32,
    }

    #[derive(Default, Copy, Clone, Debug)]
    struct RoverLocation {}
    impl TopicMeta for RoverLocation {
        type MsgType = Point;
        const TOPIC: &'static str = "rover";
    }

    #[derive(Default, Copy, Clone, Debug)]
    struct HomeLocation {}
    impl TopicMeta for HomeLocation {
        type MsgType = Point;
        const TOPIC: &'static str = "home";
    }

    #[test]
    fn setup_pubsub() {
        let mut broker: Broker<HomeLocation> = Broker::new();
        let adv = broker.advertise().unwrap();
        assert!(broker.pub_registered(0));
        let mut sb = broker.subscribe(ANY_PUBLISHER).unwrap();

        for i in 0..5 {
            let msg = Point { x: i, y: i };
            broker.publish(&adv, &msg);
            let next_msg_r = broker.poll(&mut sb);
            assert!(next_msg_r.is_ok());

            let next_msg = next_msg_r.unwrap();
            //println!("next_msg: {:?}", next_msg);
            assert_eq!(next_msg.x, i);
            assert_eq!(next_msg.y, i);
        }
    }

    #[test]
    fn two_queues_same_inner_type_diff_topics() {
        let mut broker1: Broker<HomeLocation> = Broker::new();
        let adv1 = broker1.advertise().unwrap();
        let mut broker2: Broker<RoverLocation> = Broker::new();
        let mut sb2 = broker2.subscribe(ANY_PUBLISHER).unwrap();

        for i in 0..5 {
            let msg = Point { x: i, y: i };
            broker1.publish(&adv1, &msg);
            // we publish on queue1, no data on queue2 yet
            let next_msg_r = broker2.poll(&mut sb2);
            assert!(next_msg_r.is_err());
        }
    }

    #[test]
    fn two_queues_same_topic() {
        let mut broker: Broker<HomeLocation> = Broker::new();
        let adv1 = broker.advertise().unwrap();
        let adv2 = broker.advertise().unwrap();
        assert!(broker.pub_registered(0));
        assert!(broker.pub_registered(1));

        //TODO s/b Some/None Advertiser?
        let mut sb2 = broker.subscribe(adv2.advertiser_id).unwrap();

        for i in 0..5 {
            let msg = Point { x: i, y: i };
            broker.publish(&adv1, &msg);
            // we publish on queue1, no data on queue2 yet
            let next_msg_r = broker.poll(&mut sb2);
            assert!(next_msg_r.is_err());
            broker.publish(&adv2, &msg);
            let next_msg_r = broker.poll(&mut sb2);
            assert!(next_msg_r.is_ok());
        }
    }
}