Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hyperswarm example (draft) #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/hyperchat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "example-hyperchat"
version = "0.1.0"
edition = "2021"


[[bin]]
name = "hyperswarm"
path = "main.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hyperswarm = { git = "https://github.com/datrs/hyperswarm-rs.git" }
# hyperswarm = { path = "../../../hyperswarm" }
cable = { path = "../.." }
async-std = "1.11.0"
argmap = "1.1.1"
env_logger = "0.9.0"
85 changes: 85 additions & 0 deletions examples/hyperchat/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use async_std::{io, prelude::*, task};
use cable::{Cable, ChannelOptions, MemoryStore};
use hyperswarm::{hash_topic, BootstrapNode, Config, Hyperswarm, TopicConfig};

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

const BOOTSTRAP_ADDR: &str = "127.0.0.1:6666";

fn main() -> Result<(), Error> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "debug,hyperswarm-dht=trace,hyperswarm=trace");
}
env_logger::init();
let (args, _argv) = argmap::parse(std::env::args());

task::block_on(async move {
match args.get(1).map(|x| x.as_str()) {
Some("join") => {
let topic = args.get(2).expect("topic is required");
run_chat(topic.to_owned()).await.unwrap();
}
Some("bootstrap") => {
run_bootstrap().await.unwrap();
}
_ => {
eprintln!("Command is either `join` or `bootstrap`");
}
}
});
Ok(())
}
async fn run_bootstrap() -> Result<(), Error> {
let node = BootstrapNode::with_addr(BOOTSTRAP_ADDR)?;
let (addr, task) = node.run().await?;
eprintln!("Running bootstrap node on {:?}", addr);
task.await?;
Ok(())
}
async fn run_chat(topic: String) -> Result<(), Error> {
let store = MemoryStore::default();
let cable = Cable::new(store);
{
let opts = ChannelOptions {
channel: "default".as_bytes().to_vec(),
time_start: 0,
//time_end: now(),
time_end: 0,
limit: 20,
};
let mut cable = cable.clone();
task::spawn(async move {
let mut msg_stream = cable.open_channel(&opts).await.unwrap();
while let Some(msg) = msg_stream.next().await {
println!["msg={:?}", msg];
}
});
}
{
let mut cable = cable.clone();
task::spawn(async move {
let stdin = io::stdin();
let mut line = String::new();
loop {
stdin.read_line(&mut line).await.unwrap();
if line.is_empty() {
break;
}
let channel = "default".as_bytes();
let text = line.trim_end().as_bytes();
cable.post_text(channel, &text).await.unwrap();
line.clear();
}
});
}

let topic = hash_topic(b"example:hyperchat", topic.as_bytes());
let config = Config::default().set_bootstrap_nodes(&[BOOTSTRAP_ADDR]);
let mut swarm = Hyperswarm::bind(config).await?;
swarm.configure(topic, TopicConfig::both());
while let Some(stream) = swarm.next().await.transpose()? {
cable.listen(stream).await.unwrap();
}

Ok(())
}