use std::collections::HashSet;
use std::rc::Rc;
use std::sync::mpsc::Sender;
use std::io;
use core::{EndpointId, Message};
use core::socket::{Protocol, Reply};
use core::config::ConfigOption;
use core::endpoint::Pipe;
use core::context::Context;
use super::priolist::Priolist;
use super::pipes::PipeCollection;
use super::{Timeout, PUB, SUB};
use super::policy::fair_queue;
use io_error::*;
pub struct Sub {
inner: Inner,
state: Option<State>
}
enum State {
Idle,
Receiving(EndpointId, Timeout),
RecvOnHold(Timeout)
}
struct Inner {
reply_tx: Sender<Reply>,
pipes: PipeCollection,
fq: Priolist,
subscriptions: HashSet<Vec<u8>>
}
impl Sub {
fn apply<F>(&mut self, ctx: &mut Context, transition: F) where F : FnOnce(State, &mut Context, &mut Inner) -> State {
if let Some(old_state) = self.state.take() {
#[cfg(debug_assertions)] let old_name = old_state.name();
let was_recv_ready = self.is_recv_ready();
let new_state = transition(old_state, ctx, &mut self.inner);
let is_recv_ready = self.is_recv_ready();
#[cfg(debug_assertions)] let new_name = new_state.name();
self.state = Some(new_state);
ctx.check_recv_ready_change(was_recv_ready, is_recv_ready);
#[cfg(debug_assertions)] debug!("[{:?}] switch from {} to {}", ctx, old_name, new_name);
}
}
}
impl From<Sender<Reply>> for Sub {
fn from(tx: Sender<Reply>) -> Sub {
Sub {
inner: Inner {
reply_tx: tx,
pipes: PipeCollection::new(),
fq: Priolist::new(),
subscriptions: HashSet::new()
},
state: Some(State::Idle)
}
}
}
impl Protocol for Sub {
fn id(&self) -> u16 { SUB }
fn peer_id(&self) -> u16 { PUB }
fn add_pipe(&mut self, _: &mut Context, eid: EndpointId, pipe: Pipe) {
self.inner.add_pipe(eid, pipe)
}
fn remove_pipe(&mut self, ctx: &mut Context, eid: EndpointId) -> Option<Pipe> {
let was_recv_ready = self.inner.is_recv_ready();
let pipe = self.inner.remove_pipe(eid);
let is_recv_ready = self.inner.is_recv_ready();
ctx.check_recv_ready_change(was_recv_ready, is_recv_ready);
if pipe.is_some() {
self.apply(ctx, |s, ctx, inner| s.on_pipe_removed(ctx, inner, eid));
}
pipe
}
fn send(&mut self, ctx: &mut Context, msg: Message, timeout: Timeout) {
self.apply(ctx, |s, ctx, inner| s.send(ctx, inner, Rc::new(msg), timeout))
}
fn on_send_ack(&mut self, ctx: &mut Context, eid: EndpointId) {
self.apply(ctx, |s, ctx, inner| s.on_send_ack(ctx, inner, eid))
}
fn on_send_timeout(&mut self, ctx: &mut Context) {
self.apply(ctx, |s, ctx, inner| s.on_send_timeout(ctx, inner))
}
fn on_send_ready(&mut self, ctx: &mut Context, eid: EndpointId) {
self.apply(ctx, |s, ctx, inner| s.on_send_ready(ctx, inner, eid))
}
fn on_send_not_ready(&mut self, ctx: &mut Context, eid: EndpointId) {
self.apply(ctx, |s, ctx, inner| s.on_send_not_ready(ctx, inner, eid))
}
fn recv(&mut self, ctx: &mut Context, timeout: Timeout) {
self.apply(ctx, |s, ctx, inner| s.recv(ctx, inner, timeout))
}
fn on_recv_ack(&mut self, ctx: &mut Context, eid: EndpointId, msg: Message) {
self.apply(ctx, |s, ctx, inner| s.on_recv_ack(ctx, inner, eid, msg))
}
fn on_recv_timeout(&mut self, ctx: &mut Context) {
self.apply(ctx, |s, ctx, inner| s.on_recv_timeout(ctx, inner))
}
fn on_recv_ready(&mut self, ctx: &mut Context, eid: EndpointId) {
self.apply(ctx, |s, ctx, inner| s.on_recv_ready(ctx, inner, eid))
}
fn on_recv_not_ready(&mut self, ctx: &mut Context, eid: EndpointId) {
self.apply(ctx, |s, ctx, inner| s.on_recv_not_ready(ctx, inner, eid))
}
fn is_send_ready(&self) -> bool {
false
}
fn is_recv_ready(&self) -> bool {
self.inner.is_recv_ready()
}
fn set_option(&mut self, opt: ConfigOption) -> io::Result<()> {
match opt {
ConfigOption::Subscribe(x) => Ok(self.inner.subscribe(x)),
ConfigOption::Unsubscribe(x) => Ok(self.inner.unsubscribe(x)),
_ => Err(invalid_input_io_error("option not supported"))
}
}
fn close(&mut self, ctx: &mut Context) {
self.inner.close(ctx)
}
}
impl State {
#[cfg(debug_assertions)]
fn name(&self) -> &'static str {
match *self {
State::Idle => "Idle",
State::Receiving(_, _) => "Receiving",
State::RecvOnHold(_) => "RecvOnHold"
}
}
fn on_pipe_removed(self, ctx: &mut Context, inner: &mut Inner, eid: EndpointId) -> State {
match self {
State::Receiving(id, timeout) => {
if id == eid {
State::Idle.recv(ctx, inner, timeout)
} else {
State::Receiving(id, timeout)
}
},
any => any
}
}
fn send(self, ctx: &mut Context, inner: &mut Inner, _: Rc<Message>, timeout: Timeout) -> State {
inner.send(ctx, timeout);
self
}
fn on_send_ack(self, _: &mut Context, _: &mut Inner, _: EndpointId) -> State {
self
}
fn on_send_timeout(self, _: &mut Context, _: &mut Inner) -> State {
self
}
fn on_send_ready(self, _: &mut Context, _: &mut Inner, _: EndpointId) -> State {
self
}
fn on_send_not_ready(self, _: &mut Context, _: &mut Inner, _: EndpointId) -> State {
self
}
fn recv(self, ctx: &mut Context, inner: &mut Inner, timeout: Timeout) -> State {
inner.recv(ctx).map_or_else(
| | State::RecvOnHold(timeout),
|eid| State::Receiving(eid, timeout))
}
fn on_recv_ack(self, ctx: &mut Context, inner: &mut Inner, eid: EndpointId, msg: Message) -> State {
match self {
State::Receiving(id, timeout) => {
if id == eid {
if inner.accept(&msg) {
inner.on_recv_ack(ctx, timeout, msg);
State::Idle
} else {
State::Idle.recv(ctx, inner, timeout)
}
} else {
State::Receiving(id, timeout)
}
},
any => any
}
}
fn on_recv_timeout(self, _: &mut Context, inner: &mut Inner) -> State {
inner.on_recv_timeout();
State::Idle
}
fn on_recv_ready(self, ctx: &mut Context, inner: &mut Inner, eid: EndpointId) -> State {
inner.on_recv_ready(eid);
match self {
State::RecvOnHold(timeout) => State::Idle.recv(ctx, inner, timeout),
any => any
}
}
fn on_recv_not_ready(self, _: &mut Context, inner: &mut Inner, eid: EndpointId) -> State {
inner.on_recv_not_ready(eid);
self
}
}
impl Inner {
fn add_pipe(&mut self, eid: EndpointId, pipe: Pipe) {
self.fq.insert(eid, pipe.get_recv_priority());
self.pipes.insert(eid, pipe);
}
fn remove_pipe(&mut self, eid: EndpointId) -> Option<Pipe> {
self.fq.remove(&eid);
self.pipes.remove(&eid)
}
fn send(&mut self, ctx: &mut Context, timeout: Timeout) {
let error = other_io_error("Send is not supported by pull protocol");
let _ = self.reply_tx.send(Reply::Err(error));
if let Some(sched) = timeout {
ctx.cancel(sched);
}
}
fn recv(&mut self, ctx: &mut Context) -> Option<EndpointId> {
fair_queue::recv(&mut self.fq, &mut self.pipes, ctx)
}
fn on_recv_ready(&mut self, eid: EndpointId) {
self.fq.activate(&eid)
}
fn on_recv_not_ready(&mut self, eid: EndpointId) {
self.fq.deactivate(&eid)
}
fn on_recv_ack(&self, ctx: &mut Context, timeout: Timeout, msg: Message) {
let _ = self.reply_tx.send(Reply::Recv(msg));
if let Some(sched) = timeout {
ctx.cancel(sched);
}
}
fn on_recv_timeout(&self) {
let error = timedout_io_error("Recv timed out");
let _ = self.reply_tx.send(Reply::Err(error));
}
fn is_recv_ready(&self) -> bool {
self.fq.peek()
}
fn subscribe(&mut self, subscription :String) {
self.subscriptions.insert(subscription.into_bytes());
}
fn unsubscribe(&mut self, subscription :String) {
self.subscriptions.remove(&subscription.into_bytes());
}
fn accept(&self, msg: &Message) -> bool {
let payload = msg.get_body();
self.subscriptions.iter().any(|s| payload.starts_with(s))
}
fn close(&mut self, ctx: &mut Context) {
self.pipes.close_all(ctx)
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::mpsc;
use core::{EndpointId, Message, Scheduled};
use core::socket::{Protocol, Reply};
use core::context::{Event};
use core::config::ConfigOption;
use core::tests::*;
use super::*;
#[test]
fn when_recv_succeed_it_is_notified_and_timeout_is_cancelled() {
let (tx, rx) = mpsc::channel();
let mut sub = Sub::from(tx);
let _ = sub.set_option(ConfigOption::Subscribe(String::from("")));
let ctx_sensor = Rc::new(RefCell::new(TestContextSensor::default()));
let mut ctx = TestContext::with_sensor(ctx_sensor.clone());
let eid = EndpointId::from(0);
let pipe = new_test_pipe(eid);
sub.add_pipe(&mut ctx, eid, pipe);
sub.on_recv_ready(&mut ctx, eid);
let msg = Message::new();
let timeout = Scheduled::from(1);
sub.recv(&mut ctx, Some(timeout));
sub.on_recv_ack(&mut ctx, eid, msg);
let reply = rx.recv().expect("facade should have been sent a reply !");
let is_reply_ok = match reply {
Reply::Recv(_) => true,
_ => false
};
assert!(is_reply_ok);
let sensor = ctx_sensor.borrow();
sensor.assert_one_recv_from(eid);
sensor.assert_one_cancellation(timeout);
}
#[test]
fn when_recv_starts_event_is_raised() {
let (tx, _) = mpsc::channel();
let mut sub = Sub::from(tx);
let _ = sub.set_option(ConfigOption::Subscribe(String::from("")));
let ctx_sensor = Rc::new(RefCell::new(TestContextSensor::default()));
let mut ctx = TestContext::with_sensor(ctx_sensor.clone());
let eid = EndpointId::from(1);
let pipe = new_test_pipe(eid);
sub.add_pipe(&mut ctx, eid, pipe);
sub.on_recv_ready(&mut ctx, eid);
sub.recv(&mut ctx, None);
sub.on_recv_ack(&mut ctx, eid, Message::new());
sub.on_recv_ready(&mut ctx, eid);
let sensor = ctx_sensor.borrow();
let raised_evts = sensor.get_raised_events();
assert_eq!(3, raised_evts.len());
assert_eq!(Event::CanRecv(true), raised_evts[0]);
assert_eq!(Event::CanRecv(false), raised_evts[1]);
assert_eq!(Event::CanRecv(true), raised_evts[2]);
}
}