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

value: toString prints raw data if text/plain

parent 99890fca
No related branches found
No related tags found
No related merge requests found
......@@ -45,7 +45,7 @@ Value::Filter bindFilterRaw(FilterRaw raw_filter, void* user_data) {
std::ostream& operator<< (std::ostream& s, const Value& v)
{
auto flags(s.flags());
s << "Value[id:" << std::hex << v.id << std::dec << " ";
s << "Value[id:" << std::hex << v.id << std::dec << ' ';
if (v.isEncrypted())
s << "encrypted ";
else if (v.isSigned()) {
......@@ -67,14 +67,23 @@ std::ostream& operator<< (std::ostream& s, const Value& v)
}
#endif
} else {
s << "Data (type: " << v.type << " ): ";
s << std::hex;
for (auto i : v.data)
s << std::setfill('0') << std::setw(2) << (unsigned)i;
s << std::dec;
if (v.user_type.empty())
s << "data:";
else
s << "data(" << v.user_type << "):";
if (v.user_type == "text/plain") {
s << '"';
s.write((const char*)v.data.data(), v.data.size());
s << '"';
} else {
s << std::hex;
for (auto i : v.data)
s << std::setfill('0') << std::setw(2) << (unsigned)i;
s << std::dec;
}
}
}
s << "]";
s << ']';
s.flags(flags);
return s;
}
......
......@@ -384,10 +384,11 @@ void cmd_loop(std::shared_ptr<DhtRunner>& node, dht_params& params
else if (op == "p") {
std::string v;
iss >> v;
node->put(id, dht::Value {
auto value = std::make_shared<dht::Value>(
dht::ValueType::USER_DATA.id,
std::vector<uint8_t> {v.begin(), v.end()}
}, [start](bool ok) {
std::vector<uint8_t> {v.begin(), v.end()});
value->user_type = "text/plain";
node->put(id, std::move(value), [start](bool ok) {
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Put: " << (ok ? "success" : "failure") << ", took " << print_duration(end-start) << std::endl;
});
......@@ -399,6 +400,7 @@ void cmd_loop(std::shared_ptr<DhtRunner>& node, dht_params& params
dht::ValueType::USER_DATA.id,
std::vector<uint8_t> {v.begin(), v.end()}
);
value->user_type = "text/plain";
node->put(id, value, [start,value](bool ok) {
auto end = std::chrono::high_resolution_clock::now();
auto flags(std::cout.flags());
......@@ -418,10 +420,10 @@ void cmd_loop(std::shared_ptr<DhtRunner>& node, dht_params& params
}
std::string v;
iss >> v;
node->putSigned(id, dht::Value {
dht::ValueType::USER_DATA.id,
std::vector<uint8_t> {v.begin(), v.end()}
}, [start](bool ok) {
auto value = std::make_shared<dht::Value>();
value->data = {v.begin(), v.end()};
value->user_type = "text/plain";
node->putSigned(id, std::move(value), [start](bool ok) {
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Put signed: " << (ok ? "success" : "failure") << " (took " << print_duration(end-start) << "s)" << std::endl;
});
......@@ -434,10 +436,12 @@ void cmd_loop(std::shared_ptr<DhtRunner>& node, dht_params& params
std::string tostr;
std::string v;
iss >> tostr >> v;
node->putEncrypted(id, InfoHash(tostr), dht::Value {
auto value = std::make_shared<dht::Value>(
dht::ValueType::USER_DATA.id,
std::vector<uint8_t> {v.begin(), v.end()}
}, [start](bool ok) {
);
value->user_type = "text/plain";
node->putEncrypted(id, InfoHash(tostr), std::move(value), [start](bool ok) {
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Put encrypted: " << (ok ? "success" : "failure") << " (took " << print_duration(end-start) << std::endl;
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment