mirror of https://github.com/oxen-io/oxen-mq.git
Add a category command helper class
This allows simplifying: lmq.add_category("foo", ...); lmq.add_command("foo", "a", ...); lmq.add_command("foo", "b", ...); lmq.add_request_command("foo", "c", ...); to: lmq.add_category("foo", ...) .add_command("a", ...) .add_command("b", ...) .add_request_command("b", ...) ;
This commit is contained in:
parent
a43ee15b58
commit
dcb7e4df0b
|
@ -267,7 +267,7 @@ LogLevel LokiMQ::log_level() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void LokiMQ::add_category(std::string name, Access access_level, unsigned int reserved_threads, int max_queue) {
|
CatHelper LokiMQ::add_category(std::string name, Access access_level, unsigned int reserved_threads, int max_queue) {
|
||||||
check_not_started(proxy_thread, "add a category");
|
check_not_started(proxy_thread, "add a category");
|
||||||
|
|
||||||
if (name.size() > MAX_CATEGORY_LENGTH)
|
if (name.size() > MAX_CATEGORY_LENGTH)
|
||||||
|
@ -280,7 +280,9 @@ void LokiMQ::add_category(std::string name, Access access_level, unsigned int re
|
||||||
if (it != categories.end())
|
if (it != categories.end())
|
||||||
throw std::runtime_error("Unable to add category `" + name + "': that category already exists");
|
throw std::runtime_error("Unable to add category `" + name + "': that category already exists");
|
||||||
|
|
||||||
|
CatHelper ret{*this, name};
|
||||||
categories.emplace(std::move(name), category{access_level, reserved_threads, max_queue});
|
categories.emplace(std::move(name), category{access_level, reserved_threads, max_queue});
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LokiMQ::add_command(const std::string& category, std::string name, CommandCallback callback) {
|
void LokiMQ::add_command(const std::string& category, std::string name, CommandCallback callback) {
|
||||||
|
|
|
@ -230,6 +230,7 @@ static constexpr size_t MAX_CATEGORY_LENGTH = 50;
|
||||||
/// Maximum length of a command
|
/// Maximum length of a command
|
||||||
static constexpr size_t MAX_COMMAND_LENGTH = 200;
|
static constexpr size_t MAX_COMMAND_LENGTH = 200;
|
||||||
|
|
||||||
|
class CatHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that handles LokiMQ listeners, connections, proxying, and workers. An application
|
* Class that handles LokiMQ listeners, connections, proxying, and workers. An application
|
||||||
|
@ -812,8 +813,11 @@ public:
|
||||||
* messages are processed off the queue. -1 means unlimited, 0 means we will never queue (which
|
* messages are processed off the queue. -1 means unlimited, 0 means we will never queue (which
|
||||||
* means just dropping messages for this category if no workers are available to instantly
|
* means just dropping messages for this category if no workers are available to instantly
|
||||||
* handle the request).
|
* handle the request).
|
||||||
|
*
|
||||||
|
* @returns a CatHelper object that makes adding commands slightly less verbose (see the
|
||||||
|
* CatHelper describe, above).
|
||||||
*/
|
*/
|
||||||
void add_category(std::string name, Access access_level, unsigned int reserved_threads = 0, int max_queue = 200);
|
CatHelper add_category(std::string name, Access access_level, unsigned int reserved_threads = 0, int max_queue = 200);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new command to an existing category. This method may not be invoked after `start()`
|
* Adds a new command to an existing category. This method may not be invoked after `start()`
|
||||||
|
@ -1095,6 +1099,41 @@ public:
|
||||||
void add_timer(std::function<void()> job, std::chrono::milliseconds interval, bool squelch = true);
|
void add_timer(std::function<void()> job, std::chrono::milliseconds interval, bool squelch = true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Helper class that slightly simplifies adding commands to a category.
|
||||||
|
///
|
||||||
|
/// This allows simplifying:
|
||||||
|
///
|
||||||
|
/// lmq.add_category("foo", ...);
|
||||||
|
/// lmq.add_command("foo", "a", ...);
|
||||||
|
/// lmq.add_command("foo", "b", ...);
|
||||||
|
/// lmq.add_request_command("foo", "c", ...);
|
||||||
|
///
|
||||||
|
/// to:
|
||||||
|
///
|
||||||
|
/// lmq.add_category("foo", ...)
|
||||||
|
/// .add_command("a", ...)
|
||||||
|
/// .add_command("b", ...)
|
||||||
|
/// .add_request_command("b", ...)
|
||||||
|
/// ;
|
||||||
|
class CatHelper {
|
||||||
|
LokiMQ& lmq;
|
||||||
|
std::string cat;
|
||||||
|
|
||||||
|
public:
|
||||||
|
CatHelper(LokiMQ& lmq, std::string cat) : lmq{lmq}, cat{std::move(cat)} {}
|
||||||
|
|
||||||
|
CatHelper& add_command(std::string name, LokiMQ::CommandCallback callback) {
|
||||||
|
lmq.add_command(cat, std::move(name), std::move(callback));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
CatHelper& add_request_command(std::string name, LokiMQ::CommandCallback callback) {
|
||||||
|
lmq.add_request_command(cat, std::move(name), std::move(callback));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Namespace for options to the send() method
|
/// Namespace for options to the send() method
|
||||||
namespace send_option {
|
namespace send_option {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue