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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! erlang_port helps make writing Erlang/Elixir ports in Rust easier.
//!
//! Makes use of the `serde_eetf` crate to serialize/deserialize rust datatypes
//! into erlang external term format, suitable for passing to/receiving from
//! `binary_to_term`/`term_to_binary`
//!
//! Assuming you are starting your port in packet mode, it's recommended that
//! you use the `stdio` or `nouse_stdio` functions inside the `main` fuction of
//! your application to create an `IOPort`. You can then use the `sender` and
//! `receiver` properties on that `IOPort` to communicate with Erlang/Elixir.
//!
//! For example, if you create the following rust program that reads strings
//! from a port and returns them uppercased:
//!
//! ```rust,no_run
//! fn lower(mut s: String) -> Result<String, String> {
//!     s.make_ascii_uppercase();
//!     Ok(s)
//! }
//!
//! fn main() {
//!    use erlang_port::{PortReceive, PortSend};
//!
//!   let mut port = unsafe {
//!       use erlang_port::PacketSize;
//!       erlang_port::nouse_stdio(PacketSize::Four)
//!   };
//!
//!   for string_in in port.receiver.iter() {
//!       let result = lower(string_in);
//!
//!       port.sender.reply(result);
//!   }
//! }
//! ```
//!
//! Then you can call into this port from Elixir:
//!
//! ```elixir
//! iex> port =
//! ...>   Port.open({:spawn_executable, port_path}, [
//! ...>       {:packet, 4},
//! ...>       :nouse_stdio,
//! ...>       :binary,
//! ...>       :exit_status
//! ...>   ])
//! #Port<0.1444>
//! iex> Port.command(port, :erlang.term_to_binary("hello"))
//! true
//! iex> receive do
//! ...>   {^port, {:data, binary}} ->
//! ...>     IO.puts(:erlang.binary_to_term(binary))
//! ...> end
//! "HELLO"
//! :ok
//! ```
//!
//! If you wish to implement a line-based port or a custom port protocol (using
//! the :stream option) you can do so by implementing the
//! `PortSend`/`PortReceive` traits.

extern crate byteorder;
extern crate serde;
extern crate serde_eetf;

#[macro_use]
extern crate serde_derive;

use byteorder::BigEndian;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::io::{Read, Write};
use std::marker::PhantomData;

/// Creates an IOPort using stdin & stdout with the specified packet size.
///
/// This should be used with ports that have been opened in Erlang/Elixir with
/// the `:use_stdio` and `{:packet, N}` options, where N matches packet_size.
///
/// Note: at present we don't do any locking of the underlying stdin or stdout
/// streams, so you should only call this once in your port. This may be changed
/// in the future...
pub fn stdio(
    packet_size: PacketSize,
) -> IOPort<PacketReceiver<std::io::Stdin>, PacketSender<std::io::Stdout>> {
    IOPort {
        receiver: PacketReceiver::from_reader(std::io::stdin(), packet_size),
        sender: PacketSender::from_writer(std::io::stdout(), packet_size),
    }
}

/// Creates an IOPort using file descriptors 3 and 4 with the specified packet size.
///
/// This allows the port application to continue to use stdout for logging/other
/// output.
///
/// This should be used with ports that have been opened in Erlang/Elixir with
/// the `:nouse_stdio` and `{:packet, N}` options, where N matches packet_size.
///
/// This function is unsafe, and if you call this function for a port that was
/// not created with nouse_stdio then it will panic.
pub unsafe fn nouse_stdio(
    packet_size: PacketSize,
) -> IOPort<PacketReceiver<std::fs::File>, PacketSender<std::fs::File>> {
    use std::fs::File;
    use std::os::unix::io::FromRawFd;

    IOPort {
        receiver: PacketReceiver::from_reader(File::from_raw_fd(3), packet_size),
        sender: PacketSender::from_writer(File::from_raw_fd(4), packet_size),
    }
}

/// `PortReceive` allows an app to receive messages through an Erlang port.
pub trait PortReceive {
    /// Receives a single message over the port.
    ///
    /// If there are no more messages returns None. If there's a problem reading
    /// the message it will panic.
    ///
    /// A `MessageDeserialize` implementation shold exist for the message we are
    /// reading. We provide a `serde_eetf` based default implementation for any
    /// type implementing serdes `DeserializeOwned`.
    fn receive<T>(&mut self) -> Option<T>
    where
        T: MessageDeserialize;

    /// Creates an Iterator over a series of messages read from the port.
    fn iter<'a, T>(&'a mut self) -> MessageIterator<'a, Self, T>
    where
        Self: Sized,
    {
        MessageIterator::from_receiver(self)
    }
}

/// `PortSend` allows an application to send messages through an Erlang port.
pub trait PortSend {
    /// Sends a single message over the port.
    ///
    /// A `MessageSerialize` implementation shold exist for the message we are
    /// reading. We provide a `serde_eetf` based default implementation for any
    /// type implementing serdes `Serialize`.
    ///
    /// In Erlang "Let it Crash" style, if we fail to encode the command or write it
    /// to a stream we will panic. Since this library is intended to be used as part
    /// of an Erlang system this should be picked up by the BEAM VM which can
    /// restart the Port.
    ///
    /// It's possible that panicing is _not_ what you want, for example if you have
    /// a Port that is handling multiple commands concurrently. Feel free to make a
    /// PR to better support your use case if that is the case.
    fn send<T>(&mut self, data: T)
    where
        T: MessageSerialize;

    /// Sends an `{:ok, T}` or `{:error, E}` over the port.
    ///
    /// This can be used to reply to a command that was received in the port, or to
    /// send arbitrary commands to the Port inside the BEAM.
    ///
    /// Applications can pass any data type they want into `reply`, provided there's
    /// a definition of `Into<ErlResult<E, T>>` for that type. A default
    /// implementation is provided for `Result<E, T>`.
    ///
    /// Note that both E and T must implement `Serialize` rather than
    /// `MessageSerialize` as is the case for `send`.
    ///
    /// If you wish to return a custom type you can implement `Into<ErlResult<T,
    /// E>>` for that type, or use the `send` function instead to send a custom
    /// type.
    ///
    /// At the moment this function is just a simple wrapper around `send` but that
    /// may change in the future.
    ///
    /// In Erlang "Let it Crash" style, if we fail to encode the command or write it
    /// to a stream we will panic. Since this library is intended to be used as part
    /// of an Erlang system this should be picked up by the BEAM VM which can
    /// restart the Port.
    ///
    /// It's possible that panicing is _not_ what you want, for example if you have
    /// a Port that is handling multiple commands concurrently. Feel free to make a
    /// PR to better support your use case if that is the case.
    fn reply<R, T, E>(&mut self, response: R)
    where
        R: Into<ErlResult<T, E>>,
        T: Serialize,
        E: Serialize,
    {
        self.send::<ErlResult<T, E>>(response.into());
    }
}

/// The size of a packet as sent/received by a PacketReceiver or PacketSender
///
/// You should pick the PacketSize that corresponds to the `{:packet, N}` option
/// you are opening the port with in Erlang/Elixir.
#[derive(Clone, Copy)]
pub enum PacketSize {
    One,
    Two,
    Four,
}

/// A receiver for ports opened in Packet mode.
///
/// If a port is opened with the `{:packet, N}` option then this receiver can
/// be used with the `packet_size` set to `N`
pub struct PacketReceiver<R>
where
    R: Read,
{
    reader: R,
    packet_size: PacketSize,
}

impl<R> PacketReceiver<R>
where
    R: Read,
{
    pub fn from_reader(reader: R, packet_size: PacketSize) -> Self {
        PacketReceiver {
            reader: reader,
            packet_size: packet_size,
        }
    }

    fn read_size(&mut self) -> Result<usize, std::io::Error> {
        use byteorder::ReadBytesExt;

        match self.packet_size {
            PacketSize::One => self.reader.read_u8().map(|n| n as usize),
            PacketSize::Two => self.reader.read_u16::<BigEndian>().map(|n| n as usize),
            PacketSize::Four => self.reader.read_u32::<BigEndian>().map(|n| n as usize),
        }
    }
}

impl<R> PortReceive for PacketReceiver<R>
where
    R: Read,
{
    fn receive<T>(&mut self) -> Option<T>
    where
        T: MessageDeserialize,
    {
        match self.read_size() {
            Ok(packet_size) => {
                let mut buf = vec![0; packet_size];
                self.reader
                    .read_exact(&mut buf)
                    .expect("Couldn't read full packet of data");
                Some(MessageDeserialize::deserialize_message(buf))
            }
            Err(err) => {
                if err.kind() == std::io::ErrorKind::UnexpectedEof {
                    return None;
                }
                panic!("IO when reading size {}", err);
            }
        }
    }
}

/// A sender for ports opened in Packet mode.
///
/// If a port is opened with the `{:packet, N}` option then this sender can
/// be used with the `packet_size` set to `N`
pub struct PacketSender<W>
where
    W: Write,
{
    writer: W,
    packet_size: PacketSize,
}

impl<W> PacketSender<W>
where
    W: Write,
{
    pub fn from_writer(writer: W, packet_size: PacketSize) -> Self {
        PacketSender {
            writer: writer,
            packet_size: packet_size,
        }
    }

    fn write_size(&mut self, size: usize) -> Result<(), std::io::Error> {
        use byteorder::WriteBytesExt;

        // TODO: Should probably verify size fits within packet_size here...

        match self.packet_size {
            PacketSize::One => self.writer.write_u8(size as u8),
            PacketSize::Two => self.writer.write_u16::<BigEndian>(size as u16),
            PacketSize::Four => self.writer.write_u32::<BigEndian>(size as u32),
        }
    }
}

impl<W> PortSend for PacketSender<W>
where
    W: Write,
{
    fn send<T>(&mut self, message: T)
    where
        T: MessageSerialize,
    {
        let data = message.serialize_message();

        self.write_size(data.len()).expect("write data size failed");
        self.writer.write_all(&data).expect("writing result failed");
        self.writer.flush().expect("flushing stdout failed");
    }
}

/// A wrapper around a receiver and a sender.
///
/// This struct does not implement PortSend or PortReceive itself, as you could
/// end up mutably borrowing the IOPort when calling `port.iter()` and then
/// you'd be unable to call `port.reply` inside your loop. Instead you should
/// call the functions on receiver and sender directly. This may be changed in a
/// future release if I figure out a way to do it nicely.
pub struct IOPort<R, S>
where
    R: PortReceive,
    S: PortSend,
{
    pub receiver: R,
    pub sender: S,
}

/// Iterator over messages from a PortReceive.
pub struct MessageIterator<'a, R: 'a, T>
where
    R: PortReceive,
{
    receiver: &'a mut R,
    phantom: PhantomData<T>,
}

impl<'a, R, T> MessageIterator<'a, R, T>
where
    R: PortReceive,
{
    pub fn from_receiver(receiver: &'a mut R) -> MessageIterator<'a, R, T> {
        MessageIterator {
            receiver: receiver,
            phantom: PhantomData,
        }
    }
}

impl<'a, R, T> Iterator for MessageIterator<'a, R, T>
where
    R: PortReceive,
    T: MessageDeserialize,
{
    type Item = T;

    fn next(&mut self) -> Option<T> {
        self.receiver.receive()
    }
}

/// Trait that parses some data from a Vec<u8>
///
/// This is used in the receive function to deserialize commands. A default
/// implementation is provided for anything implementing DeserializeOwned from
/// serde.
///
/// In Erlang "Let it Crash" style, if the data in `buffer` is malformed this
/// trait shoul panic. Since this library is intended to be used as part of an
/// Erlang system this should be picked up by the BEAM VM which can restart the
/// Port.
///
/// It's possible that panicing is _not_ what you want, for example if you have
/// a Port that is handling multiple commands concurrently. Feel free to make a
/// PR or raise an issue to better support your use case if that is the case.
pub trait MessageDeserialize {
    fn deserialize_message(buffer: Vec<u8>) -> Self;
}

impl<T> MessageDeserialize for T
where
    T: DeserializeOwned,
{
    fn deserialize_message(buffer: Vec<u8>) -> Self {
        serde_eetf::from_bytes(&buffer).expect("Deserialization Failed")
    }
}

/// Trait that serializes some data into a Vec<u8>
///
/// This is used in the send function to serialize commands. A default
/// implementation is provided for anything implementing Serialize from
/// serde.
///
/// In Erlang "Let it Crash" style, if we fail to serialize for whatever reason
/// trait shoul panic. Since this library is intended to be used as part of an
/// Erlang system this should be picked up by the BEAM VM which can restart the
/// Port.
///
/// It's possible that panicing is _not_ what you want, for example if you have
/// a Port that is handling multiple commands concurrently. Feel free to make a
/// PR or raise an issue to better support your use case if that is the case.
pub trait MessageSerialize {
    fn serialize_message(self) -> Vec<u8>;
}

impl<T> MessageSerialize for T
where
    T: Serialize,
{
    fn serialize_message(self: Self) -> Vec<u8> {
        serde_eetf::to_bytes(&self).expect("Serialization failed")
    }
}

/// A result enum for replying to commands from Erlang.
///
/// This will serialize into a standard erlang result tuple of either:
///
/// 1. `{:ok, result}` on Ok
/// 2. `{:error err}` on Error
///
/// All replies sent via `reply` are converted into this enum.
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub enum ErlResult<T, E> {
    Ok(T),
    Error(E),
}

impl<T, E> From<Result<T, E>> for ErlResult<T, E> {
    fn from(result: Result<T, E>) -> Self {
        match result {
            Ok(success) => ErlResult::Ok(success),
            Err(error) => ErlResult::Error(error),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use byteorder::WriteBytesExt;
    use serde_eetf;
    use std::io::Cursor;

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    struct TestCommand {
        int8: i8,
    }

    #[test]
    fn test_packet_receiver_iter() {
        let buff = serde_eetf::to_bytes(&TestCommand { int8: 100 }).unwrap();

        let mut size_buff = Vec::new();
        size_buff
            .write_u32::<BigEndian>(buff.len() as u32)
            .expect("write data size failed");
        size_buff.extend_from_slice(&buff);
        size_buff
            .write_u32::<BigEndian>(buff.len() as u32)
            .expect("write data size failed");
        size_buff.extend_from_slice(&buff);

        let results: Vec<TestCommand> =
            PacketReceiver::from_reader(&mut Cursor::new(size_buff), PacketSize::Four)
                .iter()
                .collect();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0], results[1]);
    }

    #[test]
    fn test_packet_sender_reply() {
        fn do_test(input: Result<i8, String>, expected_output: ErlResult<i8, String>) {
            let mut cursor = Cursor::new(vec![]);
            {
                let mut sender = PacketSender::from_writer(&mut cursor, PacketSize::Four);
                sender.reply(input);
            }
            cursor.set_position(0);

            let result: Vec<ErlResult<i8, String>> =
                PacketReceiver::from_reader(&mut cursor, PacketSize::Four)
                    .iter()
                    .collect();

            assert_eq!(result, [expected_output]);
        }

        do_test(Ok(1), ErlResult::Ok(1));
        do_test(
            Err("Nope".to_string()),
            ErlResult::Error("Nope".to_string()),
        );
    }

    #[test]
    fn test_packet_sender_send() {
        fn do_test(packet_size: PacketSize) {
            let input = TestCommand { int8: 127 };

            let mut cursor = Cursor::new(vec![]);
            {
                let mut sender = PacketSender::from_writer(&mut cursor, packet_size);
                sender.send(&input);
            }
            cursor.set_position(0);

            let result: Vec<TestCommand> = PacketReceiver::from_reader(&mut cursor, packet_size)
                .iter()
                .collect();

            assert_eq!(result, [input]);
        }

        do_test(PacketSize::One);
        do_test(PacketSize::Two);
        do_test(PacketSize::Four);
    }
}