Skip to content

Commit f10ed77

Browse files
feat(network) : implement network service to manage channel connection and protocol command (dev-sys-do#4)
* feat(command): implement CommandError enum and CommandService trait with CommandServiceImpl * feat(network): add network service impl with mpsc channel buffer * chore(dependencies): update dependencies in Cargo.toml * feat(main): refactor main function to use async/await and integrate network service * feat(network): fix reject other connection and send message to sender * fix(command): return correct block index in OkHousten response
1 parent 7eb0d9f commit f10ed77

File tree

14 files changed

+508
-49
lines changed

14 files changed

+508
-49
lines changed

Cargo.lock

Lines changed: 212 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ version = "0.0.0"
44
edition = "2024"
55

66
[dependencies]
7+
async-trait = "0.1.89"
78
clap = "4.5.50"
9+
tokio = { version = "1", features = ["net", "rt-multi-thread", "rt", "fs", "io-util", "sync", "macros"] }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[derive(Debug)]
2+
pub enum CommandError {
3+
InvalidCommand,
4+
ExecutionFailed(String),
5+
}

src/core/domain/command/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod entities;
2+
pub mod ports;
3+
pub mod services;

src/core/domain/command/ports.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use crate::core::domain::{command::entities::CommandError, network::entities::ProtocolMessage};
2+
3+
pub trait CommandService: Send + Sync {
4+
fn execute_protocol_command(
5+
&self,
6+
msg: &ProtocolMessage,
7+
) -> impl Future<Output = Result<ProtocolMessage, CommandError>> + Send;
8+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::core::domain::{
2+
command::{entities::CommandError, ports::CommandService},
3+
network::entities::ProtocolMessage,
4+
};
5+
6+
#[derive(Clone)]
7+
pub struct CommandServiceImpl {}
8+
9+
impl CommandServiceImpl {
10+
pub fn new() -> Self {
11+
CommandServiceImpl {}
12+
}
13+
}
14+
15+
impl CommandService for CommandServiceImpl {
16+
async fn execute_protocol_command(
17+
&self,
18+
msg: &ProtocolMessage,
19+
) -> Result<ProtocolMessage, CommandError> {
20+
match msg {
21+
ProtocolMessage::Hello { filename, filesize } => {
22+
// Démarrer la logique de préparation de réception
23+
Ok(ProtocolMessage::Ok)
24+
}
25+
ProtocolMessage::Yeet {
26+
block_index,
27+
block_size,
28+
check_sum,
29+
} => {
30+
// Logique pour gérer la réception d'un bloc de données
31+
Ok(ProtocolMessage::OkHousten(block_index.clone()))
32+
}
33+
// ... autres commandes
34+
_ => Err(CommandError::InvalidCommand),
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)