This commit is contained in:
Aode (Lion) 2021-10-03 20:24:57 -05:00
parent 25855fb97a
commit 14ac47e805
6 changed files with 94 additions and 135 deletions

2
Cargo.lock generated
View file

@ -1004,7 +1004,7 @@ dependencies = [
[[package]]
name = "tokio-actors"
version = "0.1.0"
source = "git+https://git.asonix.dog/asonix/tokio-actors?branch=main#9eb61841dc75551f3614bb83ea3f8202d94bb69a"
source = "git+https://git.asonix.dog/asonix/tokio-actors?branch=main#708f94263f769a92add9312deae71d26242dc594"
dependencies = [
"once_cell",
"tokio",

View file

@ -4,8 +4,7 @@ use std::{
time::Duration,
};
use tokio::sync::RwLock;
use tokio_actors::{Actor, BoxFuture, Context, Handle, SendHandle};
use tracing::{Instrument, Span};
use tokio_actors::{Actor, Context, Handle, SendHandle};
use zbus::SignalContext;
mod connector;
@ -100,13 +99,11 @@ impl Manager {
}
#[tracing::instrument(skip(ctx))]
fn turn<'a>(
async fn turn<'a>(
&'a mut self,
msg: ManagerMessage,
ctx: &'a mut Context<ManagerMessage>,
) -> BoxFuture<'a> {
Box::pin(
async move {
) -> tokio_actors::Result<()> {
match msg {
ManagerMessage::Found(port_name) => self.found(port_name, ctx).await,
ManagerMessage::Removed(port_name) => self.removed(port_name).await,
@ -114,18 +111,13 @@ impl Manager {
ManagerMessage::Ignored(port_name) => self.ignored(port_name).await,
}
}
.instrument(Span::current()),
)
}
#[tracing::instrument(skip(ctx))]
fn on_remove<'a>(
async fn on_remove<'a>(
&'a mut self,
child_id: usize,
ctx: &'a mut Context<ManagerMessage>,
) -> BoxFuture<'a> {
Box::pin(
async move {
) -> tokio_actors::Result<()> {
let mut inner = self.inner.write().await;
if let Some(port_name) = inner.children.remove(&child_id) {
@ -136,9 +128,6 @@ impl Manager {
Ok(())
}
.instrument(Span::current()),
)
}
fn object_name(&self) -> String {
"/dog/asonix/DeckManager".to_owned()

View file

@ -2,8 +2,7 @@ use crate::manager::{
deck::{Deck, DeckInfo},
ManagerMessage,
};
use tokio_actors::{BoxFuture, Context, SendHandle};
use tracing::{Instrument, Span};
use tokio_actors::{Context, SendHandle};
use zbus::Connection;
#[derive(Clone)]
@ -25,14 +24,12 @@ impl Connector {
}
#[tracing::instrument(skip(ctx))]
pub(super) fn turn<'a>(
pub(super) async fn turn<'a>(
&'a mut self,
port_name: String,
ctx: &'a mut Context<String>,
) -> BoxFuture<'a> {
) -> tokio_actors::Result<()> {
tracing::debug!("Connector");
Box::pin(
async move {
ctx.stop();
match self.connect(port_name.clone()).await {
@ -48,9 +45,6 @@ impl Connector {
}
}
}
.instrument(Span::current()),
)
}
#[tracing::instrument]
async fn connect(&mut self, port_name: String) -> tokio_actors::Result<Deck> {

View file

@ -1,9 +1,8 @@
use crate::manager::ManagerMessage;
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio_actors::{Actor, BoxFuture, Context, SendHandle};
use tokio_actors::{Actor, Context, SendHandle};
use tokio_serial::SerialStream;
use tracing::{Instrument, Span};
use zbus::{Connection, SignalContext};
mod port;
@ -139,30 +138,38 @@ impl Deck {
}
#[tracing::instrument(skip(_ctx))]
pub(super) fn turn<'a>(
pub(super) async fn turn<'a>(
&'a mut self,
msg: DeckMessage,
_ctx: &'a mut Context<DeckMessage>,
) -> BoxFuture<'a> {
Box::pin(
async move {
) -> tokio_actors::Result<()> {
match msg {
DeckMessage::Press(key) => self.press(key).await,
}
}
.instrument(Span::current()),
)
}
#[tracing::instrument(skip(ctx))]
pub(super) fn on_start<'a>(&'a mut self, ctx: &'a mut Context<DeckMessage>) -> BoxFuture<'a> {
Box::pin(self.start(ctx).instrument(Span::current()))
pub(super) async fn on_start<'a>(
&'a mut self,
ctx: &'a mut Context<DeckMessage>,
) -> tokio_actors::Result<()> {
let mut inner = self.inner.write().await;
if let Some(serial) = inner.port.take() {
let port = Port::new(ctx.handle(), serial);
let port_handle =
ctx.spawn_child_with_hooks(Actor::new(port, Port::turn).on_start(Port::on_start));
inner.child = Some(port_handle.actor_id());
}
Ok(())
}
#[tracing::instrument(skip(_ctx))]
pub(super) fn on_stop<'a>(&'a mut self, _ctx: &'a mut Context<DeckMessage>) -> BoxFuture<'a> {
Box::pin(
async move {
pub(super) async fn on_stop<'a>(
&'a mut self,
_ctx: &'a mut Context<DeckMessage>,
) -> tokio_actors::Result<()> {
self.connection
.object_server_mut()
.await
@ -170,18 +177,13 @@ impl Deck {
Ok(())
}
.instrument(Span::current()),
)
}
#[tracing::instrument(skip(ctx))]
pub(super) fn on_remove<'a>(
pub(super) async fn on_remove<'a>(
&'a mut self,
child_id: usize,
ctx: &'a mut Context<DeckMessage>,
) -> BoxFuture<'a> {
Box::pin(
async move {
) -> tokio_actors::Result<()> {
let mut inner = self.inner.write().await;
if let Some(id) = inner.child.take() {
@ -194,22 +196,6 @@ impl Deck {
Ok(())
}
.instrument(Span::current()),
)
}
#[tracing::instrument(skip(ctx))]
async fn start(&mut self, ctx: &'_ mut Context<DeckMessage>) -> tokio_actors::Result<()> {
let mut inner = self.inner.write().await;
if let Some(serial) = inner.port.take() {
let port = Port::new(ctx.handle(), serial);
let port_handle =
ctx.spawn_child_with_hooks(Actor::new(port, Port::turn).on_start(Port::on_start));
inner.child = Some(port_handle.actor_id());
}
Ok(())
}
#[tracing::instrument]
async fn press(&mut self, key: u8) -> tokio_actors::Result<()> {

View file

@ -1,8 +1,7 @@
use super::DeckMessage;
use tokio::io::AsyncReadExt;
use tokio_actors::{BoxFuture, Context, SendHandle};
use tokio_actors::{Context, SendHandle};
use tokio_serial::SerialStream;
use tracing::{Instrument, Span};
#[derive(Clone, Debug)]
pub(super) enum PortMessage {
@ -26,33 +25,26 @@ impl Port {
}
#[tracing::instrument(skip(ctx))]
pub(super) fn on_start<'a>(&'a mut self, ctx: &'a mut Context<PortMessage>) -> BoxFuture<'a> {
Box::pin(
async move {
pub(super) async fn on_start<'a>(
&'a mut self,
ctx: &'a mut Context<PortMessage>,
) -> tokio_actors::Result<()> {
ctx.stop_on_error();
ctx.handle().send(PortMessage::Start).await?;
Ok(())
}
.instrument(Span::current()),
)
}
#[tracing::instrument(skip(ctx))]
pub(super) fn turn<'a>(
pub(super) async fn turn<'a>(
&'a mut self,
msg: PortMessage,
ctx: &'a mut Context<PortMessage>,
) -> BoxFuture<'a> {
Box::pin(
async move {
) -> tokio_actors::Result<()> {
match msg {
PortMessage::Start => self.start(ctx).await,
}
}
.instrument(Span::current()),
)
}
#[tracing::instrument(skip(ctx))]
async fn start(&mut self, ctx: &'_ mut Context<PortMessage>) -> tokio_actors::Result<()> {

View file

@ -3,8 +3,7 @@ use std::{
collections::HashSet,
path::{Path, PathBuf},
};
use tokio_actors::{BoxFuture, Context, SendHandle};
use tracing::{Instrument, Span};
use tokio_actors::{Context, SendHandle};
#[derive(Clone)]
pub(super) struct Searcher {
@ -30,12 +29,11 @@ impl Searcher {
}
#[tracing::instrument(skip(_ctx))]
pub(super) fn turn<'a>(&'a mut self, _: (), _ctx: &'a mut Context<()>) -> BoxFuture<'a> {
Box::pin(self.search().instrument(Span::current()))
}
#[tracing::instrument]
async fn search(&mut self) -> tokio_actors::Result<()> {
pub(super) async fn turn<'a>(
&'a mut self,
_: (),
_ctx: &'a mut Context<()>,
) -> tokio_actors::Result<()> {
let ports = tokio::task::block_in_place(|| tokio_serial::available_ports())?;
let port_names = ports