-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1d18cb
commit 2411974
Showing
24 changed files
with
612 additions
and
3,859 deletions.
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
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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/*************************************************************************** | ||
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and * | ||
* Wolf Vollprecht * | ||
* Copyright (c) 2018, QuantStack * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
****************************************************************************/ | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "nlohmann/json.hpp" | ||
|
||
#include "xeus/xcomm.hpp" | ||
#include "xeus/xinterpreter.hpp" | ||
|
||
#include "pybind11_json/pybind11_json.hpp" | ||
|
||
#include "pybind11/pybind11.h" | ||
#include "pybind11/functional.h" | ||
#include "pybind11/eval.h" | ||
|
||
#include "xeus-python/xutils.hpp" | ||
|
||
#include "xcomm.hpp" | ||
#include "xinternal_utils.hpp" | ||
|
||
namespace py = pybind11; | ||
namespace nl = nlohmann; | ||
|
||
namespace xpyt | ||
{ | ||
/********************* | ||
* xcomm declaration * | ||
********************/ | ||
|
||
class xcomm | ||
{ | ||
public: | ||
|
||
using python_callback_type = std::function<void(py::object)>; | ||
using cpp_callback_type = std::function<void(const xeus::xmessage&)>; | ||
using zmq_buffers_type = std::vector<zmq::message_t>; | ||
|
||
xcomm(const py::args& args, const py::kwargs& kwargs); | ||
xcomm(xeus::xcomm&& comm); | ||
xcomm(xcomm&& comm) = default; | ||
virtual ~xcomm(); | ||
|
||
std::string comm_id() const; | ||
bool kernel() const; | ||
|
||
void close(const py::args& args, const py::kwargs& kwargs); | ||
void send(const py::args& args, const py::kwargs& kwargs); | ||
void on_msg(const python_callback_type& callback); | ||
void on_close(const python_callback_type& callback); | ||
|
||
private: | ||
|
||
xeus::xtarget* target(const py::kwargs& kwargs) const; | ||
xeus::xguid id(const py::kwargs& kwargs) const; | ||
cpp_callback_type cpp_callback(const python_callback_type& callback) const; | ||
|
||
xeus::xcomm m_comm; | ||
}; | ||
|
||
struct xcomm_manager | ||
{ | ||
xcomm_manager() = default; | ||
|
||
void register_target(const py::str& target_name, const py::object& callback); | ||
}; | ||
|
||
/************************ | ||
* xcomm implementation * | ||
************************/ | ||
|
||
xcomm::xcomm(const py::args& /*args*/, const py::kwargs& kwargs) | ||
: m_comm(target(kwargs), id(kwargs)) | ||
{ | ||
m_comm.open( | ||
kwargs.attr("get")("metadata", py::dict()), | ||
kwargs.attr("get")("data", py::dict()), | ||
pylist_to_zmq_buffers(kwargs.attr("get")("buffers", py::list())) | ||
); | ||
} | ||
|
||
xcomm::xcomm(xeus::xcomm&& comm) | ||
: m_comm(std::move(comm)) | ||
{ | ||
} | ||
|
||
xcomm::~xcomm() | ||
{ | ||
} | ||
|
||
std::string xcomm::comm_id() const | ||
{ | ||
return m_comm.id(); | ||
} | ||
|
||
bool xcomm::kernel() const | ||
{ | ||
return true; | ||
} | ||
|
||
void xcomm::close(const py::args& /*args*/, const py::kwargs& kwargs) | ||
{ | ||
m_comm.close( | ||
kwargs.attr("get")("metadata", py::dict()), | ||
kwargs.attr("get")("data", py::dict()), | ||
pylist_to_zmq_buffers(kwargs.attr("get")("buffers", py::list())) | ||
); | ||
} | ||
|
||
void xcomm::send(const py::args& /*args*/, const py::kwargs& kwargs) | ||
{ | ||
m_comm.send( | ||
kwargs.attr("get")("metadata", py::dict()), | ||
kwargs.attr("get")("data", py::dict()), | ||
pylist_to_zmq_buffers(kwargs.attr("get")("buffers", py::list())) | ||
); | ||
} | ||
|
||
void xcomm::on_msg(const python_callback_type& callback) | ||
{ | ||
m_comm.on_message(cpp_callback(callback)); | ||
} | ||
|
||
void xcomm::on_close(const python_callback_type& callback) | ||
{ | ||
m_comm.on_close(cpp_callback(callback)); | ||
} | ||
|
||
xeus::xtarget* xcomm::target(const py::kwargs& kwargs) const | ||
{ | ||
std::string target_name = kwargs["target_name"].cast<std::string>(); | ||
return xeus::get_interpreter().comm_manager().target(target_name); | ||
} | ||
|
||
xeus::xguid xcomm::id(const py::kwargs& kwargs) const | ||
{ | ||
if (py::hasattr(kwargs, "comm_id")) | ||
{ | ||
// TODO: prevent copy | ||
return xeus::xguid(kwargs["comm_id"].cast<std::string>()); | ||
} | ||
else | ||
{ | ||
return xeus::new_xguid(); | ||
} | ||
} | ||
|
||
auto xcomm::cpp_callback(const python_callback_type& py_callback) const -> cpp_callback_type | ||
{ | ||
return [this, py_callback](const xeus::xmessage& msg) { | ||
XPYT_HOLDING_GIL(py_callback(cppmessage_to_pymessage(msg))) | ||
}; | ||
} | ||
|
||
void xcomm_manager::register_target(const py::str& target_name, const py::object& callback) | ||
{ | ||
auto target_callback = [&callback] (xeus::xcomm&& comm, const xeus::xmessage& msg) { | ||
XPYT_HOLDING_GIL(callback(xcomm(std::move(comm)), cppmessage_to_pymessage(msg))); | ||
}; | ||
|
||
xeus::get_interpreter().comm_manager().register_comm_target( | ||
static_cast<std::string>(target_name), target_callback | ||
); | ||
} | ||
|
||
/*************** | ||
* comm module * | ||
***************/ | ||
|
||
py::module get_comm_module_impl() | ||
{ | ||
py::module comm_module = create_module("comm"); | ||
|
||
py::class_<xcomm>(comm_module, "Comm") | ||
.def(py::init<py::args, py::kwargs>()) | ||
.def("close", &xcomm::close) | ||
.def("send", &xcomm::send) | ||
.def("on_msg", &xcomm::on_msg) | ||
.def("on_close", &xcomm::on_close) | ||
.def_property_readonly("comm_id", &xcomm::comm_id) | ||
.def_property_readonly("kernel", &xcomm::kernel); | ||
|
||
py::class_<xcomm_manager>(comm_module, "CommManager") | ||
.def(py::init<>()) | ||
.def("register_target", &xcomm_manager::register_target); | ||
|
||
return comm_module; | ||
} | ||
|
||
py::module get_comm_module() | ||
{ | ||
static py::module comm_module = get_comm_module_impl(); | ||
return comm_module; | ||
} | ||
} |
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
Oops, something went wrong.