Skip to content
Snippets Groups Projects
Commit 830c39e8 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

tools: add usage (-h, --help)

parent 761b9b73
No related branches found
No related tags found
No related merge requests found
...@@ -33,8 +33,6 @@ ...@@ -33,8 +33,6 @@
extern "C" { extern "C" {
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
} }
#include <opendht.h>
#include <iostream>
#include <ctime> #include <ctime>
using namespace dht; using namespace dht;
......
...@@ -33,16 +33,25 @@ ...@@ -33,16 +33,25 @@
extern "C" { extern "C" {
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
} }
#include <opendht.h>
#include <iostream>
#include <set> #include <set>
using namespace dht; using namespace dht;
void print_usage() {
std::cout << "Usage: dhtnode [-p local_port] [-b bootstrap_host]" << std::endl << std::endl;
std::cout << "dhtnode, a simple OpenDHT command line node runner." << std::endl;
std::cout << "Report bugs to: http://opendht.net" << std::endl;
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
auto params = parseArgs(argc, argv); auto params = parseArgs(argc, argv);
if (params.help) {
print_usage();
return 0;
}
// TODO: remove with GnuTLS >= 3.3 // TODO: remove with GnuTLS >= 3.3
int rc = gnutls_global_init(); int rc = gnutls_global_init();
...@@ -53,8 +62,7 @@ main(int argc, char **argv) ...@@ -53,8 +62,7 @@ main(int argc, char **argv)
auto crt_tmp = dht::crypto::generateIdentity("DHT Node", ca_tmp); auto crt_tmp = dht::crypto::generateIdentity("DHT Node", ca_tmp);
DhtRunner dht; DhtRunner dht;
dht.run(params.port, crt_tmp, true, [](dht::Dht::Status /* ipv4 */, dht::Dht::Status /* ipv6 */) { dht.run(params.port, crt_tmp, true, [](dht::Dht::Status /* ipv4 */, dht::Dht::Status /* ipv6 */) {});
});
if (not params.bootstrap.empty()) if (not params.bootstrap.empty())
dht.bootstrap(params.bootstrap[0].c_str(), params.bootstrap[1].c_str()); dht.bootstrap(params.bootstrap[0].c_str(), params.bootstrap[1].c_str());
...@@ -63,6 +71,7 @@ main(int argc, char **argv) ...@@ -63,6 +71,7 @@ main(int argc, char **argv)
std::cout << "Public key ID " << dht.getId() << std::endl; std::cout << "Public key ID " << dht.getId() << std::endl;
std::cout << " (type 'h' or 'help' for a list of possible commands)" << std::endl << std::endl; std::cout << " (type 'h' or 'help' for a list of possible commands)" << std::endl << std::endl;
bool do_log {false};
while (true) while (true)
{ {
std::cout << ">> "; std::cout << ">> ";
...@@ -119,11 +128,11 @@ main(int argc, char **argv) ...@@ -119,11 +128,11 @@ main(int argc, char **argv)
std::cout << dht.getSearchesLog() << std::endl; std::cout << dht.getSearchesLog() << std::endl;
continue; continue;
} else if (op == "log") { } else if (op == "log") {
dht.setLoggers( do_log = !do_log;
[](char const* m, va_list args){ std::cerr << red; printLog(std::cerr, m, args); std::cerr << def; }, if (do_log)
[](char const* m, va_list args){ std::cout << yellow; printLog(std::cout, m, args); std::cout << def; }, enableLogging(dht);
[](char const* m, va_list args){ printLog(std::cout, m, args); } else
); disableLogging(dht);
continue; continue;
} }
...@@ -209,8 +218,6 @@ main(int argc, char **argv) ...@@ -209,8 +218,6 @@ main(int argc, char **argv)
std::cout << std::endl << "Stopping node..." << std::endl; std::cout << std::endl << "Stopping node..." << std::endl;
dht.join(); dht.join();
gnutls_global_deinit(); gnutls_global_deinit();
return 0; return 0;
} }
...@@ -33,8 +33,6 @@ ...@@ -33,8 +33,6 @@
extern "C" { extern "C" {
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
} }
#include <opendht.h>
#include <iostream>
#include <set> #include <set>
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
......
...@@ -33,10 +33,11 @@ ...@@ -33,10 +33,11 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <sstream>
#include <chrono> #include <chrono>
#include <iostream>
#include <sstream>
#include <opendht/value.h> // in_port_t #include <opendht.h>
#include <getopt.h> #include <getopt.h>
/** /**
...@@ -70,7 +71,7 @@ const Color::Modifier red(Color::FG_RED); ...@@ -70,7 +71,7 @@ const Color::Modifier red(Color::FG_RED);
const Color::Modifier yellow(Color::FG_YELLOW); const Color::Modifier yellow(Color::FG_YELLOW);
/** /**
* Print va_list to std::ostream (used for logging) * Print va_list to std::ostream (used for logging).
*/ */
void void
printLog(std::ostream& s, char const* m, va_list args) { printLog(std::ostream& s, char const* m, va_list args) {
...@@ -85,6 +86,22 @@ printLog(std::ostream& s, char const* m, va_list args) { ...@@ -85,6 +86,22 @@ printLog(std::ostream& s, char const* m, va_list args) {
s.put('\n'); s.put('\n');
} }
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); }
);
}
void
disableLogging(dht::DhtRunner& dht)
{
dht.setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG);
}
/** /**
* Converts std::chrono::duration to floating-point seconds. * Converts std::chrono::duration to floating-point seconds.
*/ */
...@@ -110,12 +127,14 @@ split(const std::string& s, char delim) { ...@@ -110,12 +127,14 @@ split(const std::string& s, char delim) {
static const constexpr in_port_t DHT_DEFAULT_PORT = 4222; static const constexpr in_port_t DHT_DEFAULT_PORT = 4222;
struct dht_params { struct dht_params {
bool help {false}; // print help and exit
in_port_t port {DHT_DEFAULT_PORT}; in_port_t port {DHT_DEFAULT_PORT};
bool is_bootstrap_node {false}; bool is_bootstrap_node {false};
std::vector<std::string> bootstrap {}; std::vector<std::string> bootstrap {};
}; };
static const struct option long_options[] = { static const struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"port", required_argument, nullptr, 'p'}, {"port", required_argument, nullptr, 'p'},
{"bootstrap", optional_argument, nullptr, 'b'}, {"bootstrap", optional_argument, nullptr, 'b'},
{nullptr, 0, nullptr, 0} {nullptr, 0, nullptr, 0}
...@@ -125,7 +144,7 @@ dht_params ...@@ -125,7 +144,7 @@ dht_params
parseArgs(int argc, char **argv) { parseArgs(int argc, char **argv) {
dht_params params; dht_params params;
int opt; int opt;
while ((opt = getopt_long(argc, argv, "p:b:", long_options, nullptr)) != -1) { while ((opt = getopt_long(argc, argv, "hp:b:", long_options, nullptr)) != -1) {
switch (opt) { switch (opt) {
case 'p': { case 'p': {
int port_arg = atoi(optarg); int port_arg = atoi(optarg);
...@@ -144,6 +163,9 @@ parseArgs(int argc, char **argv) { ...@@ -144,6 +163,9 @@ parseArgs(int argc, char **argv) {
else else
params.is_bootstrap_node = true; params.is_bootstrap_node = true;
break; break;
case 'h':
params.help = true;
break;
default: default:
break; break;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment