Skip to content

Commit

Permalink
Parent header (#384)
Browse files Browse the repository at this point in the history
Used xrequest_context for execute_request
  • Loading branch information
DerThorsten authored Mar 12, 2024
1 parent 6518c75 commit 364f0ee
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 168 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ set(XEUS_HEADERS
${XEUS_INCLUDE_DIR}/xeus/xserver.hpp
${XEUS_INCLUDE_DIR}/xeus/xstring_utils.hpp
${XEUS_INCLUDE_DIR}/xeus/xsystem.hpp
${XEUS_INCLUDE_DIR}/xeus/xrequest_context.hpp
)

set(XEUS_SOURCES
Expand All @@ -132,6 +133,7 @@ set(XEUS_SOURCES
${XEUS_SOURCE_DIR}/xmock_interpreter.hpp
${XEUS_SOURCE_DIR}/xserver.cpp
${XEUS_SOURCE_DIR}/xsystem.cpp
${XEUS_SOURCE_DIR}/xrequest_context.cpp
)

# Targets and link
Expand Down
27 changes: 16 additions & 11 deletions include/xeus/xcomm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace xeus

void operator()(xcomm&& comm, xmessage request) const;

void publish_message(const std::string&, nl::json, nl::json, buffer_sequence) const;
void publish_message(const std::string&, nl::json parent_header, nl::json, nl::json, buffer_sequence) const;

void register_comm(xguid, xcomm*) const;
void unregister_comm(xguid) const;
Expand Down Expand Up @@ -88,9 +88,9 @@ namespace xeus
xcomm& operator=(xcomm&&);
xcomm& operator=(const xcomm&);

void open(nl::json metadata, nl::json data, buffer_sequence buffers);
void close(nl::json metadata, nl::json data, buffer_sequence buffers);
void send(nl::json metadata, nl::json data, buffer_sequence buffers) const;
void open(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers);
void close(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers);
void send(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) const;

xtarget& target() noexcept;
const xtarget& target() const noexcept;
Expand Down Expand Up @@ -119,10 +119,12 @@ namespace xeus
* }
*/
void send_comm_message(const std::string& msg_type,
nl::json parent_header,
nl::json metadata,
nl::json data,
buffer_sequence) const;
void send_comm_message(const std::string& msg_type,
nl::json parent_header,
nl::json metadata,
nl::json data,
buffer_sequence,
Expand Down Expand Up @@ -262,17 +264,19 @@ namespace xeus
}

inline void xcomm::send_comm_message(const std::string& msg_type,
nl::json parent_header,
nl::json metadata,
nl::json data,
buffer_sequence buffers) const
{
nl::json content;
content["comm_id"] = m_id;
content["data"] = std::move(data);
target().publish_message(msg_type, std::move(metadata), std::move(content), std::move(buffers));
target().publish_message(msg_type, std::move(parent_header), std::move(metadata), std::move(content), std::move(buffers));
}

inline void xcomm::send_comm_message(const std::string& msg_type,
nl::json parent_header,
nl::json metadata,
nl::json data,
buffer_sequence buffers,
Expand All @@ -283,7 +287,7 @@ namespace xeus
content["target_name"] = target_name;
content["data"] = std::move(data);
target().publish_message(
msg_type, std::move(metadata), std::move(content), std::move(buffers));
msg_type, std::move(parent_header), std::move(metadata), std::move(content), std::move(buffers));
}

inline xcomm::xcomm(xcomm&& comm)
Expand Down Expand Up @@ -352,23 +356,24 @@ namespace xeus
}
}

inline void xcomm::open(nl::json metadata, nl::json data, buffer_sequence buffers)
inline void xcomm::open(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers)
{
send_comm_message("comm_open",
std::move(parent_header),
std::move(metadata),
std::move(data),
std::move(buffers),
p_target->name());
}

inline void xcomm::close(nl::json metadata, nl::json data, buffer_sequence buffers)
inline void xcomm::close(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers)
{
send_comm_message("comm_close", std::move(metadata), std::move(data), std::move(buffers));
send_comm_message("comm_close", std::move(parent_header), std::move(metadata), std::move(data), std::move(buffers));
}

inline void xcomm::send(nl::json metadata, nl::json data, buffer_sequence buffers) const
inline void xcomm::send(nl::json parent_header, nl::json metadata, nl::json data, buffer_sequence buffers) const
{
send_comm_message("comm_msg", std::move(metadata), std::move(data), std::move(buffers));
send_comm_message("comm_msg", std::move(parent_header), std::move(metadata), std::move(data), std::move(buffers));
}

inline xguid xcomm::id() const noexcept
Expand Down
33 changes: 16 additions & 17 deletions include/xeus/xinterpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "xeus.hpp"
#include "xcontrol_messenger.hpp"
#include "xhistory_manager.hpp"
#include "xrequest_context.hpp"

namespace xeus
{
Expand All @@ -41,7 +42,8 @@ namespace xeus

void configure();

nl::json execute_request(const std::string& code,
nl::json execute_request(xrequest_context context,
const std::string& code,
bool silent,
bool store_history,
nl::json user_expressions,
Expand All @@ -59,36 +61,33 @@ namespace xeus
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)>;
using publisher_type = std::function<void(xrequest_context, const std::string&, nl::json, nl::json, buffer_sequence)>;
void register_publisher(const publisher_type& publisher);

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,
void publish_stream(xrequest_context, const std::string& name, const std::string& text);
void display_data(xrequest_context, nl::json data, nl::json metadata, nl::json transient);
void update_display_data(xrequest_context, nl::json data, nl::json metadata, nl::json transient);
void publish_execution_input(xrequest_context, const std::string& code, int execution_count);
void publish_execution_result(xrequest_context, int execution_count, nl::json data, nl::json metadata);
void publish_execution_error(xrequest_context,
const std::string& ename,
const std::string& evalue,
const std::vector<std::string>& trace_back);
void clear_output(bool wait);
void clear_output(xrequest_context, bool wait);

// send_stdin(msg_type, metadata, content)
using stdin_sender_type = std::function<void(const std::string&, nl::json, nl::json)>;
using stdin_sender_type = std::function<void(xrequest_context, 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_request(xrequest_context context, 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);
Expand All @@ -102,7 +101,8 @@ namespace xeus

virtual void configure_impl() = 0;

virtual nl::json execute_request_impl(int execution_counter,
virtual nl::json execute_request_impl(xrequest_context request_context,
int execution_counter,
const std::string& code,
bool silent,
bool store_history,
Expand Down Expand Up @@ -130,7 +130,6 @@ namespace xeus
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;
Expand Down
41 changes: 41 additions & 0 deletions include/xeus/xrequest_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***************************************************************************
* Copyright (c) 2024, Dr. Thorsten Beier
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XEUS_REQUEST_CONTEXT_HPP
#define XEUS_REQUEST_CONTEXT_HPP

#include <string>
#include <vector>

#include "xeus/xeus.hpp"
#include "nlohmann/json.hpp"
#include "xeus/xmessage.hpp" // for xmessage::guid_list
#include "xeus/xserver.hpp" // for channel

namespace nl = nlohmann;

namespace xeus
{

class XEUS_API xrequest_context{
public:
using guid_list = xmessage::guid_list;
xrequest_context(nl::json header, channel origin, guid_list id);
const nl::json& header() const;
channel origin() const;
const guid_list& id() const;

private:

nl::json m_header;
channel m_origin;
guid_list m_id;

};
}

#endif
8 changes: 6 additions & 2 deletions src/xcomm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
namespace xeus
{
void xtarget::publish_message(const std::string& msg_type,
nl::json parent_header,
nl::json metadata,
nl::json content,
buffer_sequence buffers) const
{
if (p_manager->p_kernel != nullptr)
{
p_manager->p_kernel->publish_message(
msg_type, std::move(metadata), std::move(content), std::move(buffers), channel::SHELL);
msg_type, std::move(parent_header),
std::move(metadata), std::move(content),
std::move(buffers), channel::SHELL
);
}
}

Expand Down Expand Up @@ -73,7 +77,7 @@ namespace xeus
if (p_kernel != nullptr)
{
p_kernel->publish_message(
"comm_close", nl::json::object(), std::move(content), buffer_sequence(), channel::SHELL
"comm_close", request.header(), nl::json::object(), std::move(content), buffer_sequence(), channel::SHELL
);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/xinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

namespace xeus
{
std::string blocking_input_request(const std::string& prompt, bool password)
std::string blocking_input_request(
xrequest_context context,
const std::string& prompt,
bool password
)
{
auto& interpreter = xeus::get_interpreter();

Expand All @@ -23,7 +27,7 @@ namespace xeus
interpreter.register_input_handler([&value](const std::string& v) { value = v; });

// Send the input request
interpreter.input_request(prompt, password);
interpreter.input_request(std::move(context), prompt, password);

// Remove input handler
interpreter.register_input_handler(nullptr);
Expand Down
Loading

0 comments on commit 364f0ee

Please sign in to comment.