From 364f0eee5189837275887e887a797c9694c030d4 Mon Sep 17 00:00:00 2001 From: Thorsten Beier Date: Tue, 12 Mar 2024 09:58:20 +0100 Subject: [PATCH] Parent header (#384) Used xrequest_context for execute_request --- CMakeLists.txt | 2 + include/xeus/xcomm.hpp | 27 ++++--- include/xeus/xinterpreter.hpp | 33 ++++---- include/xeus/xrequest_context.hpp | 41 ++++++++++ src/xcomm.cpp | 8 +- src/xinput.cpp | 8 +- src/xinterpreter.cpp | 57 ++++++-------- src/xkernel_core.cpp | 126 ++++++++++++------------------ src/xkernel_core.hpp | 26 ++---- src/xmock_interpreter.hpp | 3 +- src/xrequest_context.cpp | 24 ++++++ test/xmock_interpreter.cpp | 15 ++-- test/xmock_interpreter.hpp | 3 +- 13 files changed, 205 insertions(+), 168 deletions(-) create mode 100644 include/xeus/xrequest_context.hpp create mode 100644 src/xrequest_context.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bc908a3..99adbe1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,7 @@ set(XEUS_HEADERS ${XEUS_INCLUDE_DIR}/xeus/xserver.hpp ${XEUS_INCLUDE_DIR}/xeus/xstring_utils.hpp ${XEUS_INCLUDE_DIR}/xeus/xsystem.hpp + ${XEUS_INCLUDE_DIR}/xeus/xrequest_context.hpp ) set(XEUS_SOURCES @@ -132,6 +133,7 @@ set(XEUS_SOURCES ${XEUS_SOURCE_DIR}/xmock_interpreter.hpp ${XEUS_SOURCE_DIR}/xserver.cpp ${XEUS_SOURCE_DIR}/xsystem.cpp + ${XEUS_SOURCE_DIR}/xrequest_context.cpp ) # Targets and link diff --git a/include/xeus/xcomm.hpp b/include/xeus/xcomm.hpp index b8a72984..63e6871f 100644 --- a/include/xeus/xcomm.hpp +++ b/include/xeus/xcomm.hpp @@ -51,7 +51,7 @@ namespace xeus void operator()(xcomm&& comm, xmessage request) const; - void publish_message(const std::string&, nl::json, nl::json, buffer_sequence) const; + void publish_message(const std::string&, nl::json parent_header, nl::json, nl::json, buffer_sequence) const; void register_comm(xguid, xcomm*) const; void unregister_comm(xguid) const; @@ -88,9 +88,9 @@ namespace xeus xcomm& operator=(xcomm&&); xcomm& operator=(const xcomm&); - void open(nl::json metadata, nl::json data, buffer_sequence buffers); - void close(nl::json metadata, nl::json data, buffer_sequence buffers); - void send(nl::json metadata, nl::json data, buffer_sequence buffers) const; + void open(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers); + void close(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers); + void send(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) const; xtarget& target() noexcept; const xtarget& target() const noexcept; @@ -119,10 +119,12 @@ namespace xeus * } */ void send_comm_message(const std::string& msg_type, + nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence) const; void send_comm_message(const std::string& msg_type, + nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence, @@ -262,6 +264,7 @@ namespace xeus } inline void xcomm::send_comm_message(const std::string& msg_type, + nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) const @@ -269,10 +272,11 @@ namespace xeus nl::json content; content["comm_id"] = m_id; content["data"] = std::move(data); - target().publish_message(msg_type, std::move(metadata), std::move(content), std::move(buffers)); + target().publish_message(msg_type, std::move(parent_header), std::move(metadata), std::move(content), std::move(buffers)); } inline void xcomm::send_comm_message(const std::string& msg_type, + nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers, @@ -283,7 +287,7 @@ namespace xeus content["target_name"] = target_name; content["data"] = std::move(data); target().publish_message( - msg_type, std::move(metadata), std::move(content), std::move(buffers)); + msg_type, std::move(parent_header), std::move(metadata), std::move(content), std::move(buffers)); } inline xcomm::xcomm(xcomm&& comm) @@ -352,23 +356,24 @@ namespace xeus } } - inline void xcomm::open(nl::json metadata, nl::json data, buffer_sequence buffers) + inline void xcomm::open(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) { send_comm_message("comm_open", + std::move(parent_header), std::move(metadata), std::move(data), std::move(buffers), p_target->name()); } - inline void xcomm::close(nl::json metadata, nl::json data, buffer_sequence buffers) + inline void xcomm::close(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) { - send_comm_message("comm_close", std::move(metadata), std::move(data), std::move(buffers)); + send_comm_message("comm_close", std::move(parent_header), std::move(metadata), std::move(data), std::move(buffers)); } - inline void xcomm::send(nl::json metadata, nl::json data, buffer_sequence buffers) const + inline void xcomm::send(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) const { - send_comm_message("comm_msg", std::move(metadata), std::move(data), std::move(buffers)); + send_comm_message("comm_msg", std::move(parent_header), std::move(metadata), std::move(data), std::move(buffers)); } inline xguid xcomm::id() const noexcept diff --git a/include/xeus/xinterpreter.hpp b/include/xeus/xinterpreter.hpp index 58383fcf..41847ce8 100644 --- a/include/xeus/xinterpreter.hpp +++ b/include/xeus/xinterpreter.hpp @@ -18,6 +18,7 @@ #include "xeus.hpp" #include "xcontrol_messenger.hpp" #include "xhistory_manager.hpp" +#include "xrequest_context.hpp" namespace xeus { @@ -41,7 +42,8 @@ namespace xeus void configure(); - nl::json execute_request(const std::string& code, + nl::json execute_request(xrequest_context context, + const std::string& code, bool silent, bool store_history, nl::json user_expressions, @@ -59,36 +61,33 @@ namespace xeus nl::json internal_request(const nl::json& message); // publish(msg_type, metadata, content) - using publisher_type = std::function; + using publisher_type = std::function; void register_publisher(const publisher_type& publisher); - void publish_stream(const std::string& name, const std::string& text); - void display_data(nl::json data, nl::json metadata, nl::json transient); - void update_display_data(nl::json data, nl::json metadata, nl::json transient); - void publish_execution_input(const std::string& code, int execution_count); - void publish_execution_result(int execution_count, nl::json data, nl::json metadata); - void publish_execution_error(const std::string& ename, + void publish_stream(xrequest_context, const std::string& name, const std::string& text); + void display_data(xrequest_context, nl::json data, nl::json metadata, nl::json transient); + void update_display_data(xrequest_context, nl::json data, nl::json metadata, nl::json transient); + void publish_execution_input(xrequest_context, const std::string& code, int execution_count); + void publish_execution_result(xrequest_context, int execution_count, nl::json data, nl::json metadata); + void publish_execution_error(xrequest_context, + const std::string& ename, const std::string& evalue, const std::vector& trace_back); - void clear_output(bool wait); + void clear_output(xrequest_context, bool wait); // send_stdin(msg_type, metadata, content) - using stdin_sender_type = std::function; + using stdin_sender_type = std::function; void register_stdin_sender(const stdin_sender_type& sender); using input_reply_handler_type = std::function; void register_input_handler(const input_reply_handler_type& handler); - void input_request(const std::string& prompt, bool pwd); + void input_request(xrequest_context context, const std::string& prompt, bool pwd); void input_reply(const std::string& value); void register_comm_manager(xcomm_manager* manager); xcomm_manager& comm_manager() noexcept; const xcomm_manager& comm_manager() const noexcept; - using parent_header_type = std::function; - void register_parent_header(const parent_header_type&); - const nl::json& parent_header() const noexcept; - void register_control_messenger(xcontrol_messenger& messenger); void register_history_manager(const xhistory_manager& history); @@ -102,7 +101,8 @@ namespace xeus virtual void configure_impl() = 0; - virtual nl::json execute_request_impl(int execution_counter, + virtual nl::json execute_request_impl(xrequest_context request_context, + int execution_counter, const std::string& code, bool silent, bool store_history, @@ -130,7 +130,6 @@ namespace xeus stdin_sender_type m_stdin; int m_execution_count; xcomm_manager* p_comm_manager; - parent_header_type m_parent_header; input_reply_handler_type m_input_reply_handler; xcontrol_messenger* p_messenger; const xhistory_manager* p_history; diff --git a/include/xeus/xrequest_context.hpp b/include/xeus/xrequest_context.hpp new file mode 100644 index 00000000..7eae9d53 --- /dev/null +++ b/include/xeus/xrequest_context.hpp @@ -0,0 +1,41 @@ +/*************************************************************************** +* Copyright (c) 2024, Dr. Thorsten Beier +* Distributed under the terms of the BSD 3-Clause License. * +* * +* The full license is in the file LICENSE, distributed with this software. * +****************************************************************************/ + +#ifndef XEUS_REQUEST_CONTEXT_HPP +#define XEUS_REQUEST_CONTEXT_HPP + +#include +#include + +#include "xeus/xeus.hpp" +#include "nlohmann/json.hpp" +#include "xeus/xmessage.hpp" // for xmessage::guid_list +#include "xeus/xserver.hpp" // for channel + +namespace nl = nlohmann; + +namespace xeus +{ + + class XEUS_API xrequest_context{ + public: + using guid_list = xmessage::guid_list; + xrequest_context(nl::json header, channel origin, guid_list id); + const nl::json& header() const; + channel origin() const; + const guid_list& id() const; + + private: + + nl::json m_header; + channel m_origin; + guid_list m_id; + + }; +} + +#endif diff --git a/src/xcomm.cpp b/src/xcomm.cpp index 27ce3b2d..5a154d63 100644 --- a/src/xcomm.cpp +++ b/src/xcomm.cpp @@ -16,6 +16,7 @@ namespace xeus { void xtarget::publish_message(const std::string& msg_type, + nl::json parent_header, nl::json metadata, nl::json content, buffer_sequence buffers) const @@ -23,7 +24,10 @@ namespace xeus if (p_manager->p_kernel != nullptr) { p_manager->p_kernel->publish_message( - msg_type, std::move(metadata), std::move(content), std::move(buffers), channel::SHELL); + msg_type, std::move(parent_header), + std::move(metadata), std::move(content), + std::move(buffers), channel::SHELL + ); } } @@ -73,7 +77,7 @@ namespace xeus if (p_kernel != nullptr) { p_kernel->publish_message( - "comm_close", nl::json::object(), std::move(content), buffer_sequence(), channel::SHELL + "comm_close", request.header(), nl::json::object(), std::move(content), buffer_sequence(), channel::SHELL ); } } diff --git a/src/xinput.cpp b/src/xinput.cpp index b879d9bf..3e965886 100644 --- a/src/xinput.cpp +++ b/src/xinput.cpp @@ -14,7 +14,11 @@ namespace xeus { - std::string blocking_input_request(const std::string& prompt, bool password) + std::string blocking_input_request( + xrequest_context context, + const std::string& prompt, + bool password + ) { auto& interpreter = xeus::get_interpreter(); @@ -23,7 +27,7 @@ namespace xeus interpreter.register_input_handler([&value](const std::string& v) { value = v; }); // Send the input request - interpreter.input_request(prompt, password); + interpreter.input_request(std::move(context), prompt, password); // Remove input handler interpreter.register_input_handler(nullptr); diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 5836f9e6..536dce12 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -29,7 +29,8 @@ namespace xeus configure_impl(); } - nl::json xinterpreter::execute_request(const std::string& code, + nl::json xinterpreter::execute_request(xrequest_context context, + const std::string& code, bool silent, bool store_history, nl::json user_expressions, @@ -38,10 +39,11 @@ namespace xeus if (!silent) { ++m_execution_count; - publish_execution_input(code, m_execution_count); + publish_execution_input(context, code, m_execution_count); } nl::json reply = execute_request_impl( + std::move(context), m_execution_count, code, silent, store_history, user_expressions, allow_stdin ); @@ -85,22 +87,23 @@ namespace xeus m_publisher = publisher; } - void xinterpreter::publish_stream(const std::string& name, const std::string& text) + void xinterpreter::publish_stream(xrequest_context context, const std::string& name, const std::string& text) { if (m_publisher) { nl::json content; content["name"] = name; content["text"] = text; - m_publisher("stream", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher(std::move(context), "stream", nl::json::object(), std::move(content), buffer_sequence()); } } - void xinterpreter::display_data(nl::json data, nl::json metadata, nl::json transient) + void xinterpreter::display_data(xrequest_context context, nl::json data, nl::json metadata, nl::json transient) { if (m_publisher) { m_publisher( + std::move(context), "display_data", nl::json::object(), build_display_content(std::move(data), std::move(metadata), std::move(transient)), @@ -108,11 +111,12 @@ namespace xeus } } - void xinterpreter::update_display_data(nl::json data, nl::json metadata, nl::json transient) + void xinterpreter::update_display_data(xrequest_context context, nl::json data, nl::json metadata, nl::json transient) { if (m_publisher) { m_publisher( + std::move(context), "update_display_data", nl::json::object(), build_display_content(std::move(data), std::move(metadata), std::move(transient)), @@ -120,18 +124,18 @@ namespace xeus } } - void xinterpreter::publish_execution_input(const std::string& code, int execution_count) + void xinterpreter::publish_execution_input(xrequest_context context, const std::string& code, int execution_count) { if (m_publisher) { nl::json content; content["code"] = code; content["execution_count"] = execution_count; - m_publisher("execute_input", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher(std::move(context), "execute_input", nl::json::object(), std::move(content), buffer_sequence()); } } - void xinterpreter::publish_execution_result(int execution_count, nl::json data, nl::json metadata) + void xinterpreter::publish_execution_result(xrequest_context context, int execution_count, nl::json data, nl::json metadata) { if (m_publisher) { @@ -139,11 +143,12 @@ namespace xeus content["execution_count"] = execution_count; content["data"] = std::move(data); content["metadata"] = std::move(metadata); - m_publisher("execute_result", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher(std::move(context), "execute_result", nl::json::object(), std::move(content), buffer_sequence()); } } - void xinterpreter::publish_execution_error(const std::string& ename, + void xinterpreter::publish_execution_error(xrequest_context context, + const std::string& ename, const std::string& evalue, const std::vector& trace_back) { @@ -153,17 +158,17 @@ namespace xeus content["ename"] = ename; content["evalue"] = evalue; content["traceback"] = trace_back; - m_publisher("error", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher(std::move(context), "error", nl::json::object(), std::move(content), buffer_sequence()); } } - void xinterpreter::clear_output(bool wait) + void xinterpreter::clear_output(xrequest_context context, bool wait) { if (m_publisher) { nl::json content; content["wait"] = wait; - m_publisher("clear_output", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher(std::move(context), "clear_output", nl::json::object(), std::move(content), buffer_sequence()); } } @@ -182,24 +187,6 @@ namespace xeus p_comm_manager = manager; } - void xinterpreter::register_parent_header(const parent_header_type& parent_header) - { - m_parent_header = parent_header; - } - - const nl::json& xinterpreter::parent_header() const noexcept - { - static const auto dummy = nl::json::object(); - if (m_parent_header) - { - return m_parent_header(); - } - else - { - return dummy; - } - } - void xinterpreter::register_control_messenger(xcontrol_messenger& messenger) { p_messenger = &messenger; @@ -220,14 +207,14 @@ namespace xeus return *p_messenger; } - void xinterpreter::input_request(const std::string& prompt, bool pwd) + void xinterpreter::input_request(xrequest_context context, const std::string& prompt, bool pwd) { if (m_stdin) { nl::json content; content["prompt"] = prompt; content["pwd"] = pwd; - m_stdin("input_request", nl::json::object(), std::move(content)); + m_stdin(std::move(context), "input_request", nl::json::object(), std::move(content)); } } @@ -239,7 +226,7 @@ namespace xeus } } - nl::json xinterpreter::internal_request_impl(const nl::json&) + nl::json xinterpreter::internal_request_impl( const nl::json&) { nl::json res; res["status"] = "error"; diff --git a/src/xkernel_core.cpp b/src/xkernel_core.cpp index 0b03088a..98ac5a20 100644 --- a/src/xkernel_core.cpp +++ b/src/xkernel_core.cpp @@ -18,6 +18,7 @@ #include "xkernel_core.hpp" #include "xeus/xhistory_manager.hpp" +#include "xeus/xrequest_context.hpp" using namespace std::placeholders; @@ -40,8 +41,6 @@ namespace xeus , p_interpreter(interpreter) , p_history_manager(history_manager) , p_debugger(debugger) - , m_parent_id({guid_list(0), guid_list(0)}) - , m_parent_header({nl::json::object(), nl::json::object()}) { // Request handlers m_handler["execute_request"] = &xkernel_core::execute_request; @@ -65,19 +64,27 @@ namespace xeus p_server->register_internal_listener(std::bind(&xkernel_core::dispatch_internal, this, _1)); // Interpreter bindings - p_interpreter->register_publisher([this](const std::string& msg_type, + p_interpreter->register_publisher([this](xrequest_context request_context, + const std::string& msg_type, nl::json metadata, nl::json content, buffer_sequence buffers) { - this->publish_message(msg_type, std::move(metadata), std::move(content), std::move(buffers), + this->publish_message(msg_type, request_context.header(), std::move(metadata), std::move(content), std::move(buffers), channel::SHELL); }); - p_interpreter->register_stdin_sender(std::bind(&xkernel_core::send_stdin, this, _1, _2, _3)); - p_interpreter->register_comm_manager(&m_comm_manager); - p_interpreter->register_parent_header([this]() -> const nl::json& { - return this->parent_header(channel::SHELL); + + p_interpreter->register_stdin_sender([this](xrequest_context request_context, + const std::string& msg_type, + nl::json metadata, + nl::json content) + { + this->send_stdin(msg_type, request_context.id(), request_context.header(), std::move(metadata), std::move(content)); }); + + + p_interpreter->register_comm_manager(&m_comm_manager); + } xkernel_core::~xkernel_core() @@ -133,6 +140,7 @@ namespace xeus } void xkernel_core::publish_message(const std::string& msg_type, + nl::json parent_header, nl::json metadata, nl::json content, buffer_sequence buffers, @@ -140,7 +148,7 @@ namespace xeus { xpub_message msg(get_topic(msg_type), make_header(msg_type, m_user_name, m_session_id), - get_parent_header(c), + std::move(parent_header), std::move(metadata), std::move(content), std::move(buffers)); @@ -149,12 +157,14 @@ namespace xeus } void xkernel_core::send_stdin(const std::string& msg_type, + const guid_list& id_list, + nl::json parent_header, nl::json metadata, nl::json content) { - xmessage msg(get_parent_id(channel::SHELL), + xmessage msg(id_list, make_header(msg_type, m_user_name, m_session_id), - get_parent_header(channel::SHELL), + std::move(parent_header), std::move(metadata), std::move(content), buffer_sequence()); @@ -177,17 +187,11 @@ namespace xeus return m_comm_manager; } - const nl::json& xkernel_core::parent_header(channel c) const noexcept - { - return m_parent_header[std::size_t(c)]; - } - void xkernel_core::dispatch(xmessage msg, channel c) { p_logger->log_received_message(msg, c == channel::SHELL ? xlogger::shell : xlogger::control); const nl::json& header = msg.header(); - set_parent(msg.identities(), header, c); - publish_status("busy", c); + publish_status(header, "busy", c); std::string msg_type = header.value("msg_type", ""); handler_type handler = get_handler(msg_type); @@ -209,7 +213,7 @@ namespace xeus } } - publish_status("idle", c); + publish_status(header, "idle", c); } auto xkernel_core::get_handler(const std::string& msg_type) -> handler_type @@ -232,13 +236,19 @@ namespace xeus bool allow_stdin = content.value("allow_stdin", true); bool stop_on_error = content.value("stop_on_error", false); - nl::json metadata = get_metadata(); - - nl::json reply = p_interpreter->execute_request( + nl::json metadata = get_metadata(); + xrequest_context request_context(request.header(), c, request.identities()); + nl::json reply = p_interpreter->execute_request(std::move(request_context), code, silent, store_history, std::move(user_expression), allow_stdin); int execution_count = reply.value("execution_count", 1); std::string status = reply.value("status", "error"); - send_reply("execute_reply", std::move(metadata), std::move(reply), c); + send_reply( + request.identities(), + "execute_reply", + request.header(), + std::move(metadata), + std::move(reply), + c); if (!silent && store_history) { @@ -263,9 +273,9 @@ namespace xeus const nl::json& content = request.content(); std::string code = content.value("code", ""); int cursor_pos = content.value("cursor_pos", -1); - nl::json reply = p_interpreter->complete_request(code, cursor_pos); - send_reply("complete_reply", nl::json::object(), std::move(reply), c); + send_reply(request.identities(), "complete_reply", request.header(), + nl::json::object(), std::move(reply), c); } void xkernel_core::inspect_request(xmessage request, channel c) @@ -274,9 +284,8 @@ namespace xeus std::string code = content.value("code", ""); int cursor_pos = content.value("cursor_pos", -1); int detail_level = content.value("detail_level", 0); - nl::json reply = p_interpreter->inspect_request(code, cursor_pos, detail_level); - send_reply("inspect_reply", nl::json::object(), std::move(reply), c); + send_reply(request.identities(), "inspect_reply", request.header(), nl::json::object(), std::move(reply), c); } void xkernel_core::history_request(xmessage request, channel c) @@ -285,16 +294,15 @@ namespace xeus nl::json history = p_history_manager->process_request(content); - send_reply("history_reply", nl::json::object(), std::move(history), c); + send_reply(request.identities(), "history_reply", request.header(), nl::json::object(), std::move(history), c); } void xkernel_core::is_complete_request(xmessage request, channel c) { const nl::json& content = request.content(); std::string code = content.value("code", ""); - nl::json reply = p_interpreter->is_complete_request(code); - send_reply("is_complete_reply", nl::json::object(), std::move(reply), c); + send_reply(request.identities(), "is_complete_reply", request.header(), nl::json::object(), std::move(reply), c); } void xkernel_core::comm_info_request(xmessage request, channel c) @@ -315,14 +323,14 @@ namespace xeus nl::json reply; reply["comms"] = comms; reply["status"] = "ok"; - send_reply("comm_info_reply", nl::json::object(), std::move(reply), c); + send_reply(request.identities(), "comm_info_reply",request.header(), nl::json::object(), std::move(reply), c); } - void xkernel_core::kernel_info_request(xmessage /* request */, channel c) + void xkernel_core::kernel_info_request(xmessage request , channel c) { nl::json reply = p_interpreter->kernel_info_request(); reply["protocol_version"] = get_protocol_version(); - send_reply("kernel_info_reply", nl::json::object(), std::move(reply), c); + send_reply(request.identities(), "kernel_info_reply", request.header(), nl::json::object(), std::move(reply), c); } void xkernel_core::shutdown_request(xmessage request, channel c) @@ -333,15 +341,15 @@ namespace xeus p_server->stop(); nl::json reply; reply["restart"] = restart; - publish_message("shutdown", nl::json::object(), std::move(reply), buffer_sequence(), channel::CONTROL); - send_reply("shutdown_reply", nl::json::object(), std::move(reply), c); + publish_message("shutdown", request.header(), nl::json::object(), std::move(reply), buffer_sequence(), channel::CONTROL); + send_reply(request.identities(), "shutdown_reply", request.header(), nl::json::object(), std::move(reply), c); } - void xkernel_core::interrupt_request(xmessage, channel c) + void xkernel_core::interrupt_request(xmessage request, channel c) { nl::json reply = nl::json::object(); - publish_message("interrupt", nl::json::object(), std::move(reply), buffer_sequence(), channel::CONTROL); - send_reply("interrupt_reply", nl::json::object(), std::move(reply), c); + publish_message("interrupt", request.header(), nl::json::object(), std::move(reply), buffer_sequence(), channel::CONTROL); + send_reply(request.identities(), "interrupt_reply", request.header(), nl::json::object(), std::move(reply), c); } void xkernel_core::debug_request(xmessage request, channel c) @@ -350,43 +358,32 @@ namespace xeus { nl::json reply = p_debugger->process_request(request.header(), request.content()); nl::json metadata = get_metadata(); - send_reply("debug_reply", std::move(metadata), std::move(reply), c); + send_reply(request.identities(), "debug_reply", request.header(), std::move(metadata), std::move(reply), c); } } - void xkernel_core::publish_status(const std::string& status, channel c) + void xkernel_core::publish_status( nl::json parent_header, const std::string& status, channel c) { nl::json content; content["execution_state"] = status; - publish_message("status", nl::json::object(), std::move(content), buffer_sequence(), c); + publish_message("status", parent_header, nl::json::object(), std::move(content), buffer_sequence(), c); } - void xkernel_core::publish_execute_input(const std::string& code, + void xkernel_core::publish_execute_input(nl::json parent_header, + const std::string& code, int execution_count) { nl::json content; content["code"] = code; content["execution_count"] = execution_count; publish_message("execute_input", + std::move(parent_header), nl::json::object(), std::move(content), buffer_sequence(), channel::SHELL); } - void xkernel_core::send_reply(const std::string& reply_type, - nl::json metadata, - nl::json reply_content, - channel c) - { - send_reply(get_parent_id(c), - reply_type, - get_parent_header(c), - std::move(metadata), - std::move(reply_content), - c); - } - void xkernel_core::send_reply(const guid_list& id_list, const std::string& reply_type, nl::json parent_header, @@ -439,25 +436,6 @@ namespace xeus return metadata; } - void xkernel_core::set_parent(const guid_list& parent_id, - const nl::json& parent_header, - channel c) - { - auto idx = static_cast(c); - m_parent_id[idx] = parent_id; - m_parent_header[idx] = parent_header; - } - - const xkernel_core::guid_list& xkernel_core::get_parent_id(channel c) const - { - return m_parent_id[std::size_t(c)]; - } - - nl::json xkernel_core::get_parent_header(channel c) const - { - return m_parent_header[std::size_t(c)]; - } - void xkernel_core::comm_open(xmessage request, channel) { m_comm_manager.comm_open(std::move(request)); diff --git a/src/xkernel_core.hpp b/src/xkernel_core.hpp index 47b45124..a901e354 100644 --- a/src/xkernel_core.hpp +++ b/src/xkernel_core.hpp @@ -36,6 +36,7 @@ namespace xeus using interpreter_ptr = xinterpreter*; using history_manager_ptr = xhistory_manager*; using debugger_ptr = xdebugger*; + using guid_list = xmessage::guid_list; xkernel_core(const std::string& kernel_id, const std::string& user_name, @@ -54,14 +55,15 @@ namespace xeus void dispatch_control(xmessage msg); void dispatch_stdin(xmessage msg); nl::json dispatch_internal(nl::json msg); - + void publish_message(const std::string& msg_type, + nl::json parent_header, nl::json metadata, nl::json content, buffer_sequence buffers, - channel origin); + channel origin ); - void send_stdin(const std::string& msg_type, nl::json metadata, nl::json content); + void send_stdin(const std::string& msg_type, const guid_list& id_list, nl::json parent_header, nl::json metadata, nl::json content); xcomm_manager& comm_manager() & noexcept; const xcomm_manager& comm_manager() const & noexcept; @@ -71,7 +73,7 @@ namespace xeus private: using handler_type = void (xkernel_core::*)(xmessage, channel); - using guid_list = xmessage::guid_list; + void dispatch(xmessage msg, channel c); @@ -92,14 +94,8 @@ namespace xeus void interrupt_request(xmessage request, channel c); void debug_request(xmessage request, channel c); - void publish_status(const std::string& status, channel c); - - void publish_execute_input(const std::string& code, int execution_count); - - void send_reply(const std::string& reply_type, - nl::json metadata, - nl::json reply_content, - channel c); + void publish_status(nl::json parent_header, const std::string& status, channel c); + void publish_execute_input(nl::json parent_header, const std::string& code, int execution_count); void send_reply(const guid_list& id_list, const std::string& reply_type, @@ -113,9 +109,6 @@ namespace xeus std::string get_topic(const std::string& msg_type) const; nl::json get_metadata() const; - void set_parent(const guid_list& list, const nl::json& parent, channel c); - const guid_list& get_parent_id(channel c) const; - nl::json get_parent_header(channel c) const; std::string m_kernel_id; std::string m_user_name; @@ -128,9 +121,6 @@ namespace xeus interpreter_ptr p_interpreter; history_manager_ptr p_history_manager; debugger_ptr p_debugger; - - std::array m_parent_id; - std::array m_parent_header; }; } diff --git a/src/xmock_interpreter.hpp b/src/xmock_interpreter.hpp index 87f3d8e5..5ef465c3 100644 --- a/src/xmock_interpreter.hpp +++ b/src/xmock_interpreter.hpp @@ -38,7 +38,8 @@ namespace xeus { } - nl::json execute_request_impl(int /*execution_counter*/, + nl::json execute_request_impl(xrequest_context /*request_context*/, + int /*execution_counter*/, const std::string& /*code*/, bool /*silent*/, bool /*store_history*/, diff --git a/src/xrequest_context.cpp b/src/xrequest_context.cpp new file mode 100644 index 00000000..bb11c783 --- /dev/null +++ b/src/xrequest_context.cpp @@ -0,0 +1,24 @@ +#include "xeus/xrequest_context.hpp" + +namespace xeus +{ + xrequest_context::xrequest_context(nl::json header, channel origin, guid_list id) + : m_header(std::move(header)), m_origin(origin), m_id(std::move(id)) + { + } + + const nl::json& xrequest_context::header() const + { + return m_header; + } + + channel xrequest_context::origin() const + { + return m_origin; + } + + const xmessage::guid_list& xrequest_context::id() const + { + return m_id; + } +} \ No newline at end of file diff --git a/test/xmock_interpreter.cpp b/test/xmock_interpreter.cpp index 078e80bc..f498db49 100644 --- a/test/xmock_interpreter.cpp +++ b/test/xmock_interpreter.cpp @@ -29,7 +29,8 @@ namespace xeus using function_type = std::function; } - nl::json xmock_interpreter::execute_request_impl(int execution_counter, + nl::json xmock_interpreter::execute_request_impl(xrequest_context request_context, + int execution_counter, const std::string& code, bool /* silent */, bool /* store_history */, @@ -38,12 +39,12 @@ namespace xeus { if (code.compare("hello, world") == 0) { - publish_stream("stdout", code); + publish_stream(request_context, "stdout", code); } if (code.compare("error") == 0) { - publish_stream("stderr", code); + publish_stream(request_context, "stderr", code); } if (code.compare("?") == 0) @@ -66,20 +67,20 @@ namespace xeus nl::json pub_data; pub_data["text/plain"] = code; - publish_execution_result(execution_counter, std::move(pub_data), nl::json::object()); + publish_execution_result(request_context, execution_counter, std::move(pub_data), nl::json::object()); return xeus::create_successful_reply(); } nl::json xmock_interpreter::complete_request_impl(const std::string& /* code */, - int /* cursor_pos */) + int /* cursor_pos */) { return xeus::create_complete_reply({"a.test1", "a.test2"}, 2, 6); } nl::json xmock_interpreter::inspect_request_impl(const std::string& /* code */, - int /* cursor_pos */, - int /* detail_level */) + int /* cursor_pos */, + int /* detail_level */) { return xeus::create_inspect_reply(true, {{"text/plain", ""}}, {{"text/plain", ""}}); } diff --git a/test/xmock_interpreter.hpp b/test/xmock_interpreter.hpp index d5bd413a..2ec010c7 100644 --- a/test/xmock_interpreter.hpp +++ b/test/xmock_interpreter.hpp @@ -25,7 +25,8 @@ namespace xeus void configure_impl() override; - nl::json execute_request_impl(int execution_counter, + nl::json execute_request_impl(xrequest_context request_context, + int execution_counter, const std::string& code, bool silent, bool store_history,