Commit b5474bbc authored by Dominik Charousset's avatar Dominik Charousset

Coding style nitpicks and drop exception in ctor

parent 16c48252
......@@ -36,8 +36,9 @@ namespace caf {
class serializer;
struct invalid_node_id_t {
constexpr invalid_node_id_t() {}
constexpr invalid_node_id_t() {
// nop
}
};
/// Identifies an invalid {@link node_id}.
......@@ -50,8 +51,12 @@ constexpr invalid_node_id_t invalid_node_id = invalid_node_id_t{};
class node_id : detail::comparable<node_id>,
detail::comparable<node_id, invalid_node_id_t> {
public:
~node_id();
node_id() = default;
node_id(const node_id&) = default;
node_id(const invalid_node_id_t&);
node_id& operator=(const node_id&) = default;
......@@ -64,7 +69,28 @@ public:
/// Represents a 160 bit hash.
using host_id_type = std::array<uint8_t, host_id_size>;
/// A reference counted container for host ID and process ID.
/// Creates a node ID from `process_id` and `hash`.
/// @param process_id System-wide unique process identifier.
/// @param hash Unique node id as hexadecimal string representation.
node_id(uint32_t process_id, const std::string& hash);
/// Creates a node ID from `process_id` and `hash`.
/// @param process_id System-wide unique process identifier.
/// @param node_id Unique node id.
node_id(uint32_t process_id, const host_id_type& node_id);
/// Identifies the running process.
/// @returns A system-wide unique process identifier.
uint32_t process_id() const;
/// Identifies the host system.
/// @returns A hash build from the MAC address of the first network device
/// and the UUID of the root partition (mounted in "/" or "C:").
const host_id_type& host_id() const;
/// @cond PRIVATE
// A reference counted container for host ID and process ID.
class data : public ref_counted {
public:
// for singleton API
......@@ -84,41 +110,16 @@ public:
int compare(const node_id& other) const;
data() = default;
~data();
data(uint32_t procid, host_id_type hid);
uint32_t process_id;
host_id_type host_id;
};
~node_id();
node_id(const node_id&);
/// Creates this from `process_id` and `hash`.
/// @param process_id System-wide unique process identifier.
/// @param hash Unique node id as hexadecimal string representation.
node_id(uint32_t process_id, const std::string& hash);
/// Creates this from `process_id` and `hash`.
/// @param process_id System-wide unique process identifier.
/// @param node_id Unique node id.
node_id(uint32_t process_id, const host_id_type& node_id);
/// Identifies the running process.
/// @returns A system-wide unique process identifier.
uint32_t process_id() const;
data(uint32_t procid, const std::string& hash);
/// Identifies the host system.
/// @returns A hash build from the MAC address of the first network device
/// and the UUID of the root partition (mounted in "/" or "C:").
const host_id_type& host_id() const;
uint32_t pid_;
/// @cond PRIVATE
host_id_type host_;
};
// "inherited" from comparable<node_id>
int compare(const node_id& other) const;
......@@ -126,7 +127,7 @@ public:
// "inherited" from comparable<node_id, invalid_node_id_t>
int compare(const invalid_node_id_t&) const;
node_id(intrusive_ptr<data> dataptr);
explicit node_id(intrusive_ptr<data> dataptr);
/// @endcond
......
......@@ -31,7 +31,6 @@
#include "caf/detail/logging.hpp"
#include "caf/detail/singletons.hpp"
#include "caf/detail/ripemd_160.hpp"
#include "caf/detail/safe_equal.hpp"
#include "caf/detail/get_root_uuid.hpp"
#include "caf/detail/get_process_id.hpp"
#include "caf/detail/get_mac_addresses.hpp"
......@@ -40,64 +39,12 @@ namespace caf {
namespace {
uint32_t s_invalid_process_id = 0;
uint32_t invalid_process_id = 0;
node_id::host_id_type s_invalid_host_id;
uint8_t hex_char_value(char c) {
if (isdigit(c)) {
return static_cast<uint8_t>(c - '0');
}
if (isalpha(c)) {
if (c >= 'a' && c <= 'f') {
return static_cast<uint8_t>((c - 'a') + 10);
} else if (c >= 'A' && c <= 'F') {
return static_cast<uint8_t>((c - 'A') + 10);
}
}
throw std::invalid_argument(std::string("illegal character: ") + c);
}
void host_id_from_string(const std::string& hash,
node_id::host_id_type& node_id) {
if (hash.size() != (node_id.size() * 2)) {
throw std::invalid_argument("string argument is not a node id hash");
}
auto j = hash.c_str();
for (size_t i = 0; i < node_id.size(); ++i) {
// read two characters, each representing 4 bytes
auto& val = node_id[i];
val = static_cast<uint8_t>(hex_char_value(j[0]) << 4)
| hex_char_value(j[1]);
j += 2;
}
}
node_id::host_id_type invalid_host_id;
} // namespace <anonymous>
bool equal(const std::string& hash, const node_id::host_id_type& node_id) {
if (hash.size() != (node_id.size() * 2)) {
return false;
}
auto j = hash.c_str();
try {
for (size_t i = 0; i < node_id.size(); ++i) {
// read two characters, each representing 4 bytes
uint8_t val;
val = static_cast<uint8_t>(hex_char_value(j[0]) << 4)
| hex_char_value(j[1]);
j += 2;
if (val != node_id[i]) {
return false;
}
}
}
catch (std::invalid_argument&) {
return false;
}
return true;
}
node_id::~node_id() {
// nop
}
......@@ -106,22 +53,22 @@ node_id::node_id(const invalid_node_id_t&) {
// nop
}
node_id::node_id(const node_id& other) : data_(other.data_) {
// nop
node_id& node_id::operator=(const invalid_node_id_t&) {
data_.reset();
return *this;
}
node_id::node_id(intrusive_ptr<data> dataptr) : data_(std::move(dataptr)) {
// nop
}
node_id::node_id(uint32_t procid, const std::string& b) {
data_ = make_counted<data>();
data_->process_id = procid;
host_id_from_string(b, data_->host_id);
node_id::node_id(uint32_t procid, const std::string& hash)
: data_(make_counted<data>(procid, hash)) {
// nop
}
node_id::node_id(uint32_t a, const host_id_type& b)
: data_(make_counted<data>(a, b)) {
node_id::node_id(uint32_t procid, const host_id_type& host_id)
: data_(make_counted<data>(procid, host_id)) {
// nop
}
......@@ -130,34 +77,48 @@ int node_id::compare(const invalid_node_id_t&) const {
}
int node_id::compare(const node_id& other) const {
if (this == &other) {
return 0; // shortcut for comparing to self
}
if (data_ == other.data_) {
return 0; // shortcut for identical instances
}
if ((data_ != nullptr) != (other.data_ != nullptr)) {
if (this == &other || data_ == other.data_)
return 0; // shortcut for comparing to self or identical instances
if (! data_ != ! other.data_)
return data_ ? 1 : -1; // invalid instances are always smaller
}
int tmp = strncmp(reinterpret_cast<const char*>(host_id().data()),
reinterpret_cast<const char*>(other.host_id().data()),
host_id_size);
if (tmp == 0) {
if (process_id() < other.process_id()) {
return -1;
} else if (process_id() == other.process_id()) {
return 0;
}
return 1;
}
return tmp;
return tmp != 0
? tmp
: (process_id() < other.process_id()
? -1
: (process_id() == other.process_id() ? 0 : 1));
}
node_id::data::data(uint32_t procid, host_id_type hid)
: process_id(procid), host_id(hid) {
: pid_(procid),
host_(hid) {
// nop
}
node_id::data::data(uint32_t procid, const std::string& hash) : pid_(procid) {
if (hash.size() != (host_id_size * 2)) {
host_ = invalid_host_id;
return;
}
auto hex_value = [](char c) -> uint8_t {
if (isalpha(c)) {
if (c >= 'a' && c <= 'f')
return static_cast<uint8_t>((c - 'a') + 10);
if (c >= 'A' && c <= 'F')
return static_cast<uint8_t>((c - 'A') + 10);
}
return isdigit(c) ? static_cast<uint8_t>(c - '0') : 0;
};
auto j = hash.c_str();
for (size_t i = 0; i < host_id_size; ++i) {
// read two characters, each representing 4 bytes
host_[i] = static_cast<uint8_t>(hex_value(j[0]) << 4) | hex_value(j[1]);
j += 2;
}
}
node_id::data::~data() {
// nop
}
......@@ -183,16 +144,11 @@ node_id::data* node_id::data::create_singleton() {
}
uint32_t node_id::process_id() const {
return data_ ? data_->process_id : s_invalid_process_id;
return data_ ? data_->pid_ : invalid_process_id;
}
const node_id::host_id_type& node_id::host_id() const {
return data_ ? data_->host_id : s_invalid_host_id;
}
node_id& node_id::operator=(const invalid_node_id_t&) {
data_.reset();
return *this;
return data_ ? data_->host_ : invalid_host_id;
}
} // namespace caf
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment