diff --git a/tools/dhtchat.cpp b/tools/dhtchat.cpp index f343f6fe15366bdc9e18b6eb823c7035e8a7cbbb..a6fb1459180f5fda67e760bb226b1ca83b7bd1ed 100644 --- a/tools/dhtchat.cpp +++ b/tools/dhtchat.cpp @@ -62,80 +62,84 @@ main(int argc, char **argv) #endif DhtRunner dht; - dht.run(params.port, dht::crypto::generateIdentity("DHT Chat Node"), true, params.network); + try { + dht.run(params.port, dht::crypto::generateIdentity("DHT Chat Node"), true, params.network); - if (not params.bootstrap.first.empty()) - dht.bootstrap(params.bootstrap.first.c_str(), params.bootstrap.second.c_str()); + if (not params.bootstrap.first.empty()) + dht.bootstrap(params.bootstrap.first.c_str(), params.bootstrap.second.c_str()); - print_node_info(dht, params); - std::cout << " type 'c {hash}' to join a channel" << std::endl << std::endl; + print_node_info(dht, params); + std::cout << " type 'c {hash}' to join a channel" << std::endl << std::endl; - bool connected {false}; - InfoHash room; - const InfoHash myid = dht.getId(); + bool connected {false}; + InfoHash room; + const InfoHash myid = dht.getId(); -#ifndef WIN32_NATIVE - // using the GNU History API - using_history(); +#ifndef WIN32_NATIVE + // using the GNU History API + using_history(); #endif - while (true) - { - // using the GNU Readline API - std::string line = readLine(connected ? PROMPT : "> "); - if (!line.empty() && line[0] == '\0') - break; - if (line.empty()) - continue; - - static constexpr dht::InfoHash INVALID_ID {}; - - std::istringstream iss(line); - std::string op, idstr; - iss >> op; - if (not connected) { - if (op == "x" || op == "q" || op == "exit" || op == "quit") + while (true) + { + // using the GNU Readline API + std::string line = readLine(connected ? PROMPT : "> "); + if (!line.empty() && line[0] == '\0') break; - else if (op == "c") { - iss >> idstr; - room = InfoHash(idstr); - if (room == INVALID_ID) { - room = InfoHash::get(idstr); - std::cout << "Joining h(" << idstr << ") = " << room << std::endl; - } - - dht.listen<dht::ImMessage>(room, [&](dht::ImMessage&& msg) { - if (msg.from != myid) - std::cout << msg.from.toString() << " at " << printTime(msg.date) - << " (took " << print_dt(std::chrono::system_clock::now() - std::chrono::system_clock::from_time_t(msg.date)) - << "s) " << (msg.to == myid ? "ENCRYPTED ":"") << ": " << msg.id << " - " << msg.msg << std::endl; - return true; - }); - connected = true; - } else { - std::cout << "Unknown command. Type 'c {hash}' to join a channel" << std::endl << std::endl; - } - } else { - auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - if (op == "d") { - connected = false; + if (line.empty()) continue; - } else if (op == "e") { - iss >> idstr; - std::getline(iss, line); - dht.putEncrypted(room, InfoHash(idstr), dht::ImMessage(rand_id(rd), std::move(line), now), [](bool ok) { - //dht.cancelPut(room, id); - if (not ok) - std::cout << "Message publishing failed !" << std::endl; - }); + + static constexpr dht::InfoHash INVALID_ID {}; + + std::istringstream iss(line); + std::string op, idstr; + iss >> op; + if (not connected) { + if (op == "x" || op == "q" || op == "exit" || op == "quit") + break; + else if (op == "c") { + iss >> idstr; + room = InfoHash(idstr); + if (room == INVALID_ID) { + room = InfoHash::get(idstr); + std::cout << "Joining h(" << idstr << ") = " << room << std::endl; + } + + dht.listen<dht::ImMessage>(room, [&](dht::ImMessage&& msg) { + if (msg.from != myid) + std::cout << msg.from.toString() << " at " << printTime(msg.date) + << " (took " << print_dt(std::chrono::system_clock::now() - std::chrono::system_clock::from_time_t(msg.date)) + << "s) " << (msg.to == myid ? "ENCRYPTED ":"") << ": " << msg.id << " - " << msg.msg << std::endl; + return true; + }); + connected = true; + } else { + std::cout << "Unknown command. Type 'c {hash}' to join a channel" << std::endl << std::endl; + } } else { - dht.putSigned(room, dht::ImMessage(rand_id(rd), std::move(line), now), [](bool ok) { - //dht.cancelPut(room, id); - if (not ok) - std::cout << "Message publishing failed !" << std::endl; - }); + auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + if (op == "d") { + connected = false; + continue; + } else if (op == "e") { + iss >> idstr; + std::getline(iss, line); + dht.putEncrypted(room, InfoHash(idstr), dht::ImMessage(rand_id(rd), std::move(line), now), [](bool ok) { + //dht.cancelPut(room, id); + if (not ok) + std::cout << "Message publishing failed !" << std::endl; + }); + } else { + dht.putSigned(room, dht::ImMessage(rand_id(rd), std::move(line), now), [](bool ok) { + //dht.cancelPut(room, id); + if (not ok) + std::cout << "Message publishing failed !" << std::endl; + }); + } } } + } catch(const std::exception&e) { + std::cerr << std::endl << e.what() << std::endl; } std::cout << std::endl << "Stopping node..." << std::endl; diff --git a/tools/dhtscanner.cpp b/tools/dhtscanner.cpp index 2377700f427c7e68926c32a76475ff260ac1a3f4..b01c19fa2e99bdef190bc78d9cf0d5dda0a95482 100644 --- a/tools/dhtscanner.cpp +++ b/tools/dhtscanner.cpp @@ -85,35 +85,39 @@ main(int argc, char **argv) } DhtRunner dht; - dht.run(params.port, {}, true, params.network); + try { + dht.run(params.port, {}, true, params.network); - if (not params.bootstrap.first.empty()) - dht.bootstrap(params.bootstrap.first.c_str(), params.bootstrap.second.c_str()); + if (not params.bootstrap.first.empty()) + dht.bootstrap(params.bootstrap.first.c_str(), params.bootstrap.second.c_str()); - std::cout << "OpenDht node " << dht.getNodeId() << " running on port " << params.port << std::endl; - std::cout << "Scanning network..." << std::endl; - auto all_nodes = std::make_shared<NodeSet>(); + std::cout << "OpenDht node " << dht.getNodeId() << " running on port " << params.port << std::endl; + std::cout << "Scanning network..." << std::endl; + auto all_nodes = std::make_shared<NodeSet>(); - // Set hash to 1 because 0 is the null hash - dht::InfoHash cur_h {}; - cur_h.setBit(8*HASH_LEN-1, 1); + // Set hash to 1 because 0 is the null hash + dht::InfoHash cur_h {}; + cur_h.setBit(8*HASH_LEN-1, 1); - std::this_thread::sleep_for(std::chrono::seconds(2)); + std::this_thread::sleep_for(std::chrono::seconds(2)); - std::atomic_uint done {0}; - step(dht, done, all_nodes, cur_h, 0); + std::atomic_uint done {0}; + step(dht, done, all_nodes, cur_h, 0); - { - std::mutex m; - std::unique_lock<std::mutex> lk(m); - cv.wait(lk, [&](){ - return done.load() == 0; - }); - } + { + std::mutex m; + std::unique_lock<std::mutex> lk(m); + cv.wait(lk, [&](){ + return done.load() == 0; + }); + } - std::cout << std::endl << "Scan ended: " << all_nodes->size() << " nodes found." << std::endl; - for (const auto& n : *all_nodes) - std::cout << "Node " << *n << std::endl; + std::cout << std::endl << "Scan ended: " << all_nodes->size() << " nodes found." << std::endl; + for (const auto& n : *all_nodes) + std::cout << "Node " << *n << std::endl; + } catch(const std::exception&e) { + std::cerr << std::endl << e.what() << std::endl; + } dht.join(); #ifdef WIN32_NATIVE