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
use rand;
use std::sync::*;
use std::sync::mpsc::*;

use super::super::Event;

pub trait NoOp: Send {
    fn send_no_change(&mut self) -> bool;
    fn send_exit(&self);
}

pub trait RunInput: Send {
    fn run(mut self: Box<Self>, usize, Arc<Mutex<Vec<Box<NoOp>>>>);
    fn boxed_no_op(&self) -> Box<NoOp>;
}

pub struct ReceiverInput<A> {
    rx: Receiver<A>,
    tx: SyncSender<Event<A>>,
}

impl<A> ReceiverInput<A> {
    pub fn new(rx: Receiver<A>, tx: SyncSender<Event<A>>) -> ReceiverInput<A> {
        ReceiverInput {
            rx: rx,
            tx: tx,
        }
    }
}

impl<A> RunInput for ReceiverInput<A> where
    A: 'static + Send + Clone,
{
    fn boxed_no_op(&self) -> Box<NoOp> {
        Box::new(self.tx.clone())
    }

    fn run(self: Box<Self>, idx: usize, txs: Arc<Mutex<Vec<Box<NoOp>>>>) {
        debug!("SETUP: running ReceiverInput");
        let inner = *self;
        let ReceiverInput {rx, tx} = inner;

        loop {
            match rx.recv() {
                Ok(ref a) => {
                    info!("RUN: ReceiverInput received data, sending");
                    for (i, mut no_op_tx) in txs.lock().unwrap().iter_mut().enumerate() {
                        if i == idx {
                            match tx.send(Event::Changed(a.clone())) {
                                Err(_) => return,
                                _ => {},
                            }
                        } else {
                            if no_op_tx.send_no_change() { return }
                        }
                    }
                },
                Err(e) => {
                    info!("RUN: ReceiverInput sending error {}, exiting", e);
                    for no_op_tx in txs.lock().unwrap().iter() {
                        no_op_tx.send_exit();
                    }
                    return
                },
            }
        }
    }
}


impl<A> NoOp for SyncSender<Event<A>> where
A: Send
{
    fn send_no_change(&mut self) -> bool {
        info!("RUN: Sender sending Unchanged");
        match self.send(Event::Unchanged) {
            Err(_) => true,
            _ => false,
        }
    }

    fn send_exit(&self) {
        info!("RUN: Sender sending Exit");
        match self.send(Event::Exit) {
            _ => {}
        }
    }
}

#[derive(Clone)]
pub struct AckInput<A> where
A: Send + Clone,
{
    initial: A,
    tx: SyncSender<Event<A>>,
}

impl<A> AckInput<A> where 
    A: Send + Clone
{
    pub fn new(v: A, tx: SyncSender<Event<A>>) -> Self {
        AckInput { initial: v, tx: tx}
    }
}

impl<A> RunInput for AckInput<A> where
A: 'static + Send + Clone,
{
    fn run(self: Box<Self>, _: usize, _: Arc<Mutex<Vec<Box<NoOp>>>>) {
        // Nothing to do here - all the work is done on NoOp
    }

    fn boxed_no_op(&self) -> Box<NoOp> {
        Box::new(self.clone())
    }
}

impl<A> NoOp for AckInput<A> where
A: Send + Clone
{
    fn send_no_change(&mut self) -> bool {
        info!("RUN: Ack sending value");
        match self.tx.send(Event::Changed(self.initial.clone())) {
            Err(_) => true,
            _ => false,
        }
    }

    fn send_exit(&self) {
        info!("RUN: Ack sending Exit");
        match self.tx.send(Event::Exit) {
            _ => {}
        }
    }
}

#[derive(Clone)]
pub struct RngInput<R, A> where
R: rand::Rng + Clone + Send,
A: Send + Clone + rand::Rand,
{
    rng: R,
    tx: SyncSender<Event<A>>,
}

impl<R, A> RngInput<R, A> where 
R: rand::Rng + Clone + Send,
A: Send + Clone + rand::Rand,
{
    pub fn new(rng: R, tx: SyncSender<Event<A>>) -> Self {
        RngInput { rng: rng, tx: tx}
    }
}

impl<R, A> RunInput for RngInput<R, A> where
R: 'static + rand::Rng + Clone + Send,
A: 'static + Send + Clone + rand::Rand,
{
    fn run(self: Box<Self>, _: usize, _: Arc<Mutex<Vec<Box<NoOp>>>>) {
        // Nothing to do here - all the work is done on NoOp
    }

    fn boxed_no_op(&self) -> Box<NoOp> {
        Box::new(self.clone())
    }
}

impl<R, A> NoOp for RngInput<R, A> where
R: rand::Rng + Clone + Send,
A: Send + Clone + rand::Rand,
{
    fn send_no_change(&mut self) -> bool {
        info!("RUN: Rng sending value");
        let a = self.rng.gen();
        match self.tx.send(Event::Changed(a)) {
            Err(_) => true,
            _ => false,
        }
    }

    fn send_exit(&self) {
        info!("RUN: Rng sending Exit");
        match self.tx.send(Event::Exit) {
            _ => {}
        }
    }
}