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
//! Various channel implementations for different purposes.
//!
//! All of the channels within this module can be used to build up Join Patterns.
//! However, they serve different functions within the patterns. For instance,
//! a `RecvChannel` is used to get the value generated by a Join Pattern firing
//! asynchronously.

use std::marker::PhantomData;
use std::sync::mpsc::{channel, RecvError, SendError, Sender};
use std::{any::Any, marker::Send};

use super::types::{ids, Message, Packet};

/***************************
 * Sending Channel Structs *
 ***************************/

/// Asynchronous, message sending channel.
///
/// This channel type is characterized by the argument type of its `send` method.
/// It will only be able to send messages to the Junction but not recover values
/// generated by Join Patterns that have been fired.
///
/// Sending a message this channel will *not* block the current thread, but may
/// allow a Join Pattern that it is part of to fire.
#[derive(Clone)]
pub struct SendChannel<T> {
    id: ids::ChannelId,
    junction_id: ids::JunctionId,
    sender: Sender<Packet>,
    send_type: PhantomData<T>,
}

impl<T> SendChannel<T> {
    /// Return the channel's ID.
    pub(crate) fn id(&self) -> ids::ChannelId {
        self.id
    }

    /// Return the ID of the `Junction` this channel is associated to.
    pub(crate) fn junction_id(&self) -> ids::JunctionId {
        self.junction_id
    }

    /// Create a stripped down representation of this channel.
    pub(crate) fn strip(&self) -> StrippedSendChannel<T> {
        StrippedSendChannel::new(self.id)
    }
}

impl<T> SendChannel<T>
where
    T: Any + Send,
{
    pub(crate) fn new(
        id: ids::ChannelId,
        junction_id: ids::JunctionId,
        sender: Sender<Packet>,
    ) -> SendChannel<T> {
        SendChannel {
            id,
            junction_id,
            sender,
            send_type: PhantomData,
        }
    }

    pub fn send(&self, value: T) -> Result<(), SendError<Packet>> {
        self.sender.send(Packet::Message {
            channel_id: self.id,
            msg: Message::new(value),
        })
    }
}

/// Stripped down version of `SendChannel`.
///
/// The main purpose of this struct is to be used in the Join Pattern types to
/// increase readability and maintainability.
///
/// This version of the `SendChannel` does not carry the same functionality as
/// the actual `SendChannel`, however, it holds the bare minimum information
/// necessary for the creation of Join Patterns. Specifically, this channel
/// cannot send and does not know of its Junction, but is able to provide a
/// channel ID and type associated with it.
pub(crate) struct StrippedSendChannel<T> {
    id: ids::ChannelId,
    send_type: PhantomData<T>,
}

impl<T> StrippedSendChannel<T> {
    pub(crate) fn new(id: ids::ChannelId) -> StrippedSendChannel<T> {
        StrippedSendChannel {
            id,
            send_type: PhantomData,
        }
    }

    /// Return the channel's ID.
    pub(crate) fn id(&self) -> ids::ChannelId {
        self.id
    }
}

/*****************************
 * Receiving Channel Structs *
 *****************************/

/// Synchronous, value receiving channel.
///
/// This channel type is characterized by the return type of its `recv` method.
/// No messages can be sent through this channel, but the value generated by
/// running a Join Pattern
///
/// Sending a message on this channel *will* block the current thread until a Join
/// Pattern that this channel is part of has fired.
#[derive(Clone)]
pub struct RecvChannel<R> {
    id: ids::ChannelId,
    junction_id: ids::JunctionId,
    sender: Sender<Packet>,
    recv_type: PhantomData<R>,
}

impl<R> RecvChannel<R> {
    /// Return the channel's ID.
    pub(crate) fn id(&self) -> ids::ChannelId {
        self.id
    }

    /// Return the ID of the `Junction` this channel is associated to.
    pub(crate) fn junction_id(&self) -> ids::JunctionId {
        self.junction_id
    }

    /// Create a stripped down representation of this channel.
    pub(crate) fn strip(&self) -> StrippedRecvChannel<R> {
        StrippedRecvChannel::new(self.id)
    }
}

impl<R> RecvChannel<R>
where
    R: Any + Send,
{
    pub(crate) fn new(
        id: ids::ChannelId,
        junction_id: ids::JunctionId,
        sender: Sender<Packet>,
    ) -> RecvChannel<R> {
        RecvChannel {
            id,
            junction_id,
            sender,
            recv_type: PhantomData,
        }
    }

    /// Receive value generated by fired Join Pattern.
    ///
    /// # Panics
    ///
    /// Panics if it was not possible to send a return `Sender` to the Junction.
    pub fn recv(&self) -> Result<R, RecvError> {
        let (tx, rx) = channel::<R>();

        self.sender
            .send(Packet::Message {
                channel_id: self.id,
                msg: Message::new(tx),
            })
            .unwrap();

        rx.recv()
    }
}

/// Stripped down version of `RecvChannel`.
///
/// The main purpose of this struct is to be used in the Join Pattern types to
/// increase readability and maintainability.
///
/// This version of the `RecvChannel` does not carry the same functionality as
/// the actual `RecvChannel`, however, it holds the bare minimum information
/// necessary for the creation of Join Patterns. Specifically, this channel
/// cannot receive and does not know of its Junction, but is able to provide a
/// channel ID and type associated with it.
pub(crate) struct StrippedRecvChannel<R> {
    id: ids::ChannelId,
    recv_type: PhantomData<R>,
}

impl<R> StrippedRecvChannel<R> {
    pub(crate) fn new(id: ids::ChannelId) -> StrippedRecvChannel<R> {
        StrippedRecvChannel {
            id,
            recv_type: PhantomData,
        }
    }

    /// Return the channel's ID.
    pub(crate) fn id(&self) -> ids::ChannelId {
        self.id
    }
}

/*********************************
 * Bidirectional Channel Structs *
 *********************************/

/// Synchronous, bidirectional message channel.
///
/// This channel type is characterized by both the argument and return type of its
/// `send_recv` method. A message can be sent through this channel which will then
/// also cause the channel to wait for a Join Pattern involving this channel to fire.
///
/// The subtle difference between using this channel type over a combination of a
/// `SendChannel` and a `RecvChannel` is that this channel ensures that `Message`s
/// necessary to perform the sending and receiving happen *atomically* together. In
/// fact, only one `Message` is sent for both. Therefore, a call to `send_recv`
/// can be viewed as an atomic operation, whereas a separate `SendChannel::send`
/// and `RecvChannel::recv` may have an arbitrary amount of actions happen between
/// them.
///
/// Sending a message on this channel *will* block the current thread until a Join
/// Pattern that this channel is part of has fired.
#[derive(Clone)]
pub struct BidirChannel<T, R> {
    id: ids::ChannelId,
    junction_id: ids::JunctionId,
    sender: Sender<Packet>,
    send_type: PhantomData<T>,
    recv_type: PhantomData<R>,
}

impl<T, R> BidirChannel<T, R> {
    /// Return the channel's ID.
    pub(crate) fn id(&self) -> ids::ChannelId {
        self.id
    }

    /// Return the ID of the `Junction` this channel is associated to.
    pub(crate) fn junction_id(&self) -> ids::JunctionId {
        self.junction_id
    }

    /// Create a stripped down representation of this channel.
    pub(crate) fn strip(&self) -> StrippedBidirChannel<T, R> {
        StrippedBidirChannel::new(self.id)
    }
}

impl<T, R> BidirChannel<T, R>
where
    T: Any + Send,
    R: Any + Send,
{
    pub(crate) fn new(
        id: ids::ChannelId,
        junction_id: ids::JunctionId,
        sender: Sender<Packet>,
    ) -> BidirChannel<T, R> {
        BidirChannel {
            id,
            junction_id,
            sender,
            send_type: PhantomData,
            recv_type: PhantomData,
        }
    }

    /// Send a message and receive value generated by fired Junction.
    ///
    /// # Panics
    ///
    /// Panics if it was not possible to send the given message and return
    /// `Sender` to the Junction.
    pub fn send_recv(&self, msg: T) -> Result<R, RecvError> {
        let (tx, rx) = channel::<R>();

        self.sender
            .send(Packet::Message {
                channel_id: self.id,
                msg: Message::new((msg, tx)),
            })
            .unwrap();

        rx.recv()
    }
}

/// Stripped down version of `BidirChannel`.
///
/// The main purpose of this struct is to be used in the Join Pattern types to
/// increase readability and maintainability.
///
/// This version of the `BidirChannel` does not carry the same functionality as
/// the actual `BidirChannel`, however, it holds the bare minimum information
/// necessary for the creation of Join Patterns. Specifically, this channel
/// cannot send or receive and does not know of its Junction, but is able to
/// provide a channel ID and type associated with it.
pub(crate) struct StrippedBidirChannel<T, R> {
    id: ids::ChannelId,
    send_type: PhantomData<T>,
    recv_type: PhantomData<R>,
}

impl<T, R> StrippedBidirChannel<T, R> {
    pub(crate) fn new(id: ids::ChannelId) -> StrippedBidirChannel<T, R> {
        StrippedBidirChannel {
            id,
            send_type: PhantomData,
            recv_type: PhantomData,
        }
    }

    /// Return the channel's ID.
    pub(crate) fn id(&self) -> ids::ChannelId {
        self.id
    }
}