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

tools: catch exceptions

parent 2fc7cc57
Branches
Tags
No related merge requests found
......@@ -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;
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment