Skip to content

Commit

Permalink
Use connect and stop_channel functions
Browse files Browse the repository at this point in the history
  • Loading branch information
anutosh491 committed Jun 14, 2024
1 parent 9f45a0e commit 1cc0721
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 84 deletions.
8 changes: 8 additions & 0 deletions test/test_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ class debugger_client
bool test_rich_inspect_variables();
bool test_variables();
bool test_copy_to_globals();
void start();
void shutdown();

private:
Expand Down Expand Up @@ -965,8 +966,14 @@ bool debugger_client::test_copy_to_globals()
return global_var["value"] == local_var["value"] && global_var["type"] == local_var["type"];
}

void debugger_client::start()
{
m_client.start();
}

void debugger_client::shutdown()
{
m_client.stop_channels();
m_client.send_on_control("shutdown_request", make_shutdown_request());
m_client.receive_on_control();
}
Expand Down Expand Up @@ -1147,6 +1154,7 @@ TEST_SUITE("debugger")
xeus::xcontext context;
{
debugger_client deb(context, KERNEL_JSON, "debugger_init.log");
deb.start();
bool res = deb.test_init();
deb.shutdown();
std::this_thread::sleep_for(2s);
Expand Down
86 changes: 7 additions & 79 deletions test/xeus_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ xeus_logger_client::xeus_logger_client(xeus::xcontext& context,
std::ofstream out(m_file_name);
out << "STARTING CLIENT" << std::endl;

// Register listeners for shell and kernel status
register_shell_listener();
register_kernel_status_listener();
// TODO
p_client->connect();
}

xeus_logger_client::~xeus_logger_client()
Expand Down Expand Up @@ -128,12 +127,13 @@ std::size_t xeus_logger_client::iopub_queue_size() const

nl::json xeus_logger_client::pop_iopub_message()
{
std::optional<xeus::xmessage> msg_opt = p_client->pop_iopub_message();
optional<xeus::xpub_message> msg_opt = p_client->pop_iopub_message();
nl::json result = nl::json::object();

if (msg_opt.has_value())
{
xeus::xmessage msg = std::move(*msg_opt);
xeus::xpub_xmessage msg = std::move(*msg_opt);
result["topic"] = msg.topic();
result["header"] = msg.header();
result["parent_header"] = msg.parent_header();
result["metadata"] = msg.metadata();
Expand All @@ -143,86 +143,14 @@ nl::json xeus_logger_client::pop_iopub_message()
return result;
}

nl::json xeus_logger_client::wait_for_debug_event(const std::string& event)
{
bool event_found = false;
nl::json msg;
while(!event_found)
{
std::size_t s = iopub_queue_size();
if(s != 0)
{
msg = pop_iopub_message();
if(msg["topic"] == "debug_event" && msg["content"]["event"] == event)
{
event_found = true;
}
}
else
{
std::unique_lock<std::mutex> lk(m_notify_mutex);
if(iopub_queue_size())
{
continue;
}
m_notify_cond.wait(lk);
}
}
return msg;
}

void xeus_logger_client::start()
{
p_client->start();
}

void xeus_logger_client::register_shell_listener()
{
p_client->register_shell_listener([this](xeus::xmessage msg) {
handle_shell_message(std::move(msg));
});
}

void xeus_logger_client::register_kernel_status_listener()
{
p_client->register_kernel_status_listener([this](bool status) {
handle_kernel_status_message(status);
});
}

void xeus_logger_client::handle_shell_message(xeus::xmessage msg)
void xeus_logger_client::stop_channels()
{
nl::json json_msg;
json_msg["header"] = msg.header();
json_msg["parent_header"] = msg.parent_header();
json_msg["metadata"] = msg.metadata();
json_msg["content"] = msg.content();

log_message(json_msg);

std::string topic = json_msg["topic"];
std::size_t topic_size = topic.size();
if (topic.substr(topic_size - 8, topic_size) == "shutdown")
{
std::cout << "Received shutdown, exiting" << std::endl;
// TODO
}
}

void xeus_logger_client::handle_kernel_status_message(bool status)
{
nl::json msg;
if (status)
{
// TODO
// msg["topic"] = "Kernel is dead";
}
else
{
// TODO
// msg["topic"] = "Kernel is alive";
}
log_message(msg);
p_client->stop_channels();
}

void xeus_logger_client::log_message(nl::json msg)
Expand Down
11 changes: 6 additions & 5 deletions test/xeus_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ class xeus_logger_client
std::size_t iopub_queue_size() const;
nl::json pop_iopub_message();

nl::json wait_for_debug_event(const std::string& event);
// nl::json wait_for_debug_event(const std::string& event);
void start();
void stop_channels();
void log_message(nl::json msg);

private:

void register_shell_listener();
void register_kernel_status_listener();
// void register_shell_listener();
// void register_kernel_status_listener();

void handle_shell_message(xeus::xmessage msg);
void handle_kernel_status_message(bool status);
// void handle_shell_message(xeus::xmessage msg);
// void handle_kernel_status_message(bool status);

std::string m_user_name;
std::string m_file_name;
Expand Down

0 comments on commit 1cc0721

Please sign in to comment.