This commit is contained in:
Jeff Becker 2020-07-20 08:19:09 -04:00
parent 12c0791518
commit 116cc5f164
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
9 changed files with 150 additions and 2 deletions

View File

@ -41,7 +41,6 @@ if(SUBMODULE_CHECK)
endfunction ()
check_submodule(external/pybind11)
check_submodule(external/loki-mq)
endif()
endif()

2
external/loki-mq vendored

@ -1 +1 @@
Subproject commit d28e39ffeb6de59e80c78aa6db0b221386ab8f0b
Subproject commit af189a8d72f1c0614a72014785fc8b65cdb0fbff

View File

@ -1,5 +1,7 @@
pybind11_add_module(pylokimq MODULE
batch_job.cpp
lokimq.cpp
module.cpp
)

15
pylokimq/common.hpp Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
namespace py = pybind11;
namespace lokimq
{
void
LokiMQ_Init(py::module &mod);
void
BatchJob_Init(py::module & mod);
}

76
pylokimq/lokimq.cpp Normal file
View File

@ -0,0 +1,76 @@
#include "common.hpp"
#include <lokimq/lokimq.h>
#include <lokimq/address.h>
#include <future>
#include <memory>
namespace lokimq
{
void
LokiMQ_Init(py::module & mod)
{
py::class_<ConnectionID>(mod, "ConnectionID");
py::class_<address>(mod, "Address")
.def(py::init<std::string>());
py::class_<LokiMQ>(mod, "LokiMQ")
.def(py::init<>())
.def("start", &LokiMQ::start)
.def("listen_plain",
[](LokiMQ & self, std::string path) {
self.listen_plain(path);
})
.def("listen_curve", &LokiMQ::listen_curve)
.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,
std::function<std::string(std::vector<std::string>)> handler)
{
self.add_request_command(category, name,
[handler](Message & msg) {
std::vector<std::string> args;
for(const auto & arg : msg.data)
args.emplace_back(arg);
auto result = handler(std::move(args));
msg.send_reply(result);
});
})
.def("connect_remote",
[](LokiMQ & self,
std::string remote) -> std::optional<ConnectionID>
{
std::promise<std::optional<ConnectionID>> promise;
self.connect_remote(
remote,
[&promise](ConnectionID id) { promise.set_value(std::move(id)); },
[&promise](auto, auto) { promise.set_value(std::nullopt); });
return promise.get_future().get();
})
.def("request",
[](LokiMQ & self,
ConnectionID conn,
std::string name,
std::string arg) -> std::optional<std::vector<std::string>>
{
std::promise<std::optional<std::vector<std::string>>> result;
self.request(conn, std::move(name),
[&result](bool success, std::vector<std::string> value)
{
if(not success)
{
result.set_value(std::nullopt);
return;
}
result.set_value(std::move(value));
}, arg);
return result.get_future().get();
});
}
}

View File

@ -0,0 +1,6 @@
#include "common.hpp"
PYBIND11_MODULE(pylokimq, m)
{
lokimq::LokiMQ_Init(m);
}

20
test/client_test.py Normal file
View File

@ -0,0 +1,20 @@
import pylokimq
def do_connected(lmq, conn):
print("connected via", conn)
return lmq.request(conn, "exit.auth", "dq3j4dj99w6wi4t4yjnya8sxtqr1rojt8jgnn6467o6aoenm3o3o.loki")
def do_request(lmq):
print('connect')
conn = lmq.connect_remote("ipc:///tmp/lokinet-exit.sock")
if conn:
return do_connected(lmq, conn)
def main():
lmq = pylokimq.LokiMQ()
print("start")
lmq.start()
print(do_request(lmq))
print("done")
main()

15
test/server_test.py Normal file
View File

@ -0,0 +1,15 @@
import pylokimq
import time
def handle_auth(args):
print(args)
return "OK"
lmq = pylokimq.LokiMQ()
lmq.listen_plain("ipc:///tmp/lmq.sock")
lmq.add_anonymous_category("llarp")
lmq.add_request_command("llarp", "authexit", handle_auth)
lmq.start()
while True:
time.sleep(1)

15
test/test.py Normal file
View File

@ -0,0 +1,15 @@
import pylokimq
import time
def handle_ping(args):
print(args)
return args
lmq = pylokimq.LokiMQ()
lmq.listen_plain("ipc:///tmp/lmq.sock")
lmq.add_anonymous_category("python")
lmq.add_request_command("python", "ping", handle_ping)
lmq.start()
while True:
time.sleep(1)