void enableDhtLogging(dht::DhtRunner& dht) { dht.setLoggers( [](char const* m, va_list args){ dout << "ERR"; printDhtLog(dout, m, args); }, [](char const* m, va_list args){ dout << "WARN"; printDhtLog(dout, m, args); }, [](char const* m, va_list args){ printDhtLog(dout, m, args); } ); }
void enableFileLogging(dht::DhtRunner &dht, const std::string &path) { auto logfile = std::make_shared<std::fstream>(); logfile->open(path, std::ios::out); dht.setLoggers( [=](char const *m, va_list args) { printLog(*logfile, m, args); }, [=](char const *m, va_list args) { printLog(*logfile, m, args); }, [=](char const *m, va_list args) { printLog(*logfile, m, args); } ); }
void enableLogging(dht::DhtRunner &dht) { dht.setLoggers( [](char const *m, va_list args) { std::cerr << red; printLog(std::cerr, m, args); std::cerr << def; }, [](char const *m, va_list args) { std::cout << yellow; printLog(std::cout, m, args); std::cout << def; }, [](char const *m, va_list args) { printLog(std::cout, m, args); } ); }
size_t listen(dht::DhtRunner& node, std::string chain, map_type& map) { std::future<size_t> token = node.listen(chain, [&map](const std::vector<std::shared_ptr<dht::Value>>& values) { // For every value found... for (const auto& value : values) { // Unpack then register and display it, if it's a new value. std::string content = value->unpack<std::string>(); if(!map.count(content)) { map.insert(std::make_pair(content, get_timestamp())); disp(content); } } // Continue lookup until no values are left. return true; }); if(token.wait_for(std::chrono::seconds(1)) != std::future_status::ready) { verbose("Warning: Could not create a listener since 1000ms."); verbose(" Trying again for 30s..."); if(token.wait_for(std::chrono::seconds(30)) != std::future_status::ready) verbose("Error: Failure."); else verbose(" Done."); } auto v = token.get(); verbose("Starting listening to " + chain + " with token " + std::to_string(v) + "."); return v; }
void disableLogging(dht::DhtRunner &dht) { dht.setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG); }
void build_in_dht() { #ifdef DHT static bool running = false; static mutex mut; static dht::DhtRunner ht; static int port = 4443; if (!running) { running = true; int port = 4443; dout << "firing up DHT on port " << port << endl; // Launch a dht node on a new thread, using a // generated RSA key pair, and listen on port 4222. ht.run(port, dht::crypto::generateIdentity(), true); // Join the network through any running node, // here using a known bootstrap node. ht.bootstrap("127.0.0.1", "4444"); // put some data on the dht std::vector<uint8_t> some_data(5, 10); ht.put("unique_key", some_data); // put some data on the dht, signed with our generated private key ht.putSigned("unique_key_42", some_data); } auto ht_ = &ht; EEE; string bu = "http://idni.org/dht#put"; auto bui = dict.set(mkiri(pstr(bu))); builtins[bui].push_back( [bu, entry, ht_](Thing *dummy, Thing *x) mutable { setproc(bu); TRACE_ENTRY; dout <<"sssss" << endl; switch(entry){ case 0: x = getValue(x); if(is_node(*x)) { node n = dict[get_node(*x)]; string v = *n.value; string key,val; if (n._type == node::IRI) { if (has(mykb->first, v)) { key = "root_graph_" + v; stringstream ss; ss << mykb->first[v]; val = ss.str(); } else { string h = strhash(v); key = "root_iri_" + h; val = v; } } else if (n._type == node::LITERAL) { string h = strhash(v); key = "root_lit_" + h; val = v; } else { dout << "nope." << endl; DONE; } dout << "putting " << key << "=" << val << endl; ht_->put(key, val); } else dout << "nope." << endl; END; } }); bu = "http://idni.org/dht#dbg"; bui = dict.set(mkiri(pstr(bu))); builtins[bui].push_back( [bu, entry, ht_](Thing *dummy, Thing *x) mutable { setproc(bu); TRACE_ENTRY; switch(entry){ case 0: x = getValue(x); if(is_node(*x)) { node n = dict[get_node(*x)]; string v = *n.value; if (v == "on") { MSG("dht dbg on"); enableDhtLogging(*ht_); } else{ MSG("dht dbg off"); ht_->setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG); } } END; } }); bu = "http://idni.org/dht#setPort"; bui = dict.set(mkiri(pstr(bu))); builtins[bui].push_back( [bu, entry, ht_](Thing *dummy, Thing *x) mutable { setproc(bu); TRACE_ENTRY; switch(entry){ case 0: x = getValue(x); if(is_node(*x)) { node n = dict[get_node(*x)]; string v = *n.value; if (v == "on") { MSG("dht dbg on"); enableDhtLogging(*ht_); } else{ MSG("dht dbg off"); ht_->setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG); } } END; } }); // get data from the dht ht_->get("other_unique_key", [](const std::vector<std::shared_ptr<dht::Value>>& values) { // Callback called when values are found for (const auto& value : values) dout << "Found value: " << *value << std::endl; return true; // return false to stop the search }); #endif }