[][src]Crate chamomile

chamomile is a crate for building a solid and efficient p2p network.

Example Use

We running a p2p peer, and if others add, we will get the info.

This example is not tested
use async_std::task;
use std::env::args;
use std::net::SocketAddr;

use chamomile::prelude::{start, Config, ReceiveMessage, SendMessage};

fn main() {
    task::block_on(async {
        let self_addr: SocketAddr = args()
            .nth(1)
            .expect("missing path")
            .parse()
            .expect("invalid addr");

        let (peer_id, send, recv) = start(Config::default(self_addr)).await.unwrap();
        println!("peer id: {}", peer_id.short_show());

        if args().nth(2).is_some() {
            let remote_addr: SocketAddr = args().nth(2).unwrap().parse().expect("invalid addr");
            println!("start connect to remote: {}", remote_addr);
            send.send(SendMessage::Connect(remote_addr, None))
                .await;
        }

        while let Some(message) = recv.recv().await {
            match message {
                ReceiveMessage::Data(peer_id, bytes) => {
                    println!("recv data from: {}, {:?}", peer_id.short_show(), bytes);
                }
                ReceiveMessage::PeerJoin(peer_id, _addr, join_data) => {
                    println!("peer join: {:?}, join data: {:?}", peer_id, join_data);
                    send.send(SendMessage::PeerJoinResult(peer_id, true, false, vec![1]))
                        .await;
                    println!("Debug: when join send message test: {:?}", vec![1, 2, 3, 4]);
                    send.send(SendMessage::Data(peer_id, vec![1, 2, 3, 4])).await;
                }
                ReceiveMessage::PeerLeave(peer_id) => {
                    println!("peer_leave: {:?}", peer_id);
                }
            }
        }
    });
}

Features

  • Directly connection & DHT-based connection & Relay connection.
  • Diff transports: UDP/TCP/UDP-Based Special Protocol.
  • Support common broadcast.

Modules

prelude
primitives
transports