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
use std::sync::Arc;

use crossbeam::sync::Parker;

use crate::event::EventRead;
use crate::ring_buffer::{EventWrapper, RingBuffer};
use crate::sequence::Sequence;
use crate::WaitStrategy;

/// A handle to receive events that were subscribed to from the event-bus.
///
/// The [`Subscriber`] will not receive intended events that were published to the event-bus
/// before time of subscription. It will only receive intended events that are published after the
/// time of subscription, as they will have a higher sequence number than the Subscriber's internal
/// sequence value.
///
/// # Example
///
/// Basic usage:
///
/// ```ignore
/// let eventbus = Eventador::new(4)?;
///
/// // subscribe first, before publishing!
/// let subscriber = eventbus.subscribe::<usize>();
///
/// let mut i: usize = 1234;
/// eventbus.publish(i);
///
/// let mut msg = subscriber.recv();
/// assert_eq!(i, *msg);
/// ```
///
pub struct Subscriber<T> {
    ring: Arc<RingBuffer>,
    sequence: Arc<Sequence>,
    _marker: std::marker::PhantomData<T>,
}

impl<T: 'static> Subscriber<T>
where
    T: Send,
{
    pub(crate) fn new(ring: Arc<RingBuffer>, sequence: Arc<Sequence>) -> Self {
        Self {
            ring,
            sequence,
            _marker: std::marker::PhantomData,
        }
    }

    /// Get the current internal sequence number for the [`Subscriber`].
    ///
    /// This sequence number signifies what events the Subscriber may have already read, and any
    /// events with a sequence value higher than this are events that are still unread.
    pub fn sequence(&self) -> u64 {
        self.sequence.get()
    }

    pub(crate) fn read_event<'b>(&self, envelope: EventWrapper) -> Option<EventRead<'b, T>> {
        let event_opt: Option<EventRead<T>> = unsafe { envelope.read() };
        envelope.stop_waiting();

        self.sequence.increment();
        return event_opt;
    }

    /// Synchronously read an event of the correct type from the event-bus.
    ///
    /// # Example
    ///
    /// Basic usage:
    ///
    /// ```ignore
    /// let eventbus = Eventador::new(4)?;
    /// let subscriber = eventbus.subscribe::<usize>();
    ///
    /// let mut i: usize = 1234;
    /// eventbus.publish(i);
    ///
    /// let mut msg = subscriber.recv();
    /// assert_eq!(i, *msg);
    /// ```
    ///
    pub fn recv<'b>(&self) -> EventRead<'b, T> {
        loop {
            let sequence = self.sequence.get();

            let envelope = self
                .ring
                .get_envelope(sequence)
                .expect("ring buffer was not pre-populated with empty event envelopes")
                .clone();

            envelope.start_waiting();

            let envelope_sequence = envelope.sequence();
            if sequence == envelope_sequence {
                if let Some(event) = self.read_event(envelope) {
                    return event;
                }
            } else if sequence > envelope_sequence {
                let parker = Parker::new();
                envelope.add_subscriber(Box::new(parker.unparker().clone()));

                parker.park();

                if let Some(event) = self.read_event(envelope) {
                    return event;
                }
            } else {
                // Publisher has overwritten an event that has not been read yet
                match self.ring.wait_strategy() {
                    WaitStrategy::AllSubscribers => unreachable!(),

                    _ => {
                        self.sequence.set(envelope_sequence);
                    }
                }
            }
        }
    }
}