Skip to content

Instantly share code, notes, and snippets.

@AmarOk1412
Last active April 8, 2018 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmarOk1412/c9ef8b1a7c90cbd7cb3a6dbf47aaa170 to your computer and use it in GitHub Desktop.
Save AmarOk1412/c9ef8b1a7c90cbd7cb3a6dbf47aaa170 to your computer and use it in GitHub Desktop.
#include <opendht.h>
#include <vector>
int main()
{
dht::DhtRunner node;
// Launch a dht node on a new thread, using a
// generated RSA key pair, and listen on port 4222.
node.run(4222, dht::crypto::generateIdentity(), true);
// Join the network through any running node,
// here using a known bootstrap node.
node.bootstrap("bootstrap.ring.cx", "4222");
for (int i = 0; i < 10000000; ++i) {}
// put some data on the dht
std::string v = "test";
std::cout << "test" << std::endl;
node.put("test2", std::vector<uint8_t> {v.begin(), v.end()}, [](bool ok) {
std::cout << "Put: " << (ok ? "success" : "failure") << std::endl;
});
for (int i = 0; i < 10000000; ++i) {}
// get data from the dht
node.get("test2", [](const std::vector<std::shared_ptr<dht::Value>>& values) {
std::cout << "Found!";
// Callback called when values are found
for (const auto& value : values)
std::cout << "Found value: " << *value << std::endl;
return true; // return false to stop the search
});
node.shutdown([&]()
{
std::lock_guard<std::mutex> lk(m);
done = true;
cv.notify_all();
});
// wait for shutdown
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, [&](){ return done.load(); });
// wait for dht threads to end
node.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment