Skip to content

Commit

Permalink
Pass reply callback as additional argument ot execute_request
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille committed Mar 20, 2024
1 parent 94b2f28 commit 938b704
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 143 deletions.
16 changes: 8 additions & 8 deletions docs/source/example/src/custom_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ namespace nl = nlohmann;
namespace custom
{

nl::json custom_interpreter::execute_request_impl(xrequest_context request_context, // data required by other functions
int execution_counter, // Typically the cell number
const std::string& /*code*/, // Code to execute
bool /*silent*/,
bool /*store_history*/,
nl::json /*user_expressions*/,
bool /*allow_stdin*/)
void custom_interpreter::execute_request_impl(xrequest_context request_context,
send_reply_callback cb,
int execution_counter,
const std::string& code,
execute_request_config config,
nl::json user_expressions)
{
// You can use the C-API of your target language for executing the code,
// e.g. `PyRun_String` for the Python C-API
Expand All @@ -45,7 +44,8 @@ namespace custom
// publish_execution_error(error_name, error_value, error_traceback);
publish_execution_error(request_context, "TypeError", "123", {"!@#$", "*(*"});

return xeus::create_successful_reply();
// Call the callback parameter to send the reply
cb(xeus::create_successful_reply());
}

void custom_interpreter::configure_impl()
Expand Down
13 changes: 6 additions & 7 deletions docs/source/example/src/custom_interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ namespace custom

void configure_impl() override;

nl::json execute_request_impl(xrequest_context request_context,
int execution_counter,
const std::string& code,
bool silent,
bool store_history,
nl::json user_expressions,
bool allow_stdin) override;
void execute_request_impl(xrequest_context request_context,
send_reply_callback cb,
int execution_counter,
const std::string& code,
execute_request_config config,
nl::json user_expressions) override;

nl::json complete_request_impl(const std::string& code,
int cursor_pos) override;
Expand Down
2 changes: 1 addition & 1 deletion docs/source/kernel_implementation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ course the ``execute_request_impl`` which executes the code whenever the client
.. literalinclude:: ./example/src/custom_interpreter.cpp
:language: cpp
:dedent: 4
:lines: 22-48
:lines: 22-49

The result and arguments of the execution request are described in the execute_request_ documentation.

Expand Down
32 changes: 19 additions & 13 deletions include/xeus/xinterpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ namespace xeus
XEUS_API bool register_interpreter(xinterpreter* interpreter);
XEUS_API xinterpreter& get_interpreter();

struct XEUS_API execute_request_config
{
bool silent;
bool store_history;
bool allow_stdin;
};

class XEUS_API xinterpreter
{
public:
Expand All @@ -42,12 +49,12 @@ namespace xeus

void configure();

void execute_request( xexecute_request_context context,
const std::string& code,
bool silent,
bool store_history,
nl::json user_expressions,
bool allow_stdin);
using send_reply_callback = std::function<void(nl::json)>;
void execute_request(xrequest_context context,
send_reply_callback callback,
const std::string& code,
execute_request_config config,
nl::json user_expressions);

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

Expand Down Expand Up @@ -101,13 +108,12 @@ namespace xeus

virtual void configure_impl() = 0;

virtual void execute_request_impl(xexecute_request_context request_context,
int execution_counter,
const std::string& code,
bool silent,
bool store_history,
nl::json user_expressions,
bool allow_stdin) = 0;
virtual void execute_request_impl(xrequest_context request_context,
send_reply_callback cb,
int execution_counter,
const std::string& code,
execute_request_config config,
nl::json user_expressions) = 0;

virtual nl::json complete_request_impl(const std::string& code,
int cursor_pos) = 0;
Expand Down
16 changes: 0 additions & 16 deletions include/xeus/xrequest_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include "xeus/xmessage.hpp" // for xmessage::guid_list
#include "xeus/xserver.hpp" // for channel

#include <functional> // for std::function

namespace nl = nlohmann;

namespace xeus
Expand All @@ -40,20 +38,6 @@ namespace xeus
channel m_origin;
guid_list m_id;
};


class XEUS_API xexecute_request_context : public xrequest_context
{
public:
xexecute_request_context(nl::json header, channel origin, guid_list id, std::function<void(const xexecute_request_context&,nl::json)> on_send_reply);

void send_reply(nl::json reply);
void set_on_send_callback(std::function<void(const xexecute_request_context&,nl::json)> augment_reply);
std::function<void(const xexecute_request_context&, nl::json)> get_on_send_callback() const;

private:
std::function<void(const xexecute_request_context&, nl::json)> m_on_send_reply;
};
}

#endif
31 changes: 15 additions & 16 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,34 @@ namespace xeus
configure_impl();
}

void xinterpreter::execute_request(xexecute_request_context context,
const std::string& code,
bool silent,
bool store_history,
nl::json user_expressions,
bool allow_stdin)
{
if (!silent)
void xinterpreter::execute_request(xrequest_context context,
send_reply_callback callback,
const std::string& code,
execute_request_config config,
nl::json user_expressions)
{
if (!config.silent)
{
++m_execution_count;
publish_execution_input(context, code, m_execution_count);
}
// copy m_execution_count in a local variable to capture it in the lambda
auto execution_count = m_execution_count;

auto inner_callback = context.get_on_send_callback();
context.set_on_send_callback( [inner_callback, execution_count](const xexecute_request_context& context,nl::json reply)
auto callback_impl = [execution_count, callback = std::move(callback)](nl::json reply)
{
reply["execution_count"] = execution_count;
inner_callback(context, reply);
});

callback(std::move(reply));
};

execute_request_impl(
std::move(context),
m_execution_count, code, silent,
store_history, user_expressions, allow_stdin
std::move(callback_impl),
m_execution_count,
code,
std::move(config),
user_expressions
);

}

nl::json xinterpreter::complete_request(const std::string& code, int cursor_pos)
Expand Down
63 changes: 36 additions & 27 deletions src/xkernel_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,37 +239,46 @@ namespace xeus
bool allow_stdin = content.value("allow_stdin", true);
bool stop_on_error = content.value("stop_on_error", false);

nl::json metadata = get_metadata();
xexecute_request_context request_context(request.header(), c, request.identities(),
[this, silent, store_history, code, stop_on_error](const xexecute_request_context & ctx , nl::json reply)
xrequest_context request_context(request.header(), c, request.identities());
execute_request_config config { silent, store_history, allow_stdin };
int execution_count = 1;
std::string status;
auto reply_callback = [&](nl::json reply)
{
execution_count = reply.value("execution_count", 1);
status = reply.value("status", "error");
nl::json metadata = get_metadata();

send_reply(
request.identities(),
"execute_reply",
request.header(),
std::move(metadata),
std::move(reply),
c
);

if (!silent && store_history)
{
this->send_reply(ctx.id(), "execute_reply", ctx.header(), nl::json::object(), std::move(reply), ctx.origin());

int execution_count = reply.value("execution_count", 1);
std::string status = reply.value("status", "error");


if (!silent && store_history)
{
this->p_history_manager->store_inputs(0, execution_count, code);
}

if (!silent && status == "error" && stop_on_error)
{
constexpr long polling_interval = 50;
p_server->abort_queue(std::bind(&xkernel_core::abort_request, this, _1), polling_interval);
}

p_history_manager->store_inputs(0, execution_count, code);
}
if (!silent && status == "error" && stop_on_error)
{
constexpr long polling_interval = 50;
p_server->abort_queue(std::bind(&xkernel_core::abort_request, this, _1), polling_interval);
}

// idle
publish_status("idle", ctx.header(), ctx.origin());
// idle
publish_status("idle", request_context.header(), request_context.origin());
};

}
p_interpreter->execute_request(
std::move(request_context),
std::move(reply_callback),
code,
config,
std::move(user_expression)
);

p_interpreter->execute_request(std::move(request_context),
code, silent, store_history, std::move(user_expression), allow_stdin);

}
catch (std::exception& e)
{
Expand Down
15 changes: 7 additions & 8 deletions src/xmock_interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ namespace xeus
{
}

void execute_request_impl(xexecute_request_context request_context,
int /*execution_counter*/,
const std::string& /*code*/,
bool /*silent*/,
bool /*store_history*/,
nl::json /*user_expressions*/,
bool /*allow_stdin*/) override
void execute_request_impl(xrequest_context /*request_context*/,
send_reply_callback cb,
int /*execution_counter*/,
const std::string& /*code*/,
execute_request_config /*config*/,
nl::json /*user_expressions*/) override
{
request_context.send_reply(nl::json());
cb(nl::json());
}

nl::json complete_request_impl(const std::string& /*code*/, int /*cursor_pos*/) override
Expand Down
32 changes: 1 addition & 31 deletions src/xrequest_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,4 @@ namespace xeus
{
return m_id;
}


void xexecute_request_context::send_reply(nl::json reply)
{
m_on_send_reply(*this, std::move(reply));
}


xexecute_request_context::xexecute_request_context(nl::json header,
channel origin,
guid_list id,
std::function<void(const xexecute_request_context&,nl::json)> on_send_reply)
: xrequest_context(std::move(header),
origin,
std::move(id)),
m_on_send_reply(std::move(on_send_reply))
{
}

// get and set callback
void xexecute_request_context::set_on_send_callback(std::function<void(const xexecute_request_context&,nl::json)> augment_reply)
{
m_on_send_reply = std::move(augment_reply);
}

// get callback
std::function<void(const xexecute_request_context&, nl::json)> xexecute_request_context::get_on_send_callback() const
{
return m_on_send_reply;
}
}
}
17 changes: 8 additions & 9 deletions test/xmock_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ namespace xeus
using function_type = std::function<void(xeus::xcomm&&, const xeus::xmessage&)>;
}

void xmock_interpreter::execute_request_impl(xexecute_request_context request_context,
int execution_counter,
const std::string& code,
bool /* silent */,
bool /* store_history */,
nl::json /* user_expressions */,
bool /* allow_stdin */)
void xmock_interpreter::execute_request_impl(xrequest_context request_context,
send_reply_callback cb,
int execution_counter,
const std::string& code,
execute_request_config /*config*/,
nl::json /*user_expressions*/)
{
if (code.compare("hello, world") == 0)
{
Expand All @@ -62,14 +61,14 @@ namespace xeus
{"start", 0}
});

request_context.send_reply(xeus::create_successful_reply(payload));
cb(xeus::create_successful_reply(payload));
}

nl::json pub_data;
pub_data["text/plain"] = code;
publish_execution_result(request_context, execution_counter, std::move(pub_data), nl::json::object());

request_context.send_reply( xeus::create_successful_reply());
cb(xeus::create_successful_reply());
}

nl::json xmock_interpreter::complete_request_impl(const std::string& /* code */,
Expand Down
13 changes: 6 additions & 7 deletions test/xmock_interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ namespace xeus

void configure_impl() override;

void execute_request_impl(xexecute_request_context request_context,
int execution_counter,
const std::string& code,
bool silent,
bool store_history,
nl::json user_expressions,
bool allow_stdin) override;
void execute_request_impl(xrequest_context request_context,
send_reply_callback cb,
int execution_counter,
const std::string& code,
execute_request_config config,
nl::json user_expressions) override;

nl::json complete_request_impl(const std::string& code,
int cursor_pos) override;
Expand Down

0 comments on commit 938b704

Please sign in to comment.