Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing msg in ccb core #41

Merged
merged 10 commits into from
Jul 6, 2020
7 changes: 2 additions & 5 deletions src/ccb_core/bbdo/acceptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "com/centreon/broker/bbdo/internal.hh"
#include "com/centreon/broker/bbdo/stream.hh"
#include "com/centreon/broker/bbdo/version_response.hh"
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/broker/io/events.hh"
#include "com/centreon/broker/io/protocols.hh"
#include "com/centreon/broker/logging/logging.hh"
Expand Down Expand Up @@ -68,7 +67,7 @@ acceptor::acceptor(std::string const& name,
_one_peer_retention_mode(one_peer_retention_mode),
_timeout(timeout),
_ack_limit(ack_limit) {
if ((_timeout == (time_t)-1) || (_timeout == 0))
if ((_timeout == (time_t) - 1) || (_timeout == 0))
_timeout = 3;
}

Expand All @@ -90,9 +89,7 @@ acceptor::acceptor(acceptor const& other)
/**
* Destructor.
*/
acceptor::~acceptor() {
_from.reset();
}
acceptor::~acceptor() { _from.reset(); }

/**
* Assignment operator.
Expand Down
3 changes: 1 addition & 2 deletions src/ccb_core/bbdo/connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "com/centreon/broker/bbdo/internal.hh"
#include "com/centreon/broker/bbdo/stream.hh"
#include "com/centreon/broker/bbdo/version_response.hh"
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/broker/io/events.hh"
#include "com/centreon/broker/io/protocols.hh"
#include "com/centreon/broker/logging/logging.hh"
Expand Down Expand Up @@ -58,7 +57,7 @@ connector::connector(bool negotiate,
_negotiate{negotiate},
_timeout{timeout},
_ack_limit{ack_limit} {
if (_timeout == (time_t)-1 || _timeout == 0)
if (_timeout == (time_t) - 1 || _timeout == 0)
_timeout = 3;
}

Expand Down
148 changes: 87 additions & 61 deletions src/ccb_core/bbdo/input.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "com/centreon/broker/bbdo/ack.hh"
#include "com/centreon/broker/bbdo/internal.hh"
#include "com/centreon/broker/bbdo/version_response.hh"
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/exceptions/timeout.hh"
#include "com/centreon/broker/io/events.hh"
#include "com/centreon/broker/io/raw.hh"
Expand All @@ -37,6 +37,7 @@
#include "com/centreon/broker/mapping/entry.hh"
#include "com/centreon/broker/misc/misc.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::bbdo;

Expand All @@ -57,8 +58,7 @@ static uint32_t set_boolean(io::data& t,
log_v2::bbdo()->error(
"BBDO: cannot extract boolean value: 0 bytes left in "
"packet");
throw exceptions::msg() << "BBDO: cannot extract boolean value: "
<< "0 bytes left in packet";
throw msg_fmt("BBDO: cannot extract boolean value: 0 bytes left in packet");
}
member.set_bool(t, *static_cast<char const*>(data));
return 1;
Expand All @@ -75,12 +75,13 @@ static uint32_t set_double(io::data& t,
uint32_t len(strlen(str));
if (len >= size) {
log_v2::bbdo()->error(
"BBDO: cannot extract double value: not terminating '\0' in remaining "
"BBDO: cannot extract double value: not terminating '\\0' in remaining "
"{} bytes of packet",
size);
throw msg_fmt(
"BBDO: cannot extract double value: not terminating '\\0' in remaining "
"{} bytes of packet",
size);
throw exceptions::msg() << "BBDO: cannot extract double value: "
<< "not terminating '\0' in remaining " << size
<< " bytes of packet";
}
member.set_double(t, strtod(str, nullptr));
return len + 1;
Expand All @@ -96,8 +97,8 @@ static uint32_t set_integer(io::data& t,
if (size < sizeof(uint32_t)) {
log_v2::bbdo()->error(
"BBDO: cannot extract integer value: {} bytes left in packet", size);
throw exceptions::msg() << "BBDO: cannot extract integer value: " << size
<< " bytes left in packet";
throw msg_fmt("BBDO: cannot extract integer value: {} bytes left in packet",
size);
}
member.set_int(t, ntohl(*static_cast<uint32_t const*>(data)));
return sizeof(uint32_t);
Expand All @@ -113,8 +114,8 @@ static uint32_t set_short(io::data& t,
if (size < sizeof(uint16_t)) {
log_v2::bbdo()->error(
"BBDO: cannot extract short value: {} bytes left in packet", size);
throw exceptions::msg() << "BBDO: cannot extract short value: " << size
<< " bytes left in packet";
throw msg_fmt("BBDO: cannot extract short value: {} bytes left in packet",
size);
}
member.set_short(t, ntohs(*static_cast<uint16_t const*>(data)));
return sizeof(uint16_t);
Expand All @@ -135,9 +136,10 @@ static uint32_t set_string(io::data& t,
"{} bytes left in packet",
size);

throw exceptions::msg() << "BBDO: cannot extract string value: "
<< "no terminating '\\0' in remaining " << size
<< " bytes of packet";
throw msg_fmt(
"BBDO: cannot extract string value: no terminating '\\0' in remaining "
"{} bytes of packet",
size);
}
member.set_string(t, str);
return len + 1;
Expand All @@ -153,8 +155,8 @@ static uint32_t set_timestamp(io::data& t,
if (size < 2 * sizeof(uint32_t)) {
log_v2::bbdo()->error(
"BBDO: cannot extract timestamp value: {} bytes left in packet", size);
throw exceptions::msg() << "BBDO: cannot extract timestamp value: " << size
<< " bytes left in packet";
throw msg_fmt(
"BBDO: cannot extract timestamp value: {} bytes left in packet", size);
}
uint32_t const* ptr(static_cast<uint32_t const*>(data));
uint64_t val(ntohl(*ptr));
Expand All @@ -176,8 +178,9 @@ static uint32_t set_uint(io::data& t,
log_v2::bbdo()->error(
"BBDO: cannot extract uint32_t integer value: {} bytes left in packet",
size);
throw exceptions::msg() << "BBDO: cannot extract uint32_teger value: "
<< size << " bytes left in packet";
throw msg_fmt(
"BBDO: cannot extract uint32_teger value: {} bytes left in packet",
size);
}
member.set_uint(t, ntohl(*static_cast<uint32_t const*>(data)));
return sizeof(uint32_t);
Expand Down Expand Up @@ -209,7 +212,8 @@ static io::data* unserialize(uint32_t event_type,
t->destination_id = destination_id;
// Browse all mapping to unserialize the object.
for (mapping::entry const* current_entry(info->get_mapping());
!current_entry->is_null(); ++current_entry)
!current_entry->is_null();
++current_entry)
// Skip entries that should not be serialized.
if (current_entry->get_serialize()) {
uint32_t rb;
Expand Down Expand Up @@ -239,11 +243,13 @@ static io::data* unserialize(uint32_t event_type,
log_v2::bbdo()->error(
"BBDO: invalid mapping for object of type '{0}': {1} is not "
"a known type ID",
info->get_name(), current_entry->get_type());
throw exceptions::msg() << "BBDO: invalid mapping for "
<< "object of type '" << info->get_name()
<< "': " << current_entry->get_type()
<< " is not a known type ID";
info->get_name(),
current_entry->get_type());
throw msg_fmt(
"BBDO: invalid mapping for object of type '{}': {} is not a "
"known type ID",
info->get_name(),
current_entry->get_type());
}
buffer += rb;
size -= rb;
Expand All @@ -253,9 +259,9 @@ static io::data* unserialize(uint32_t event_type,
log_v2::bbdo()->error(
"BBDO: cannot create object of ID {} whereas it has been registered",
event_type);
throw exceptions::msg()
<< "BBDO: cannot create object of ID " << event_type
<< " whereas it has been registered";
throw msg_fmt(
"BBDO: cannot create object of ID {} whereas it has been registered",
event_type);
}
} else {
log_v2::bbdo()->info(
Expand Down Expand Up @@ -335,32 +341,47 @@ bool input::read(std::shared_ptr<io::data>& d, time_t deadline) {
"BBDO: peer is using protocol version {0}.{1}.{2} , whereas we're "
"using protocol version "
"{3}.{4}.{5}",
version->bbdo_major, version->bbdo_minor, version->bbdo_patch,
BBDO_VERSION_MAJOR, BBDO_VERSION_MINOR, BBDO_VERSION_PATCH);
throw exceptions::msg()
<< "BBDO: peer is using protocol version " << version->bbdo_major
<< "." << version->bbdo_minor << "." << version->bbdo_patch
<< " whereas we're using protocol version " << BBDO_VERSION_MAJOR
<< "." << BBDO_VERSION_MINOR << "." << BBDO_VERSION_PATCH;
version->bbdo_major,
version->bbdo_minor,
version->bbdo_patch,
BBDO_VERSION_MAJOR,
BBDO_VERSION_MINOR,
BBDO_VERSION_PATCH);
throw msg_fmt(
"BBDO: peer is using protocol version {}.{}.{} whereas we're using "
"protocol version {}.{}.{}",
version->bbdo_major,
version->bbdo_minor,
version->bbdo_patch,
BBDO_VERSION_MAJOR,
BBDO_VERSION_MINOR,
BBDO_VERSION_PATCH);
}
log_v2::bbdo()->info(
"BBDO: peer is using protocol version {0}.{1}.{2} , we're using version "
"BBDO: peer is using protocol version {0}.{1}.{2} , we're using "
"version "
"{3}.{4}.{5}",
version->bbdo_major, version->bbdo_minor, version->bbdo_patch,
BBDO_VERSION_MAJOR, BBDO_VERSION_MINOR, BBDO_VERSION_PATCH);
logging::info(logging::medium)
<< "BBDO: peer is using protocol version " << version->bbdo_major
<< "." << version->bbdo_minor << "." << version->bbdo_patch
<< ", we're using version " << BBDO_VERSION_MAJOR << "."
<< BBDO_VERSION_MINOR << "." << BBDO_VERSION_PATCH;
version->bbdo_major,
version->bbdo_minor,
version->bbdo_patch,
BBDO_VERSION_MAJOR,
BBDO_VERSION_MINOR,
BBDO_VERSION_PATCH);
logging::info(logging::medium) << "BBDO: peer is using protocol version "
<< version->bbdo_major << "."
<< version->bbdo_minor << "."
<< version->bbdo_patch
<< ", we're using version "
<< BBDO_VERSION_MAJOR << "."
<< BBDO_VERSION_MINOR << "."
<< BBDO_VERSION_PATCH;
} else if ((event_id & 0xFFFF) == 2) {
log_v2::bbdo()->info(
"BBDO: received acknowledgement for {} events",
std::static_pointer_cast<ack const>(d)->acknowledged_events);
logging::info(logging::medium)
<< "BBDO: received acknowledgement for "
<< std::static_pointer_cast<ack const>(d)->acknowledged_events
<< " events";
logging::info(logging::medium) << "BBDO: received acknowledgement for "
<< std::static_pointer_cast<ack const>(d)
->acknowledged_events << " events";
acknowledge_events(
std::static_pointer_cast<ack const>(d)->acknowledged_events);
}
Expand Down Expand Up @@ -434,13 +455,15 @@ bool input::read_any(std::shared_ptr<io::data>& d, time_t deadline) {
source_id != current_source_id || destination_id != current_dest_id) {
if (!_skipped) { // First corrupted byte.
log_v2::bbdo()->error(
"BBDO: peer {0} is sending corrupted data: {1}", peer(),
"BBDO: peer {0} is sending corrupted data: {1}",
peer(),
((chksum != expected) ? "invalid CRC"
: "invalid multi-packet event"));
logging::error(logging::high)
<< "BBDO: peer " << peer() << " is sending corrupted data: "
<< ((chksum != expected) ? "invalid CRC"
: "invalid multi-packet event");
logging::error(logging::high) << "BBDO: peer " << peer()
<< " is sending corrupted data: "
<< ((chksum != expected)
? "invalid CRC"
: "invalid multi-packet event");
}
++_skipped;
_buffer.erase(1);
Expand All @@ -463,39 +486,42 @@ bool input::read_any(std::shared_ptr<io::data>& d, time_t deadline) {
log_v2::bbdo()->info(
"BBDO: peer {0} sent {1} corrupted payload bytes, resuming "
"processing",
peer(), _skipped);
peer(),
_skipped);
logging::info(logging::high)
<< "BBDO: peer " << peer() << " sent " << _skipped
<< " corrupted payload bytes, resuming processing";
_skipped = 0;
}

// Unserialize event.
d.reset(unserialize(event_id, source_id, destination_id, packet.data(),
packet.size()));
d.reset(unserialize(
event_id, source_id, destination_id, packet.data(), packet.size()));
if (d) {
log_v2::bbdo()->debug(
"BBDO: unserialized {0} bytes for event of type {1}", raw_size,
"BBDO: unserialized {0} bytes for event of type {1}",
raw_size,
event_id);
logging::debug(logging::medium)
<< "BBDO: unserialized " << raw_size << " bytes for event of type "
<< event_id;
logging::debug(logging::medium) << "BBDO: unserialized " << raw_size
<< " bytes for event of type "
<< event_id;
} else {
log_v2::bbdo()->error(
"BBDO: unknown event type {} event cannot be decoded", event_id);
log_v2::bbdo()->debug("BBDO: discarded {} bytes", raw_size);

logging::error(logging::medium) << "BBDO: unknown event type " << event_id
<< ": event cannot be decoded";
logging::debug(logging::medium)
<< "BBDO: discarded " << raw_size << " bytes";
logging::debug(logging::medium) << "BBDO: discarded " << raw_size
<< " bytes";
}

// Mark data as processed.
_buffer.erase(raw_size);

return true;
} catch (exceptions::timeout const& e) {
}
catch (exceptions::timeout const& e) {
(void)e;
return false;
}
Expand Down
27 changes: 14 additions & 13 deletions src/ccb_core/bbdo/input_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

#include "com/centreon/broker/bbdo/input_buffer.hh"

#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/log_v2.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::bbdo;

Expand All @@ -34,9 +35,7 @@ input_buffer::input_buffer() : _first_offset(0), _size(0) {}
*
* @param[in] other Object to copy.
*/
input_buffer::input_buffer(input_buffer const& other) {
_internal_copy(other);
}
input_buffer::input_buffer(input_buffer const& other) { _internal_copy(other); }

/**
* Destructor.
Expand Down Expand Up @@ -136,12 +135,16 @@ void input_buffer::extract(std::string& output, int offset, int size) {
"BBDO: cannot extract {0} bytes at offset {1} from input buffer, only "
"{2} bytes available: this is likely a software bug that you should "
"report to Centreon Broker developers",
size, offset, _size);
throw exceptions::msg()
<< "BBDO: cannot extract " << size << " bytes at offset " << offset
<< " from input buffer, only " << _size
<< " bytes available: this is likely a software bug"
<< " that you should report to Centreon Broker developers";
size,
offset,
_size);
throw msg_fmt(
"BBDO: cannot extract {} bytes at offset {} from input buffer, only {} "
"bytes available: this is likely a software bug that you should report "
"to Centreon Broker developers",
size,
offset,
_size);
}
}

Expand All @@ -150,9 +153,7 @@ void input_buffer::extract(std::string& output, int offset, int size) {
*
* @return Buffer size in bytes.
*/
int input_buffer::size() const {
return _size;
}
int input_buffer::size() const { return _size; }

/**
* Copy internal data members.
Expand Down
Loading