From 6c19953bfcfdeb97ccf10369b0a316bb539ca41d Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Thu, 11 Apr 2024 21:56:51 +0200 Subject: [PATCH] Moved zmq dependent implementation of xdebugger_base to src --- CMakeLists.txt | 2 + include/xeus-zmq/xdebugger_base.hpp | 30 +++++++++++---- src/xdebugger_base.cpp | 51 ++++++++++++++++--------- src/xdebugger_middleware.cpp | 58 +++++++++++++++++++++++++++++ src/xdebugger_middleware.hpp | 46 +++++++++++++++++++++++ 5 files changed, 161 insertions(+), 26 deletions(-) create mode 100644 src/xdebugger_middleware.cpp create mode 100644 src/xdebugger_middleware.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fdc50bd..c483385 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,8 @@ set(XEUS_ZMQ_SOURCES ${XEUS_ZMQ_SOURCE_DIR}/xdap_tcp_client_impl.cpp ${XEUS_ZMQ_SOURCE_DIR}/xdap_tcp_client_impl.hpp ${XEUS_ZMQ_SOURCE_DIR}/xdebugger_base.cpp + ${XEUS_ZMQ_SOURCE_DIR}/xdebugger_middleware.cpp + ${XEUS_ZMQ_SOURCE_DIR}/xdebugger_middleware.hpp ${XEUS_ZMQ_SOURCE_DIR}/xheartbeat.cpp ${XEUS_ZMQ_SOURCE_DIR}/xheartbeat.hpp ${XEUS_ZMQ_SOURCE_DIR}/xmiddleware.cpp diff --git a/include/xeus-zmq/xdebugger_base.hpp b/include/xeus-zmq/xdebugger_base.hpp index c3bd02a..66a0d1b 100644 --- a/include/xeus-zmq/xdebugger_base.hpp +++ b/include/xeus-zmq/xdebugger_base.hpp @@ -14,12 +14,12 @@ #include #include #include +#include #include #include #include #include -#include "zmq.hpp" #include "nlohmann/json.hpp" #include "xeus/xdebugger.hpp" #include "xeus/xeus_context.hpp" @@ -46,11 +46,13 @@ namespace xeus }; + class xdebugger_middleware; + class XEUS_ZMQ_API xdebugger_base : public xdebugger { public: - virtual ~xdebugger_base() = default; + virtual ~xdebugger_base(); protected: @@ -89,10 +91,24 @@ namespace xeus const std::set& get_stopped_threads() const; + /******************* + * Middleware APIs * + *******************/ + + void bind_sockets(const std::string& header_end_point, + const std::string& request_end_point); + + void unbind_sockets(const std::string& header_end_point, + const std::string& request_end_point); + + std::string send_recv_header(const std::string& header); + std::string send_recv_request(const std::string& request); + protected: virtual nl::json variables_request_impl(const nl::json& message); + private: void handle_event(const nl::json& message); @@ -100,15 +116,13 @@ namespace xeus nl::json process_request_impl(const nl::json& header, const nl::json& message) override; - virtual bool start(zmq::socket_t& header_socket, - zmq::socket_t& request_socket) = 0; - virtual void stop(zmq::socket_t& header_socket, - zmq::socket_t& request_socket) = 0; + virtual bool start() = 0; + virtual void stop() = 0; + virtual xdebugger_info get_debugger_info() const = 0; virtual std::string get_cell_temporary_file(const std::string& code) const = 0; - zmq::socket_t m_header_socket; - zmq::socket_t m_request_socket; + std::unique_ptr p_middleware; using request_handler_map_t = std::map; request_handler_map_t m_started_handler; diff --git a/src/xdebugger_base.cpp b/src/xdebugger_base.cpp index 6624969..ffb4af5 100644 --- a/src/xdebugger_base.cpp +++ b/src/xdebugger_base.cpp @@ -13,7 +13,7 @@ #include "xeus-zmq/xdap_tcp_client.hpp" #include "xeus-zmq/xdebugger_base.hpp" -#include "xeus-zmq/xmiddleware.hpp" +#include "xdebugger_middleware.hpp" using namespace std::placeholders; @@ -34,14 +34,12 @@ namespace xeus { } + xdebugger_base::~xdebugger_base() = default; + xdebugger_base::xdebugger_base(xcontext& context) - : m_header_socket(context.get_wrapped_context(), zmq::socket_type::req) - , m_request_socket(context.get_wrapped_context(), zmq::socket_type::req) + : p_middleware(new xdebugger_middleware(context)) , m_is_started(false) { - m_header_socket.set(zmq::sockopt::linger, xeus::get_socket_linger()); - m_request_socket.set(zmq::sockopt::linger, xeus::get_socket_linger()); - register_request_handler("debugInfo", std::bind(&xdebugger_base::debug_info_request, this, _1), false); register_request_handler("dumpCell", std::bind(&xdebugger_base::dump_cell_request, this, _1), true); register_request_handler("setBreakpoints", std::bind(&xdebugger_base::set_breakpoints_request, this, _1), true); @@ -242,13 +240,7 @@ namespace xeus + std::to_string(content_length) + xdap_tcp_client::SEPARATOR + content; - zmq::message_t raw_message(buffer.c_str(), buffer.length()); - m_request_socket.send(raw_message, zmq::send_flags::none); - - zmq::message_t raw_reply; - (void)m_request_socket.recv(raw_reply); - - return nl::json::parse(std::string(raw_reply.data(), raw_reply.size())); + return nl::json::parse(send_recv_request(buffer)); } /******************* @@ -323,6 +315,31 @@ namespace xeus return reply; } + /******************* + * Middleware APIs * + *******************/ + void xdebugger_base::bind_sockets(const std::string& header_end_point, + const std::string& request_end_point) + { + p_middleware->bind_sockets(header_end_point, request_end_point); + } + + void xdebugger_base::unbind_sockets(const std::string& header_end_point, + const std::string& request_end_point) + { + p_middleware->unbind_sockets(header_end_point, request_end_point); + } + + std::string xdebugger_base::send_recv_header(const std::string& header) + { + return p_middleware->send_recv_header(header); + } + + std::string xdebugger_base::send_recv_request(const std::string& request) + { + return p_middleware->send_recv_request(request); + } + /************************** * Private implementation * **************************/ @@ -350,7 +367,7 @@ namespace xeus } else { - m_is_started = start(m_header_socket, m_request_socket); + m_is_started = start(); if (m_is_started) { std::clog << "XDEBUGGER: the debugger has started" << std::endl; @@ -377,10 +394,8 @@ namespace xeus else if (m_is_started) { std::string header_buffer = header.dump(); - zmq::message_t raw_header(header_buffer.c_str(), header_buffer.length()); - m_header_socket.send(raw_header, zmq::send_flags::none); // client responds with ACK message - (void)m_header_socket.recv(raw_header); + send_recv_header(header_buffer); auto it = m_started_handler.find(message["command"]); if (it != m_started_handler.end()) @@ -395,7 +410,7 @@ namespace xeus if (message["command"] == "disconnect") { - stop(m_header_socket, m_request_socket); + stop(); m_breakpoint_list.clear(); m_stopped_threads.clear(); m_is_started = false; diff --git a/src/xdebugger_middleware.cpp b/src/xdebugger_middleware.cpp new file mode 100644 index 0000000..f526163 --- /dev/null +++ b/src/xdebugger_middleware.cpp @@ -0,0 +1,58 @@ +/*************************************************************************** +* 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 "xeus-zmq/xmiddleware.hpp" + +#include "xdebugger_middleware.hpp" + +namespace xeus +{ + xdebugger_middleware::xdebugger_middleware(xcontext& context) + : m_header_socket(context.get_wrapped_context(), zmq::socket_type::req) + , m_request_socket(context.get_wrapped_context(), zmq::socket_type::req) + { + m_header_socket.set(zmq::sockopt::linger, xeus::get_socket_linger()); + m_request_socket.set(zmq::sockopt::linger, xeus::get_socket_linger()); + } + + void xdebugger_middleware::bind_sockets(const std::string& header_end_point, + const std::string& request_end_point) + { + m_header_socket.bind(header_end_point); + m_request_socket.bind(request_end_point); + } + + void xdebugger_middleware::unbind_sockets(const std::string& header_end_point, + const std::string& request_end_point) + { + m_header_socket.unbind(header_end_point); + m_request_socket.unbind(request_end_point); + } + + std::string xdebugger_middleware::send_recv_header(const std::string& header) + { + return send_recv(header, m_header_socket); + } + + std::string xdebugger_middleware::send_recv_request(const std::string& request) + { + return send_recv(request, m_request_socket); + } + + std::string xdebugger_middleware::send_recv(const std::string& msg, zmq::socket_t& socket) + { + zmq::message_t raw_msg(msg.c_str(), msg.length()); + socket.send(raw_msg, zmq::send_flags::none); + zmq::message_t raw_reply; + (void)socket.recv(raw_reply); + return std::string(raw_reply.data(), raw_reply.size()); + } +} + diff --git a/src/xdebugger_middleware.hpp b/src/xdebugger_middleware.hpp new file mode 100644 index 0000000..9e12961 --- /dev/null +++ b/src/xdebugger_middleware.hpp @@ -0,0 +1,46 @@ +/*************************************************************************** +* 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. * +****************************************************************************/ + +#ifndef XEUS_DEBUGGER_MIDDLEWARE_HPP +#define XEUS_DEBUGGER_MIDDLEWARE_HPP + +#include + +#include "zmq.hpp" +#include "xeus/xeus_context.hpp" + +namespace xeus +{ + class xdebugger_middleware + { + public: + + explicit xdebugger_middleware(xcontext& context); + + void bind_sockets(const std::string& header_end_point, + const std::string& request_end_point); + + void unbind_sockets(const std::string& header_end_point, + const std::string& request_end_point); + + std::string send_recv_header(const std::string& header); + std::string send_recv_request(const std::string& request); + + private: + + std::string send_recv(const std::string& msg, zmq::socket_t& socket); + + zmq::socket_t m_header_socket; + zmq::socket_t m_request_socket; + }; +} + +#endif +