From 0c19f6ebd36397b972fabc9ddf6d7328b35a6c64 Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Fri, 24 May 2024 17:42:23 +0200 Subject: [PATCH] Back to storing the request context instead of passing it to all public APIs --- include/xeus/xcomm.hpp | 28 ++++----- include/xeus/xinput.hpp | 2 - include/xeus/xinterpreter.hpp | 26 +++++---- include/xeus/xrequest_context.hpp | 8 +-- src/xcomm.cpp | 3 +- src/xinput.cpp | 3 +- src/xinterpreter.cpp | 94 ++++++++++++++++++++++++------- src/xkernel.cpp | 5 ++ src/xkernel_core.cpp | 18 ++++-- src/xkernel_core.hpp | 4 +- src/xmock_interpreter.hpp | 3 +- src/xrequest_context.cpp | 9 +-- test/xmock_interpreter.cpp | 9 ++- test/xmock_interpreter.hpp | 3 +- 14 files changed, 132 insertions(+), 83 deletions(-) diff --git a/include/xeus/xcomm.hpp b/include/xeus/xcomm.hpp index a0d409eb..3f04e92a 100644 --- a/include/xeus/xcomm.hpp +++ b/include/xeus/xcomm.hpp @@ -50,7 +50,7 @@ namespace xeus void operator()(xcomm&& comm, xmessage request) const; - void publish_message(const std::string&, nl::json parent_header, nl::json, nl::json, buffer_sequence) const; + void publish_message(const std::string&, nl::json, nl::json, buffer_sequence) const; void register_comm(xguid, xcomm*) const; void unregister_comm(xguid) const; @@ -87,9 +87,9 @@ namespace xeus xcomm& operator=(xcomm&&); xcomm& operator=(const xcomm&); - 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; + 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; xtarget& target() noexcept; const xtarget& target() const noexcept; @@ -118,12 +118,10 @@ 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, @@ -263,7 +261,6 @@ 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 @@ -271,11 +268,10 @@ namespace xeus nl::json content; content["comm_id"] = m_id; content["data"] = std::move(data); - target().publish_message(msg_type, std::move(parent_header), std::move(metadata), std::move(content), std::move(buffers)); + target().publish_message(msg_type, 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, @@ -285,8 +281,7 @@ namespace xeus content["comm_id"] = m_id; content["target_name"] = target_name; content["data"] = std::move(data); - target().publish_message( - msg_type, std::move(parent_header), std::move(metadata), std::move(content), std::move(buffers)); + target().publish_message(msg_type, std::move(metadata), std::move(content), std::move(buffers)); } inline xcomm::xcomm(xcomm&& comm) @@ -355,24 +350,23 @@ namespace xeus } } - inline void xcomm::open(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) + inline void xcomm::open(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 parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) + inline void xcomm::close(nl::json metadata, nl::json data, buffer_sequence buffers) { - send_comm_message("comm_close", std::move(parent_header), std::move(metadata), std::move(data), std::move(buffers)); + send_comm_message("comm_close", std::move(metadata), std::move(data), std::move(buffers)); } - inline void xcomm::send(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) const + inline void xcomm::send(nl::json metadata, nl::json data, buffer_sequence buffers) const { - send_comm_message("comm_msg", std::move(parent_header), std::move(metadata), std::move(data), std::move(buffers)); + send_comm_message("comm_msg", std::move(metadata), std::move(data), std::move(buffers)); } inline xguid xcomm::id() const noexcept diff --git a/include/xeus/xinput.hpp b/include/xeus/xinput.hpp index 9f2b92b5..bf33248c 100644 --- a/include/xeus/xinput.hpp +++ b/include/xeus/xinput.hpp @@ -13,13 +13,11 @@ #include #include "xeus/xeus.hpp" -#include "xeus/xrequest_context.hpp" namespace xeus { XEUS_API std::string blocking_input_request( - xrequest_context context, const std::string& prompt, bool password ); diff --git a/include/xeus/xinterpreter.hpp b/include/xeus/xinterpreter.hpp index 8ebc7afb..b23b9d4c 100644 --- a/include/xeus/xinterpreter.hpp +++ b/include/xeus/xinterpreter.hpp @@ -71,16 +71,15 @@ namespace xeus using publisher_type = std::function; void register_publisher(const publisher_type& publisher); - 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, + 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, const std::string& evalue, const std::vector& trace_back); - void clear_output(xrequest_context, bool wait); + void clear_output(bool wait); // send_stdin(msg_type, metadata, content) using stdin_sender_type = std::function; @@ -88,13 +87,15 @@ namespace xeus using input_reply_handler_type = std::function; void register_input_handler(const input_reply_handler_type& handler); - void input_request(xrequest_context context, const std::string& prompt, bool pwd); + void input_request(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; + const nl::json& parent_header() const noexcept; + void register_control_messenger(xcontrol_messenger& messenger); void register_history_manager(const xhistory_manager& history); @@ -108,8 +109,7 @@ namespace xeus virtual void configure_impl() = 0; - virtual void execute_request_impl(xrequest_context request_context, - send_reply_callback cb, + virtual void execute_request_impl(send_reply_callback cb, int execution_counter, const std::string& code, execute_request_config config, @@ -132,6 +132,9 @@ namespace xeus nl::json build_display_content(nl::json data, nl::json metadata, nl::json transient); + virtual void set_request_context(xrequest_context context); + virtual const xrequest_context& get_request_context() const noexcept; + publisher_type m_publisher; stdin_sender_type m_stdin; int m_execution_count; @@ -139,6 +142,7 @@ namespace xeus input_reply_handler_type m_input_reply_handler; xcontrol_messenger* p_messenger; const xhistory_manager* p_history; + xrequest_context m_request_context; }; inline xcomm_manager& xinterpreter::comm_manager() noexcept diff --git a/include/xeus/xrequest_context.hpp b/include/xeus/xrequest_context.hpp index 0aa36b55..2119b190 100644 --- a/include/xeus/xrequest_context.hpp +++ b/include/xeus/xrequest_context.hpp @@ -14,7 +14,6 @@ #include "xeus/xeus.hpp" #include "xeus/xjson.hpp" #include "xeus/xmessage.hpp" // for xmessage::guid_list -#include "xeus/xserver.hpp" // for channel namespace nl = nlohmann; @@ -26,16 +25,15 @@ namespace xeus using guid_list = xmessage::guid_list; - xrequest_context(nl::json header, channel origin, guid_list id); + xrequest_context() = default; + xrequest_context(nl::json header, guid_list id); const nl::json& header() const; - channel origin() const; const guid_list& id() const; private: - nl::json m_header; - channel m_origin; + nl::json m_header = nl::json::object(); guid_list m_id; }; } diff --git a/src/xcomm.cpp b/src/xcomm.cpp index 5a154d63..41a826c9 100644 --- a/src/xcomm.cpp +++ b/src/xcomm.cpp @@ -16,7 +16,6 @@ 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 @@ -24,7 +23,7 @@ namespace xeus if (p_manager->p_kernel != nullptr) { p_manager->p_kernel->publish_message( - msg_type, std::move(parent_header), + msg_type, p_manager->p_kernel->parent_header(), std::move(metadata), std::move(content), std::move(buffers), channel::SHELL ); diff --git a/src/xinput.cpp b/src/xinput.cpp index 3e965886..d0448fe7 100644 --- a/src/xinput.cpp +++ b/src/xinput.cpp @@ -15,7 +15,6 @@ namespace xeus { std::string blocking_input_request( - xrequest_context context, const std::string& prompt, bool password ) @@ -27,7 +26,7 @@ namespace xeus interpreter.register_input_handler([&value](const std::string& v) { value = v; }); // Send the input request - interpreter.input_request(std::move(context), prompt, password); + interpreter.input_request(prompt, password); // Remove input handler interpreter.register_input_handler(nullptr); diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 48f4bdd1..7af96fc6 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -35,10 +35,11 @@ namespace xeus execute_request_config config, nl::json user_expressions) { + set_request_context(std::move(context)); if (!config.silent) { ++m_execution_count; - publish_execution_input(context, code, m_execution_count); + publish_execution_input(code, m_execution_count); } // copy m_execution_count in a local variable to capture it in the lambda auto execution_count = m_execution_count; @@ -50,7 +51,6 @@ namespace xeus }; execute_request_impl( - std::move(context), std::move(callback_impl), m_execution_count, code, @@ -94,55 +94,69 @@ namespace xeus m_publisher = publisher; } - void xinterpreter::publish_stream(xrequest_context context, const std::string& name, const std::string& text) + void xinterpreter::publish_stream(const std::string& name, const std::string& text) { if (m_publisher) { nl::json content; content["name"] = name; content["text"] = text; - m_publisher(std::move(context), "stream", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher( + get_request_context(), + "stream", + nl::json::object(), + std::move(content), + buffer_sequence() + ); } } - void xinterpreter::display_data(xrequest_context context, nl::json data, nl::json metadata, nl::json transient) + void xinterpreter::display_data(nl::json data, nl::json metadata, nl::json transient) { if (m_publisher) { m_publisher( - std::move(context), + get_request_context(), "display_data", nl::json::object(), build_display_content(std::move(data), std::move(metadata), std::move(transient)), - buffer_sequence()); + buffer_sequence() + ); } } - void xinterpreter::update_display_data(xrequest_context context, nl::json data, nl::json metadata, nl::json transient) + void xinterpreter::update_display_data(nl::json data, nl::json metadata, nl::json transient) { if (m_publisher) { m_publisher( - std::move(context), + get_request_context(), "update_display_data", nl::json::object(), build_display_content(std::move(data), std::move(metadata), std::move(transient)), - buffer_sequence()); + buffer_sequence() + ); } } - void xinterpreter::publish_execution_input(xrequest_context context, const std::string& code, int execution_count) + void xinterpreter::publish_execution_input(const std::string& code, int execution_count) { if (m_publisher) { nl::json content; content["code"] = code; content["execution_count"] = execution_count; - m_publisher(std::move(context), "execute_input", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher( + get_request_context(), + "execute_input", + nl::json::object(), + std::move(content), + buffer_sequence() + ); } } - void xinterpreter::publish_execution_result(xrequest_context context, int execution_count, nl::json data, nl::json metadata) + void xinterpreter::publish_execution_result(int execution_count, nl::json data, nl::json metadata) { if (m_publisher) { @@ -150,12 +164,17 @@ namespace xeus content["execution_count"] = execution_count; content["data"] = std::move(data); content["metadata"] = std::move(metadata); - m_publisher(std::move(context), "execute_result", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher( + get_request_context(), + "execute_result", + nl::json::object(), + std::move(content), + buffer_sequence() + ); } } - void xinterpreter::publish_execution_error(xrequest_context context, - const std::string& ename, + void xinterpreter::publish_execution_error(const std::string& ename, const std::string& evalue, const std::vector& trace_back) { @@ -165,17 +184,29 @@ namespace xeus content["ename"] = ename; content["evalue"] = evalue; content["traceback"] = trace_back; - m_publisher(std::move(context), "error", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher( + get_request_context(), + "error", + nl::json::object(), + std::move(content), + buffer_sequence() + ); } } - void xinterpreter::clear_output(xrequest_context context, bool wait) + void xinterpreter::clear_output(bool wait) { if (m_publisher) { nl::json content; content["wait"] = wait; - m_publisher(std::move(context), "clear_output", nl::json::object(), std::move(content), buffer_sequence()); + m_publisher( + get_request_context(), + "clear_output", + nl::json::object(), + std::move(content), + buffer_sequence() + ); } } @@ -194,6 +225,12 @@ namespace xeus p_comm_manager = manager; } + const nl::json& xinterpreter::parent_header() const noexcept + { + + return get_request_context().header(); + } + void xinterpreter::register_control_messenger(xcontrol_messenger& messenger) { p_messenger = &messenger; @@ -214,14 +251,19 @@ namespace xeus return *p_messenger; } - void xinterpreter::input_request(xrequest_context context, const std::string& prompt, bool pwd) + void xinterpreter::input_request(const std::string& prompt, bool pwd) { if (m_stdin) { nl::json content; content["prompt"] = prompt; content["pwd"] = pwd; - m_stdin(std::move(context), "input_request", nl::json::object(), std::move(content)); + m_stdin( + get_request_context(), + "input_request", + nl::json::object(), + std::move(content) + ); } } @@ -249,4 +291,14 @@ namespace xeus res["transient"] = std::move(transient); return res; } + + void xinterpreter::set_request_context(xrequest_context context) + { + m_request_context = std::move(context); + } + + const xrequest_context& xinterpreter::get_request_context() const noexcept + { + return m_request_context; + } } diff --git a/src/xkernel.cpp b/src/xkernel.cpp index 9b2f24b0..42c27ca9 100644 --- a/src/xkernel.cpp +++ b/src/xkernel.cpp @@ -7,6 +7,7 @@ * The full license is in the file LICENSE, distributed with this software. * ****************************************************************************/ +#include #include #include @@ -119,6 +120,7 @@ namespace xeus void xkernel::init(server_builder sbuilder, debugger_builder dbuilder) { + std::clog << "xkernel::init" << std::endl; m_kernel_id = new_xguid(); m_session_id = new_xguid(); @@ -135,8 +137,10 @@ namespace xeus p_server = sbuilder(*p_context, m_config, m_error_handler); p_server->update_config(m_config); + std::clog << "Before instantiating debugger" << std::endl; p_debugger = dbuilder(*p_context, m_config, m_user_name, m_session_id, m_debugger_config); + std::clog << "Debugger instantiated" << std::endl; p_core = std::make_unique(m_kernel_id, m_user_name, m_session_id, @@ -146,6 +150,7 @@ namespace xeus p_history_manager.get(), p_debugger.get()); + std::cout << "Core instantiated" << std::endl; xcontrol_messenger& messenger = p_server->get_control_messenger(); if(p_debugger != nullptr) diff --git a/src/xkernel_core.cpp b/src/xkernel_core.cpp index 2e4a6ea2..a43443eb 100644 --- a/src/xkernel_core.cpp +++ b/src/xkernel_core.cpp @@ -187,6 +187,11 @@ namespace xeus return m_comm_manager; } + const nl::json& xkernel_core::parent_header() const noexcept + { + return p_interpreter->parent_header(); + } + void xkernel_core::dispatch(xmessage msg, channel c) { p_logger->log_received_message(msg, c == channel::SHELL ? xlogger::shell : xlogger::control); @@ -228,8 +233,9 @@ namespace xeus return res; } - void xkernel_core::execute_request(xmessage request, channel c) + void xkernel_core::execute_request(xmessage request, channel) { + // xeus assumes execute_request will be executed on SHELL only try { const nl::json& content = request.content(); @@ -241,7 +247,7 @@ namespace xeus bool allow_stdin = content.value("allow_stdin", true); bool stop_on_error = content.value("stop_on_error", false); - xrequest_context request_context(request.header(), c, request.identities()); + xrequest_context request_context(request.header(), request.identities()); execute_request_config config { silent, store_history, allow_stdin }; auto reply_callback = [this, request_context, config, stop_on_error, code](nl::json reply) @@ -258,7 +264,7 @@ namespace xeus request_context.header(), std::move(metadata), std::move(reply), - request_context.origin() + channel::SHELL ); if (!config.silent && config.store_history) @@ -272,7 +278,7 @@ namespace xeus } // idle - publish_status(request_context.header(), "idle", request_context.origin()); + publish_status(request_context.header(), "idle", channel::SHELL); }; p_interpreter->execute_request( @@ -348,7 +354,7 @@ namespace xeus 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(); @@ -384,7 +390,7 @@ namespace xeus } } - void xkernel_core::publish_status( nl::json parent_header, 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; diff --git a/src/xkernel_core.hpp b/src/xkernel_core.hpp index 6391f47e..20246e19 100644 --- a/src/xkernel_core.hpp +++ b/src/xkernel_core.hpp @@ -61,7 +61,7 @@ namespace xeus nl::json metadata, nl::json content, buffer_sequence buffers, - channel origin ); + channel origin); void send_stdin(const std::string& msg_type, const guid_list& id_list, nl::json parent_header, nl::json metadata, nl::json content); @@ -69,6 +69,8 @@ namespace xeus const xcomm_manager& comm_manager() const & noexcept; xcomm_manager comm_manager() const && noexcept; + const nl::json& parent_header() const noexcept; + private: using handler_fptr_type = void (xkernel_core::*)(xmessage, channel); diff --git a/src/xmock_interpreter.hpp b/src/xmock_interpreter.hpp index 375d829f..39b5ed6a 100644 --- a/src/xmock_interpreter.hpp +++ b/src/xmock_interpreter.hpp @@ -38,8 +38,7 @@ namespace xeus { } - void execute_request_impl(xrequest_context /*request_context*/, - send_reply_callback cb, + void execute_request_impl(send_reply_callback cb, int /*execution_counter*/, const std::string& /*code*/, execute_request_config /*config*/, diff --git a/src/xrequest_context.cpp b/src/xrequest_context.cpp index 390d8b15..eb940f33 100644 --- a/src/xrequest_context.cpp +++ b/src/xrequest_context.cpp @@ -2,8 +2,8 @@ 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)) + xrequest_context::xrequest_context(nl::json header, guid_list id) + : m_header(std::move(header)), m_id(std::move(id)) { } @@ -12,11 +12,6 @@ namespace xeus return m_header; } - channel xrequest_context::origin() const - { - return m_origin; - } - const xmessage::guid_list& xrequest_context::id() const { return m_id; diff --git a/test/xmock_interpreter.cpp b/test/xmock_interpreter.cpp index 204ba539..f424a025 100644 --- a/test/xmock_interpreter.cpp +++ b/test/xmock_interpreter.cpp @@ -29,8 +29,7 @@ namespace xeus using function_type = std::function; } - void xmock_interpreter::execute_request_impl(xrequest_context request_context, - send_reply_callback cb, + void xmock_interpreter::execute_request_impl(send_reply_callback cb, int execution_counter, const std::string& code, execute_request_config /*config*/, @@ -38,12 +37,12 @@ namespace xeus { if (code.compare("hello, world") == 0) { - publish_stream(request_context, "stdout", code); + publish_stream("stdout", code); } if (code.compare("error") == 0) { - publish_stream(request_context, "stderr", code); + publish_stream("stderr", code); } if (code.compare("?") == 0) @@ -66,7 +65,7 @@ namespace xeus nl::json pub_data; pub_data["text/plain"] = code; - publish_execution_result(request_context, execution_counter, std::move(pub_data), nl::json::object()); + publish_execution_result(execution_counter, std::move(pub_data), nl::json::object()); cb(xeus::create_successful_reply()); } diff --git a/test/xmock_interpreter.hpp b/test/xmock_interpreter.hpp index 46c7c2c9..1cea9078 100644 --- a/test/xmock_interpreter.hpp +++ b/test/xmock_interpreter.hpp @@ -25,8 +25,7 @@ namespace xeus void configure_impl() override; - void execute_request_impl(xrequest_context request_context, - send_reply_callback cb, + void execute_request_impl(send_reply_callback cb, int execution_counter, const std::string& code, execute_request_config config,