You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This task handle should be `await`'ed at the end of the program to ensure thread has exited correctly.
68
+
69
+
### Commander
70
+
71
+
You can communicate with this thread using the `commander` entity. For example, here is how one would subscribe to a topic:
72
+
73
+
```rs
74
+
commander
75
+
.subscribe("your-topic")
76
+
.await
77
+
.expect("could not subscribe");
78
+
```
79
+
80
+
### Channel
81
+
82
+
The message channel should be handled with `recv` (or `recv_many` to process in batches) to process the GossipSub messages.
83
+
84
+
```rs
85
+
loop {
86
+
matchmsg_rx.recv().await {
87
+
Some(msg) => {
88
+
todo!("handle stuff")
89
+
}
90
+
None=> {
91
+
todo!("channel closed");
92
+
break
93
+
}
94
+
}
95
+
}
96
+
```
97
+
98
+
### Interactions
99
+
100
+
Here is how the whole thing works in a bit more detail:
16
101
17
102
-**Events**: When a message is received within the Swarm event handler, it is returned via a `mpsc` channel. Here, the p2p is `Sender` and your application must be the `Receiver`. The client handles many events, and only sends GossipSub message receipts via this channel so that the application can handle them however they would like.
18
103
@@ -54,35 +139,3 @@ sequenceDiagram
54
139
P ->> C: o_tx.send(output)
55
140
deactivate P
56
141
```
57
-
58
-
<!--
59
-
60
-
FIXME: REMOVE COMMENTS
61
-
62
-
You can create the client as follows:
63
-
64
-
```rs
65
-
use dkn_p2p::DriaP2PClient;
66
-
67
-
// your wallet, or something random maybe
68
-
let keypair = Keypair::generate_secp256k1();
69
-
70
-
// your listen address
71
-
let addr = Multiaddr::from_str("/ip4/0.0.0.0/tcp/4001")?;
72
-
73
-
// static bootstrap & relay addresses
74
-
let bootstraps = vec![Multiaddr::from_str(
75
-
"some-multiaddrs-here"
76
-
)?];
77
-
let relays = vec![Multiaddr::from_str(
78
-
"some-multiaddrs-here"
79
-
)?];
80
-
81
-
// protocol version number, usually derived as `{major}.{minor}`
82
-
let version = "0.2";
83
-
84
-
// create the client!
85
-
let mut client = DriaP2PClient::new(keypair, addr, &bootstraps, &relays, "0.2")?;
86
-
```
87
-
88
-
Then, you can use its underlying functions, such as `subscribe`, `process_events` and `unsubscribe`. In particular, `process_events` handles all p2p events and returns a GossipSub message when it is received. -->
0 commit comments