|
1 | | -use std::{ |
2 | | - io::{self, Read, Write as _}, |
3 | | - net::{Ipv4Addr, SocketAddrV4, TcpListener, TcpStream}, |
4 | | -}; |
| 1 | +use std::{error::Error, net::Ipv4Addr, time::Duration}; |
5 | 2 |
|
6 | 3 | use clap::Parser as _; |
| 4 | +use libp2p::{ |
| 5 | + Multiaddr, |
| 6 | + gossipsub::{self, AllowAllSubscriptionFilter, Config, IdentityTransform, MessageAuthenticity}, |
| 7 | + kad::{self, store::MemoryStore}, |
| 8 | + multiaddr::Protocol, |
| 9 | + noise, tcp, yamux, |
| 10 | +}; |
7 | 11 | use peer_node::{ |
8 | | - cli::{Args, Role}, |
9 | | - comms::message::Message, |
| 12 | + cli::Args, |
| 13 | + network::{ |
| 14 | + behaviour::PeerBehavior, |
| 15 | + event::{Topic, event_runner}, |
| 16 | + }, |
10 | 17 | }; |
11 | 18 |
|
12 | 19 | #[tokio::main] |
13 | | -async fn main() -> Result<(), std::io::Error> { |
| 20 | +async fn main() -> Result<(), Box<dyn Error>> { |
14 | 21 | peer_node::tracing::init("info"); |
15 | 22 | let args = Args::parse(); |
16 | 23 |
|
17 | | - let address = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), args.port); |
18 | | - let listerner = TcpListener::bind(address)?; |
| 24 | + let ip_addr = Ipv4Addr::new(0, 0, 0, 0); |
19 | 25 |
|
20 | | - tracing::info!("Node address: {}", address); |
21 | | - tracing::info!("A {}", args.role); |
| 26 | + let peer_multi_addr = Multiaddr::from(ip_addr).with(Protocol::Tcp(0)); |
22 | 27 |
|
23 | | - match args.role { |
24 | | - Role::Receiver => loop { |
25 | | - for mut incoming_stream in listerner.incoming().flatten() { |
26 | | - let mut msg = [0; 5]; |
27 | | - let _byte_count = incoming_stream.read(&mut msg)?; |
| 28 | + tracing::info!("A {}", args.role); |
28 | 29 |
|
29 | | - let msg: Message = String::from_utf8_lossy(&msg).trim().to_string().into(); |
| 30 | + let mut swarm = libp2p::SwarmBuilder::with_new_identity() |
| 31 | + .with_tokio() |
| 32 | + .with_tcp( |
| 33 | + tcp::Config::default(), |
| 34 | + noise::Config::new, |
| 35 | + yamux::Config::default, |
| 36 | + )? |
| 37 | + .with_behaviour(|keypair| { |
| 38 | + let peer_id = keypair.public().to_peer_id(); |
| 39 | + let store = MemoryStore::new(peer_id); |
| 40 | + let kademlia = kad::Behaviour::new(peer_id, store); |
30 | 41 |
|
31 | | - // If it's a rememberMe, store to some DHT and if Comms: Act as instructed |
| 42 | + let gossipsub: gossipsub::Behaviour<IdentityTransform, AllowAllSubscriptionFilter> = |
| 43 | + gossipsub::Behaviour::new( |
| 44 | + MessageAuthenticity::Signed(keypair.clone()), |
| 45 | + Config::default(), |
| 46 | + ) |
| 47 | + .expect("Gossipsub initiation fails"); |
32 | 48 |
|
33 | | - tracing::info!("Message received: {msg:?}"); |
| 49 | + PeerBehavior { |
| 50 | + kademlia, |
| 51 | + gossipsub, |
34 | 52 | } |
35 | | - }, |
36 | | - Role::Sender => { |
37 | | - let mut msg = String::new(); |
38 | | - io::stdin().read_line(&mut msg)?; |
39 | | - |
40 | | - tracing::info!("Sending: {msg}"); |
41 | | - |
42 | | - let mut outgoing_stream = TcpStream::connect(args.address)?; |
43 | | - |
44 | | - let msg: Message = msg.into(); |
| 53 | + })? |
| 54 | + .with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) |
| 55 | + .build(); |
45 | 56 |
|
46 | | - outgoing_stream.write_all(msg.to_string().as_bytes())?; |
| 57 | + let topic = gossipsub::IdentTopic::new("peer-network"); |
| 58 | + swarm.behaviour_mut().gossipsub.subscribe(&topic)?; |
47 | 59 |
|
48 | | - // Wait for the message to be sent before exiting |
49 | | - outgoing_stream.flush()?; |
| 60 | + swarm.listen_on(peer_multi_addr)?; |
50 | 61 |
|
51 | | - Ok(()) |
52 | | - } |
53 | | - } |
| 62 | + event_runner( |
| 63 | + swarm, |
| 64 | + args.role, |
| 65 | + args.peer_address, |
| 66 | + Topic(topic.to_string()), |
| 67 | + ) |
| 68 | + .await |
54 | 69 | } |
0 commit comments