|
| 1 | +// Copyright 2020 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Polkadot. |
| 3 | + |
| 4 | +// Polkadot is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Polkadot is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Shows a basic usage of the `Overseer`: |
| 18 | +//! * Spawning subsystems and subsystem child jobs |
| 19 | +//! * Establishing message passing |
| 20 | +
|
| 21 | +use std::time::Duration; |
| 22 | +use futures::{ |
| 23 | + pending, pin_mut, executor, select, stream, |
| 24 | + FutureExt, StreamExt, |
| 25 | +}; |
| 26 | +use futures_timer::Delay; |
| 27 | +use kv_log_macro as log; |
| 28 | + |
| 29 | +use overseer::{ |
| 30 | + AllMessages, CandidateBackingSubsystemMessage, FromOverseer, |
| 31 | + Overseer, Subsystem, SubsystemContext, SpawnedSubsystem, ValidationSubsystemMessage, |
| 32 | +}; |
| 33 | + |
| 34 | +struct Subsystem1; |
| 35 | + |
| 36 | +impl Subsystem1 { |
| 37 | + async fn run(mut ctx: SubsystemContext<CandidateBackingSubsystemMessage>) { |
| 38 | + loop { |
| 39 | + match ctx.try_recv().await { |
| 40 | + Ok(Some(msg)) => { |
| 41 | + if let FromOverseer::Communication { msg } = msg { |
| 42 | + log::info!("msg {:?}", msg); |
| 43 | + } |
| 44 | + continue; |
| 45 | + } |
| 46 | + Ok(None) => (), |
| 47 | + Err(_) => { |
| 48 | + log::info!("exiting"); |
| 49 | + return; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Delay::new(Duration::from_secs(1)).await; |
| 54 | + ctx.send_msg(AllMessages::Validation( |
| 55 | + ValidationSubsystemMessage::ValidityAttestation |
| 56 | + )).await.unwrap(); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl Subsystem<CandidateBackingSubsystemMessage> for Subsystem1 { |
| 62 | + fn start(&mut self, ctx: SubsystemContext<CandidateBackingSubsystemMessage>) -> SpawnedSubsystem { |
| 63 | + SpawnedSubsystem(Box::pin(async move { |
| 64 | + Self::run(ctx).await; |
| 65 | + })) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +struct Subsystem2; |
| 70 | + |
| 71 | +impl Subsystem2 { |
| 72 | + async fn run(mut ctx: SubsystemContext<ValidationSubsystemMessage>) { |
| 73 | + ctx.spawn(Box::pin(async { |
| 74 | + loop { |
| 75 | + log::info!("Job tick"); |
| 76 | + Delay::new(Duration::from_secs(1)).await; |
| 77 | + } |
| 78 | + })).await.unwrap(); |
| 79 | + |
| 80 | + loop { |
| 81 | + match ctx.try_recv().await { |
| 82 | + Ok(Some(msg)) => { |
| 83 | + log::info!("Subsystem2 received message {:?}", msg); |
| 84 | + continue; |
| 85 | + } |
| 86 | + Ok(None) => { pending!(); } |
| 87 | + Err(_) => { |
| 88 | + log::info!("exiting"); |
| 89 | + return; |
| 90 | + }, |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl Subsystem<ValidationSubsystemMessage> for Subsystem2 { |
| 97 | + fn start(&mut self, ctx: SubsystemContext<ValidationSubsystemMessage>) -> SpawnedSubsystem { |
| 98 | + SpawnedSubsystem(Box::pin(async move { |
| 99 | + Self::run(ctx).await; |
| 100 | + })) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +fn main() { |
| 105 | + femme::with_level(femme::LevelFilter::Trace); |
| 106 | + let spawner = executor::ThreadPool::new().unwrap(); |
| 107 | + |
| 108 | + futures::executor::block_on(async { |
| 109 | + let timer_stream = stream::repeat(()).then(|_| async { |
| 110 | + Delay::new(Duration::from_secs(1)).await; |
| 111 | + }); |
| 112 | + |
| 113 | + let (overseer, _handler) = Overseer::new( |
| 114 | + Box::new(Subsystem2), |
| 115 | + Box::new(Subsystem1), |
| 116 | + spawner, |
| 117 | + ).unwrap(); |
| 118 | + let overseer_fut = overseer.run().fuse(); |
| 119 | + let timer_stream = timer_stream; |
| 120 | + |
| 121 | + pin_mut!(timer_stream); |
| 122 | + pin_mut!(overseer_fut); |
| 123 | + |
| 124 | + loop { |
| 125 | + select! { |
| 126 | + _ = overseer_fut => break, |
| 127 | + _ = timer_stream.next() => { |
| 128 | + log::info!("tick"); |
| 129 | + } |
| 130 | + complete => break, |
| 131 | + } |
| 132 | + } |
| 133 | + }); |
| 134 | +} |
0 commit comments