-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from anutosh491/heartbeat_client
Introducing a heartbeat client
- Loading branch information
Showing
9 changed files
with
178 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/*************************************************************************** | ||
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Martin Renou * | ||
* Copyright (c) 2016, QuantStack * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
****************************************************************************/ | ||
|
||
#include "xheartbeat_client.hpp" | ||
#include "xclient_zmq_impl.hpp" | ||
|
||
#include "xeus-zmq/xmiddleware.hpp" | ||
|
||
namespace xeus | ||
{ | ||
|
||
xheartbeat_client::xheartbeat_client(zmq::context_t& context, | ||
const xeus::xconfiguration& config, | ||
const std::size_t max_retry, | ||
const long timeout) | ||
: m_heartbeat(context, zmq::socket_type::req) | ||
, m_controller(context, zmq::socket_type::rep) | ||
, m_max_retry(max_retry) | ||
, m_heartbeat_timeout(timeout) | ||
{ | ||
m_heartbeat.connect(get_end_point(config.m_transport, config.m_ip, config.m_hb_port)); | ||
init_socket(m_controller, get_controller_end_point("heartbeat")); | ||
} | ||
|
||
xheartbeat_client::~xheartbeat_client() | ||
{ | ||
} | ||
|
||
void xheartbeat_client::send_heartbeat_message() | ||
{ | ||
zmq::message_t ping_msg("ping", 4); | ||
m_heartbeat.send(ping_msg, zmq::send_flags::none); | ||
} | ||
|
||
bool xheartbeat_client::wait_for_answer(long timeout) | ||
{ | ||
m_heartbeat.set(zmq::sockopt::linger, static_cast<int>(timeout)); | ||
zmq::message_t response; | ||
return m_heartbeat.recv(response).has_value(); | ||
} | ||
|
||
void xheartbeat_client::register_kernel_status_listener(const kernel_status_listener& l) | ||
{ | ||
m_kernel_status_listener = l; | ||
} | ||
|
||
void xheartbeat_client::notify_kernel_dead(bool status) | ||
{ | ||
m_kernel_status_listener(status); | ||
} | ||
|
||
void xheartbeat_client::run() | ||
{ | ||
bool stop = false; | ||
std::size_t retry_count = 0; | ||
|
||
while(!stop) | ||
{ | ||
send_heartbeat_message(); | ||
if(!wait_for_answer(m_heartbeat_timeout)) | ||
{ | ||
if (retry_count < m_max_retry) | ||
{ | ||
++retry_count; | ||
} else { | ||
notify_kernel_dead(true); | ||
stop = true; | ||
} | ||
} else { | ||
retry_count = 0; | ||
} | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*************************************************************************** | ||
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Martin Renou * | ||
* Copyright (c) 2016, QuantStack * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
****************************************************************************/ | ||
|
||
#ifndef XEUS_HEARTBEAT_CLIENT_HPP | ||
#define XEUS_HEARTBEAT_CLIENT_HPP | ||
|
||
#include <functional> | ||
|
||
#include "zmq.hpp" | ||
|
||
#include "xeus/xkernel_configuration.hpp" | ||
|
||
namespace xeus | ||
{ | ||
class xheartbeat_client | ||
{ | ||
public: | ||
|
||
using kernel_status_listener = std::function<void(bool)>; | ||
|
||
xheartbeat_client(zmq::context_t& context, | ||
const xeus::xconfiguration& config, | ||
const std::size_t max_retry, | ||
const long timeout); | ||
|
||
~xheartbeat_client(); | ||
|
||
void run(); | ||
|
||
void register_kernel_status_listener(const kernel_status_listener& l); | ||
void notify_kernel_dead(bool status); | ||
|
||
private: | ||
void send_heartbeat_message(); | ||
bool wait_for_answer(long timeout); | ||
|
||
zmq::socket_t m_heartbeat; | ||
zmq::socket_t m_controller; | ||
|
||
kernel_status_listener m_kernel_status_listener; | ||
const std::size_t m_max_retry; | ||
const long m_heartbeat_timeout; | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters