Skip to content

Commit 661132d

Browse files
committed
Add a builder
this way people don't have to depend on pkarr directly if they just want the default config.
1 parent 75fd014 commit 661132d

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

iroh-pkarr-node-discovery/examples/chat.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ use std::str::FromStr;
1212

1313
use anyhow::Context;
1414
use iroh_net::{magic_endpoint::get_remote_node_id, MagicEndpoint, NodeId};
15-
use pkarr::PkarrClient;
1615

1716
const CHAT_ALPN: &[u8] = b"pkarr-discovery-demo-chat";
1817

1918
async fn chat_server() -> anyhow::Result<()> {
20-
let pkarr = PkarrClient::new();
2119
let secret_key = iroh_net::key::SecretKey::generate();
2220
let node_id = secret_key.public();
23-
let discovery = iroh_pkarr_node_discovery::PkarrNodeDiscovery::new(pkarr, Some(&secret_key));
21+
let discovery = iroh_pkarr_node_discovery::PkarrNodeDiscovery::builder()
22+
.secret_key(&secret_key)
23+
.build();
2424
let endpoint = MagicEndpoint::builder()
2525
.alpns(vec![CHAT_ALPN.to_vec()])
2626
.secret_key(secret_key)
@@ -51,11 +51,10 @@ async fn chat_server() -> anyhow::Result<()> {
5151
}
5252

5353
async fn chat_client(remote_node_id: NodeId) -> anyhow::Result<()> {
54-
let pkarr = PkarrClient::new();
5554
let secret_key = iroh_net::key::SecretKey::generate();
5655
let node_id = secret_key.public();
5756
// note: we don't pass a secret key here, because we don't need to publish our address, don't spam the DHT
58-
let discovery = iroh_pkarr_node_discovery::PkarrNodeDiscovery::new(pkarr, None);
57+
let discovery = iroh_pkarr_node_discovery::PkarrNodeDiscovery::builder().build();
5958
// we do not need to specify the alpn here, because we are not going to accept connections
6059
let endpoint = MagicEndpoint::builder()
6160
.secret_key(secret_key)

iroh-pkarr-node-discovery/src/lib.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ const INITIAL_PUBLISH_DELAY: Duration = Duration::from_millis(500);
4040
/// and publishes them to the bittorrent mainline DHT.
4141
///
4242
/// Calling publish will start a background task that periodically publishes the node address.
43-
#[derive(Debug, Clone)]
43+
#[derive(Debug, Clone, Default)]
4444
pub struct PkarrNodeDiscovery(Arc<Inner>);
4545

46-
#[derive(Debug)]
46+
#[derive(Debug, Default)]
4747
struct Inner {
4848
/// Pkarr client for interacting with the DHT.
4949
pkarr: PkarrClient,
@@ -56,12 +56,45 @@ struct Inner {
5656
keypair: Option<pkarr::Keypair>,
5757
}
5858

59+
/// Builder for PkarrNodeDiscovery.
60+
#[derive(Debug, Default)]
61+
pub struct Builder<'a> {
62+
client: Option<PkarrClient>,
63+
secret_key: Option<&'a SecretKey>,
64+
}
65+
66+
impl<'a> Builder<'a> {
67+
/// Explicitly set the pkarr client to use.
68+
pub fn client(mut self, client: PkarrClient) -> Self {
69+
self.client = Some(client);
70+
self
71+
}
72+
73+
/// Set the secret key to use for signing the DNS packets.
74+
///
75+
/// Without a secret key, the node will not publish its address to the DHT.
76+
pub fn secret_key(mut self, secret_key: &'a SecretKey) -> Self {
77+
self.secret_key = Some(secret_key);
78+
self
79+
}
80+
81+
/// Build the discovery mechanism.
82+
pub fn build(self) -> PkarrNodeDiscovery {
83+
let client = self.client.unwrap_or_else(PkarrClient::new);
84+
PkarrNodeDiscovery::new(client, self.secret_key)
85+
}
86+
}
87+
5988
impl PkarrNodeDiscovery {
89+
pub fn builder() -> Builder<'static> {
90+
Builder::default()
91+
}
92+
6093
/// Create a new discovery mechanism.
6194
///
6295
/// If a secret key is provided, the node will publish its address to the DHT.
6396
/// If no secret key is provided, publish will be a no-op, but resolving other nodes will still work.
64-
pub fn new(pkarr: PkarrClient, secret_key: Option<&SecretKey>) -> Self {
97+
fn new(pkarr: PkarrClient, secret_key: Option<&SecretKey>) -> Self {
6598
let keypair =
6699
secret_key.map(|secret_key| pkarr::Keypair::from_secret_key(&secret_key.to_bytes()));
67100
Self(Arc::new(Inner {

0 commit comments

Comments
 (0)