Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"erasure-coding",
"network",
"network/test",
"overseer",
"primitives",
"runtime/common",
"runtime/polkadot",
Expand Down
18 changes: 18 additions & 0 deletions overseer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "overseer"
version = "0.1.0"
authors = ["Fedor Sakharov <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
futures = "0.3.5"
log = "0.4.8"
exit-future = "0.2.0"

[dev-dependencies]
futures-timer = "3.0.2"
femme = "2.0.1"
log = "0.4.8"
kv-log-macro = "1.0.6"
111 changes: 111 additions & 0 deletions overseer/examples/minimal-example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::collections::HashSet;
use std::time::Duration;

use futures::pending;
use futures_timer::Delay;
use kv_log_macro as log;

use overseer::{Overseer, Subsystem, SubsystemContext, SubsystemJob};

struct Subsystem1;

impl Subsystem1 {
async fn run(mut ctx: SubsystemContext<usize>) {
loop {
match ctx.try_recv().await {
Ok(Some(msg)) => {
log::info!("Subsystem1 received message {}", msg);
}
Ok(None) => (),
Err(_) => {}
}

Delay::new(Duration::from_secs(1)).await;
ctx.send_msg(10).await;
pending!();
}
}

fn new() -> Self {
Self
}
}

impl Subsystem<usize> for Subsystem1 {
fn start(&mut self, ctx: SubsystemContext<usize>) -> SubsystemJob {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a fan of start, spawn / block_on/run tend to give a better idea, of what it does, but that might be a personal pref

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block_on is probably not the best choice since we are not blocking here, but spawn works for me

SubsystemJob(Box::pin(async move {
Self::run(ctx).await;
Ok(())
}))
}
}

struct Subsystem2;


impl Subsystem2 {
async fn run(mut ctx: SubsystemContext<usize>) {
let ss3 = Box::new(Subsystem3);

let ss3_id = ctx.spawn(ss3).await;
log::info!("Received subsystem id {:?}", ss3_id);
loop {
match ctx.try_recv().await {
Ok(Some(msg)) => {
log::info!("Subsystem2 received message {}", msg);
}
Ok(None) => (),
Err(_) => {}
}
pending!();
}
}

fn new() -> Self {
Self
}
}

impl Subsystem<usize> for Subsystem2 {
fn start(&mut self, ctx: SubsystemContext<usize>) -> SubsystemJob {
SubsystemJob(Box::pin(async move {
Self::run(ctx).await;
Ok(())
}))
}
}

struct Subsystem3;

impl Subsystem<usize> for Subsystem3 {
fn start(&mut self, mut ctx: SubsystemContext<usize>) -> SubsystemJob {
SubsystemJob(Box::pin(async move {
// TODO: ctx actually has to be used otherwise the channels are dropped
loop {
// ignore all incoming msgs
while let Ok(Some(_)) = ctx.try_recv().await {
}
log::info!("Subsystem3 tick");
Delay::new(Duration::from_secs(1)).await;

pending!();
}
}))
}

fn can_recv_msg(&self, _msg: &usize) -> bool { false }
}

fn main() {
femme::with_level(femme::LevelFilter::Trace);

futures::executor::block_on(async {
let subsystems: Vec<Box<dyn Subsystem<usize>>> = vec![
Box::new(Subsystem1::new()),
Box::new(Subsystem2::new()),
];

let overseer = Overseer::new(subsystems);
overseer.run().await;
});
}
Loading