7
7
* The full license is in the file LICENSE, distributed with this software. *
8
8
****************************************************************************/
9
9
10
+ #include < iostream>
11
+
10
12
#include " xheartbeat_client.hpp"
11
13
#include " xclient_zmq_impl.hpp"
12
14
#include " ../common/xmiddleware_impl.hpp"
@@ -23,6 +25,7 @@ namespace xeus
23
25
, m_max_retry(max_retry)
24
26
, m_heartbeat_timeout(timeout)
25
27
, m_heartbeat_end_point(" " )
28
+ , m_request_stop(false )
26
29
{
27
30
m_heartbeat_end_point = get_end_point (config.m_transport , config.m_ip , config.m_hb_port );
28
31
m_heartbeat.connect (m_heartbeat_end_point);
@@ -42,9 +45,35 @@ namespace xeus
42
45
43
46
bool xheartbeat_client::wait_for_answer (long timeout)
44
47
{
45
- m_heartbeat.set (zmq::sockopt::linger, static_cast <int >(timeout));
46
- zmq::message_t response;
47
- return m_heartbeat.recv (response).has_value ();
48
+ zmq::pollitem_t items[] = {
49
+ { m_heartbeat, 0 , ZMQ_POLLIN, 0 }, { m_controller, 0 , ZMQ_POLLIN, 0 }
50
+ };
51
+
52
+ zmq::poll (&items[0 ], 2 , std::chrono::milliseconds (timeout));
53
+ try
54
+ {
55
+ if (items[0 ].revents & ZMQ_POLLIN)
56
+ {
57
+ zmq::multipart_t wire_msg;
58
+ wire_msg.recv (m_heartbeat);
59
+ }
60
+
61
+ if (items[1 ].revents & ZMQ_POLLIN)
62
+ {
63
+ // stop message
64
+ zmq::multipart_t wire_msg;
65
+ wire_msg.recv (m_controller);
66
+ wire_msg.send (m_controller);
67
+ m_request_stop = true ;
68
+ }
69
+
70
+ return true ;
71
+ }
72
+ catch (std::exception & e)
73
+ {
74
+ std::cerr << e.what () << std::endl;
75
+ }
76
+ return false ;
48
77
}
49
78
50
79
void xheartbeat_client::register_kernel_status_listener (const kernel_status_listener& l)
@@ -59,10 +88,9 @@ namespace xeus
59
88
60
89
void xheartbeat_client::run ()
61
90
{
62
- bool stop = false ;
63
91
std::size_t retry_count = 0 ;
64
92
65
- while (!stop )
93
+ while (!m_request_stop )
66
94
{
67
95
send_heartbeat_message ();
68
96
if (!wait_for_answer (m_heartbeat_timeout))
@@ -74,7 +102,7 @@ namespace xeus
74
102
else
75
103
{
76
104
notify_kernel_dead (true );
77
- stop = true ;
105
+ break ;
78
106
}
79
107
}
80
108
else
0 commit comments