BootstraperΒΆ
The following example demonstrates a console application used to bootstrap a Kademlia network.
1#include <cstdint>
2#include <cstdlib>
3
4#include <future>
5#include <iostream>
6#include <iterator>
7#include <sstream>
8
9#include <kademlia/endpoint.hpp>
10#include <kademlia/error.hpp>
11#include <kademlia/first_session.hpp>
12
13namespace k = kademlia;
14
15int main
16 ( int argc
17 , char** argv )
18{
19 // Check command line arguments count
20 if ( argc != 2 )
21 {
22 std::cerr << "usage: " << argv[0] << " <PORT>" << std::endl;
23 return EXIT_FAILURE;
24 }
25
26 // Parse command line arguments
27 std::uint16_t const port = std::atoi( argv[1] );
28
29 // Create the session
30 k::first_session session{ k::endpoint{ "0.0.0.0", port }
31 , k::endpoint{ "::", port } };
32
33 // Start the main loop thread
34 auto main_loop = std::async( std::launch::async
35 , &k::first_session::run, &session );
36
37 // Wait for exit request
38 std::cout << "Press any key to exit" << std::endl;
39 std::cin.get();
40
41 // Stop the main loop thread
42 session.abort();
43
44 // Wait for the main loop thread termination
45 auto failure = main_loop.get();
46 if ( failure != k::RUN_ABORTED )
47 std::cerr << failure.message() << std::endl;
48}