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
//
// Copyright © 2009-2019 NeoBirth Developers, Reality Jockey, Ltd.
//
// This file is part of PureZen (a fork of ZenGarden)
//
// PureZen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// PureZen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with PureZen.  If not, see <http://www.gnu.org/licenses/>.
//

//! Ordered message queue
//!
//! ## Translation Notes
//!
//! //! This file was translated from the following ZenGarden C++ sources:
//!
//! - `message::OrderedQueue.cpp`
//! - `message::OrderedQueue.h`

use super::object::{self, connection};
use crate::{list::List, pd, Error};
use heapless::ArrayLength;

/// Message entries in the queue.
// TODO(tarcieri): refactor this when the overall translation is done
#[derive(Clone, Debug, PartialEq)]
pub struct MessageEntry<'pd, N: ArrayLength<pd::message::Atom<'pd>>> {
    /// Message to be delivered to the given connection index.
    ///
    /// Named `first` for historical reasons: in Zg this was modeled as a
    /// C++ `std::pair`
    // TODO(tarcieri): eliminate all usages of `first` and rename this field to `object_id`
    pub first: pd::Message<'pd, N>,

    /// Index into the connection array.
    ///
    /// Named `second` for historical reasons: in Zg this was modeled as a
    /// C++ `std::pair`
    // TODO(tarcieri): eliminate all usages of `second`
    pub second: connection::Index,
}

/// Object and `MessageEntry` in the message queue.
// TODO(tarcieri): refactor this when the overall translation is done
#[derive(Clone, Debug, PartialEq)]
pub struct ObjectMessageEntry<'pd, N: ArrayLength<pd::message::Atom<'pd>>> {
    /// Identifier for the target object.
    ///
    /// Named `first` for historical reasons: in Zg this was modeled as a
    /// C++ `std::pair`
    // TODO(tarcieri): eliminate all usages of `first` and rename this field to `object_id`
    pub first: object::Id,

    /// `MessageEntry` to be delivered to this object.
    ///
    /// Named `second` for historical reasons: in Zg this was modeled as a
    /// C++ `std::pair`
    // TODO(tarcieri): eliminate all usages of `second`
    pub second: MessageEntry<'pd, N>,
}

impl<'pd, N> ObjectMessageEntry<'pd, N>
where
    N: ArrayLength<pd::message::Atom<'pd>>,
{
    /// Create a new ObjectMessageEntry
    pub fn new(
        message_obj_id: object::Id,
        outlet_index: connection::Index,
        message: pd::Message<'pd, N>,
    ) -> Self {
        let message_entry: MessageEntry<'pd, N> = MessageEntry {
            first: message,
            second: outlet_index,
        };

        ObjectMessageEntry {
            first: message_obj_id,
            second: message_entry,
        }
    }
}

/// Ordered message queue
#[derive(Clone, Debug)]
pub struct OrderedQueue<'pd, N: ArrayLength<pd::message::Atom<'pd>>> {
    entries: List<ObjectMessageEntry<'pd, N>>,
}

impl<'pd, N> OrderedQueue<'pd, N>
where
    N: ArrayLength<pd::message::Atom<'pd>>,
{
    /// Create a new ordered message queue
    pub fn new() -> Self {
        OrderedQueue {
            entries: List::new(),
        }
    }

    /// Inserts the message into the ordered queue based on its scheduled time.
    pub fn insert_message(
        &mut self,
        message_obj_id: object::Id,
        outlet_index: connection::Index,
        message: pd::Message<'pd, N>,
    ) -> Result<(), Error> {
        let mut index = None;

        for (i, entry) in self.entries.iter().enumerate() {
            if message.get_timestamp() < entry.second.first.get_timestamp() {
                index = Some(i);
                break;
            }
        }

        let new_entry = ObjectMessageEntry::new(message_obj_id, outlet_index, message);

        // TODO(tarcieri): this is probably bogus, inefficient, and unnecessary
        if let Some(i) = index {
            self.entries.insert_at(i, new_entry)
        } else {
            self.entries.push_back(new_entry) // insert at end
        }
    }

    /// Removes the given message addressed to the given `message::Object` from the queue.
    pub fn remove_message(
        &mut self,
        message_obj_id: object::Id,
        outlet_index: connection::Index,
        message: &pd::Message<'pd, N>,
    ) {
        let mut index = None;

        for (i, entry) in self.entries.iter().enumerate() {
            if entry.first == message_obj_id
                && entry.second.first.eq(message)
                && entry.second.second == outlet_index
            {
                index = Some(i);
                break;
            }
        }

        if let Some(i) = index {
            self.entries.remove_at(i).unwrap();
        }
    }

    /// Peek at the first message in the queue
    pub fn peek(&self) -> Option<&ObjectMessageEntry<'pd, N>> {
        self.entries.peek()
    }

    /// Pop the first message off of the front of the queue
    // TODO(tarcieri): this is a bad name. We should use `Take` or something.
    pub fn pop(&mut self) -> Option<ObjectMessageEntry<'pd, N>> {
        self.entries.pop()
    }

    /// Is this message queue empty?
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

impl<'pd, N> Default for OrderedQueue<'pd, N>
where
    N: ArrayLength<pd::message::Atom<'pd>>,
{
    fn default() -> Self {
        Self::new()
    }
}