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
extern crate byteorder;
extern crate rand;

use std::io;

mod metadata;
pub use metadata::Metadata;

pub mod lt;
pub use lt::{LtClient, LtSource};

mod distributions;

// TODO: Make Data more generic
type Data = Vec<u8>;

pub trait Packet: Sized {
    fn from_bytes(bytes: Vec<u8>) -> io::Result<Self>;

    fn to_bytes(&self) -> io::Result<Vec<u8>>;
}

pub trait Encoder<P: Packet> {
    fn create_packet(&self) -> P;
}

pub trait PartialEncoder<P: Packet> {
    fn try_create_packet(&self) -> Option<P>;
}

impl<P: Packet> PartialEncoder<P> for Encoder<P> {
    fn try_create_packet(&self) -> Option<P> {
        Some(self.create_packet())
    }
}

pub trait Decoder<P: Packet> {
    fn receive_packet(&mut self, packet: P);

    fn decoding_progress(&self) -> f64;

    fn get_result(&self) -> Option<Data>;
}

pub trait Source<P: Packet> : Encoder<P> + Sized {
    fn new(metadata: Metadata, data: Data) -> Result<Self, CreationError>;
}

// TODO: Figure out if Clients should be generic over some sort of "parameter" type
pub trait Client<P: Packet> : Decoder<P> + PartialEncoder<P> + Sized {
    fn new(metadata: Metadata) -> Result<Self, CreationError>;
}

#[derive(Debug)]
pub enum CreationError {
    DataZeroBytes,
    DataTooBig,
    InvalidMetadata,
    RandomInitializationError(io::Error)
}