Skip to content

Commit

Permalink
Merge pull request #37 from anutosh491/implement_xshell_client
Browse files Browse the repository at this point in the history
[WIP] Adding support for xshell_client class
  • Loading branch information
JohanMabille authored Mar 26, 2024
2 parents 746abb6 + 157a987 commit ed541c0
Show file tree
Hide file tree
Showing 14 changed files with 785 additions and 4 deletions.
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ message(STATUS "XEUS_ZMQ_BUILD_TESTS: ${XEUS_ZMQ_BUILD_TESTS}")
# ============

set(xeus_REQUIRED_VERSION 3.2.0)
set(nlohmann_json_REQUIRED_VERSION 3.2.0)
set(nlohmann_json_REQUIRED_VERSION 3.11.2)
set(cppzmq_REQUIRED_VERSION 4.8.1)
set(zeromq_REQUIRED_VERSION 4.3.2)

Expand Down Expand Up @@ -132,6 +132,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/xclient_zmq.hpp
)

set(XEUS_ZMQ_SOURCES
Expand Down Expand Up @@ -164,6 +165,15 @@ set(XEUS_ZMQ_SOURCES
${XEUS_ZMQ_SOURCE_DIR}/xzmq_messenger.hpp
${XEUS_ZMQ_SOURCE_DIR}/xzmq_messenger.cpp
${XEUS_ZMQ_SOURCE_DIR}/xzmq_serializer.cpp
${XEUS_ZMQ_SOURCE_DIR}/xclient_zmq.cpp
${XEUS_ZMQ_SOURCE_DIR}/xclient_messenger.hpp
${XEUS_ZMQ_SOURCE_DIR}/xclient_messenger.cpp
${XEUS_ZMQ_SOURCE_DIR}/xclient_zmq_impl.hpp
${XEUS_ZMQ_SOURCE_DIR}/xclient_zmq_impl.cpp
${XEUS_ZMQ_SOURCE_DIR}/xdealer_channel.hpp
${XEUS_ZMQ_SOURCE_DIR}/xdealer_channel.cpp
${XEUS_ZMQ_SOURCE_DIR}/xiopub_client.hpp
${XEUS_ZMQ_SOURCE_DIR}/xiopub_client.cpp
)

# Targets and link
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ See the [documentation](http://xeus.readthedocs.io/) for an exhaustive list of t
We have packaged all these dependencies on conda-forge. The simplest way to install them is to run:
```bash
mamba install cmake pkg-config zeromq cppzmq OpenSSL nlohmann_json xtl xeus -c conda-forge
mamba install cmake pkg-config zeromq cppzmq OpenSSL nlohmann_json=3.11.2 xtl xeus -c conda-forge
```

Once you have installed the dependencies, you can build and install `xeus-zmq`:
Expand Down
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
- xeus>=3.2.0,<4.0
- OpenSSL=1
- libopenssl-static=1
- nlohmann_json
- nlohmann_json=3.11.2
# Test dependencies
- doctest >= 2.4.6
- pytest
Expand Down
67 changes: 67 additions & 0 deletions include/xeus-zmq/xclient_zmq.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Martin Renou *
* Copyright (c) 2016, 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_CLIENT_ZMQ_HPP
#define XEUS_CLIENT_ZMQ_HPP

#include <optional>

#include <nlohmann/json.hpp>

#include "xeus/xeus_context.hpp"
#include "xeus/xkernel_configuration.hpp"
#include "xeus/xmessage.hpp"

#include "xeus-zmq.hpp"

namespace xeus
{
class xclient_zmq_impl;

class XEUS_ZMQ_API xclient_zmq
{
public:

using listener = std::function<void(xmessage)>;

explicit xclient_zmq(std::unique_ptr<xclient_zmq_impl> impl);
~xclient_zmq();

void send_on_shell(xmessage msg);
void send_on_control(xmessage msg);

std::optional<xmessage> check_shell_answer();
std::optional<xmessage> check_control_answer();

void register_shell_listener(const listener& l);
void register_control_listener(const listener& l);
void register_iopub_listener(const listener& l);

void notify_shell_listener(xmessage msg);
void notify_control_listener(xmessage msg);
void notify_iopub_listener(xmessage msg);

std::size_t iopub_queue_size() const;
std::optional<xmessage> pop_iopub_message();
void connect();
void stop_channels();
void start();
void wait_for_message();

private:
std::unique_ptr<xclient_zmq_impl> p_client_impl;
};

XEUS_ZMQ_API
std::unique_ptr<xclient_zmq> make_xclient_zmq(xcontext& context,
const xconfiguration& config,
nl::json::error_handler_t eh = nl::json::error_handler_t::strict);
}

#endif
42 changes: 42 additions & 0 deletions src/xclient_messenger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Martin Renou *
* Copyright (c) 2016, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include "nlohmann/json.hpp"
#include "xeus-zmq/xmiddleware.hpp"
#include "xclient_messenger.hpp"

namespace nl = nlohmann;

namespace xeus
{
xclient_messenger::xclient_messenger(zmq::context_t& context)
: m_iopub_controller(context, zmq::socket_type::req)
{
}

xclient_messenger::~xclient_messenger()
{
}

void xclient_messenger::connect()
{
m_iopub_controller.set(zmq::sockopt::linger, get_socket_linger());
m_iopub_controller.connect(get_controller_end_point("iopub"));
}

void xclient_messenger::stop_channels()
{
zmq::message_t stop_msg("stop", 4);
zmq::message_t response;

// Wait for iopub answer
m_iopub_controller.send(stop_msg, zmq::send_flags::none);
(void)m_iopub_controller.recv(response);
}
}
31 changes: 31 additions & 0 deletions src/xclient_messenger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Martin Renou *
* Copyright (c) 2016, 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_CLIENT_MESSENGER_HPP
#define XEUS_CLIENT_MESSENGER_HPP

#include <zmq.hpp>

namespace xeus
{
class xclient_messenger
{
public:
explicit xclient_messenger(zmq::context_t& context);
virtual ~xclient_messenger();

void connect();
void stop_channels();

private:
zmq::socket_t m_iopub_controller;
};
}

#endif
113 changes: 113 additions & 0 deletions src/xclient_zmq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Martin Renou *
* Copyright (c) 2016, 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/xclient_zmq.hpp"
#include "xclient_zmq_impl.hpp"

namespace xeus
{

xclient_zmq::xclient_zmq(std::unique_ptr<xclient_zmq_impl> impl)
: p_client_impl(std::move(impl))
{
}

// Has to be in the cpp because incomplete
// types are used in unique_ptr in the header
xclient_zmq::~xclient_zmq() = default;


void xclient_zmq::send_on_shell(xmessage msg)
{
p_client_impl->send_on_shell(std::move(msg));
}

void xclient_zmq::send_on_control(xmessage msg)
{
p_client_impl->send_on_control(std::move(msg));
}

std::optional<xmessage> xclient_zmq::check_shell_answer()
{
return p_client_impl->receive_on_shell(-1);
}

std::optional<xmessage> xclient_zmq::check_control_answer()
{
return p_client_impl->receive_on_control(-1);
}

void xclient_zmq::register_shell_listener(const listener& l)
{
p_client_impl->register_shell_listener(l);
}

void xclient_zmq::register_control_listener(const listener& l)
{
p_client_impl->register_control_listener(l);
}

void xclient_zmq::register_iopub_listener(const listener& l)
{
p_client_impl->register_iopub_listener(l);
}

void xclient_zmq::notify_shell_listener(xmessage msg)
{
p_client_impl->notify_shell_listener(std::move(msg));
}

void xclient_zmq::notify_control_listener(xmessage msg)
{
p_client_impl->notify_control_listener(std::move(msg));
}

void xclient_zmq::notify_iopub_listener(xmessage msg)
{
p_client_impl->notify_iopub_listener(std::move(msg));
}

std::size_t xclient_zmq::iopub_queue_size() const
{
return p_client_impl->iopub_queue_size();
}

std::optional<xmessage> xclient_zmq::pop_iopub_message()
{
return p_client_impl->pop_iopub_message();
}

void xclient_zmq::connect()
{
p_client_impl->connect();
}

void xclient_zmq::stop_channels()
{
p_client_impl->stop_channels();
}

void xclient_zmq::start()
{
p_client_impl->start();
}

void xclient_zmq::wait_for_message()
{
p_client_impl->wait_for_message();
}

std::unique_ptr<xclient_zmq> make_xclient_zmq(xcontext& context,
const xconfiguration& config,
nl::json::error_handler_t eh)
{
auto impl = std::make_unique<xclient_zmq_impl>(context.get_wrapped_context<zmq::context_t>(), config, eh);
return std::make_unique<xclient_zmq>(std::move(impl));
}
}
Loading

0 comments on commit ed541c0

Please sign in to comment.