Files
@ 0a71d0af9edf
Branch filter:
Location: CSY/reowolf/src/common.rs
0a71d0af9edf
3.5 KiB
application/rls-services+xml
histories
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 | ///////////////////// PRELUDE /////////////////////
pub use core::{
cmp::Ordering,
fmt::{Debug, Formatter},
hash::{Hash, Hasher},
ops::{Range, RangeFrom},
time::Duration,
};
pub use indexmap::{IndexMap, IndexSet};
pub use maplit::{hashmap, hashset};
pub use mio::{
net::{TcpListener, TcpStream},
Event, Evented, Events, Poll, PollOpt, Ready, Token,
};
pub use std::{
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::TryInto,
net::SocketAddr,
sync::Arc,
time::Instant,
};
pub use Polarity::*;
///////////////////// DEFS /////////////////////
pub type Payload = Vec<u8>;
pub type ControllerId = u32;
pub type ChannelIndex = u32;
/// This is a unique identifier for a channel (i.e., port).
#[derive(Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd)]
pub struct ChannelId {
pub(crate) controller_id: ControllerId,
pub(crate) channel_index: ChannelIndex,
}
#[derive(Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd)]
pub enum Polarity {
Putter, // output port (from the perspective of the component)
Getter, // input port (from the perspective of the component)
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)]
#[repr(C)]
pub struct Port(pub usize); // ports are COPY
pub type Key = Port;
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum MainComponentErr {
NoSuchComponent,
NonPortTypeParameters,
CannotMovePort(Port),
WrongNumberOfParamaters { expected: usize },
UnknownPort(Port),
WrongPortPolarity { param_index: usize, port: Port },
DuplicateMovedPort(Port),
}
pub trait ProtocolDescription: Sized {
type S: ComponentState<D = Self>;
fn parse(pdl: &[u8]) -> Result<Self, String>;
fn component_polarities(&self, identifier: &[u8]) -> Result<Vec<Polarity>, MainComponentErr>;
fn new_main_component(&self, identifier: &[u8], ports: &[Key]) -> Self::S;
}
pub trait ComponentState: Sized + Clone {
type D: ProtocolDescription;
fn pre_sync_run<C: MonoContext<D = Self::D, S = Self>>(
&mut self,
runtime_ctx: &mut C,
protocol_description: &Self::D,
) -> MonoBlocker;
fn sync_run<C: PolyContext<D = Self::D>>(
&mut self,
runtime_ctx: &mut C,
protocol_description: &Self::D,
) -> PolyBlocker;
}
#[derive(Debug, Clone)]
pub enum MonoBlocker {
Inconsistent,
ComponentExit,
SyncBlockStart,
}
#[derive(Debug, Clone)]
pub enum PolyBlocker {
Inconsistent,
SyncBlockEnd,
CouldntReadMsg(Key),
CouldntCheckFiring(Key),
PutMsg(Key, Payload),
}
pub trait MonoContext {
type D: ProtocolDescription;
type S: ComponentState<D = Self::D>;
fn new_component(&mut self, moved_keys: HashSet<Key>, init_state: Self::S);
fn new_channel(&mut self) -> [Key; 2];
fn new_random(&mut self) -> u64;
}
pub trait PolyContext {
type D: ProtocolDescription;
fn is_firing(&mut self, ekey: Key) -> Option<bool>;
fn read_msg(&mut self, ekey: Key) -> Option<&Payload>;
}
///////////////////// IMPL /////////////////////
impl Debug for Port {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Port({})", self.0)
}
}
impl Key {
pub fn from_raw(raw: usize) -> Self {
Self(raw)
}
pub fn to_raw(self) -> usize {
self.0
}
pub fn to_token(self) -> mio::Token {
mio::Token(self.0.try_into().unwrap())
}
pub fn from_token(t: mio::Token) -> Self {
Self(t.0.try_into().unwrap())
}
}
|