File: xeus_client.cpp

package info (click to toggle)
xeus-python 0.17.2%2B~0.6.3-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,752 kB
  • sloc: cpp: 4,890; python: 369; makefile: 18; javascript: 14
file content (197 lines) | stat: -rw-r--r-- 5,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and     *
* Wolf Vollprecht                                                          *
* Copyright (c) 2018, QuantStack                                           *
*                                                                          *
* Distributed under the terms of the BSD 3-Clause License.                 *
*                                                                          *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <chrono>
#include <fstream>
#include <iostream>

#include "xeus_client.hpp"
#include "xeus/xguid.hpp"
#include "xeus/xmessage.hpp"
#include "xeus-zmq/xmiddleware.hpp"

using namespace std::chrono_literals;

/*************************************
 * xeus_logger_client implementation *
 *************************************/

xeus_logger_client::xeus_logger_client(xeus::xcontext& context,
                                       const std::string& user_name,
                                       const xeus::xconfiguration& config,
                                       const std::string& file_name)
    : m_user_name(user_name)
    , m_file_name(file_name)
    , m_session_id(xeus::new_xguid())
    , p_client(xeus::make_xclient_zmq(context, config))
{
    std::ofstream out(m_file_name);
    out << "STARTING CLIENT" << std::endl;

    p_client->connect();
    register_kernel_status_listener();
}

xeus_logger_client::~xeus_logger_client()
{
}

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_kernel_status_message(bool status)
{
    kernel_dead = status;
}

void xeus_logger_client::send_on_shell(const std::string& msg_type, nl::json content)
{
    nl::json header = xeus::make_header(msg_type, m_user_name, m_session_id);

    nl::json result;
    result["header"] = header;
    result["parent_header"] = nl::json::object();
    result["metadata"] = nl::json::object();
    result["content"] = content;
    log_message(result);

    xeus::xmessage msg(xeus::xmessage::guid_list(),
                       std::move(header),
                       nl::json::object(),
                       nl::json::object(),
                       std::move(content),
                       xeus::buffer_sequence());
    
    p_client->send_on_shell(std::move(msg));
}

void xeus_logger_client::send_on_control(const std::string& msg_type, nl::json content)
{
    nl::json header = xeus::make_header(msg_type, m_user_name, m_session_id);

    nl::json result;
    result["header"] = header;
    result["parent_header"] = nl::json::object();
    result["metadata"] = nl::json::object();
    result["content"] = content;
    log_message(result);

    xeus::xmessage msg(xeus::xmessage::guid_list(),
                       std::move(header),
                       nl::json::object(),
                       nl::json::object(),
                       std::move(content),
                       xeus::buffer_sequence());
    
    p_client->send_on_control(std::move(msg));
}

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

    if (msg_opt.has_value())
    {
        xeus::xmessage msg = std::move(*msg_opt);
        result["header"] = msg.header();
        result["parent_header"] = msg.parent_header();
        result["metadata"] = msg.metadata();
        result["content"] = msg.content();
    }
    log_message(result);

    return result;
}

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

    if (msg_opt.has_value())
    {
        xeus::xmessage msg = std::move(*msg_opt);
        result["header"] = msg.header();
        result["parent_header"] = msg.parent_header();
        result["metadata"] = msg.metadata();
        result["content"] = msg.content();
    }
    log_message(result);

    return result;
}

std::size_t xeus_logger_client::iopub_queue_size() const
{
    return p_client->iopub_queue_size();
}

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

    if (msg_opt.has_value())
    {
        xeus::xpub_message msg = std::move(*msg_opt);
        result["topic"] = msg.topic();
        result["header"] = msg.header();
        result["parent_header"] = msg.parent_header();
        result["metadata"] = msg.metadata();
        result["content"] = msg.content();
    }

    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::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
    }
    return msg;
}

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

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

void xeus_logger_client::log_message(nl::json msg)
{
    std::lock_guard<std::mutex> guard(m_file_mutex);
    std::ofstream out(m_file_name, std::ios_base::app);
    out << msg.dump(4) << std::endl;
}