Formatting.

This commit is contained in:
Alejandro Cabeza Romero 2024-12-20 17:27:40 +01:00
parent 79a22e9bc2
commit a550e99083
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
4 changed files with 51 additions and 44 deletions

View File

@ -6,9 +6,9 @@ use overwatch_rs::services::handle::ServiceHandle;
use crate::service_ping::PingService;
use crate::service_pong::PongService;
mod messages;
mod service_ping;
mod service_pong;
mod messages;
#[derive(Services)]
struct PingPong {
@ -17,10 +17,8 @@ struct PingPong {
}
fn main() {
let ping_pong_settings = PingPongServiceSettings {
ping: (),
pong: (),
};
let ping_pong = OverwatchRunner::<PingPong>::run(ping_pong_settings, None).expect("OverwatchRunner failed");
let ping_pong_settings = PingPongServiceSettings { ping: (), pong: () };
let ping_pong =
OverwatchRunner::<PingPong>::run(ping_pong_settings, None).expect("OverwatchRunner failed");
ping_pong.wait_finished();
}

View File

@ -1,15 +1,16 @@
// Crate
use overwatch_rs::services::relay::RelayMessage;
#[derive(Debug)]
pub enum PingMessage {
Pong
Pong,
}
impl RelayMessage for PingMessage {}
#[derive(Debug)]
pub enum PongMessage {
Ping
Ping,
}
impl RelayMessage for PongMessage {}

View File

@ -1,16 +1,16 @@
// Crates
use std::time::Duration;
use tokio::time::sleep;
use overwatch_rs::DynError;
use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId};
use overwatch_rs::services::handle::ServiceStateHandle;
use overwatch_rs::services::state::{NoOperator, NoState};
use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId};
use overwatch_rs::DynError;
use std::time::Duration;
use tokio::time::sleep;
// Internal
use crate::messages::{PingMessage, PongMessage};
use crate::service_pong::PongService;
pub struct PingService {
service_state_handle: ServiceStateHandle<Self>
service_state_handle: ServiceStateHandle<Self>,
}
impl ServiceData for PingService {
@ -25,44 +25,48 @@ impl ServiceData for PingService {
impl ServiceCore for PingService {
fn init(service_state_handle: ServiceStateHandle<Self>) -> Result<Self, DynError> {
Ok(Self {
service_state_handle
service_state_handle,
})
}
async fn run(self) -> Result<(), DynError> {
let Self {
service_state_handle
service_state_handle,
} = self;
let mut inbound_relay = service_state_handle.inbound_relay;
let pong_outbound_relay = service_state_handle.overwatch_handle.relay::<PongService>().connect().await?;
let pong_outbound_relay = service_state_handle
.overwatch_handle
.relay::<PongService>()
.connect()
.await?;
let mut pong_count = 0;
loop {
tokio::select! {
_ = sleep(Duration::from_secs(1)) => {
println!("Sending Ping");
pong_outbound_relay.send(PongMessage::Ping).await.unwrap();
}
Some(message) = inbound_relay.recv() => {
match message {
PingMessage::Pong => {
println!("Received Pong");
pong_count += 1;
}
}
}
true = async {
pong_count >= 5
} => {
println!("Received {} Pongs. Exiting...", pong_count);
break;
}
}
_ = sleep(Duration::from_secs(1)) => {
println!("Sending Ping");
pong_outbound_relay.send(PongMessage::Ping).await.unwrap();
}
Some(message) = inbound_relay.recv() => {
match message {
PingMessage::Pong => {
println!("Received Pong");
pong_count += 1;
}
}
}
true = async {
pong_count >= 5
} => {
println!("Received {} Pongs. Exiting...", pong_count);
break;
}
}
}
service_state_handle.overwatch_handle.shutdown().await;
Ok(())
}
}
}

View File

@ -1,13 +1,13 @@
// Crates
use overwatch_rs::DynError;
use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId};
use overwatch_rs::services::handle::ServiceStateHandle;
use overwatch_rs::services::state::{NoOperator, NoState};
use crate::messages::{PingMessage, PongMessage};
use crate::service_ping::PingService;
use overwatch_rs::services::handle::ServiceStateHandle;
use overwatch_rs::services::state::{NoOperator, NoState};
use overwatch_rs::services::{ServiceCore, ServiceData, ServiceId};
use overwatch_rs::DynError;
pub struct PongService {
service_state_handle: ServiceStateHandle<Self>
service_state_handle: ServiceStateHandle<Self>,
}
impl ServiceData for PongService {
@ -22,17 +22,21 @@ impl ServiceData for PongService {
impl ServiceCore for PongService {
fn init(service_state_handle: ServiceStateHandle<Self>) -> Result<Self, DynError> {
Ok(Self {
service_state_handle
service_state_handle,
})
}
async fn run(self) -> Result<(), DynError> {
let Self {
service_state_handle
service_state_handle,
} = self;
let mut inbound_relay = service_state_handle.inbound_relay;
let ping_outbound_relay = service_state_handle.overwatch_handle.relay::<PingService>().connect().await?;
let ping_outbound_relay = service_state_handle
.overwatch_handle
.relay::<PingService>()
.connect()
.await?;
while let Some(message) = inbound_relay.recv().await {
match message {