Skip to content

Commit 75edcfc

Browse files
authored
docs(examples): add a README to each example
Resolves libp2p#3853. Pull-Request: libp2p#3974.
1 parent 87e863e commit 75edcfc

File tree

26 files changed

+501
-223
lines changed

26 files changed

+501
-223
lines changed

examples/autonat/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Description
2+
3+
This example consists of a client and a server, which demonstrate the usage of the AutoNAT and identify protocols in **libp2p**.
4+
5+
## Usage
6+
7+
### Client
8+
9+
The client-side part of the example showcases the combination of the AutoNAT and identify protocols.
10+
The identify protocol allows the local peer to determine its external addresses, which are then included in AutoNAT dial-back requests sent to the server.
11+
12+
To run the client example, follow these steps:
13+
14+
1. Start the server by following the instructions provided in the `examples/server` directory.
15+
16+
2. Open a new terminal.
17+
18+
3. Run the following command in the terminal:
19+
```sh
20+
cargo run --bin autonat_client -- --server-address <server-addr> --server-peer-id <server-peer-id> --listen-port <port>
21+
```
22+
Note: The `--listen-port` parameter is optional and allows you to specify a fixed port at which the local client should listen.
23+
24+
### Server
25+
26+
The server-side example demonstrates a basic AutoNAT server that supports the autonat and identify protocols.
27+
28+
To start the server, follow these steps:
29+
30+
1. Open a terminal.
31+
32+
2. Run the following command:
33+
```sh
34+
cargo run --bin autonat_server -- --listen-port <port>
35+
```
36+
Note: The `--listen-port` parameter is optional and allows you to set a fixed port at which the local peer should listen.
37+
38+
## Conclusion
39+
40+
By combining the AutoNAT and identify protocols, the example showcases the establishment of direct connections between peers and the exchange of external address information.
41+
Users can explore the provided client and server code to gain insights into the implementation details and functionality of **libp2p**.

examples/autonat/src/bin/autonat_client.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
//! Basic example that combines the AutoNAT and identify protocols.
22-
//!
23-
//! The identify protocol informs the local peer of its external addresses, that are then send in AutoNAT dial-back
24-
//! requests to the server.
25-
//!
26-
//! To run this example, follow the instructions in `examples/server` to start a server, then run in a new terminal:
27-
//! ```sh
28-
//! cargo run --bin autonat_client -- --server-address <server-addr> --server-peer-id <server-peer-id> --listen-port <port>
29-
//! ```
30-
//! The `listen-port` parameter is optional and allows to set a fixed port at which the local client should listen.
21+
#![doc = include_str!("../../README.md")]
3122

3223
use clap::Parser;
3324
use futures::prelude::*;

examples/autonat/src/bin/autonat_server.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
//! Basic example for a AutoNAT server that supports the /libp2p/autonat/1.0.0 and "/ipfs/0.1.0" protocols.
22-
//!
23-
//! To start the server run:
24-
//! ```sh
25-
//! cargo run --bin autonat_server -- --listen-port <port>
26-
//! ```
27-
//! The `listen-port` parameter is optional and allows to set a fixed port at which the local peer should listen.
21+
#![doc = include_str!("../../README.md")]
2822

2923
use clap::Parser;
3024
use futures::prelude::*;

examples/chat-example/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Description
2+
3+
A basic chat application with logs demonstrating libp2p and the gossipsub protocol combined with mDNS for the discovery of peers to gossip with.
4+
It showcases how peers can connect, discover each other using mDNS, and engage in real-time chat sessions.
5+
6+
## Usage
7+
8+
1. Using two terminal windows, start two instances, typing the following in each:
9+
```sh
10+
cargo run
11+
```
12+
13+
2. Mutual mDNS discovery may take a few seconds. When each peer does discover the other
14+
it will print a message like:
15+
```sh
16+
mDNS discovered a new peer: {peerId}
17+
```
18+
19+
3. Type a message and hit return: the message is sent and printed in the other terminal.
20+
21+
4. Close with `Ctrl-c`. You can open more terminal windows and add more peers using the same line above.
22+
23+
When a new peer is discovered through mDNS, it can join the conversation, and all peers will receive messages sent by that peer.
24+
If a participant exits the application using `Ctrl-c` or any other method, the remaining peers will receive an mDNS expired event and remove the expired peer from their list of known peers.
25+
26+
## Conclusion
27+
28+
This chat application demonstrates the usage of **libp2p** and the gossipsub protocol for building a decentralized chat system.
29+
By leveraging mDNS for peer discovery, users can easily connect with other peers and engage in real-time conversations.
30+
The example provides a starting point for developing more sophisticated chat applications using **libp2p** and exploring the capabilities of decentralized communication.

examples/chat-example/src/main.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
//! A basic chat application with logs demonstrating libp2p and the gossipsub protocol
22-
//! combined with mDNS for the discovery of peers to gossip with.
23-
//!
24-
//! Using two terminal windows, start two instances, typing the following in each:
25-
//!
26-
//! ```sh
27-
//! cargo run
28-
//! ```
29-
//!
30-
//! Mutual mDNS discovery may take a few seconds. When each peer does discover the other
31-
//! it will print a message like:
32-
//!
33-
//! ```sh
34-
//! mDNS discovered a new peer: {peerId}
35-
//! ```
36-
//!
37-
//! Type a message and hit return: the message is sent and printed in the other terminal.
38-
//! Close with Ctrl-c.
39-
//!
40-
//! You can open more terminal windows and add more peers using the same line above.
41-
//!
42-
//! Once an additional peer is mDNS discovered it can participate in the conversation
43-
//! and all peers will receive messages sent from it.
44-
//!
45-
//! If a participant exits (Control-C or otherwise) the other peers will receive an mDNS expired
46-
//! event and remove the expired peer from the list of known peers.
21+
#![doc = include_str!("../README.md")]
4722

4823
use async_std::io;
4924
use futures::{future::Either, prelude::*, select};

examples/dcutr/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Description
2+
3+
The "Direct Connection Upgrade through Relay" (DCUTR) protocol allows peers in a peer-to-peer network to establish direct connections with each other.
4+
In other words, DCUTR is libp2p's version of hole-punching.
5+
This example provides a basic usage of this protocol in **libp2p**.
6+
7+
## Usage
8+
9+
To run the example, follow these steps:
10+
11+
1. Run the example using Cargo:
12+
```sh
13+
cargo run -- <OPTIONS>
14+
```
15+
Replace `<OPTIONS>` with specific options (you can use the `--help` command to see the available options).
16+
17+
### Example usage
18+
19+
- Example usage in client-listen mode:
20+
```sh
21+
cargo run -- --mode listen --secret-key-seed 42 --relay-address /ip4/127.0.0.1/tcp/12345
22+
```
23+
24+
- Example usage in client-dial mode:
25+
```sh
26+
cargo run -- --mode dial --secret-key-seed 42 --relay-address /ip4/127.0.0.1/tcp/12345 --remote-peer-id <REMOTE_PEER_ID>
27+
```
28+
29+
For this example to work, it is also necessary to turn on a relay server (you will find the related instructions in the example in the `examples/relay-server` folder).
30+
31+
## Conclusion
32+
33+
The DCUTR protocol offers a solution for achieving direct connectivity between peers in a peer-to-peer network.
34+
By utilizing hole punching and eliminating the need for signaling servers, the protocol allows peers behind NATs to establish direct connections.
35+
This example provides instructions on running an example implementation of the protocol, allowing users to explore its functionality and benefits.

examples/dcutr/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21+
#![doc = include_str!("../README.md")]
22+
2123
use clap::Parser;
2224
use futures::{
2325
executor::{block_on, ThreadPool},
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Description
2+
3+
This example showcases a basic distributed key-value store implemented using **libp2p**, along with the mDNS and Kademlia protocols.
4+
5+
## Usage
6+
7+
### Key-Value Store
8+
9+
1. Open two terminal windows, type `cargo run` and press Enter.
10+
11+
2. In terminal one, type `PUT my-key my-value` and press Enter.
12+
This command will store the value `my-value` with the key `my-key` in the distributed key-value store.
13+
14+
3. In terminal two, type `GET my-key` and press Enter.
15+
This command will retrieve the value associated with the key `my-key` from the key-value store.
16+
17+
4. To exit, press `Ctrl-c` in each terminal window to gracefully close the instances.
18+
19+
20+
### Provider Records
21+
22+
You can also use provider records instead of key-value records in the distributed store.
23+
24+
1. Open two terminal windows and start two instances of the key-value store.
25+
If your local network supports mDNS, the instances will automatically connect.
26+
27+
2. In terminal one, type `PUT_PROVIDER my-key` and press Enter.
28+
This command will register the peer as a provider for the key `my-key` in the distributed key-value store.
29+
30+
3. In terminal two, type `GET_PROVIDERS my-key` and press Enter.
31+
This command will retrieve the list of providers for the key `my-key` from the key-value store.
32+
33+
4. To exit, press `Ctrl-c` in each terminal window to gracefully close the instances.
34+
35+
36+
Feel free to explore and experiment with the distributed key-value store example, and observe how the data is distributed and retrieved across the network using **libp2p**, mDNS, and the Kademlia protocol.
37+
38+
## Conclusion
39+
40+
This example demonstrates the implementation of a basic distributed key-value store using **libp2p**, mDNS, and the Kademlia protocol.
41+
By leveraging these technologies, peers can connect, store, and retrieve key-value pairs in a decentralized manner.
42+
The example provides a starting point for building more advanced distributed systems and exploring the capabilities of **libp2p** and its associated protocols.

examples/distributed-key-value-store/src/main.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
//! A basic key value store demonstrating libp2p and the mDNS and Kademlia protocols.
22-
//!
23-
//! 1. Using two terminal windows, start two instances. If you local network
24-
//! allows mDNS, they will automatically connect.
25-
//!
26-
//! 2. Type `PUT my-key my-value` in terminal one and hit return.
27-
//!
28-
//! 3. Type `GET my-key` in terminal two and hit return.
29-
//!
30-
//! 4. Close with Ctrl-c.
31-
//!
32-
//! You can also store provider records instead of key value records.
33-
//!
34-
//! 1. Using two terminal windows, start two instances. If you local network
35-
//! allows mDNS, they will automatically connect.
36-
//!
37-
//! 2. Type `PUT_PROVIDER my-key` in terminal one and hit return.
38-
//!
39-
//! 3. Type `GET_PROVIDERS my-key` in terminal two and hit return.
40-
//!
41-
//! 4. Close with Ctrl-c.
21+
#![doc = include_str!("../README.md")]
4222

4323
use async_std::io;
4424
use futures::{prelude::*, select};

examples/file-sharing/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## Description
2+
3+
The File Sharing example demonstrates a basic file sharing application built using **libp2p**.
4+
This example showcases how to integrate **rust-libp2p** into a larger application while providing a simple file sharing functionality.
5+
6+
In this application, peers in the network can either act as file providers or file retrievers.
7+
Providers advertise the files they have available on a Distributed Hash Table (DHT) using `libp2p-kad`.
8+
Retrievers can locate and retrieve files by their names from any node in the network.
9+
10+
## How it Works
11+
12+
Let's understand the flow of the file sharing process:
13+
14+
- **File Providers**: Nodes A and B serve as file providers.
15+
Each node offers a specific file: file FA for node A and file FB for node B.
16+
To make their files available, they advertise themselves as providers on the DHT using `libp2p-kad`.
17+
This enables other nodes in the network to discover and retrieve their files.
18+
19+
- **File Retrievers**: Node C acts as a file retriever.
20+
It wants to retrieve either file FA or FB.
21+
Using `libp2p-kad`, it can locate the providers for these files on the DHT without being directly connected to them.
22+
Node C connects to the corresponding provider node and requests the file content using `libp2p-request-response`.
23+
24+
- **DHT and Network Connectivity**: The DHT (Distributed Hash Table) plays a crucial role in the file sharing process.
25+
It allows nodes to store and discover information about file providers.
26+
Nodes in the network are interconnected via the DHT, enabling efficient file discovery and retrieval.
27+
28+
## Architectural Properties
29+
30+
The File Sharing application has the following architectural properties:
31+
32+
- **Clean and Clonable Interface**: The application provides a clean and clonable async/await interface, allowing users to interact with the network layer seamlessly.
33+
The `Client` module encapsulates the necessary functionality for network communication.
34+
35+
- **Efficient Network Handling**: The application operates with a single task that drives the network layer.
36+
This design choice ensures efficient network communication without the need for locks or complex synchronization mechanisms.
37+
38+
## Usage
39+
40+
To set up a simple file sharing scenario with a provider and a retriever, follow these steps:
41+
42+
1. **Start a File Provider**: In one terminal, run the following command to start a file provider node:
43+
```sh
44+
cargo run -- --listen-address /ip4/127.0.0.1/tcp/40837 \
45+
--secret-key-seed 1 \
46+
provide \
47+
--path <path-to-your-file> \
48+
--name <name-for-others-to-find-your-file>
49+
```
50+
This command initiates a node that listens on the specified address and provides a file located at the specified path.
51+
The file is identified by the provided name, which allows other nodes to discover and retrieve it.
52+
53+
2. **Start a File Retriever**: In another terminal, run the following command to start a file retriever node:
54+
```sh
55+
cargo run -- --peer /ip4/127.0.0.1/tcp/40837/p2p/12D3KooWPjceQrSwdWXPyLLeABRXmuqt69Rg3sBYbU1Nft9HyQ6X \
56+
get \
57+
--name <name-for-others-to-find-your-file>
58+
```
59+
This command initiates a node that connects to the specified peer (the provider) and requests the file with the given name.
60+
61+
Note: It is not necessary for the retriever node to be directly connected to the provider.
62+
As long as both nodes are connected to any node in the same DHT network, the file can be successfully retrieved.
63+
64+
This File Sharing example demonstrates the fundamental concepts of building a file sharing application using **libp2p**.
65+
By understanding the flow and architectural properties of this example, you can leverage the power of **libp2p** to integrate peer-to-peer networking capabilities into your own applications.
66+
67+
## Conclusion
68+
69+
The File Sharing example provides a practical implementation of a basic file sharing application using **libp2p**.
70+
By leveraging the capabilities of **libp2p**, such as the DHT and network connectivity protocols, it demonstrates how peers can share files in a decentralized manner.
71+
72+
By exploring and understanding the file sharing process and architectural properties presented in this example, developers can gain insights into building their own file sharing applications using **libp2p**.

0 commit comments

Comments
 (0)