Skip to content

Commit

Permalink
Moved zmq dependent implementation of xdebugger_base to src
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille committed Apr 11, 2024
1 parent 4588873 commit 6c19953
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions include/xeus-zmq/xdebugger_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <vector>

#include "zmq.hpp"
#include "nlohmann/json.hpp"
#include "xeus/xdebugger.hpp"
#include "xeus/xeus_context.hpp"
Expand All @@ -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:

Expand Down Expand Up @@ -89,26 +91,38 @@ namespace xeus

const std::set<int>& 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);

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<xdebugger_middleware> p_middleware;

using request_handler_map_t = std::map<std::string, request_handler_t>;
request_handler_map_t m_started_handler;
Expand Down
51 changes: 33 additions & 18 deletions src/xdebugger_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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::context_t>(), zmq::socket_type::req)
, m_request_socket(context.get_wrapped_context<zmq::context_t>(), 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);
Expand Down Expand Up @@ -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<const char>(), raw_reply.size()));
return nl::json::parse(send_recv_request(buffer));
}

/*******************
Expand Down Expand Up @@ -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 *
**************************/
Expand Down Expand Up @@ -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;
Expand All @@ -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())
Expand All @@ -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;
Expand Down
58 changes: 58 additions & 0 deletions src/xdebugger_middleware.cpp
Original file line number Diff line number Diff line change
@@ -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::context_t>(), zmq::socket_type::req)
, m_request_socket(context.get_wrapped_context<zmq::context_t>(), 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<const char>(), raw_reply.size());
}
}

46 changes: 46 additions & 0 deletions src/xdebugger_middleware.hpp
Original file line number Diff line number Diff line change
@@ -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 <string>

#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

0 comments on commit 6c19953

Please sign in to comment.