Skip to content

Commit

Permalink
Add hook base
Browse files Browse the repository at this point in the history
Fix linking issue
  • Loading branch information
IsabelParedes committed Mar 13, 2024
1 parent d8f27f6 commit d1c5f07
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 16 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ set(XEUS_ZMQ_HEADERS
${XEUS_ZMQ_INCLUDE_DIR}/xeus-zmq/xthread.hpp
${XEUS_ZMQ_INCLUDE_DIR}/xeus-zmq/xzmq_context.hpp
${XEUS_ZMQ_INCLUDE_DIR}/xeus-zmq/xzmq_serializer.hpp
${XEUS_ZMQ_INCLUDE_DIR}/xeus-zmq/hook_base.hpp
)

set(XEUS_ZMQ_SOURCES
Expand Down
28 changes: 28 additions & 0 deletions include/xeus-zmq/hook_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef HOOK_BASE_HPP
#define HOOK_BASE_HPP

#ifndef UVW_AS_LIB
#define UVW_AS_LIB
#include <uvw.hpp>
#endif

namespace xeus
{
class hook_base
{
public:

virtual ~hook_base() = default;

virtual void pre_hook() = 0;
virtual void post_hook() = 0;

virtual void run(std::shared_ptr<uvw::loop> loop)
{
loop->run();
};

};
}

#endif
4 changes: 3 additions & 1 deletion include/xeus-zmq/xserver_zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "xeus/xserver.hpp"

#include "xeus-zmq.hpp"
#include "hook_base.hpp"

namespace xeus
{
Expand Down Expand Up @@ -76,7 +77,8 @@ namespace xeus
xcontext& context,
const xconfiguration& config,
nl::json::error_handler_t eh = nl::json::error_handler_t::strict,
std::shared_ptr<uvw::loop> loop_ptr = nullptr);
std::shared_ptr<uvw::loop> loop_ptr = nullptr,
std::unique_ptr<hook_base> hook = nullptr);

}

Expand Down
5 changes: 3 additions & 2 deletions src/xserver_shell_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ namespace xeus
xserver_shell_main::xserver_shell_main(zmq::context_t& context,
const xconfiguration& config,
nl::json::error_handler_t eh,
std::shared_ptr<uvw::loop> loop_ptr)
: xserver_zmq_split(context, config, eh, loop_ptr)
std::shared_ptr<uvw::loop> loop_ptr,
std::unique_ptr<hook_base> hook)
: xserver_zmq_split(context, config, eh, loop_ptr, std::move(hook))
{
}

Expand Down
4 changes: 3 additions & 1 deletion src/xserver_shell_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "xeus-zmq/xeus-zmq.hpp"
#include "xserver_zmq_split.hpp"
#include "xeus-zmq/hook_base.hpp"

namespace xeus
{
Expand All @@ -35,7 +36,8 @@ namespace xeus
xserver_shell_main(zmq::context_t& context,
const xconfiguration& config,
nl::json::error_handler_t he,
std::shared_ptr<uvw::loop> loop_ptr);
std::shared_ptr<uvw::loop> loop_ptr,
std::unique_ptr<hook_base> hook);

virtual ~xserver_shell_main();

Expand Down
5 changes: 3 additions & 2 deletions src/xserver_zmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ namespace xeus
xcontext& context,
const xconfiguration& config,
nl::json::error_handler_t eh,
std::shared_ptr<uvw::loop> loop_ptr)
std::shared_ptr<uvw::loop> loop_ptr,
std::unique_ptr<hook_base> hook)
{
auto impl = std::make_unique<xserver_shell_main>(
context.get_wrapped_context<zmq::context_t>(), config, eh, loop_ptr);
context.get_wrapped_context<zmq::context_t>(), config, eh, loop_ptr, std::move(hook));
return std::make_unique<xserver_zmq>(std::move(impl));
}
}
5 changes: 3 additions & 2 deletions src/xserver_zmq_split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ namespace xeus
xserver_zmq_split::xserver_zmq_split(zmq::context_t& context,
const xconfiguration& config,
nl::json::error_handler_t eh,
std::shared_ptr<uvw::loop> loop_ptr)
std::shared_ptr<uvw::loop> loop_ptr,
std::unique_ptr<hook_base> hook)
: p_auth(make_xauthentication(config.m_signature_scheme, config.m_key))
, p_controller(new xcontrol(context, config.m_transport, config.m_ip ,config.m_control_port, this))
, p_heartbeat(new xheartbeat(context, config.m_transport, config.m_ip, config.m_hb_port))
Expand All @@ -64,7 +65,7 @@ namespace xeus
config.m_transport, config.m_ip, config.m_iopub_port))
, p_shell(new xshell_uv(context, config.m_transport, config.m_ip,
config.m_shell_port, config.m_stdin_port, this,
loop_ptr))
loop_ptr, std::move(hook)))
, m_control_thread()
, m_hb_thread()
, m_iopub_thread()
Expand Down
4 changes: 3 additions & 1 deletion src/xserver_zmq_split.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "xeus-zmq/xeus-zmq.hpp"
#include "xeus-zmq/xthread.hpp"
#include "xeus-zmq/hook_base.hpp"

#include "xserver_zmq_impl.hpp"

Expand Down Expand Up @@ -51,7 +52,8 @@ namespace xeus
xserver_zmq_split(zmq::context_t& context,
const xconfiguration& config,
nl::json::error_handler_t eh,
std::shared_ptr<uvw::loop> loop_ptr);
std::shared_ptr<uvw::loop> loop_ptr,
std::unique_ptr<hook_base> hook);

~xserver_zmq_split() override;

Expand Down
32 changes: 29 additions & 3 deletions src/xshell_uv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ namespace xeus
const std::string& shell_port,
const std::string& stdin_port,
xserver_zmq_split* server,
std::shared_ptr<uvw::loop> loop_ptr)
std::shared_ptr<uvw::loop> loop_ptr,
std::unique_ptr<hook_base> hook)
: xshell_base(context, transport, ip, shell_port, stdin_port, server)
, p_loop{loop_ptr}
, p_hook{std::move(hook)}
{
create_polls();
}
Expand All @@ -53,24 +55,42 @@ namespace xeus
p_shell_poll->on<uvw::poll_event>(
[this](uvw::poll_event&, uvw::poll_handle&)
{
if (this->p_hook)
{
this->p_hook->pre_hook();
}
zmq::multipart_t wire_msg;
if (wire_msg.recv(m_shell, ZMQ_DONTWAIT)) // non-blocking
{
xmessage msg = p_server->deserialize(wire_msg);
p_server->notify_shell_listener(std::move(msg));
}
if (this->p_hook)
{
this->p_hook->post_hook();
}
}
);

p_controller_poll->on<uvw::poll_event>(
[this](uvw::poll_event&, uvw::poll_handle&)
{
if (this->p_hook)
{
this->p_hook->pre_hook();
}

zmq::multipart_t wire_msg;
if (wire_msg.recv(m_controller, ZMQ_DONTWAIT))
{
zmq::multipart_t wire_reply = p_server->notify_internal_listener(wire_msg);
wire_reply.send(m_controller);
}

if (this->p_hook)
{
this->p_hook->post_hook();
}
}
);

Expand All @@ -92,8 +112,14 @@ namespace xeus
p_shell_poll->start(uvw::poll_handle::poll_event_flags::READABLE);
p_controller_poll->start(uvw::poll_handle::poll_event_flags::READABLE);

// NOTE: to be removed once gil (python) issue is solved
p_loop->run();
if (p_hook)
{
std::cout << "Running PPP hook\n";
p_hook->run(p_loop);
} else {
std::cout << "Running normal loop\n";
p_loop->run();
}
}

} // namespace xeus
12 changes: 8 additions & 4 deletions src/xshell_uv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
#endif

#include "xshell_base.hpp"
#include "xeus-zmq/hook_base.hpp"

namespace xeus
{


class xshell_uv : public xshell_base
{
public:
Expand All @@ -32,7 +34,8 @@ namespace xeus
const std::string& shell_port,
const std::string& stdin_port,
xserver_zmq_split* server,
std::shared_ptr<uvw::loop> loop_ptr);
std::shared_ptr<uvw::loop> loop_ptr,
std::unique_ptr<hook_base> hook);

~xshell_uv();

Expand All @@ -42,9 +45,10 @@ namespace xeus

void create_polls();

std::shared_ptr<uvw::loop> p_loop;
std::shared_ptr<uvw::poll_handle> p_shell_poll;
std::shared_ptr<uvw::poll_handle> p_controller_poll;
std::shared_ptr<uvw::loop> p_loop{ nullptr };
std::shared_ptr<uvw::poll_handle> p_shell_poll{ nullptr };
std::shared_ptr<uvw::poll_handle> p_controller_poll{ nullptr };
std::unique_ptr<hook_base> p_hook{ nullptr };
};

} // namespace xeus
Expand Down

0 comments on commit d1c5f07

Please sign in to comment.