Commit 6531cc64 authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen

add unsubscribe function

parent e86d2677
...@@ -81,17 +81,17 @@ void nrf_app::handle_register_nf_instance( ...@@ -81,17 +81,17 @@ void nrf_app::handle_register_nf_instance(
std::shared_ptr<nrf_profile> sn = { }; std::shared_ptr<nrf_profile> sn = { };
switch (type) { switch (type) {
case NF_TYPE_AMF: { case NF_TYPE_AMF: {
sn = std::make_shared<amf_profile>(m_event_sub); sn = std::make_shared < amf_profile > (m_event_sub);
} }
break; break;
case NF_TYPE_SMF: { case NF_TYPE_SMF: {
sn = std::make_shared<smf_profile>(m_event_sub); sn = std::make_shared < smf_profile > (m_event_sub);
} }
break; break;
default: { default: {
sn = std::make_shared<nrf_profile>(m_event_sub); sn = std::make_shared < nrf_profile > (m_event_sub);
} }
} }
...@@ -107,10 +107,10 @@ void nrf_app::handle_register_nf_instance( ...@@ -107,10 +107,10 @@ void nrf_app::handle_register_nf_instance(
add_nf_profile(nf_instance_id, sn); add_nf_profile(nf_instance_id, sn);
//get current time //get current time
uint64_t ms = std::chrono::duration_cast < std::chrono::milliseconds // uint64_t ms = std::chrono::duration_cast < std::chrono::milliseconds
> (std::chrono::system_clock::now().time_since_epoch()).count(); // > (std::chrono::system_clock::now().time_since_epoch()).count();
subscribe_task_tick(ms); // subscribe_task_tick(ms);
Logger::nrf_app().debug("Added/Updated NF Profile to the DB"); Logger::nrf_app().debug("Added/Updated NF Profile to the DB");
//display the info //display the info
...@@ -141,9 +141,9 @@ void nrf_app::handle_update_nf_instance( ...@@ -141,9 +141,9 @@ void nrf_app::handle_update_nf_instance(
bool op_success = true; bool op_success = true;
//get current time //get current time
uint64_t ms = std::chrono::duration_cast < std::chrono::milliseconds //uint64_t ms = std::chrono::duration_cast < std::chrono::milliseconds
> (std::chrono::system_clock::now().time_since_epoch()).count(); // > (std::chrono::system_clock::now().time_since_epoch()).count();
subscribe_task_tick2(ms); //subscribe_task_tick2(ms);
if (sn.get() != nullptr) { if (sn.get() != nullptr) {
for (auto p : patchItem) { for (auto p : patchItem) {
...@@ -275,6 +275,7 @@ bool nrf_app::update_nf_profile(const std::string &profile_id, ...@@ -275,6 +275,7 @@ bool nrf_app::update_nf_profile(const std::string &profile_id,
Logger::nrf_app().info("Update a NF profile to the list (profile ID %s)", Logger::nrf_app().info("Update a NF profile to the list (profile ID %s)",
profile_id.c_str()); profile_id.c_str());
instance_id2nrf_profile.at(profile_id) = p; instance_id2nrf_profile.at(profile_id) = p;
p.get()->unsubscribe_task_tick();
return true; return true;
} else { } else {
Logger::nrf_app().info("NF profile (ID %d) not found", profile_id.c_str()); Logger::nrf_app().info("NF profile (ID %d) not found", profile_id.c_str());
...@@ -381,14 +382,10 @@ void nrf_app::subscribe_task_tick(uint64_t ms) { ...@@ -381,14 +382,10 @@ void nrf_app::subscribe_task_tick(uint64_t ms) {
const uint64_t interval = its.it_value.tv_sec * 1000 const uint64_t interval = its.it_value.tv_sec * 1000
+ its.it_value.tv_nsec / 1000000; // convert sec, nsec to msec + its.it_value.tv_nsec / 1000000; // convert sec, nsec to msec
//uint64_t interval =10;
// m_event_sub.subscribe_task_tick(
// boost::bind<void>(&nrf_app::handle_heartbeart_timeout, _1), interval, 0 /* start at time 0 */);
Logger::nrf_app().debug("subscribe_task_tick1: %d", ms); Logger::nrf_app().debug("subscribe_task_tick1: %d", ms);
m_event_sub.subscribe_task_tick( m_event_sub.subscribe_task_tick(
boost::bind(&nrf_app::handle_heartbeart_timeout, this, _1), interval, boost::bind(&nrf_app::handle_heartbeart_timeout, this, _1), interval,
ms%20000/* start at time 0 */); ms % 20000);
} }
...@@ -402,10 +399,6 @@ void nrf_app::subscribe_task_tick2(uint64_t ms) { ...@@ -402,10 +399,6 @@ void nrf_app::subscribe_task_tick2(uint64_t ms) {
const uint64_t interval = its.it_value.tv_sec * 1000 const uint64_t interval = its.it_value.tv_sec * 1000
+ its.it_value.tv_nsec / 1000000; // convert sec, nsec to msec + its.it_value.tv_nsec / 1000000; // convert sec, nsec to msec
//uint64_t interval =10;
// m_event_sub.subscribe_task_tick(
// boost::bind<void>(&nrf_app::handle_heartbeart_timeout, _1), interval, 0 /* start at time 0 */);
Logger::nrf_app().debug("subscribe_task_tick2 %d", ms); Logger::nrf_app().debug("subscribe_task_tick2 %d", ms);
m_event_sub.subscribe_task_tick( m_event_sub.subscribe_task_tick(
boost::bind(&nrf_app::handle_heartbeart_timeout2, this, _1), interval, boost::bind(&nrf_app::handle_heartbeart_timeout2, this, _1), interval,
......
...@@ -49,4 +49,20 @@ bs2::connection nrf_event::subscribe_task_tick( ...@@ -49,4 +49,20 @@ bs2::connection nrf_event::subscribe_task_tick(
return task_tick.connect(f); return task_tick.connect(f);
} }
//------------------------------------------------------------------------------
bs2::connection nrf_event::subscribe_task_tick_extended(
const task_sig_t::extended_slot_type& sig, uint64_t period, uint64_t start) {
/* Wrap the actual callback in a lambda. The latter checks whether the
* current time is after start time, and ensures that the callback is only
* called every X ms with X being the period time. This way, it is possible
* to register to be notified every X ms instead of every ms, which provides
* greater freedom to implementations. */
auto f = [period,start,sig] (const bs2::connection& c, uint64_t t)
{
if (t >= start && (t - start) % period == 0) sig(c, t);
};
return task_tick.connect_extended(f);
}
...@@ -66,6 +66,10 @@ class nrf_event { ...@@ -66,6 +66,10 @@ class nrf_event {
bs2::connection subscribe_task_tick(const task_sig_t::slot_type &sig, bs2::connection subscribe_task_tick(const task_sig_t::slot_type &sig,
uint64_t period, uint64_t start = 0); uint64_t period, uint64_t start = 0);
bs2::connection subscribe_task_tick_extended(const task_sig_t::extended_slot_type &sig,
uint64_t period, uint64_t start = 0);
private: private:
task_sig_t task_tick; task_sig_t task_tick;
......
...@@ -453,26 +453,27 @@ void nrf_profile::to_json(nlohmann::json &data) const { ...@@ -453,26 +453,27 @@ void nrf_profile::to_json(nlohmann::json &data) const {
void nrf_profile::subscribe_task_tick(uint64_t ms) { void nrf_profile::subscribe_task_tick(uint64_t ms) {
struct itimerspec its; struct itimerspec its;
its.it_value.tv_sec = 10; //seconds its.it_value.tv_sec = HEART_BEAT_TIMER; //seconds
its.it_value.tv_nsec = 0; //100 * 1000 * 1000; //100ms its.it_value.tv_nsec = 0; //100 * 1000 * 1000; //100ms
const uint64_t interval = its.it_value.tv_sec * 1000 const uint64_t interval = its.it_value.tv_sec * 1000
+ its.it_value.tv_nsec / 1000000; // convert sec, nsec to msec + its.it_value.tv_nsec / 1000000; // convert sec, nsec to msec
//uint64_t interval =10;
// m_event_sub.subscribe_task_tick(
// boost::bind<void>(&nrf_app::handle_heartbeart_timeout, _1), interval, 0 /* start at time 0 */);
Logger::nrf_app().debug("subscribe_task_tick2 %d", ms); Logger::nrf_app().debug("subscribe_task_tick2 %d", ms);
m_event_sub.subscribe_task_tick( task_connection = m_event_sub.subscribe_task_tick(
boost::bind(&nrf_profile::handle_heartbeart_timeout, this, _1), interval, boost::bind(&nrf_profile::handle_heartbeart_timeout, this, _1), interval,
ms % 10000 /* start at time 0 */); ms % (HEART_BEAT_TIMER*1000) /* start at time 0 */);
}
//------------------------------------------------------------------------------
void nrf_profile::unsubscribe_task_tick() {
task_connection.disconnect();
Logger::nrf_app().debug("Un subscribe_task_tick" );
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void nrf_profile::handle_heartbeart_timeout(uint64_t ms) { void nrf_profile::handle_heartbeart_timeout(uint64_t ms) {
Logger::nrf_app().info("NRF PROFILE handle_heartbeart_timeout1 %d, PROFILE ID %s", ms, nf_instance_id.c_str()); Logger::nrf_app().info("Handle heartbeart timeout profile %s, time %d", nf_instance_id.c_str(), ms);
set_nf_status("SUSPENDED");
} }
......
...@@ -94,7 +94,7 @@ class nrf_profile : public std::enable_shared_from_this<nrf_profile> { ...@@ -94,7 +94,7 @@ class nrf_profile : public std::enable_shared_from_this<nrf_profile> {
nrf_profile(nrf_profile &b) = delete; nrf_profile(nrf_profile &b) = delete;
virtual ~nrf_profile() { virtual ~nrf_profile() {
task_connection.disconnect();
} }
/* /*
...@@ -321,8 +321,10 @@ class nrf_profile : public std::enable_shared_from_this<nrf_profile> { ...@@ -321,8 +321,10 @@ class nrf_profile : public std::enable_shared_from_this<nrf_profile> {
virtual void subscribe_task_tick (uint64_t ms); virtual void subscribe_task_tick (uint64_t ms);
virtual void handle_heartbeart_timeout(uint64_t ms); virtual void handle_heartbeart_timeout(uint64_t ms);
virtual void unsubscribe_task_tick();
protected: protected:
nrf_event &m_event_sub; nrf_event &m_event_sub;
bs2::connection task_connection;
//From NFProfile (Section 6.1.6.2.2@3GPP TS 29.510 V16.0.0 (2019-06)) //From NFProfile (Section 6.1.6.2.2@3GPP TS 29.510 V16.0.0 (2019-06))
std::string nf_instance_id; std::string nf_instance_id;
std::string nf_instance_name; std::string nf_instance_name;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment