2020-07-20 14:19:09 +02:00
|
|
|
#include "common.hpp"
|
2020-08-13 17:28:51 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <exception>
|
2020-07-20 14:19:09 +02:00
|
|
|
#include <lokimq/lokimq.h>
|
|
|
|
#include <lokimq/address.h>
|
2020-08-13 17:28:51 +02:00
|
|
|
#include <pybind11/chrono.h>
|
2020-07-20 14:19:09 +02:00
|
|
|
#include <future>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace lokimq
|
|
|
|
{
|
2020-08-18 03:07:51 +02:00
|
|
|
template <typename... Options>
|
2020-08-19 23:24:23 +02:00
|
|
|
std::future<std::vector<std::string>> LokiMQ_start_request(
|
2020-08-13 17:28:51 +02:00
|
|
|
LokiMQ& lmq,
|
|
|
|
ConnectionID conn,
|
|
|
|
std::string name,
|
2020-08-18 03:07:51 +02:00
|
|
|
std::vector<py::bytes> byte_args,
|
|
|
|
Options&&... opts)
|
2020-08-13 17:28:51 +02:00
|
|
|
{
|
|
|
|
std::vector<std::string> args;
|
|
|
|
args.reserve(byte_args.size());
|
|
|
|
for (auto& b : byte_args)
|
|
|
|
args.push_back(b);
|
|
|
|
|
2020-08-19 23:24:23 +02:00
|
|
|
auto result = std::make_shared<std::promise<std::vector<std::string>>>();
|
2020-08-13 17:28:51 +02:00
|
|
|
auto fut = result->get_future();
|
|
|
|
lmq.request(conn, std::move(name),
|
|
|
|
[result=std::move(result)](bool success, std::vector<std::string> value)
|
|
|
|
{
|
|
|
|
if (success)
|
2020-08-19 23:24:23 +02:00
|
|
|
result->set_value(std::move(value));
|
2020-08-13 17:28:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string err;
|
|
|
|
for (auto& m : value) {
|
|
|
|
if (!err.empty()) err += ", ";
|
|
|
|
err += m;
|
|
|
|
}
|
|
|
|
result->set_exception(std::make_exception_ptr(std::runtime_error{"Request failed: " + err}));
|
|
|
|
}
|
2020-08-18 03:07:51 +02:00
|
|
|
},
|
|
|
|
lokimq::send_option::data_parts(args.begin(), args.end()),
|
|
|
|
std::forward<Options>(opts)...
|
|
|
|
);
|
2020-08-13 17:28:51 +02:00
|
|
|
return fut;
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:24:23 +02:00
|
|
|
// Binds a stl future. `Conv` is a lambda that converts the future's .get() value into something
|
|
|
|
// Python-y (it can be the value directly, if the value is convertible to Python already).
|
|
|
|
template <typename F, typename Conv>
|
|
|
|
void bind_future(py::module& m, std::string class_name, Conv conv)
|
2020-08-13 17:28:51 +02:00
|
|
|
{
|
|
|
|
py::class_<F>(m, class_name.c_str())
|
2020-08-19 23:24:23 +02:00
|
|
|
.def("get", [conv=std::move(conv)](F& f) { return conv(f.get()); },
|
2020-08-13 17:28:51 +02:00
|
|
|
"Gets the result (or raises an exception if the result set an exception); must only be called once")
|
|
|
|
.def("valid", [](F& f) { return f.valid(); },
|
|
|
|
"Returns true if the result is available")
|
|
|
|
.def("wait", &F::wait,
|
|
|
|
"Waits indefinitely for the result to become available")
|
|
|
|
.def("wait_for", &F::template wait_for<double, std::ratio<1>>,
|
|
|
|
"Waits up to the given timedelta for the result to become available")
|
|
|
|
.def("wait_for", [](F& f, double seconds) { return f.wait_for(std::chrono::duration<double>{seconds}); },
|
|
|
|
"Waits up to the given number of seconds for the result to become available")
|
|
|
|
.def("wait_until", &F::template wait_until<std::chrono::system_clock, std::chrono::system_clock::duration>,
|
|
|
|
"Wait until the given datetime for the result to become available")
|
|
|
|
;
|
|
|
|
}
|
2020-08-13 17:27:25 +02:00
|
|
|
|
|
|
|
static std::mutex log_mutex;
|
|
|
|
|
2020-07-20 14:19:09 +02:00
|
|
|
void
|
|
|
|
LokiMQ_Init(py::module & mod)
|
|
|
|
{
|
2020-08-13 17:28:51 +02:00
|
|
|
using namespace pybind11::literals;
|
2020-09-13 18:19:51 +02:00
|
|
|
py::class_<ConnectionID>(mod, "ConnectionID")
|
|
|
|
.def("__eq__", [](const ConnectionID & self, const ConnectionID & other) {
|
|
|
|
return self == other;
|
|
|
|
});
|
2020-09-24 17:59:19 +02:00
|
|
|
py::class_<Message>(mod, "Message")
|
|
|
|
.def_readonly("remote", &Message::remote)
|
|
|
|
.def_readonly("conn", &Message::conn);
|
|
|
|
|
2020-07-20 14:19:09 +02:00
|
|
|
py::class_<address>(mod, "Address")
|
|
|
|
.def(py::init<std::string>());
|
2020-07-22 19:41:46 +02:00
|
|
|
py::class_<TaggedThreadID>(mod, "TaggedThreadID");
|
2020-08-13 17:27:25 +02:00
|
|
|
py::enum_<LogLevel>(mod, "LogLevel")
|
|
|
|
.value("fatal", LogLevel::fatal).value("error", LogLevel::error).value("warn", LogLevel::warn)
|
|
|
|
.value("info", LogLevel::info).value("debug", LogLevel::debug).value("trace", LogLevel::trace);
|
|
|
|
|
2020-08-13 17:28:51 +02:00
|
|
|
py::enum_<std::future_status>(mod, "future_status")
|
|
|
|
.value("deferred", std::future_status::deferred)
|
|
|
|
.value("ready", std::future_status::ready)
|
|
|
|
.value("timeout", std::future_status::timeout);
|
2020-08-19 23:24:23 +02:00
|
|
|
bind_future<std::future<std::vector<std::string>>>(mod, "ResultFuture",
|
|
|
|
[](std::vector<std::string> bytes) {
|
|
|
|
py::list l;
|
|
|
|
for (const auto& v : bytes)
|
|
|
|
l.append(py::bytes(v));
|
|
|
|
return l;
|
|
|
|
});
|
2020-08-13 17:28:51 +02:00
|
|
|
|
2020-07-20 14:19:09 +02:00
|
|
|
py::class_<LokiMQ>(mod, "LokiMQ")
|
|
|
|
.def(py::init<>())
|
2020-08-13 17:27:25 +02:00
|
|
|
.def(py::init([](LogLevel level) {
|
|
|
|
// Quick and dirty logger that logs to stderr. It would be much nicer to take a python
|
|
|
|
// function, but that deadlocks pretty much right away because of the crappiness of the gil.
|
|
|
|
return std::make_unique<LokiMQ>([] (LogLevel lvl, const char* file, int line, std::string msg) mutable {
|
|
|
|
std::lock_guard l{log_mutex};
|
|
|
|
std::cerr << '[' << lvl << "][" << file << ':' << line << "]: " << msg << "\n";
|
|
|
|
}, level);
|
|
|
|
}))
|
2020-08-18 05:47:55 +02:00
|
|
|
.def_readwrite("handshake_time", &LokiMQ::HANDSHAKE_TIME)
|
|
|
|
.def_readwrite("pubkey_base_routing_id", &LokiMQ::PUBKEY_BASED_ROUTING_ID)
|
|
|
|
.def_readwrite("max_message_size", &LokiMQ::MAX_MSG_SIZE)
|
|
|
|
.def_readwrite("max_sockets", &LokiMQ::MAX_SOCKETS)
|
|
|
|
.def_readwrite("reconnect_interval", &LokiMQ::RECONNECT_INTERVAL)
|
|
|
|
.def_readwrite("close_longer", &LokiMQ::CLOSE_LINGER)
|
|
|
|
.def_readwrite("connection_check_interval", &LokiMQ::CONN_CHECK_INTERVAL)
|
|
|
|
.def_readwrite("connection_heartbeat", &LokiMQ::CONN_HEARTBEAT)
|
|
|
|
.def_readwrite("connection_heartbeat_timeout", &LokiMQ::CONN_HEARTBEAT_TIMEOUT)
|
|
|
|
.def_readwrite("startup_umask", &LokiMQ::STARTUP_UMASK)
|
2020-07-20 14:19:09 +02:00
|
|
|
.def("start", &LokiMQ::start)
|
|
|
|
.def("listen_plain",
|
|
|
|
[](LokiMQ & self, std::string path) {
|
|
|
|
self.listen_plain(path);
|
|
|
|
})
|
|
|
|
.def("listen_curve", &LokiMQ::listen_curve)
|
2020-07-22 19:41:46 +02:00
|
|
|
.def("add_tagged_thread",
|
|
|
|
[](LokiMQ & self, const std::string & name) {
|
|
|
|
return self.add_tagged_thread(name);
|
|
|
|
})
|
|
|
|
.def("add_timer",
|
|
|
|
[](LokiMQ & self, std::chrono::milliseconds interval, std::function<void(void)> callback) {
|
|
|
|
self.add_timer(callback, interval);
|
|
|
|
})
|
|
|
|
.def("call_soon",
|
|
|
|
[](LokiMQ & self, std::function<void(void)> job, std::optional<TaggedThreadID> thread)
|
|
|
|
{
|
|
|
|
self.job(std::move(job), std::move(thread));
|
|
|
|
})
|
2020-07-20 14:19:09 +02:00
|
|
|
.def("add_anonymous_category",
|
|
|
|
[](LokiMQ & self, std::string name)
|
|
|
|
{
|
|
|
|
self.add_category(std::move(name), AuthLevel::none);
|
|
|
|
})
|
|
|
|
.def("add_request_command",
|
|
|
|
[](LokiMQ &self,
|
|
|
|
const std::string & category,
|
|
|
|
const std::string & name,
|
2020-09-14 12:57:05 +02:00
|
|
|
std::function<std::string(std::vector<py::bytes>)> handler)
|
2020-07-20 14:19:09 +02:00
|
|
|
{
|
|
|
|
self.add_request_command(category, name,
|
|
|
|
[handler](Message & msg) {
|
2020-07-22 19:41:46 +02:00
|
|
|
std::string result;
|
|
|
|
{
|
|
|
|
py::gil_scoped_acquire gil;
|
2020-09-24 17:42:24 +02:00
|
|
|
|
|
|
|
std::vector<py::bytes> data;
|
|
|
|
for (auto& arg : msg.data)
|
|
|
|
{
|
|
|
|
data.emplace_back(arg.begin(), arg.size());
|
|
|
|
}
|
|
|
|
|
2020-08-13 00:30:15 +02:00
|
|
|
try
|
|
|
|
{
|
2020-09-14 12:57:05 +02:00
|
|
|
result = handler(data);
|
2020-08-13 00:30:15 +02:00
|
|
|
}
|
|
|
|
catch(std::exception & ex)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, ex.what());
|
|
|
|
}
|
2020-09-13 17:50:29 +02:00
|
|
|
}
|
|
|
|
msg.send_reply(result);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.def("add_request_command_ex",
|
|
|
|
[](LokiMQ &self,
|
|
|
|
const std::string & category,
|
|
|
|
const std::string & name,
|
2020-09-14 12:57:05 +02:00
|
|
|
std::function<std::string(std::vector<py::bytes>, std::string_view, ConnectionID)> handler)
|
2020-09-13 17:50:29 +02:00
|
|
|
{
|
|
|
|
self.add_request_command(category, name,
|
|
|
|
[handler](Message & msg) {
|
|
|
|
std::string result;
|
|
|
|
{
|
|
|
|
py::gil_scoped_acquire gil;
|
2020-09-24 17:42:24 +02:00
|
|
|
|
|
|
|
std::vector<py::bytes> data;
|
|
|
|
for (auto& arg : msg.data)
|
|
|
|
{
|
|
|
|
data.emplace_back(arg.begin(), arg.size());
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:50:29 +02:00
|
|
|
try
|
|
|
|
{
|
2020-09-14 12:57:05 +02:00
|
|
|
result = handler(data, msg.remote, msg.conn);
|
2020-09-13 17:50:29 +02:00
|
|
|
}
|
|
|
|
catch(std::exception & ex)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, ex.what());
|
|
|
|
}
|
2020-07-22 19:41:46 +02:00
|
|
|
}
|
2020-07-20 14:19:09 +02:00
|
|
|
msg.send_reply(result);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.def("connect_remote",
|
|
|
|
[](LokiMQ & self,
|
2020-08-13 17:28:51 +02:00
|
|
|
std::string remote) -> ConnectionID
|
2020-07-20 14:19:09 +02:00
|
|
|
{
|
2020-08-13 17:28:51 +02:00
|
|
|
std::promise<ConnectionID> promise;
|
2020-07-20 14:19:09 +02:00
|
|
|
self.connect_remote(
|
|
|
|
remote,
|
|
|
|
[&promise](ConnectionID id) { promise.set_value(std::move(id)); },
|
2020-08-13 17:28:51 +02:00
|
|
|
[&promise](auto, std::string_view reason) {
|
|
|
|
promise.set_exception(std::make_exception_ptr(
|
|
|
|
std::runtime_error{"Connection failed: " + std::string{reason}}));
|
|
|
|
});
|
2020-07-20 14:19:09 +02:00
|
|
|
return promise.get_future().get();
|
|
|
|
})
|
|
|
|
.def("request",
|
|
|
|
[](LokiMQ & self,
|
|
|
|
ConnectionID conn,
|
|
|
|
std::string name,
|
2020-08-18 03:07:51 +02:00
|
|
|
std::vector<py::bytes> args,
|
2020-08-19 23:24:23 +02:00
|
|
|
std::optional<double> timeout) -> py::list
|
2020-08-13 17:28:51 +02:00
|
|
|
{
|
2020-08-19 23:24:23 +02:00
|
|
|
py::list l;
|
|
|
|
for (auto& s : LokiMQ_start_request(self, conn, std::move(name), std::move(args),
|
2020-08-18 03:07:51 +02:00
|
|
|
lokimq::send_option::request_timeout{timeout ? std::chrono::milliseconds(long(*timeout * 1000)) : DEFAULT_REQUEST_TIMEOUT}
|
2020-08-19 23:24:23 +02:00
|
|
|
).get())
|
|
|
|
l.append(py::bytes(s));
|
|
|
|
return l;
|
2020-08-13 17:28:51 +02:00
|
|
|
},
|
2020-08-18 03:07:51 +02:00
|
|
|
"conn"_a, "name"_a, "args"_a = std::vector<py::bytes>{}, "timeout"_a = py::none{})
|
2020-08-13 17:28:51 +02:00
|
|
|
.def("request_future",
|
|
|
|
[](LokiMQ & self,
|
|
|
|
ConnectionID conn,
|
|
|
|
std::string name,
|
2020-08-18 03:07:51 +02:00
|
|
|
std::vector<py::bytes> args,
|
2020-08-19 23:24:23 +02:00
|
|
|
std::optional<double> timeout) -> std::future<std::vector<std::string>>
|
2020-07-20 14:19:09 +02:00
|
|
|
{
|
2020-08-18 03:07:51 +02:00
|
|
|
return LokiMQ_start_request(self, conn, std::move(name), std::move(args),
|
|
|
|
lokimq::send_option::request_timeout{timeout ? std::chrono::milliseconds(long(*timeout * 1000)) : DEFAULT_REQUEST_TIMEOUT}
|
|
|
|
);
|
2020-08-13 17:28:51 +02:00
|
|
|
},
|
2020-08-18 03:07:51 +02:00
|
|
|
"conn"_a, "name"_a, "args"_a = std::vector<py::bytes>{}, "timeout"_a = py::none{})
|
2020-08-13 17:28:51 +02:00
|
|
|
;
|
2020-07-20 14:19:09 +02:00
|
|
|
}
|
|
|
|
}
|