Skip to content

Commit

Permalink
Update for latest stewart
Browse files Browse the repository at this point in the history
  • Loading branch information
LaylBongers committed Jul 3, 2023
1 parent 42408b8 commit 6bac41f
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 175 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ members = ["crates/*"]
resolver = "2"

[workspace.dependencies]
anyhow = "1.0.69"
bytemuck = "1.13.0"
anyhow = "1.0"
bytemuck = "1.13"
clap = "4.2.7"
getrandom = "0.2.9"
js-sys = "0.3.63"
serde = "1.0.163"
stewart = "0.6.0"
thiserror = "1.0.40"
serde = "1.0"
stewart = "0.7.0"
thiserror = "1.0"
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
uuid = "1.3.0"
uuid = "1.3"
wasm-bindgen = "0.2.86"
wasm-bindgen-futures = "0.4.36"
web-sys = "0.3.63"
daicon = { version = "0.12.0", path = "./crates/daicon" }
daicon-native = { version = "0.3.0", path = "./crates/daicon-native" }
daicon-types = { version = "0.1.0", path = "./crates/daicon-types" }
daicon-types = { version = "0.1.1", path = "./crates/daicon-types" }
26 changes: 16 additions & 10 deletions crates/daicon-native/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ use std::{

use anyhow::{Context as _, Error};
use daicon::protocol::file;
use stewart::{Actor, Context, Sender, State};
use stewart::{Actor, Context, Handler, State, World};
use tracing::{event, instrument, Level};

#[instrument("daicon-native::open_system_file", skip_all)]
pub fn open_system_file(
ctx: &mut Context,
world: &mut World,
cx: &Context,
path: String,
truncate: bool,
) -> Result<Sender<file::Message>, Error> {
) -> Result<Handler<file::Message>, Error> {
event!(Level::INFO, "opening");

let (mut ctx, sender) = ctx.create("daicon-system-file")?;
let (_cx, id) = world.create(cx, "daicon-system-file")?;

let file = OpenOptions::new()
.read(true)
Expand All @@ -27,9 +28,9 @@ pub fn open_system_file(
.context("failed to open system file for writing")?;

let actor = SystemFile { file };
ctx.start(actor)?;
world.start(id, actor)?;

Ok(sender)
Ok(Handler::to(id))
}

struct SystemFile {
Expand All @@ -39,7 +40,12 @@ struct SystemFile {
impl Actor for SystemFile {
type Message = file::Message;

fn process(&mut self, ctx: &mut Context, state: &mut State<Self>) -> Result<(), Error> {
fn process(
&mut self,
world: &mut World,
_cx: &Context,
state: &mut State<Self>,
) -> Result<(), Error> {
while let Some(message) = state.next() {
match message.action {
file::Action::Read(action) => {
Expand All @@ -56,7 +62,7 @@ impl Actor for SystemFile {
id: message.id,
result: Ok(data),
};
action.on_result.send(ctx, result);
action.on_result.handle(world, result);
}
file::Action::Write(action) => {
// Seek to given location
Expand All @@ -70,7 +76,7 @@ impl Actor for SystemFile {
id: message.id,
result: Ok(()),
};
action.on_result.send(ctx, result);
action.on_result.handle(world, result);
}
file::Action::Insert(action) => {
// Seek to given location
Expand All @@ -85,7 +91,7 @@ impl Actor for SystemFile {
id: message.id,
result: Ok(offset),
};
action.on_result.send(ctx, result);
action.on_result.handle(world, result);
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions crates/daicon-tools/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Error;
use clap::Args;
use daicon::{open_file_source, FileSourceOptions};
use daicon_native::open_system_file;
use stewart::{Actor, Context, State};
use stewart::{Actor, Context, State, World};
use tracing::{event, instrument, Level};

/// Create a new daicon file.
Expand All @@ -14,18 +14,18 @@ pub struct Command {
}

#[instrument("daicon-tools::start_create", skip_all)]
pub fn start(ctx: &mut Context, command: Command) -> Result<(), Error> {
pub fn start(world: &mut World, cx: &Context, command: Command) -> Result<(), Error> {
event!(Level::INFO, "creating package");

let (mut ctx, _) = ctx.create::<()>("command-create")?;
let (cx, id) = world.create(cx, "command-create")?;

// Open the target file
let file = open_system_file(&mut ctx, command.target.clone(), true)?;
let _source = open_file_source(&mut ctx, file, FileSourceOptions::default())?;
let file = open_system_file(world, &cx, command.target.clone(), true)?;
let _source = open_file_source(world, &cx, file, FileSourceOptions::default())?;

// Start the command actor
let actor = CreateCommandService {};
ctx.start(actor)?;
world.start(id, actor)?;

Ok(())
}
Expand All @@ -36,7 +36,12 @@ impl Actor for CreateCommandService {
type Message = ();

#[instrument("CreateCommandService", skip_all)]
fn process(&mut self, _ctx: &mut Context, _state: &mut State<Self>) -> Result<(), Error> {
fn process(
&mut self,
_world: &mut World,
_cx: &Context,
_state: &mut State<Self>,
) -> Result<(), Error> {
Ok(())
}
}
28 changes: 17 additions & 11 deletions crates/daicon-tools/src/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Error;
use clap::Args;
use daicon::{open_file_source, protocol::source, FileSourceOptions};
use daicon_native::open_system_file;
use stewart::{Actor, Context, State};
use stewart::{Actor, Context, Handler, State, World};
use tracing::{event, instrument, Level};
use uuid::Uuid;

Expand All @@ -25,31 +25,32 @@ pub struct Command {
}

#[instrument("daicon-tools::start_get", skip_all)]
pub fn start(ctx: &mut Context, command: Command) -> Result<(), Error> {
pub fn start(world: &mut World, cx: &Context, command: Command) -> Result<(), Error> {
event!(Level::INFO, "getting file from package");

let id = parse_hex(&command.id)?;
let asset_id = parse_hex(&command.id)?;

let (mut ctx, sender) = ctx.create("command-get")?;
let (cx, id) = world.create(cx, "command-get")?;
let handler = Handler::to(id);

// Open the target file
let file = open_system_file(&mut ctx, command.target.clone(), false)?;
let file = open_system_file(world, &cx, command.target.clone(), false)?;
let options = FileSourceOptions::default().open_table(0);
let source = open_file_source(&mut ctx, file, options)?;
let source = open_file_source(world, &cx, file, options)?;

// Add the data to the source
let action = source::GetAction {
id,
on_result: sender.map(Message::Result),
id: asset_id,
on_result: handler.map(Message::Result),
};
let message = source::Message {
id: Uuid::new_v4(),
action: source::Action::Get(action),
};
source.send(&mut ctx, message);
source.handle(world, message);

let actor = GetCommandService { command };
ctx.start(actor)?;
world.start(id, actor)?;

Ok(())
}
Expand All @@ -61,7 +62,12 @@ struct GetCommandService {
impl Actor for GetCommandService {
type Message = Message;

fn process(&mut self, _ctx: &mut Context, state: &mut State<Self>) -> Result<(), Error> {
fn process(
&mut self,
_world: &mut World,
_cx: &Context,
state: &mut State<Self>,
) -> Result<(), Error> {
while let Some(message) = state.next() {
match message {
Message::Result(response) => {
Expand Down
28 changes: 17 additions & 11 deletions crates/daicon-tools/src/commands/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Error;
use clap::Args;
use daicon::{open_file_source, protocol::source, FileSourceOptions};
use daicon_native::open_system_file;
use stewart::{Actor, Context, State};
use stewart::{Actor, Context, Handler, State, World};
use tracing::{event, instrument, Level};
use uuid::Uuid;

Expand All @@ -25,33 +25,34 @@ pub struct Command {
}

#[instrument("daicon-tools::start_set", skip_all)]
pub fn start(ctx: &mut Context, command: Command) -> Result<(), Error> {
pub fn start(world: &mut World, cx: &Context, command: Command) -> Result<(), Error> {
event!(Level::INFO, "setting file in package");

let id = parse_hex(&command.id)?;
let asset_id = parse_hex(&command.id)?;

let (mut ctx, sender) = ctx.create("command-set")?;
let (cx, id) = world.create(cx, "command-set")?;
let handler = Handler::to(id);

// Open the target file
let file = open_system_file(&mut ctx, command.target.clone(), false)?;
let file = open_system_file(world, &cx, command.target.clone(), false)?;
let options = FileSourceOptions::default().open_table(0);
let source = open_file_source(&mut ctx, file, options)?;
let source = open_file_source(world, &cx, file, options)?;

// Add the data to the source
let data = std::fs::read(&command.input)?;
let action = source::SetAction {
id,
id: asset_id,
data,
on_result: sender.map(Message::Result),
on_result: handler.map(Message::Result),
};
let message = source::Message {
id: Uuid::new_v4(),
action: source::Action::Set(action),
};
source.send(&mut ctx, message);
source.handle(world, message);

let actor = SetCommandService {};
ctx.start(actor)?;
world.start(id, actor)?;

Ok(())
}
Expand All @@ -62,7 +63,12 @@ impl Actor for SetCommandService {
type Message = Message;

#[instrument("SetCommandService", skip_all)]
fn process(&mut self, _ctx: &mut Context, state: &mut State<Self>) -> Result<(), Error> {
fn process(
&mut self,
_world: &mut World,
_cx: &Context,
state: &mut State<Self>,
) -> Result<(), Error> {
while let Some(message) = state.next() {
match message {
Message::Result(_) => {
Expand Down
13 changes: 6 additions & 7 deletions crates/daicon-tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod commands;
use anyhow::{bail, Error};
use clap::{Parser, Subcommand};
use daicon_types::Id;
use stewart::{Context, Schedule, World};
use stewart::{Context, World};
use tracing::{event, Level};
use tracing_subscriber::{prelude::*, EnvFilter, FmtSubscriber};

Expand Down Expand Up @@ -34,18 +34,17 @@ fn main() {
fn try_main(args: CliArgs) -> Result<(), Error> {
// Set up the runtime
let mut world = World::default();
let mut schedule = Schedule::default();
let mut ctx = Context::root(&mut world, &mut schedule);
let cx = Context::default();

// Start the command actor
match args.command {
Command::Create(command) => commands::create::start(&mut ctx, command)?,
Command::Set(command) => commands::set::start(&mut ctx, command)?,
Command::Get(command) => commands::get::start(&mut ctx, command)?,
Command::Create(command) => commands::create::start(&mut world, &cx, command)?,
Command::Set(command) => commands::set::start(&mut world, &cx, command)?,
Command::Get(command) => commands::get::start(&mut world, &cx, command)?,
};

// Run the command until it's done
schedule.run_until_idle(&mut world)?;
world.run_until_idle(&cx)?;

// TODO: Receive command errors
Ok(())
Expand Down
31 changes: 18 additions & 13 deletions crates/daicon-web/examples/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use daicon::{
FileSourceOptions,
};
use daicon_web::open_fetch_file;
use stewart::{Actor, Context, Schedule, State, World};
use stewart::{Actor, Context, Handler, State, World};
use tracing::{event, Level};
use uuid::Uuid;

Expand All @@ -16,56 +16,61 @@ fn main() {

event!(Level::INFO, "initializing world...");
let world = World::default();
let mut schedule = Schedule::default();
let hnd = Rc::new(RefCell::new(world));

let mut world = hnd.borrow_mut();
let mut ctx = Context::root(&mut world, &mut schedule);
let cx = Context::default();

let (mut ctx, sender) = ctx.create("fetch-example").unwrap();
let (cx, id) = world.create(&cx, "fetch-example").unwrap();
let handler = Handler::to(id);

event!(Level::INFO, "initializing fetch service...");
let url = "http://localhost:8080/package.example";
let file = open_fetch_file(&mut ctx, url.to_string(), hnd.clone()).unwrap();
let file = open_fetch_file(&mut world, &cx, url.to_string(), hnd.clone()).unwrap();

event!(Level::INFO, "initializing daicon service...");
let options = FileSourceOptions::default().open_table(0);
let source = open_file_source(&mut ctx, file, options).unwrap();
let source = open_file_source(&mut world, &cx, file, options).unwrap();

event!(Level::INFO, "starting example service...");
ctx.start(ExampleService).unwrap();
world.start(id, ExampleService).unwrap();

event!(Level::INFO, "dispatching requests...");
let action = source::GetAction {
id: Id(0xbacc2ba1),
on_result: sender.clone(),
on_result: handler.clone(),
};
let message = source::Message {
id: Uuid::new_v4(),
action: source::Action::Get(action),
};
source.send(&mut ctx, message);
source.handle(&mut world, message);

let action = source::GetAction {
id: Id(0x1f063ad4),
on_result: sender,
on_result: handler,
};
let message = source::Message {
id: Uuid::new_v4(),
action: source::Action::Get(action),
};
source.send(&mut ctx, message);
source.handle(&mut world, message);

// Process everything
schedule.run_until_idle(&mut world).unwrap();
world.run_until_idle(&cx).unwrap();
}

struct ExampleService;

impl Actor for ExampleService {
type Message = source::GetResponse;

fn process(&mut self, _ctx: &mut Context, state: &mut State<Self>) -> Result<(), Error> {
fn process(
&mut self,
_world: &mut World,
_cx: &Context,
state: &mut State<Self>,
) -> Result<(), Error> {
while let Some(message) = state.next() {
event!(Level::INFO, "received result");

Expand Down
Loading

0 comments on commit 6bac41f

Please sign in to comment.