Quick help for libdht

Introduction

libdht is a cross-platform C++ library providing an interface for accessing Distributed Hash Tables (DHTs).

At the moment eDonkey's Overnet network is implemented using KadC library.

The library provides an event based access to following DHT features:

Metadata for a key/value pair is also supported if the underlying network supports it.

Download

Go to the sourceforge project page for SVN access and releases: http://sourceforge.net/projects/libdht

Building

Dependencies

Compiling

After compiling and installing ACE, KadC and SCons, copy the file build_config.py.dist to build_config.py. Edit build_config.py to have correct paths to the needed libraries.

Compiling after that is a simple matter of issuing

   scons 

The library will be compiled under build directory, under the corresponding architecture (eg. build/win32 or build/linux etc.). Compilation has been tested on win32 with MinGW and under Linux. Other platforms may need additional compilation settings.

Examples

See example subdirectory for some examples. At the moment, there is no makefile or a scons file for creating executables of these yet, so if you want to compile these you have to create them yourself. Something to change for the future, and if someone comes up with a good scons file for it before that, send me a patch.

A quite minimal example of how to use the library is the following example which tries finding values for the key "ubuntu" from Overnet DHT:

#include <string>
#include <iostream>

#include <dht/client.h>
#include <dht/kadc/client.h>

// A simple handler that just outputs the
// found data
class handle_search : public dht::search_handler {
public:
    bool find_started; 
    bool find_finished;

    handle_search() : find_started(false), find_finished(false) {}
    
    // dht::search_handler interface
    virtual int found(const dht::key &k, const dht::value &v) {
        std::cout << "Found key: " << k.c_str() << " "
                  << "with value: " << v.c_str() << std::endl;
        // Output meta information, if any
        const dht::name_value_map &meta = v.meta();
        dht::name_value_map::const_iterator i = meta.begin();
        for (; i != meta.end(); i++) {
            std::cout << "  Meta key/value: " 
                      << i->first << "/"
                      << i->second << std::endl;
        }
        return 0;
    }

    virtual void success(const dht::key &k) {
        std::cout << "Finding finished for key: " << k.c_str() << std::endl;
        find_finished = true;
    }
    
    virtual void failure(const dht::key &k, int /*errcode*/, const char *errstr) {
        std::cout << "Finding finished with error key: " << k.c_str() 
                  << " error: " << errstr << std::endl;
        find_finished = true;
    }    
};

int main(int, char **) {
    dht::kadc::client client;
    dht::name_value_map conf;

    conf.set("init_file", "kadc.ini");
    client.init(conf);
    
    client.connect();
    
    handle_search handler;
    
    bool done = false;
    while (!done) {
        client.process();

        switch (client.in_state()) {
        case dht::client::connected:
            if (handler.find_finished) {
                std::cout << "Find done, disconnecting" << std::endl;
                client.disconnect();
                done = true;
            } else if (!handler.find_started) {
                client.find("ubuntu", &handler);
                handler.find_started = true;
            }
            break;
        case dht::client::disconnected:
            done = true;
            break;
        }
    }
    return 0;
}

Generated on Thu Dec 6 17:23:38 2007 for libdht by  doxygen 1.5.3