Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[experimental] async exec requests with xeus #382

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ set(XEUS_HEADERS
${XEUS_INCLUDE_DIR}/xeus/xhistory_manager.hpp
${XEUS_INCLUDE_DIR}/xeus/xinput.hpp
${XEUS_INCLUDE_DIR}/xeus/xinterpreter.hpp
${XEUS_INCLUDE_DIR}/xeus/xainterpreter.hpp
${XEUS_INCLUDE_DIR}/xeus/xaresponse_sender.hpp
${XEUS_INCLUDE_DIR}/xeus/xkernel.hpp
${XEUS_INCLUDE_DIR}/xeus/xkernel_configuration.hpp
${XEUS_INCLUDE_DIR}/xeus/xlogger.hpp
Expand All @@ -110,6 +112,7 @@ set(XEUS_HEADERS
)

set(XEUS_SOURCES
${XEUS_SOURCE_DIR}/xaresponse_sender.cpp
${XEUS_SOURCE_DIR}/xcomm.cpp
${XEUS_SOURCE_DIR}/xcontrol_messenger.cpp
${XEUS_SOURCE_DIR}/xdebugger.cpp
Expand All @@ -119,6 +122,7 @@ set(XEUS_SOURCES
${XEUS_SOURCE_DIR}/xin_memory_history_manager.hpp
${XEUS_SOURCE_DIR}/xin_memory_history_manager.cpp
${XEUS_SOURCE_DIR}/xinterpreter.cpp
${XEUS_SOURCE_DIR}/xainterpreter.cpp
${XEUS_SOURCE_DIR}/xkernel.cpp
${XEUS_SOURCE_DIR}/xkernel_configuration.cpp
${XEUS_SOURCE_DIR}/xkernel_core.cpp
Expand Down
169 changes: 169 additions & 0 deletions include/xeus/xainterpreter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/***************************************************************************
* 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_AINTERPRETER_HPP
#define XEUS_AINTERPRETER_HPP

#include <functional>
#include <string>
#include <vector>

#include "xcomm.hpp"
#include "xeus.hpp"
#include "xcontrol_messenger.hpp"
#include "xhistory_manager.hpp"
#include "xaresponse_sender.hpp"

namespace xeus
{
class xainterpreter;

XEUS_API bool register_ainterpreter(xainterpreter* ainterpreter);
XEUS_API xainterpreter& get_ainterpreter();

class XEUS_API xainterpreter
{
public:

xainterpreter();
virtual ~xainterpreter() = default;

xainterpreter(const xainterpreter&) = delete;
xainterpreter& operator=(const xainterpreter&) = delete;

xainterpreter(xainterpreter&&) = delete;
xainterpreter& operator=(xainterpreter&&) = delete;

void configure();

void async_execute_request(const std::string& code, bool silent,
bool store_history,nl::json user_expressions,
bool allow_stdin,
xaresponse_sender);

nl::json complete_request(const std::string& code, int cursor_pos);

nl::json inspect_request(const std::string& code, int cursor_pos, int detail_level);

nl::json is_complete_request(const std::string& code);
nl::json kernel_info_request();

void shutdown_request();

nl::json internal_request(const nl::json& message);

// publish(msg_type, metadata, content)
using publisher_type = std::function<void(const std::string&, nl::json, nl::json, buffer_sequence)>;
// publish(msg_type, parent, metadata, content) // refactor to use only one publisher
using publisher_with_parent_type = std::function<void(const std::string&, nl::json, nl::json, nl::json, buffer_sequence)>;
void register_publishers(const publisher_type& publisher,
const publisher_with_parent_type& publisher_with_parent);




void publish_stream(const std::string& name, const std::string& text, nl::json parent_header);
void display_data(nl::json data, nl::json metadata, nl::json transient, nl::json parent_header);
void update_display_data(nl::json data, nl::json metadata, nl::json transient, nl::json parent_header);
void publish_execution_input(const std::string& code, int execution_count, nl::json parent_header);
void publish_execution_result(int execution_count, nl::json data, nl::json metadata, nl::json parent_header);
void publish_execution_error(const std::string& ename,
const std::string& evalue,
const std::vector<std::string>& trace_back,
nl::json parent_header);


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<std::string>& trace_back);
void clear_output(bool wait);

// send_stdin(msg_type, metadata, content)
using stdin_sender_type = std::function<void(const std::string&, nl::json, nl::json)>;
void register_stdin_sender(const stdin_sender_type& sender);
using input_reply_handler_type = std::function<void(const std::string&)>;
void register_input_handler(const input_reply_handler_type& handler);

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;

using parent_header_type = std::function<const nl::json&()>;
void register_parent_header(const parent_header_type&);
const nl::json& parent_header() const noexcept;

void register_control_messenger(xcontrol_messenger& messenger);

void register_history_manager(const xhistory_manager& history);
const xhistory_manager& get_history_manager() const noexcept;

protected:

xcontrol_messenger& get_control_messenger();

private:

virtual void configure_impl() = 0;

virtual void async_execute_request_impl(int execution_counter,
const std::string& code,
bool silent,
bool store_history,
nl::json user_expressions,
bool allow_stdin,
xaresponse_sender) = 0;

virtual nl::json complete_request_impl(const std::string& code,
int cursor_pos) = 0;

virtual nl::json inspect_request_impl(const std::string& code,
int cursor_pos,
int detail_level) = 0;

virtual nl::json is_complete_request_impl(const std::string& code) = 0;

virtual nl::json kernel_info_request_impl() = 0;

virtual void shutdown_request_impl() = 0;

virtual nl::json internal_request_impl(const nl::json& message);

nl::json build_display_content(nl::json data, nl::json metadata, nl::json transient);

publisher_type m_publisher;
publisher_with_parent_type m_publisher_with_parent; // refactor to use only one publisher
stdin_sender_type m_stdin;
int m_execution_count;
xcomm_manager* p_comm_manager;
parent_header_type m_parent_header;
input_reply_handler_type m_input_reply_handler;
xcontrol_messenger* p_messenger;
const xhistory_manager* p_history;
};

inline xcomm_manager& xainterpreter::comm_manager() noexcept
{
return *p_comm_manager;
}

inline const xcomm_manager& xainterpreter::comm_manager() const noexcept
{
return *p_comm_manager;
}
}

#endif
56 changes: 56 additions & 0 deletions include/xeus/xaresponse_sender.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/***************************************************************************
* Copyright (c) 2024, Dr. Thorsten Beier *
* 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_ARESPONSE_SENDER_HPP
#define XEUS_ARESPONSE_SENDER_HPP


#include "xeus.hpp"
#include "nlohmann/json.hpp"
#include <iostream>


namespace nl = nlohmann;

namespace xeus
{



class xaresponse_sender
{
public:
xaresponse_sender() = default;
//inline
void operator()(nl::json msg)
{
if(m_post_send == nullptr)
{
std::cout<<" sender is not set "<<std::endl;
throw std::runtime_error("sender is not set");
}

std::cout<<" in xaresponse_sender sending reply "<<std::endl;
m_post_send(std::move(m_parent_header), std::move(msg), m_metadata);


}
nl::json parent_header() const
{
return m_parent_header;
}
nl::json m_parent_header;
nl::json m_metadata;
// void(nl::json, nl::json, nl::json) == void(parent, reply, metadata)
std::function<void(nl::json, nl::json, nl::json)> m_post_send = nullptr;
};

} // namespace xeus

#endif
2 changes: 1 addition & 1 deletion include/xeus/xeus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

// Binary version
#define XEUS_BINARY_CURRENT 9
#define XEUS_BINARY_REVISION 5
#define XEUS_BINARY_REVISION 6
#define XEUS_BINARY_AGE 0

// Kernel protocol version
Expand Down
Loading
Loading