This commit is contained in:
Jeff Becker 2020-07-22 13:41:46 -04:00
parent 9fb5895d8c
commit 32de27c2fe
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
13 changed files with 105 additions and 29 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
*~
*\#*
build/
build/
__pycache__

21
examples/client.py Normal file
View File

@ -0,0 +1,21 @@
import pylokimq
def do_connected(lmq, conn):
print("connected via", conn)
return lmq.request(conn, "llarp.auth", ["dq3j4dj99w6wi4t4yjnya8sxtqr1rojt8jgnn6467o6aoenm3o3o.loki", "token"])
def do_request(lmq):
print('connect')
conn = lmq.connect_remote("ipc:///tmp/lmq.sock")
if conn:
return do_connected(lmq, conn)
def main():
lmq = pylokimq.LokiMQ()
print("start")
lmq.start()
print(do_request(lmq))
print("done")
if __name__ == '__main__':
main()

View File

@ -2,11 +2,11 @@ import pylokimq
def do_connected(lmq, conn):
print("connected via", conn)
return lmq.request(conn, "exit.auth", "dq3j4dj99w6wi4t4yjnya8sxtqr1rojt8jgnn6467o6aoenm3o3o.loki")
return lmq.request(conn, "llarp.auth", "dq3j4dj99w6wi4t4yjnya8sxtqr1rojt8jgnn6467o6aoenm3o3o.loki")
def do_request(lmq):
print('connect')
conn = lmq.connect_remote("ipc:///tmp/lokinet-exit.sock")
conn = lmq.connect_remote("ipc:///tmp/lmq.sock")
if conn:
return do_connected(lmq, conn)
@ -17,4 +17,5 @@ def main():
print(do_request(lmq))
print("done")
main()
if __name__ == '__main__':
main()

19
examples/server.py Normal file
View File

@ -0,0 +1,19 @@
import pylokimq
import time
def handle_auth(args):
print(args)
return "OK"
def main():
lmq = pylokimq.LokiMQ()
lmq.listen_plain("ipc:///tmp/lmq.sock")
lmq.add_anonymous_category("llarp")
lmq.add_request_command("llarp", "auth", handle_auth)
lmq.start()
print("server started")
while True:
time.sleep(1)
if __name__ == '__main__':
main()

18
examples/server_test.py Normal file
View File

@ -0,0 +1,18 @@
import pylokimq
import time
def handle_auth(args):
print(args)
return "OK"
def main():
lmq = pylokimq.LokiMQ()
lmq.listen_plain("ipc:///tmp/lmq.sock")
lmq.add_anonymous_category("llarp")
lmq.add_request_command("llarp", "auth", handle_auth)
lmq.start()
while True:
time.sleep(1)
if __name__ == '__main__':
main()

2
external/loki-mq vendored

@ -1 +1 @@
Subproject commit af189a8d72f1c0614a72014785fc8b65cdb0fbff
Subproject commit e5cf174b830d5450626e29b9e46018eab71a9c7c

View File

@ -1,5 +1,6 @@
pybind11_add_module(pylokimq MODULE
bencode.cpp
lokimq.cpp
module.cpp
)

15
pylokimq/bencode.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "common.hpp"
#include "lokimq/bt_serialize.h"
namespace lokimq
{
void
BEncode_Init(py::module & mod)
{
auto & submod = mod.def_submodule("bencode");
submod.def("decode", [](py::bytes data) {
});
}
}

View File

@ -11,5 +11,5 @@ namespace lokimq
LokiMQ_Init(py::module &mod);
void
BatchJob_Init(py::module & mod);
BEnocde_Init(py::module & mod);
}

View File

@ -10,9 +10,9 @@ namespace lokimq
LokiMQ_Init(py::module & mod)
{
py::class_<ConnectionID>(mod, "ConnectionID");
py::class_<address>(mod, "Address")
.def(py::init<std::string>());
py::class_<TaggedThreadID>(mod, "TaggedThreadID");
py::class_<LokiMQ>(mod, "LokiMQ")
.def(py::init<>())
@ -22,6 +22,19 @@ namespace lokimq
self.listen_plain(path);
})
.def("listen_curve", &LokiMQ::listen_curve)
.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));
})
.def("add_anonymous_category",
[](LokiMQ & self, std::string name)
{
@ -31,14 +44,15 @@ namespace lokimq
[](LokiMQ &self,
const std::string & category,
const std::string & name,
std::function<std::string(std::vector<std::string>)> handler)
std::function<std::string(std::vector<std::string_view>)> 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));
std::string result;
{
py::gil_scoped_acquire gil;
result = handler(msg.data);
}
msg.send_reply(result);
});
})
@ -57,7 +71,7 @@ namespace lokimq
[](LokiMQ & self,
ConnectionID conn,
std::string name,
std::string arg) -> std::optional<std::vector<std::string>>
std::vector<std::string> args) -> std::optional<std::vector<std::string>>
{
std::promise<std::optional<std::vector<std::string>>> result;
self.request(conn, std::move(name),
@ -69,7 +83,7 @@ namespace lokimq
return;
}
result.set_value(std::move(value));
}, arg);
}, lokimq::send_option::data_parts(args.begin(), args.end()));
return result.get_future().get();
});
}

View File

@ -3,4 +3,5 @@
PYBIND11_MODULE(pylokimq, m)
{
lokimq::LokiMQ_Init(m);
lokimq::BEncode_Init(m);
}

View File

@ -1,15 +0,0 @@
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)