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
extern crate lossyq;
extern crate parking_lot;
extern crate time;
extern crate libc;

pub mod scheduler;
pub mod elem;

use lossyq::spsc::Receiver;
use std::fmt;
use std::sync::atomic::{AtomicUsize, Ordering};

#[derive(Copy,Clone,Debug)]
pub enum Error {
  Busy,
  NonExistent,
  Stopping,
  AlreadyExists,
}

#[derive(Copy,Clone,Debug)]
pub enum Message<T: Send>
{
  Empty,                      //
  Value(T),                   //
  Ack(usize,usize),           // from-to
  Error(usize,&'static str),  // error at
}

#[derive(Clone,Debug)]
pub struct ChannelId {
  task_name  : String,
  id         : usize,
}

#[derive(Copy,Clone,Debug)]
pub enum Schedule {
  Loop,
  OnMessage(usize, usize), // channel id, msg id
  DelayUSec(u64),
  OnExternalEvent,
  Stop,
}

#[derive(Copy,Clone,Debug,PartialEq)]
pub enum TaskState {
  Execute,
  TimeWait(usize),
  MessageWait(usize, usize), // ch_id, msg_id
  ExtEventWait(usize),
  Stop,
}

#[derive(Copy,Clone,Debug)]
pub enum Event {
  User(Schedule),
  Execute,
  TimerExpired,
  MessageArrived,
  ExtTrigger,
  Delay,
}

#[derive(Copy,Clone,Debug)]
pub struct EvalInfo<'a> {
  task_id: usize,
  name: &'a String,
  at_usec: usize,
  eval_id: usize,
}

impl <'a> EvalInfo<'a> {
  pub fn new(task_id: usize, name: &'a String, at_usec: &'a AtomicUsize, eval_id: usize) -> EvalInfo<'a> {
    EvalInfo{
      task_id:   task_id,
      name:      name,
      at_usec:   at_usec.load(Ordering::Acquire),
      eval_id:   eval_id
    }
  }
  pub fn new_with_usec(task_id: usize, name: &'a String, at_usec: usize, eval_id: usize) -> EvalInfo<'a> {
    EvalInfo{
      task_id:   task_id,
      name:      name,
      at_usec:   at_usec,
      eval_id:   eval_id
    }
  }
  pub fn update_at(&mut self, at_usec: &AtomicUsize) {
    self.at_usec = at_usec.load(Ordering::Acquire);
  }
  pub fn get_usec(&self) -> usize {
    self.at_usec
  }
}

pub trait Observer {
  fn eval_started(&mut self, info: &EvalInfo);
  fn executed(&mut self, info: &EvalInfo);
  fn message_sent(&mut self, channel_id: usize, last_msg_id: usize, info: &EvalInfo);
  fn transition(&mut self, from: &TaskState, event: &Event, to: &TaskState, info: &EvalInfo);
  fn eval_finished(&mut self, info: &EvalInfo);
}

pub trait Task {
  fn execute(&mut self) -> Schedule;
  fn name(&self)  -> &String;
  fn input_count(&self) -> usize;
  fn output_count(&self) -> usize;
  fn input_id(&self, ch_id: usize) -> Option<ChannelId>;

  fn output_id(&self, ch_id: usize) -> Option<ChannelId> {
    if ch_id >= self.output_count() {
      None
    } else {
      Some( new_id(self.name().clone(), ch_id))
    }
  }
  fn tx_count(&self, ch_id: usize) -> usize;
}

pub struct IdentifiedReceiver<Input: Send> {
  pub id    : ChannelId,
  pub input : Receiver<Message<Input>>,
}

impl fmt::Display for ChannelId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Id:({} {})", self.task_name, self.id)
    }
}

pub fn new_id(name: String, id: usize) -> ChannelId {
  ChannelId {
    task_name  : name,
    id         : id,
  }
}


#[cfg(test)]
pub mod tests;