2021-07-06 18:56:36 +00:00
|
|
|
use bytes::Buf;
|
|
|
|
use bytes::BytesMut;
|
|
|
|
|
2021-09-19 14:53:37 +00:00
|
|
|
use crate::language;
|
2021-07-06 18:56:36 +00:00
|
|
|
use crate::actor::*;
|
2021-07-15 07:13:31 +00:00
|
|
|
use crate::during;
|
2021-07-06 18:56:36 +00:00
|
|
|
use crate::error::Error;
|
2021-07-08 22:04:11 +00:00
|
|
|
use crate::error::error;
|
2021-07-15 07:13:31 +00:00
|
|
|
use crate::schemas::gatekeeper;
|
2021-09-24 11:04:15 +00:00
|
|
|
use crate::schemas::protocol as P;
|
2021-07-08 22:04:11 +00:00
|
|
|
use crate::schemas::sturdy;
|
2021-07-06 18:56:36 +00:00
|
|
|
|
|
|
|
use futures::Sink;
|
|
|
|
use futures::SinkExt;
|
|
|
|
use futures::Stream;
|
|
|
|
use futures::StreamExt;
|
|
|
|
|
2021-09-30 11:32:41 +00:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
2021-07-21 21:29:53 +00:00
|
|
|
use preserves::error::Error as PreservesError;
|
2021-07-15 07:13:31 +00:00
|
|
|
use preserves::error::is_eof_io_error;
|
2021-07-06 18:56:36 +00:00
|
|
|
use preserves::value::BinarySource;
|
|
|
|
use preserves::value::BytesBinarySource;
|
|
|
|
use preserves::value::DomainDecode;
|
|
|
|
use preserves::value::DomainEncode;
|
|
|
|
use preserves::value::Map;
|
2021-07-08 22:04:11 +00:00
|
|
|
use preserves::value::NestedValue;
|
2021-07-06 18:56:36 +00:00
|
|
|
use preserves::value::NoEmbeddedDomainCodec;
|
|
|
|
use preserves::value::PackedWriter;
|
2021-08-09 14:02:45 +00:00
|
|
|
use preserves::value::TextWriter;
|
|
|
|
use preserves::value::ViaCodec;
|
2021-07-06 18:56:36 +00:00
|
|
|
use preserves::value::Writer;
|
2021-07-08 22:04:11 +00:00
|
|
|
use preserves::value::signed_integer::SignedInteger;
|
2021-07-06 18:56:36 +00:00
|
|
|
|
2021-09-19 14:53:37 +00:00
|
|
|
use preserves_schema::Codec;
|
|
|
|
use preserves_schema::Deserialize;
|
|
|
|
use preserves_schema::ParseError;
|
2021-07-21 21:29:53 +00:00
|
|
|
|
2021-07-06 18:56:36 +00:00
|
|
|
use std::io;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::sync::Arc;
|
2021-07-08 22:04:11 +00:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering;
|
2021-07-06 18:56:36 +00:00
|
|
|
|
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
use tokio::io::AsyncWrite;
|
|
|
|
use tokio::io::AsyncWriteExt;
|
|
|
|
|
2021-07-08 22:04:11 +00:00
|
|
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender, UnboundedReceiver};
|
|
|
|
|
2021-09-08 11:11:54 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
enum WireSymbolSide {
|
|
|
|
Imported,
|
|
|
|
Exported,
|
|
|
|
}
|
|
|
|
|
2021-07-06 18:56:36 +00:00
|
|
|
struct WireSymbol {
|
2021-07-08 22:04:11 +00:00
|
|
|
oid: sturdy::Oid,
|
2021-07-22 14:53:56 +00:00
|
|
|
obj: Arc<Cap>,
|
2021-07-08 22:04:11 +00:00
|
|
|
ref_count: AtomicUsize,
|
2021-09-08 11:11:54 +00:00
|
|
|
side: WireSymbolSide,
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Membrane {
|
2021-09-08 11:11:54 +00:00
|
|
|
side: WireSymbolSide,
|
2021-07-08 22:04:11 +00:00
|
|
|
oid_map: Map<sturdy::Oid, Arc<WireSymbol>>,
|
2021-07-22 14:53:56 +00:00
|
|
|
ref_map: Map<Arc<Cap>, Arc<WireSymbol>>,
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 11:11:54 +00:00
|
|
|
#[derive(Debug)]
|
2021-07-06 18:56:36 +00:00
|
|
|
struct Membranes {
|
|
|
|
exported: Membrane,
|
|
|
|
imported: Membrane,
|
2021-07-08 22:04:11 +00:00
|
|
|
next_export_oid: usize,
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Input {
|
|
|
|
Packets(Pin<Box<dyn Stream<Item = Result<Vec<u8>, Error>> + Send>>),
|
|
|
|
Bytes(Pin<Box<dyn AsyncRead + Send>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Output {
|
|
|
|
Packets(Pin<Box<dyn Sink<Vec<u8>, Error = Error> + Send>>),
|
|
|
|
Bytes(Pin<Box<dyn AsyncWrite + Send>>),
|
|
|
|
}
|
|
|
|
|
2021-07-25 21:12:07 +00:00
|
|
|
type TunnelRelayRef = Arc<Mutex<Option<TunnelRelay>>>;
|
2021-07-22 14:53:56 +00:00
|
|
|
|
2021-07-06 18:56:36 +00:00
|
|
|
// There are other kinds of relay. This one has exactly two participants connected to each other.
|
|
|
|
pub struct TunnelRelay
|
|
|
|
{
|
2021-07-22 14:53:56 +00:00
|
|
|
self_ref: TunnelRelayRef,
|
2021-07-22 08:07:49 +00:00
|
|
|
inbound_assertions: Map</* remote */ P::Handle, (/* local */ Handle, Vec<Arc<WireSymbol>>)>,
|
|
|
|
outbound_assertions: Map<P::Handle, Vec<Arc<WireSymbol>>>,
|
2021-07-06 18:56:36 +00:00
|
|
|
membranes: Membranes,
|
2021-09-24 11:04:15 +00:00
|
|
|
pending_outbound: Vec<P::TurnEvent<AnyValue>>,
|
2021-07-24 23:10:43 +00:00
|
|
|
self_entity: Arc<Ref<()>>,
|
2021-07-15 11:13:22 +00:00
|
|
|
output: UnboundedSender<LoanedItem<Vec<u8>>>,
|
2021-08-09 14:02:45 +00:00
|
|
|
output_text: bool,
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 22:04:11 +00:00
|
|
|
struct RelayEntity {
|
2021-07-22 14:53:56 +00:00
|
|
|
relay_ref: TunnelRelayRef,
|
2021-07-08 22:04:11 +00:00
|
|
|
oid: sturdy::Oid,
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 23:10:43 +00:00
|
|
|
struct TunnelRefEntity {
|
|
|
|
relay_ref: TunnelRelayRef,
|
|
|
|
}
|
|
|
|
|
2021-09-08 11:11:54 +00:00
|
|
|
struct ActivatedMembranes<'a, 'activation, 'm> {
|
|
|
|
turn: &'a mut Activation<'activation>,
|
|
|
|
tr_ref: &'m TunnelRelayRef,
|
|
|
|
membranes: &'m mut Membranes,
|
|
|
|
}
|
|
|
|
|
2021-07-06 18:56:36 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
impl WireSymbol {
|
2021-09-08 11:11:54 +00:00
|
|
|
#[inline]
|
|
|
|
fn inc_ref<'a>(self: &'a Arc<Self>) -> &'a Arc<Self> {
|
2021-07-08 22:04:11 +00:00
|
|
|
self.ref_count.fetch_add(1, Ordering::SeqCst);
|
2021-09-08 11:11:54 +00:00
|
|
|
tracing::trace!(?self, "after inc_ref");
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn dec_ref(&self) -> bool {
|
|
|
|
let old_count = self.ref_count.fetch_sub(1, Ordering::SeqCst);
|
|
|
|
tracing::trace!(?self, "after dec_ref");
|
|
|
|
old_count == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn current_ref_count(&self) -> usize {
|
|
|
|
self.ref_count.load(Ordering::SeqCst)
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
2021-09-08 11:11:54 +00:00
|
|
|
}
|
2021-07-06 18:56:36 +00:00
|
|
|
|
2021-09-08 11:11:54 +00:00
|
|
|
impl std::fmt::Debug for WireSymbol {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
|
|
|
write!(f, "#<WireSymbol oid={:?}:{} obj={:?} ref_count={}>",
|
|
|
|
self.side,
|
|
|
|
self.oid.0,
|
|
|
|
self.obj,
|
|
|
|
self.current_ref_count())
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Membrane {
|
2021-09-08 11:11:54 +00:00
|
|
|
fn new(side: WireSymbolSide) -> Self {
|
2021-07-06 18:56:36 +00:00
|
|
|
Membrane {
|
2021-09-08 11:11:54 +00:00
|
|
|
side,
|
2021-07-06 18:56:36 +00:00
|
|
|
oid_map: Map::new(),
|
|
|
|
ref_map: Map::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 14:53:56 +00:00
|
|
|
fn insert(&mut self, oid: sturdy::Oid, obj: Arc<Cap>) -> Arc<WireSymbol> {
|
2021-07-06 18:56:36 +00:00
|
|
|
let ws = Arc::new(WireSymbol {
|
|
|
|
oid: oid.clone(),
|
|
|
|
obj: Arc::clone(&obj),
|
2021-07-08 22:04:11 +00:00
|
|
|
ref_count: AtomicUsize::new(0),
|
2021-09-08 11:11:54 +00:00
|
|
|
side: self.side,
|
2021-07-06 18:56:36 +00:00
|
|
|
});
|
|
|
|
self.oid_map.insert(oid, Arc::clone(&ws));
|
|
|
|
self.ref_map.insert(obj, Arc::clone(&ws));
|
|
|
|
ws
|
|
|
|
}
|
|
|
|
|
2021-09-08 11:11:54 +00:00
|
|
|
fn insert_inert_entity(&mut self, t: &mut Activation, oid: sturdy::Oid) -> Arc<WireSymbol> {
|
|
|
|
self.insert(oid, Cap::new(&t.inert_entity()))
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 07:13:31 +00:00
|
|
|
pub fn connect_stream<I, O, E, F>(
|
|
|
|
t: &mut Activation,
|
|
|
|
i: I,
|
|
|
|
o: O,
|
2021-10-05 19:11:16 +00:00
|
|
|
output_text: bool,
|
2021-07-15 07:13:31 +00:00
|
|
|
sturdyref: sturdy::SturdyRef,
|
|
|
|
initial_state: E,
|
|
|
|
mut f: F,
|
|
|
|
) where
|
|
|
|
I: 'static + Send + AsyncRead,
|
|
|
|
O: 'static + Send + AsyncWrite,
|
2021-07-26 08:53:56 +00:00
|
|
|
E: 'static + Send,
|
|
|
|
F: 'static + Send + FnMut(&mut E, &mut Activation, Arc<Cap>) -> during::DuringResult<E>
|
2021-07-15 07:13:31 +00:00
|
|
|
{
|
|
|
|
let i = Input::Bytes(Box::pin(i));
|
|
|
|
let o = Output::Bytes(Box::pin(o));
|
2021-10-05 19:11:16 +00:00
|
|
|
let gatekeeper = TunnelRelay::run(t, i, o, None, Some(sturdy::Oid(0.into())), output_text).unwrap();
|
2021-08-27 13:31:18 +00:00
|
|
|
let main_entity = t.create(during::entity(initial_state).on_asserted(move |state, t, a: AnyValue| {
|
2021-07-15 07:13:31 +00:00
|
|
|
let denotation = a.value().to_embedded()?;
|
|
|
|
f(state, t, Arc::clone(denotation))
|
|
|
|
}));
|
2021-09-19 14:53:37 +00:00
|
|
|
gatekeeper.assert(t, language(), &gatekeeper::Resolve {
|
2021-07-15 07:13:31 +00:00
|
|
|
sturdyref,
|
2021-07-22 14:53:56 +00:00
|
|
|
observer: Cap::new(&main_entity),
|
2021-07-15 07:13:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-08 11:11:54 +00:00
|
|
|
impl std::fmt::Debug for Membrane {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
|
|
|
f.debug_struct("Membrane")
|
|
|
|
.field("side", &self.side)
|
|
|
|
.field("refs", &self.oid_map.values())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! dump_membranes { ($e:expr) => { tracing::trace!("membranes: {:#?}", $e); } }
|
|
|
|
// macro_rules! dump_membranes { ($e:expr) => { (); } }
|
|
|
|
|
2021-07-06 18:56:36 +00:00
|
|
|
impl TunnelRelay {
|
2021-07-08 22:04:11 +00:00
|
|
|
pub fn run(
|
|
|
|
t: &mut Activation,
|
|
|
|
i: Input,
|
|
|
|
o: Output,
|
2021-07-22 14:53:56 +00:00
|
|
|
initial_ref: Option<Arc<Cap>>,
|
2021-07-08 22:04:11 +00:00
|
|
|
initial_oid: Option<sturdy::Oid>,
|
2021-10-05 19:11:16 +00:00
|
|
|
output_text: bool,
|
2021-07-22 14:53:56 +00:00
|
|
|
) -> Option<Arc<Cap>> {
|
2021-07-08 22:04:11 +00:00
|
|
|
let (output_tx, output_rx) = unbounded_channel();
|
2021-07-25 21:12:07 +00:00
|
|
|
let tr_ref = Arc::new(Mutex::new(None));
|
2021-08-27 13:31:18 +00:00
|
|
|
let self_entity = t.create(TunnelRefEntity {
|
2021-07-24 23:10:43 +00:00
|
|
|
relay_ref: Arc::clone(&tr_ref),
|
|
|
|
});
|
2021-07-08 22:04:11 +00:00
|
|
|
let mut tr = TunnelRelay {
|
2021-07-24 23:10:43 +00:00
|
|
|
self_ref: Arc::clone(&tr_ref),
|
2021-07-08 22:04:11 +00:00
|
|
|
output: output_tx,
|
2021-10-05 19:11:16 +00:00
|
|
|
output_text,
|
2021-07-06 18:56:36 +00:00
|
|
|
inbound_assertions: Map::new(),
|
|
|
|
outbound_assertions: Map::new(),
|
|
|
|
membranes: Membranes {
|
2021-09-08 11:11:54 +00:00
|
|
|
exported: Membrane::new(WireSymbolSide::Exported),
|
|
|
|
imported: Membrane::new(WireSymbolSide::Imported),
|
2021-07-08 22:04:11 +00:00
|
|
|
next_export_oid: 0,
|
2021-07-06 18:56:36 +00:00
|
|
|
},
|
|
|
|
pending_outbound: Vec::new(),
|
2021-07-24 23:10:43 +00:00
|
|
|
self_entity: self_entity.clone(),
|
2021-07-08 22:04:11 +00:00
|
|
|
};
|
|
|
|
if let Some(ir) = initial_ref {
|
2021-09-08 11:11:54 +00:00
|
|
|
tr.membranes.export_ref(ir).inc_ref();
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-07-22 14:53:56 +00:00
|
|
|
let result = initial_oid.map(
|
2021-09-08 11:11:54 +00:00
|
|
|
|io| Arc::clone(&tr.membranes.import_oid(t, &tr_ref, io).inc_ref().obj));
|
|
|
|
dump_membranes!(tr.membranes);
|
2021-09-30 11:32:41 +00:00
|
|
|
*tr_ref.lock() = Some(tr);
|
2021-08-27 13:31:18 +00:00
|
|
|
t.linked_task(crate::name!("writer"), output_loop(o, output_rx));
|
|
|
|
t.linked_task(crate::name!("reader"), input_loop(t.facet.clone(), i, tr_ref));
|
2021-07-24 23:10:43 +00:00
|
|
|
t.state.add_exit_hook(&self_entity);
|
2021-07-08 22:04:11 +00:00
|
|
|
result
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:04:15 +00:00
|
|
|
fn deserialize_one(&mut self, t: &mut Activation, bs: &[u8]) -> (Result<P::Packet<AnyValue>, ParseError>, usize) {
|
2021-07-24 23:10:43 +00:00
|
|
|
let mut src = BytesBinarySource::new(&bs);
|
2021-09-08 11:11:54 +00:00
|
|
|
let mut dec = ActivatedMembranes {
|
|
|
|
turn: t,
|
|
|
|
tr_ref: &self.self_ref,
|
|
|
|
membranes: &mut self.membranes,
|
|
|
|
};
|
2021-08-09 14:02:45 +00:00
|
|
|
match src.peek() {
|
|
|
|
Ok(v) => if v >= 128 {
|
|
|
|
self.output_text = false;
|
2021-09-19 14:53:37 +00:00
|
|
|
let mut r = src.packed(&mut dec);
|
2021-08-09 14:02:45 +00:00
|
|
|
let res = P::Packet::deserialize(&mut r);
|
|
|
|
(res, r.source.index)
|
|
|
|
} else {
|
|
|
|
self.output_text = true;
|
|
|
|
let mut dec = ViaCodec::new(dec);
|
2021-09-19 14:53:37 +00:00
|
|
|
let mut r = src.text::<AnyValue, _>(&mut dec);
|
2021-08-09 14:02:45 +00:00
|
|
|
let res = P::Packet::deserialize(&mut r);
|
|
|
|
(res, r.source.index)
|
|
|
|
},
|
|
|
|
Err(e) => (Err(e.into()), 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_inbound_datagram(&mut self, t: &mut Activation, bs: &[u8]) -> ActorResult {
|
2021-09-26 09:02:55 +00:00
|
|
|
tracing::trace!(bytes = ?bs, "inbound datagram");
|
2021-08-09 14:02:45 +00:00
|
|
|
let item = self.deserialize_one(t, bs).0?;
|
2021-07-24 23:10:43 +00:00
|
|
|
self.handle_inbound_packet(t, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_inbound_stream(&mut self, t: &mut Activation, buf: &mut BytesMut) -> ActorResult {
|
|
|
|
loop {
|
2021-09-26 09:02:55 +00:00
|
|
|
tracing::trace!(buffer = ?buf, "inbound stream");
|
2021-08-09 14:02:45 +00:00
|
|
|
let (result, count) = self.deserialize_one(t, buf);
|
|
|
|
match result {
|
|
|
|
Err(ParseError::Preserves(PreservesError::Io(e)))
|
|
|
|
if is_eof_io_error(&e) => return Ok(()),
|
|
|
|
Err(e) => return Err(e)?,
|
|
|
|
Ok(item) => {
|
2021-07-24 23:10:43 +00:00
|
|
|
buf.advance(count);
|
|
|
|
self.handle_inbound_packet(t, item)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 11:04:15 +00:00
|
|
|
fn handle_inbound_packet(&mut self, t: &mut Activation, p: P::Packet<AnyValue>) -> ActorResult {
|
2021-09-26 09:02:55 +00:00
|
|
|
tracing::debug!(packet = ?p, "-->");
|
2021-07-06 18:56:36 +00:00
|
|
|
match p {
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Packet::Error(b) => {
|
2021-08-30 10:01:47 +00:00
|
|
|
tracing::info!(message = ?b.message.clone(),
|
|
|
|
detail = ?b.detail.clone(),
|
2021-07-06 18:56:36 +00:00
|
|
|
"received Error from peer");
|
|
|
|
Err(*b)
|
|
|
|
},
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Packet::Turn(b) => {
|
|
|
|
let P::Turn(events) = *b;
|
|
|
|
for P::TurnEvent { oid, event } in events {
|
2021-12-01 10:06:39 +00:00
|
|
|
tracing::trace!(?oid, ?event, "handle_inbound");
|
2021-07-08 22:04:11 +00:00
|
|
|
let target = match self.membranes.exported.oid_map.get(&sturdy::Oid(oid.0.clone())) {
|
2021-09-08 11:11:54 +00:00
|
|
|
Some(ws) =>
|
|
|
|
ws.inc_ref(),
|
|
|
|
None => {
|
2021-10-07 16:10:59 +00:00
|
|
|
tracing::debug!(
|
2021-09-19 14:53:37 +00:00
|
|
|
event = ?language().unparse(&P::TurnEvent { oid, event }),
|
2021-09-08 11:11:54 +00:00
|
|
|
"Cannot deliver event: nonexistent oid");
|
2021-10-18 15:21:09 +00:00
|
|
|
continue;
|
2021-09-08 11:11:54 +00:00
|
|
|
}
|
2021-07-08 22:04:11 +00:00
|
|
|
};
|
2021-09-08 11:11:54 +00:00
|
|
|
let mut pins = vec![target.clone()];
|
|
|
|
let target = Arc::clone(&target.obj);
|
2021-07-08 22:04:11 +00:00
|
|
|
match event {
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Event::Assert(b) => {
|
|
|
|
let P::Assert { assertion: P::Assertion(a), handle: remote_handle } = *b;
|
2021-09-08 11:11:54 +00:00
|
|
|
a.foreach_embedded::<_, Error>(
|
|
|
|
&mut |r| Ok(pins.push(self.membranes.lookup_ref(r))))?;
|
2021-09-19 14:53:37 +00:00
|
|
|
if let Some(local_handle) = target.assert(t, &(), &a) {
|
2021-09-08 11:11:54 +00:00
|
|
|
if let Some(_) = self.inbound_assertions.insert(remote_handle, (local_handle, pins)) {
|
2021-08-11 21:16:01 +00:00
|
|
|
return Err(error("Assertion with duplicate handle", AnyValue::new(false)));
|
2021-07-22 14:53:56 +00:00
|
|
|
}
|
2021-09-08 11:11:54 +00:00
|
|
|
} else {
|
|
|
|
self.membranes.release(pins);
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-09-08 11:11:54 +00:00
|
|
|
dump_membranes!(self.membranes);
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Event::Retract(b) => {
|
|
|
|
let P::Retract { handle: remote_handle } = *b;
|
2021-09-08 11:11:54 +00:00
|
|
|
let (local_handle, previous_pins) = match self.inbound_assertions.remove(&remote_handle) {
|
2021-09-19 14:53:37 +00:00
|
|
|
None => return Err(error("Retraction of nonexistent handle", language().unparse(&remote_handle))),
|
2021-07-08 22:04:11 +00:00
|
|
|
Some(wss) => wss,
|
|
|
|
};
|
2021-09-08 11:11:54 +00:00
|
|
|
self.membranes.release(previous_pins);
|
|
|
|
self.membranes.release(pins);
|
2021-07-08 22:04:11 +00:00
|
|
|
t.retract(local_handle);
|
2021-09-08 11:11:54 +00:00
|
|
|
dump_membranes!(self.membranes);
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Event::Message(b) => {
|
|
|
|
let P::Message { body: P::Assertion(a) } = *b;
|
2021-07-08 22:04:11 +00:00
|
|
|
a.foreach_embedded(&mut |r| {
|
2021-09-08 11:11:54 +00:00
|
|
|
let ws = self.membranes.lookup_ref(r);
|
|
|
|
let rc = ws.current_ref_count();
|
|
|
|
pins.push(ws);
|
|
|
|
match rc {
|
2021-08-11 21:16:01 +00:00
|
|
|
1 => Err(error("Cannot receive transient reference", AnyValue::new(false))),
|
2021-07-08 22:04:11 +00:00
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
})?;
|
2021-09-19 14:53:37 +00:00
|
|
|
target.message(t, &(), &a);
|
2021-09-08 11:11:54 +00:00
|
|
|
self.membranes.release(pins);
|
|
|
|
dump_membranes!(self.membranes);
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Event::Sync(b) => {
|
|
|
|
let P::Sync { peer } = *b;
|
2021-09-08 11:11:54 +00:00
|
|
|
pins.push(self.membranes.lookup_ref(&peer));
|
|
|
|
dump_membranes!(self.membranes);
|
2021-07-08 22:04:11 +00:00
|
|
|
struct SyncPeer {
|
2021-07-24 23:10:43 +00:00
|
|
|
relay_ref: TunnelRelayRef,
|
2021-07-22 14:53:56 +00:00
|
|
|
peer: Arc<Cap>,
|
2021-09-08 11:11:54 +00:00
|
|
|
pins: Vec<Arc<WireSymbol>>,
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-07-22 14:53:56 +00:00
|
|
|
impl Entity<Synced> for SyncPeer {
|
|
|
|
fn message(&mut self, t: &mut Activation, _a: Synced) -> ActorResult {
|
2021-09-19 14:53:37 +00:00
|
|
|
self.peer.message(t, &(), &AnyValue::new(true));
|
2021-09-30 11:32:41 +00:00
|
|
|
let mut g = self.relay_ref.lock();
|
2021-07-24 23:10:43 +00:00
|
|
|
let tr = g.as_mut().expect("initialized");
|
2021-09-08 11:11:54 +00:00
|
|
|
tr.membranes.release(std::mem::take(&mut self.pins));
|
|
|
|
dump_membranes!(tr.membranes);
|
2021-07-08 22:04:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 13:31:18 +00:00
|
|
|
let k = t.create(SyncPeer {
|
2021-07-24 23:10:43 +00:00
|
|
|
relay_ref: Arc::clone(&self.self_ref),
|
2021-07-08 22:04:11 +00:00
|
|
|
peer: Arc::clone(&peer),
|
2021-09-08 11:11:54 +00:00
|
|
|
pins,
|
2021-07-08 22:04:11 +00:00
|
|
|
});
|
2021-07-22 14:53:56 +00:00
|
|
|
t.sync(&peer.underlying, k);
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
2021-07-24 21:22:01 +00:00
|
|
|
t.deliver();
|
2021-07-06 18:56:36 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 11:11:54 +00:00
|
|
|
fn outbound_event_bookkeeping(
|
|
|
|
&mut self,
|
2021-09-19 14:53:37 +00:00
|
|
|
_t: &mut Activation,
|
2021-09-08 11:11:54 +00:00
|
|
|
remote_oid: sturdy::Oid,
|
2021-09-24 11:04:15 +00:00
|
|
|
event: &P::Event<AnyValue>,
|
2021-09-08 11:11:54 +00:00
|
|
|
) -> ActorResult {
|
|
|
|
match event {
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Event::Assert(b) => {
|
|
|
|
let P::Assert { assertion: P::Assertion(a), handle } = &**b;
|
2021-12-01 10:14:02 +00:00
|
|
|
if let Some(target_ws) = self.membranes.imported.oid_map.get(&remote_oid).map(Arc::clone) {
|
|
|
|
target_ws.inc_ref(); // encoding won't do this; target oid is syntactically special
|
|
|
|
let mut pins = vec![target_ws];
|
|
|
|
a.foreach_embedded::<_, Error>(
|
|
|
|
&mut |r| Ok(pins.push(self.membranes.lookup_ref(r))))?;
|
|
|
|
self.outbound_assertions.insert(handle.clone(), pins);
|
|
|
|
dump_membranes!(self.membranes);
|
|
|
|
} else {
|
|
|
|
// This can happen if
|
|
|
|
// 1. remote peer asserts a value causing remote_oid to be allocated
|
|
|
|
// 2. some local actor holds a reference to that entity
|
|
|
|
// 3. remote peer retracts the value
|
|
|
|
// 4. local actor uses the ref
|
|
|
|
tracing::trace!(?remote_oid, "not registered in imported membrane (ok)");
|
|
|
|
}
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Event::Retract(b) => {
|
|
|
|
let P::Retract { handle } = &**b;
|
2021-09-08 11:11:54 +00:00
|
|
|
if let Some(pins) = self.outbound_assertions.remove(handle) {
|
|
|
|
self.membranes.release(pins);
|
2021-12-01 10:14:02 +00:00
|
|
|
dump_membranes!(self.membranes);
|
|
|
|
} else {
|
|
|
|
// This can happen e.g. if an assert leads to no
|
|
|
|
// outbound message as in the scenario in the
|
|
|
|
// P::Event::Assert stanza above, and then the
|
|
|
|
// local actor retracts their assertion again.
|
|
|
|
tracing::trace!(?handle, "not registered in outbound_assertions (ok)");
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 08:07:49 +00:00
|
|
|
P::Event::Message(b) => {
|
|
|
|
let P::Message { body: P::Assertion(a) } = &**b;
|
2021-07-08 22:04:11 +00:00
|
|
|
a.foreach_embedded(&mut |r| {
|
2021-09-08 11:11:54 +00:00
|
|
|
let ws = self.membranes.lookup_ref(r);
|
|
|
|
if self.membranes.release_one(ws) { // undo the inc_ref from encoding
|
|
|
|
Err(error("Sent transient reference", AnyValue::new(false)))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
})?;
|
2021-09-08 11:11:54 +00:00
|
|
|
dump_membranes!(self.membranes);
|
2021-07-08 22:04:11 +00:00
|
|
|
},
|
2021-09-08 11:11:54 +00:00
|
|
|
P::Event::Sync(_b) =>
|
|
|
|
todo!(),
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-09-08 11:11:54 +00:00
|
|
|
Ok(())
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:04:15 +00:00
|
|
|
pub fn send_packet(&mut self, account: &Arc<Account>, cost: usize, p: P::Packet<AnyValue>) -> ActorResult {
|
2021-09-19 14:53:37 +00:00
|
|
|
let item = language().unparse(&p);
|
2021-09-26 09:02:55 +00:00
|
|
|
tracing::debug!(packet = ?item, "<--");
|
2021-09-08 11:11:54 +00:00
|
|
|
|
|
|
|
let bs = if self.output_text {
|
2021-09-19 14:53:37 +00:00
|
|
|
let mut s = TextWriter::encode(&mut self.membranes, &item)?;
|
2021-08-10 01:55:01 +00:00
|
|
|
s.push('\n');
|
2021-09-08 11:11:54 +00:00
|
|
|
s.into_bytes()
|
2021-08-09 14:02:45 +00:00
|
|
|
} else {
|
2021-09-19 14:53:37 +00:00
|
|
|
PackedWriter::encode(&mut self.membranes, &item)?
|
2021-09-08 11:11:54 +00:00
|
|
|
};
|
2021-07-06 18:56:36 +00:00
|
|
|
|
2021-08-13 19:51:11 +00:00
|
|
|
let _ = self.output.send(LoanedItem::new(account, cost, bs));
|
2021-07-08 22:04:11 +00:00
|
|
|
Ok(())
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
2021-07-24 23:10:43 +00:00
|
|
|
|
2021-09-24 11:04:15 +00:00
|
|
|
pub fn send_event(&mut self, t: &mut Activation, remote_oid: sturdy::Oid, event: P::Event<AnyValue>) -> ActorResult {
|
2021-07-24 23:10:43 +00:00
|
|
|
if self.pending_outbound.is_empty() {
|
|
|
|
t.message_for_myself(&self.self_entity, ());
|
|
|
|
}
|
2021-09-08 11:11:54 +00:00
|
|
|
self.pending_outbound.push(P::TurnEvent { oid: P::Oid(remote_oid.0), event });
|
2021-07-24 23:10:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 22:04:11 +00:00
|
|
|
impl Membranes {
|
2021-09-08 11:11:54 +00:00
|
|
|
fn export_ref(&mut self, obj: Arc<Cap>) -> Arc<WireSymbol> {
|
|
|
|
let oid = sturdy::Oid(SignedInteger::from(self.next_export_oid as u128));
|
|
|
|
self.next_export_oid += 1;
|
|
|
|
self.exported.insert(oid, obj)
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn import_oid(
|
|
|
|
&mut self,
|
2021-08-27 13:31:18 +00:00
|
|
|
t: &mut Activation,
|
2021-07-22 14:53:56 +00:00
|
|
|
relay_ref: &TunnelRelayRef,
|
2021-07-08 22:04:11 +00:00
|
|
|
oid: sturdy::Oid,
|
|
|
|
) -> Arc<WireSymbol> {
|
2021-08-27 13:31:18 +00:00
|
|
|
let obj = t.create(RelayEntity { relay_ref: Arc::clone(relay_ref), oid: oid.clone() });
|
2021-07-22 14:53:56 +00:00
|
|
|
self.imported.insert(oid, Cap::new(&obj))
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 11:11:54 +00:00
|
|
|
#[inline]
|
|
|
|
fn lookup_ref(&mut self, r: &Arc<Cap>) -> Arc<WireSymbol> {
|
|
|
|
self.imported.ref_map.get(r).or_else(|| self.exported.ref_map.get(r)).map(Arc::clone)
|
|
|
|
.expect("WireSymbol must be present at lookup_ref() time")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn membrane(&mut self, side: WireSymbolSide) -> &mut Membrane {
|
|
|
|
match side {
|
|
|
|
WireSymbolSide::Imported => &mut self.imported,
|
|
|
|
WireSymbolSide::Exported => &mut self.exported,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn release_one(&mut self, ws: Arc<WireSymbol>) -> bool {
|
|
|
|
if ws.dec_ref() {
|
|
|
|
let membrane = self.membrane(ws.side);
|
|
|
|
membrane.oid_map.remove(&ws.oid);
|
|
|
|
membrane.ref_map.remove(&ws.obj);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn release<I: IntoIterator<Item = Arc<WireSymbol>>>(&mut self, wss: I) {
|
|
|
|
for ws in wss {
|
|
|
|
self.release_one(ws);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 18:56:36 +00:00
|
|
|
fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
|
|
|
|
&mut self,
|
2021-07-08 22:04:11 +00:00
|
|
|
t: &mut Activation,
|
2021-07-22 14:53:56 +00:00
|
|
|
relay_ref: &TunnelRelayRef,
|
2021-07-06 18:56:36 +00:00
|
|
|
src: &'src mut S,
|
|
|
|
_read_annotations: bool,
|
2021-09-24 11:04:15 +00:00
|
|
|
) -> io::Result<Arc<Cap>> {
|
2021-09-08 11:11:54 +00:00
|
|
|
let ws = match sturdy::WireRef::deserialize(&mut src.packed(NoEmbeddedDomainCodec))? {
|
2021-07-08 22:04:11 +00:00
|
|
|
sturdy::WireRef::Mine{ oid: b } => {
|
|
|
|
let oid = *b;
|
2021-09-08 11:11:54 +00:00
|
|
|
self.imported.oid_map.get(&oid).map(Arc::clone)
|
|
|
|
.unwrap_or_else(|| self.import_oid(t, relay_ref, oid))
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
sturdy::WireRef::Yours { oid: b, attenuation } => {
|
|
|
|
let oid = *b;
|
2021-09-08 11:11:54 +00:00
|
|
|
if attenuation.is_empty() {
|
|
|
|
self.exported.oid_map.get(&oid).map(Arc::clone).unwrap_or_else(
|
|
|
|
|| self.exported.insert_inert_entity(t, oid))
|
|
|
|
} else {
|
|
|
|
match self.exported.oid_map.get(&oid) {
|
|
|
|
None => self.exported.insert_inert_entity(t, oid),
|
|
|
|
Some(ws) => {
|
|
|
|
let attenuated_obj = ws.obj.attenuate(&sturdy::Attenuation(attenuation))
|
|
|
|
.map_err(|e| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
format!("Invalid capability attenuation: {:?}", e))
|
|
|
|
})?;
|
|
|
|
self.exported.insert(oid, attenuated_obj)
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 11:11:54 +00:00
|
|
|
};
|
|
|
|
Ok(Arc::clone(&ws.inc_ref().obj))
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 11:04:15 +00:00
|
|
|
impl<'a, 'activation, 'm> DomainDecode<Arc<Cap>> for ActivatedMembranes<'a, 'activation, 'm> {
|
2021-07-08 22:04:11 +00:00
|
|
|
fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
|
|
|
|
&mut self,
|
|
|
|
src: &'src mut S,
|
|
|
|
read_annotations: bool,
|
2021-09-24 11:04:15 +00:00
|
|
|
) -> io::Result<Arc<Cap>> {
|
2021-09-08 11:11:54 +00:00
|
|
|
self.membranes.decode_embedded(self.turn, self.tr_ref, src, read_annotations)
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 11:04:15 +00:00
|
|
|
impl DomainEncode<Arc<Cap>> for Membranes {
|
2021-07-06 18:56:36 +00:00
|
|
|
fn encode_embedded<W: Writer>(
|
|
|
|
&mut self,
|
|
|
|
w: &mut W,
|
2021-09-24 11:04:15 +00:00
|
|
|
d: &Arc<Cap>,
|
2021-07-06 18:56:36 +00:00
|
|
|
) -> io::Result<()> {
|
2021-09-19 14:53:37 +00:00
|
|
|
w.write(&mut NoEmbeddedDomainCodec, &language().unparse(&match self.exported.ref_map.get(d) {
|
2021-07-15 07:13:31 +00:00
|
|
|
Some(ws) => sturdy::WireRef::Mine {
|
2021-09-08 11:11:54 +00:00
|
|
|
oid: Box::new(ws.inc_ref().oid.clone()),
|
2021-07-15 07:13:31 +00:00
|
|
|
},
|
2021-07-08 22:04:11 +00:00
|
|
|
None => match self.imported.ref_map.get(d) {
|
|
|
|
Some(ws) => {
|
2021-07-15 07:13:31 +00:00
|
|
|
if d.attenuation.is_empty() {
|
|
|
|
sturdy::WireRef::Yours {
|
2021-09-08 11:11:54 +00:00
|
|
|
oid: Box::new(ws.inc_ref().oid.clone()),
|
2021-07-15 07:13:31 +00:00
|
|
|
attenuation: vec![],
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We may trust the peer to enforce attenuation on our behalf, in
|
|
|
|
// which case we can return sturdy::WireRef::Yours with an attenuation
|
|
|
|
// attached here, but for now we don't.
|
|
|
|
sturdy::WireRef::Mine {
|
2021-09-08 11:11:54 +00:00
|
|
|
oid: Box::new(self.export_ref(Arc::clone(d)).inc_ref().oid.clone()),
|
2021-07-15 07:13:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
None =>
|
2021-07-15 07:13:31 +00:00
|
|
|
sturdy::WireRef::Mine {
|
2021-09-08 11:11:54 +00:00
|
|
|
oid: Box::new(self.export_ref(Arc::clone(d)).inc_ref().oid.clone()),
|
2021-07-15 07:13:31 +00:00
|
|
|
},
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}))
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 14:53:56 +00:00
|
|
|
async fn input_loop(
|
2021-08-27 13:31:18 +00:00
|
|
|
facet: FacetRef,
|
2021-07-06 18:56:36 +00:00
|
|
|
i: Input,
|
2021-07-22 14:53:56 +00:00
|
|
|
relay: TunnelRelayRef,
|
2021-09-28 10:53:11 +00:00
|
|
|
) -> Result<LinkedTaskTermination, Error> {
|
2021-08-13 19:51:11 +00:00
|
|
|
let account = Account::new(crate::name!("input-loop"));
|
2021-07-06 18:56:36 +00:00
|
|
|
match i {
|
|
|
|
Input::Packets(mut src) => {
|
|
|
|
loop {
|
2021-08-13 19:51:11 +00:00
|
|
|
account.ensure_clear_funds().await;
|
2021-07-06 18:56:36 +00:00
|
|
|
match src.next().await {
|
2021-09-28 10:53:11 +00:00
|
|
|
None => return Ok(LinkedTaskTermination::Normal),
|
2021-08-27 13:31:18 +00:00
|
|
|
Some(bs) => facet.activate(Arc::clone(&account), |t| {
|
2021-09-30 11:32:41 +00:00
|
|
|
let mut g = relay.lock();
|
2021-07-24 23:10:43 +00:00
|
|
|
let tr = g.as_mut().expect("initialized");
|
|
|
|
tr.handle_inbound_datagram(t, &bs?)
|
|
|
|
})?,
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Input::Bytes(mut r) => {
|
2021-07-21 21:29:53 +00:00
|
|
|
const BUFSIZE: usize = 65536;
|
|
|
|
let mut buf = BytesMut::with_capacity(BUFSIZE);
|
2021-07-06 18:56:36 +00:00
|
|
|
loop {
|
2021-08-13 19:51:11 +00:00
|
|
|
account.ensure_clear_funds().await;
|
2021-07-21 21:29:53 +00:00
|
|
|
buf.reserve(BUFSIZE);
|
2021-07-15 07:13:31 +00:00
|
|
|
let n = match r.read_buf(&mut buf).await {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(e) =>
|
|
|
|
if e.kind() == io::ErrorKind::ConnectionReset {
|
2021-09-28 10:53:11 +00:00
|
|
|
return Ok(LinkedTaskTermination::Normal);
|
2021-07-15 07:13:31 +00:00
|
|
|
} else {
|
|
|
|
return Err(e)?;
|
|
|
|
},
|
|
|
|
};
|
2021-07-06 18:56:36 +00:00
|
|
|
match n {
|
2021-09-28 10:53:11 +00:00
|
|
|
0 => return Ok(LinkedTaskTermination::Normal),
|
2021-08-27 13:31:18 +00:00
|
|
|
_ => facet.activate(Arc::clone(&account), |t| {
|
2021-09-30 11:32:41 +00:00
|
|
|
let mut g = relay.lock();
|
2021-07-24 23:10:43 +00:00
|
|
|
let tr = g.as_mut().expect("initialized");
|
|
|
|
tr.handle_inbound_stream(t, &mut buf)
|
|
|
|
})?,
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 14:53:56 +00:00
|
|
|
async fn output_loop(
|
2021-07-08 22:04:11 +00:00
|
|
|
mut o: Output,
|
2021-07-15 11:13:22 +00:00
|
|
|
mut output_rx: UnboundedReceiver<LoanedItem<Vec<u8>>>,
|
2021-09-28 10:53:11 +00:00
|
|
|
) -> Result<LinkedTaskTermination, Error> {
|
2021-07-08 22:04:11 +00:00
|
|
|
loop {
|
|
|
|
match output_rx.recv().await {
|
|
|
|
None =>
|
2021-09-28 10:53:11 +00:00
|
|
|
return Ok(LinkedTaskTermination::KeepFacet),
|
2021-07-15 11:13:22 +00:00
|
|
|
Some(mut loaned_item) => {
|
|
|
|
match &mut o {
|
|
|
|
Output::Packets(sink) => sink.send(std::mem::take(&mut loaned_item.item)).await?,
|
|
|
|
Output::Bytes(w) => {
|
|
|
|
w.write_all(&loaned_item.item).await?;
|
|
|
|
w.flush().await?;
|
|
|
|
}
|
2021-07-15 07:13:31 +00:00
|
|
|
}
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 23:10:43 +00:00
|
|
|
impl Entity<()> for TunnelRefEntity {
|
|
|
|
fn message(&mut self, t: &mut Activation, _m: ()) -> ActorResult {
|
2021-09-30 11:32:41 +00:00
|
|
|
let mut g = self.relay_ref.lock();
|
2021-07-24 23:10:43 +00:00
|
|
|
let tr = g.as_mut().expect("initialized");
|
|
|
|
let events = std::mem::take(&mut tr.pending_outbound);
|
2021-09-08 11:11:54 +00:00
|
|
|
tr.send_packet(&t.account(), events.len(), P::Packet::Turn(Box::new(P::Turn(events.clone()))))?;
|
|
|
|
for P::TurnEvent { oid, event } in events.into_iter() {
|
|
|
|
tr.outbound_event_bookkeeping(t, sturdy::Oid(oid.0), &event)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 14:53:56 +00:00
|
|
|
fn exit_hook(&mut self, t: &mut Activation, exit_status: &Arc<ActorResult>) -> ActorResult {
|
|
|
|
if let Err(e) = &**exit_status {
|
2021-07-06 18:56:36 +00:00
|
|
|
let e = e.clone();
|
2021-09-30 11:32:41 +00:00
|
|
|
let mut g = self.relay_ref.lock();
|
2021-07-24 23:10:43 +00:00
|
|
|
let tr = g.as_mut().expect("initialized");
|
2021-08-13 19:51:11 +00:00
|
|
|
tr.send_packet(&t.account(), 1, P::Packet::Error(Box::new(e)))?;
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
2021-07-21 23:05:08 +00:00
|
|
|
Ok(())
|
2021-07-06 18:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-08 22:04:11 +00:00
|
|
|
|
2021-08-11 21:16:01 +00:00
|
|
|
impl Entity<AnyValue> for RelayEntity {
|
|
|
|
fn assert(&mut self, t: &mut Activation, a: AnyValue, h: Handle) -> ActorResult {
|
2021-09-30 11:32:41 +00:00
|
|
|
self.relay_ref.lock().as_mut().expect("initialized")
|
|
|
|
.send_event(t, self.oid.clone(), P::Event::Assert(Box::new(P::Assert {
|
|
|
|
assertion: P::Assertion(a),
|
|
|
|
handle: P::Handle(h.into()),
|
|
|
|
})))
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
fn retract(&mut self, t: &mut Activation, h: Handle) -> ActorResult {
|
2021-09-30 11:32:41 +00:00
|
|
|
self.relay_ref.lock().as_mut().expect("initialized")
|
|
|
|
.send_event(t, self.oid.clone(), P::Event::Retract(Box::new(P::Retract {
|
|
|
|
handle: P::Handle(h.into()),
|
|
|
|
})))
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-08-11 21:16:01 +00:00
|
|
|
fn message(&mut self, t: &mut Activation, m: AnyValue) -> ActorResult {
|
2021-09-30 11:32:41 +00:00
|
|
|
self.relay_ref.lock().as_mut().expect("initialized")
|
|
|
|
.send_event(t, self.oid.clone(), P::Event::Message(Box::new(P::Message {
|
|
|
|
body: P::Assertion(m)
|
|
|
|
})))
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
2021-07-22 14:53:56 +00:00
|
|
|
fn sync(&mut self, t: &mut Activation, peer: Arc<Ref<Synced>>) -> ActorResult {
|
2021-09-30 11:32:41 +00:00
|
|
|
self.relay_ref.lock().as_mut().expect("initialized")
|
|
|
|
.send_event(t, self.oid.clone(), P::Event::Sync(Box::new(P::Sync {
|
|
|
|
peer: Cap::guard(Arc::new(()), peer)
|
|
|
|
})))
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|