syndicate-rs/syndicate-server/examples/consumer.rs

56 lines
1.7 KiB
Rust
Raw Permalink Normal View History

2021-07-15 07:13:31 +00:00
use std::sync::Arc;
use structopt::StructOpt;
use syndicate::actor::*;
use syndicate::language;
2021-07-15 07:13:31 +00:00
use syndicate::relay;
use syndicate::schemas::dataspace::Observe;
use syndicate::sturdy;
use syndicate::value::NestedValue;
2020-05-11 20:02:56 +00:00
use tokio::net::TcpStream;
2021-07-15 07:13:31 +00:00
2020-05-11 21:31:00 +00:00
use core::time::Duration;
2020-05-11 20:02:56 +00:00
2021-07-15 07:13:31 +00:00
#[derive(Clone, Debug, StructOpt)]
pub struct Config {
2023-02-08 23:17:12 +00:00
#[structopt(short = "d", default_value = "b4b303726566b7b3036f6964b10973796e646963617465b303736967b21069ca300c1dbfa08fba692102dd82311a8484")]
2021-07-15 07:13:31 +00:00
dataspace: String,
2020-06-15 14:15:52 +00:00
}
2020-05-11 20:02:56 +00:00
#[tokio::main]
async fn main() -> ActorResult {
2021-07-15 07:13:31 +00:00
syndicate::convenient_logging()?;
2021-09-28 11:00:48 +00:00
let config = Config::from_args();
let sturdyref = sturdy::SturdyRef::from_hex(&config.dataspace)?;
2023-02-06 16:09:17 +00:00
let (i, o) = TcpStream::connect("127.0.0.1:9001").await?.into_split();
2022-01-19 13:40:50 +00:00
Actor::top(None, |t| {
2021-10-05 19:11:16 +00:00
relay::connect_stream(t, i, o, false, sturdyref, (), |_state, t, ds| {
2021-09-28 11:00:48 +00:00
let consumer = syndicate::entity(0)
.on_message(|message_count, _t, m: AnyValue| {
if m.value().is_boolean() {
tracing::info!("{:?} messages in the last second", message_count);
*message_count = 0;
} else {
*message_count += 1;
}
Ok(())
})
.create_cap(t);
ds.assert(t, language(), &Observe {
pattern: syndicate_macros::pattern!{<Says $ $>},
observer: Arc::clone(&consumer),
});
2021-07-15 07:13:31 +00:00
2022-01-20 09:08:49 +00:00
t.every(Duration::from_secs(1), move |t| {
consumer.message(t, &(), &AnyValue::new(true));
Ok(())
})?;
2021-09-28 11:00:48 +00:00
Ok(None)
2023-02-08 21:27:41 +00:00
})
}).await??;
2021-07-15 07:13:31 +00:00
Ok(())
2020-05-11 20:02:56 +00:00
}