-
Notifications
You must be signed in to change notification settings - Fork 14
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
[WIP] Refactor, stage 2 #113
Draft
SwooshyCueb
wants to merge
1
commit into
irods:main
Choose a base branch
from
SwooshyCueb:refactor.main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,11 @@ | |
|
||
#include "irods/private/audit_amqp.hpp" | ||
|
||
#include <cstdint> | ||
#include <fstream> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
#include <proton/connection.hpp> | ||
#include <proton/connection_options.hpp> | ||
|
@@ -16,12 +20,49 @@ | |
#include <proton/sender.hpp> | ||
#include <proton/session.hpp> | ||
|
||
#include <fmt/core.h> | ||
#include <fmt/compile.h> | ||
#include <nlohmann/json.hpp> | ||
|
||
// filesystem | ||
// clang-format off | ||
#ifdef __cpp_lib_filesystem | ||
#include <filesystem> | ||
namespace fs = std::filesystem; | ||
#else | ||
#include <boost/filesystem.hpp> | ||
namespace fs = boost::filesystem; | ||
#endif | ||
// clang-format on | ||
|
||
namespace irods::plugin::rule_engine::audit_amqp | ||
{ | ||
class amqp_endpoint | ||
{ | ||
public: | ||
amqp_endpoint( | ||
const std::string_view& _scheme, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason these |
||
const std::string_view& _host, | ||
const std::uint_fast16_t& _port, | ||
const std::string_view& _topic, | ||
const std::string_view& _user, | ||
const std::string_view& _password); | ||
|
||
const std::string url; | ||
const std::string user; | ||
const std::string password; | ||
|
||
static constexpr const std::string_view default_scheme{"amqp"}; | ||
static constexpr const std::uint_fast16_t default_amqp_port = 5672; | ||
static constexpr const std::uint_fast16_t default_amqps_port = 5671; | ||
static constexpr const std::string_view default_user{""}; | ||
static constexpr const std::string_view default_password{""}; | ||
}; | ||
|
||
class send_handler : public proton::messaging_handler | ||
{ | ||
public: | ||
send_handler(const proton::message& _message, const std::string& _url); | ||
send_handler(const proton::message& _message, const std::vector<amqp_endpoint>& _endpoints); | ||
void on_container_start(proton::container& _container) override; | ||
void on_sendable(proton::sender& _sender) override; | ||
void on_tracker_accept(proton::tracker& _tracker) override; | ||
|
@@ -34,10 +75,26 @@ namespace irods::plugin::rule_engine::audit_amqp | |
void on_error(const proton::error_condition& _err_cond) override; | ||
|
||
private: | ||
const std::string& _amqp_url; | ||
const std::vector<amqp_endpoint>& _endpoints; | ||
std::vector<amqp_endpoint>::size_type _endpoint_idx; | ||
const proton::message& _message; | ||
bool _message_sent; | ||
}; | ||
|
||
class amqp_sender | ||
{ | ||
public: | ||
amqp_sender(const std::vector<amqp_endpoint>& _endpoints); | ||
~amqp_sender(); | ||
void enable_test_mode(const fs::path _test_mode_log_path); | ||
void disable_test_mode(); | ||
void send_message(const nlohmann::json& _message_body, const std::uint64_t _timestamp_ms); | ||
private: | ||
const std::vector<amqp_endpoint>& _endpoints; | ||
std::vector<amqp_endpoint>::size_type _endpoint_idx; | ||
fs::path _log_file_path; | ||
std::ofstream _log_file_ofstream; | ||
}; | ||
} //namespace irods::plugin::rule_engine::audit_amqp | ||
|
||
#endif // IRODS_AUDIT_AMQP_SENDER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,23 @@ | |
#include "irods/private/amqp_sender.hpp" | ||
|
||
#include <boost/config.hpp> | ||
#include <fmt/core.h> | ||
#include <fmt/compile.h> | ||
|
||
namespace irods::plugin::rule_engine::audit_amqp | ||
{ | ||
namespace | ||
{ | ||
BOOST_FORCEINLINE std::string build_amqp_url( | ||
const std::string_view& _scheme, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be using refs with |
||
const std::string_view& _host, | ||
const std::uint_fast16_t& _port, | ||
const std::string_view& _topic) | ||
{ | ||
// TODO: use Boost.URL (if available at compile-time) to validate URLs | ||
return fmt::format(FMT_COMPILE("{0:s}://{1:s}:{2:d}/{3:s}"), _scheme, _host, _port, _topic); | ||
} | ||
|
||
BOOST_FORCEINLINE void log_proton_error(const proton::error_condition& err_cond, const std::string& log_message) | ||
{ | ||
// clang-format off | ||
|
@@ -21,17 +33,84 @@ namespace irods::plugin::rule_engine::audit_amqp | |
} | ||
} // namespace | ||
|
||
send_handler::send_handler(const proton::message& _message, const std::string& _url) | ||
: _amqp_url(_url) | ||
amqp_endpoint::amqp_endpoint( | ||
const std::string_view& _scheme, | ||
const std::string_view& _host, | ||
const std::uint_fast16_t& _port, | ||
const std::string_view& _topic, | ||
const std::string_view& _user, | ||
const std::string_view& _password) | ||
: url(build_amqp_url(_scheme, _host, _port, _topic)) | ||
, user(_user) | ||
, password(_password) | ||
{ | ||
} | ||
|
||
amqp_sender::amqp_sender(const std::vector<amqp_endpoint>& _endpoints) | ||
: _endpoints(_endpoints), _endpoint_idx(static_cast<std::vector<amqp_endpoint>::size_type>(0)) | ||
{ | ||
} | ||
|
||
amqp_sender::~amqp_sender() | ||
{ | ||
if (_log_file_ofstream.is_open()) { | ||
_log_file_ofstream.close(); | ||
} | ||
} | ||
|
||
void amqp_sender::enable_test_mode(const fs::path _test_mode_log_path) | ||
{ | ||
if (_log_file_ofstream.is_open()) { | ||
_log_file_ofstream.close(); | ||
} | ||
_log_file_path.assign(_test_mode_log_path); | ||
} | ||
|
||
void amqp_sender::disable_test_mode() | ||
{ | ||
_log_file_path.clear(); | ||
if (_log_file_ofstream.is_open()) { | ||
_log_file_ofstream.close(); | ||
} | ||
} | ||
|
||
void amqp_sender::send_message(const nlohmann::json& _message_body, const std::uint64_t _timestamp_ms) | ||
{ | ||
const std::string msg_str = _message_body.dump(); | ||
|
||
proton::message msg(msg_str); | ||
msg.content_type("application/json"); | ||
msg.creation_time(proton::timestamp(static_cast<proton::timestamp::numeric_type>(_timestamp_ms))); | ||
send_handler handler(msg, _endpoints); | ||
proton::container(handler).run(); | ||
|
||
if (!_log_file_path.empty()) { | ||
if (!_log_file_ofstream.is_open()) { | ||
_log_file_ofstream.open(_log_file_path); | ||
} | ||
_log_file_ofstream << msg_str << std::endl; | ||
} | ||
} | ||
|
||
send_handler::send_handler(const proton::message& _message, const std::vector<amqp_endpoint>& _endpoints) | ||
: _endpoints(_endpoints) | ||
, _endpoint_idx(static_cast<std::vector<amqp_endpoint>::size_type>(0)) | ||
, _message(_message) | ||
, _message_sent(false) | ||
{ | ||
} | ||
|
||
void send_handler::on_container_start(proton::container& _container) | ||
{ | ||
const amqp_endpoint& endpoint = _endpoints[_endpoint_idx]; | ||
proton::connection_options conn_opts; | ||
_container.open_sender(_amqp_url, conn_opts); | ||
if (!endpoint.user.empty()) { | ||
conn_opts.user(endpoint.user); | ||
} | ||
if (!endpoint.password.empty()) { | ||
conn_opts.password(endpoint.password); | ||
} | ||
_container.open_sender(endpoint.url, conn_opts); | ||
} | ||
|
||
void send_handler::on_sendable(proton::sender& _sender) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious: Is this feature check for purposes of portability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. It made me wonder to what extent we wanted to do things like this. For instance, if this is to support older compilers, do we need to do something similar for, i.e.
string_view
(>=C++17)? I don't have a problem with this one, was just wondering where we're drawing this particular line, and/or if I missed the point. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's less about the compiler and more about the stdlib. If you think it's unlikely that someone would compile against a stdlib that would have
string_view
and notstd::filesystem
I can remove this