Files
@ cecf94fdb875
Branch filter:
Location: CSY/reowolf/src/common.rs
cecf94fdb875
3.8 KiB
application/rls-services+xml
simplified approach to the piecewise acquisition of port info. starting to reintegrate communication phase
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 | ///////////////////// PRELUDE /////////////////////
pub use crate::protocol::{ComponentState, ProtocolDescription};
pub use crate::runtime::{NonsyncProtoContext, SyncProtoContext};
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},
Events, Interest, Poll, Token,
};
pub use std::{
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::TryInto,
io::{Read, Write},
net::SocketAddr,
sync::Arc,
time::Instant,
};
pub use Polarity::*;
///////////////////// DEFS /////////////////////
pub type ControllerId = u32;
pub type PortSuffix = u32;
// globally unique
#[derive(
Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub struct PortId {
pub(crate) controller_id: ControllerId,
pub(crate) port_index: PortSuffix,
}
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Payload(Arc<Vec<u8>>);
#[derive(
Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub enum Polarity {
Putter, // output port (from the perspective of the component)
Getter, // input port (from the perspective of the component)
}
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum AddComponentError {
NoSuchComponent,
NonPortTypeParameters,
CannotMovePort(PortId),
WrongNumberOfParamaters { expected: usize },
UnknownPort(PortId),
WrongPortPolarity { port: PortId, expected_polarity: Polarity },
DuplicateMovedPort(PortId),
}
#[derive(Debug, Clone)]
pub enum NonsyncBlocker {
Inconsistent,
ComponentExit,
SyncBlockStart,
}
#[derive(Debug, Clone)]
pub enum SyncBlocker {
Inconsistent,
SyncBlockEnd,
CouldntReadMsg(PortId),
CouldntCheckFiring(PortId),
PutMsg(PortId, Payload),
}
///////////////////// IMPL /////////////////////
impl Payload {
pub fn new(len: usize) -> Payload {
let mut v = Vec::with_capacity(len);
unsafe {
v.set_len(len);
}
Payload(Arc::new(v))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn as_slice(&self) -> &[u8] {
&self.0
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
Arc::make_mut(&mut self.0) as _
}
pub fn concat_with(&mut self, other: &Self) {
let bytes = other.as_slice().iter().copied();
let me = Arc::make_mut(&mut self.0);
me.extend(bytes);
}
}
impl serde::Serialize for Payload {
fn serialize<S>(
&self,
serializer: S,
) -> std::result::Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
where
S: serde::Serializer,
{
let inner: &Vec<u8> = &self.0;
inner.serialize(serializer)
}
}
impl<'de> serde::Deserialize<'de> for Payload {
fn deserialize<D>(
deserializer: D,
) -> std::result::Result<Self, <D as serde::Deserializer<'de>>::Error>
where
D: serde::Deserializer<'de>,
{
let inner: Vec<u8> = Vec::deserialize(deserializer)?;
Ok(Self(Arc::new(inner)))
}
}
impl std::iter::FromIterator<u8> for Payload {
fn from_iter<I: IntoIterator<Item = u8>>(it: I) -> Self {
Self(Arc::new(it.into_iter().collect()))
}
}
impl From<Vec<u8>> for Payload {
fn from(s: Vec<u8>) -> Self {
Self(s.into())
}
}
impl Debug for PortId {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "PortId({},{})", self.controller_id, self.port_index)
}
}
impl std::ops::Not for Polarity {
type Output = Self;
fn not(self) -> Self::Output {
use Polarity::*;
match self {
Putter => Getter,
Getter => Putter,
}
}
}
|