/kademlia

Dead simple C++11 kademlia distributed hash table library

Primary LanguageC++

Build Status Linux Build status Windows Coverage Status

Description

C++11 distributed hash table library

This software is experimental and under active development.

Example

Initialization:

#include <kademlia/session.hpp>

// [...]

{
    // The session need to know at least one member of the
    // distributed table to find neighbors.
    kademlia::endpoint const initial_peer{ "1.2.3.4", 27980 };

    // If an error occurs, this will throw.
    // Following runtime errors will be reported
    // through an std::error_code.
    kademlia::session s{ initial_peer };

    // Run the library main loop in a dedicated thread.
    auto main_loop_result = std::async( std::launch::async
                                      , &kademlia::session::run, &s );

    // [...]

Searching for value associated with "key1":

    // This callback will be called once the load succeed or failed.
    auto on_load = []( std::error_code const& failure
                     , kademlia::session::data_type const& data )
    {
        if ( failure )
            std::cerr << failure.message() << std::endl;
        else
            std::copy( data.begin(), data.end()
                     , std::ostream_iterator< std::uint32_t >{ std::cout, " " } );
    };

    // Schedule an asynchronous load.
    s.async_load( "key1", on_load );

    // [...]

Saving a data into the table is similar:

    // Copy data from your source.
    kademlia::session::data_type const data{ ?.begin(), ?.end() };

    // Create the handler.
    auto on_save = []( std::error_code const& failure )
    {
        if ( failure )
            std::cerr << failure.message() << std::endl;
    }

    // And perform the saving.
    s.async_save( "key2", data, on_save );

    // [...]

Destruction:

    // Once we've finished, abort the session::run()
    // blocking call.
    s.abort();

    auto failure = main_loop_result.get();
    if ( failure != kademlia::RUN_ABORTED )
        std::cerr << failure.message() << std::endl;
}

Development

Bug and feature requests

Supported targets

OSCompiler
FreeBSDclang
Fedoragcc
Ubuntugcc
Centosgcc
Debiangcc
Windows 7+MSVC

Project structure

kademlia/
    |
    |-- include             API public headers files.
    |-- src                 Implementation sources and headers files.
    |-- test/unit_tests     Unit tests.
    |-- test/simulator      Chain test application.
    |-- doc                 User and development documentation.