Commit 74858891 authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen

add tick event

parent 670ee4f6
......@@ -126,5 +126,4 @@ typedef struct smf_info_s {
std::vector<snssai_smf_info_item_t> snssai_smf_info_list;
} smf_info_t;
#endif
......@@ -30,5 +30,9 @@ add_library (NRF STATIC
nrf_app.cpp
nrf_config.cpp
nrf_profile.cpp
task_manager.cpp
nrf_event.cpp
)
......@@ -33,15 +33,19 @@
#include "logger.hpp"
#include "api_conversions.hpp"
#include "3gpp_29.500.h"
#include <chrono>
using namespace oai::nrf::app;
using namespace oai::nrf::model;
using namespace std::chrono;
extern nrf_app *nrf_app_inst;
extern nrf_config nrf_cfg;
//------------------------------------------------------------------------------
nrf_app::nrf_app(const std::string &config_file) {
nrf_app::nrf_app(const std::string &config_file, nrf_event &ev)
:
m_event_sub(ev) {
Logger::nrf_app().startup("Starting...");
Logger::nrf_app().startup("Started");
}
......@@ -101,6 +105,13 @@ void nrf_app::handle_register_nf_instance(
http_code = HTTP_STATUS_CODE_201_CREATED;
//add to the DB
add_nf_profile(nf_instance_id, sn);
//get current time
uint64_t ms = std::chrono::duration_cast < std::chrono::milliseconds
> (std::chrono::system_clock::now().time_since_epoch()).count();
subscribe_task_tick(ms);
Logger::nrf_app().debug("Added/Updated NF Profile to the DB");
//display the info
sn.get()->display();
......@@ -129,6 +140,11 @@ void nrf_app::handle_update_nf_instance(
sn = find_nf_profile(nf_instance_id);
bool op_success = true;
//get current time
uint64_t ms = std::chrono::duration_cast < std::chrono::milliseconds
> (std::chrono::system_clock::now().time_since_epoch()).count();
subscribe_task_tick2(ms);
if (sn.get() != nullptr) {
for (auto p : patchItem) {
patch_op_type_t op = api_conv::string_to_patch_operation(p.getOp());
......@@ -348,3 +364,55 @@ bool nrf_app::remove_nf_profile(std::string &profile_id) {
}
}
//------------------------------------------------------------------------------
void nrf_app::subscribe_task_tick(uint64_t ms) {
struct itimerspec its;
its.it_value.tv_sec = 20; //seconds
its.it_value.tv_nsec = 0; //100 * 1000 * 1000; //100ms
const uint64_t interval = its.it_value.tv_sec * 1000
+ 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);
m_event_sub.subscribe_task_tick(
boost::bind(&nrf_app::handle_heartbeart_timeout, this, _1), interval,
ms % 20000/* start at time 0 */);
}
//------------------------------------------------------------------------------
void nrf_app::subscribe_task_tick2(uint64_t ms) {
struct itimerspec its;
its.it_value.tv_sec = 20; //seconds
its.it_value.tv_nsec = 0; //100 * 1000 * 1000; //100ms
const uint64_t interval = its.it_value.tv_sec * 1000
+ 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);
m_event_sub.subscribe_task_tick(
boost::bind(&nrf_app::handle_heartbeart_timeout2, this, _1), interval,
ms % 20000 /* start at time 0 */);
}
//------------------------------------------------------------------------------
void nrf_app::handle_heartbeart_timeout(uint64_t ms) {
Logger::nrf_app().info("handle_heartbeart_timeout1 %d", ms);
}
//------------------------------------------------------------------------------
void nrf_app::handle_heartbeart_timeout2(uint64_t ms) {
Logger::nrf_app().info("handle_heartbeart_timeout2 %d", ms);
}
......@@ -33,9 +33,11 @@
#include <string>
#include "NFProfile.h"
#include "nrf_profile.hpp"
#include "nrf_event.hpp"
#include "PatchItem.h"
#include "ProblemDetails.h"
namespace oai {
namespace nrf {
namespace app {
......@@ -45,7 +47,7 @@ class nrf_config;
class nrf_app {
public:
explicit nrf_app(const std::string &config_file);
explicit nrf_app(const std::string &config_file, nrf_event& ev);
nrf_app(nrf_app const&) = delete;
void operator=(nrf_app const&) = delete;
......@@ -155,9 +157,14 @@ class nrf_app {
*/
bool remove_nf_profile(std::string &profile_id);
void subscribe_task_tick (uint64_t ms);
void handle_heartbeart_timeout(uint64_t ms);
void subscribe_task_tick2 (uint64_t ms);
void handle_heartbeart_timeout2(uint64_t ms);
private:
std::map<std::string, std::shared_ptr<nrf_profile>> instance_id2nrf_profile;
mutable std::shared_mutex m_instance_id2nrf_profile;
nrf_event& m_event_sub;
};
}
}
......
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file nrf_event.cpp
\brief
\author
\company Eurecom
\date 2020
\email:
*/
#include "nrf_event.hpp"
#include "nrf_app.hpp"
#include "nrf_event_sig.hpp"
using namespace oai::nrf::app;
//------------------------------------------------------------------------------
bs2::connection nrf_event::subscribe_task_tick(
const task_sig_t::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] (uint64_t t)
{
if (t >= start && (t - start) % period == 0) sig(t);
};
return task_tick.connect(f);
}
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file nrf_event.hpp
\brief
\author
\company Eurecom
\date 2020
\email:
*/
#ifndef FILE_NRF_EVENT_HPP_SEEN
#define FILE_NRF_EVENT_HPP_SEEN
#include <boost/signals2.hpp>
namespace bs2 = boost::signals2;
#include "nrf.h"
#include "nrf_event_sig.hpp"
#include "task_manager.hpp"
namespace oai {
namespace nrf {
namespace app {
class task_manager;
class nrf_event {
public:
nrf_event() {
}
;
nrf_event(nrf_event const&) = delete;
void operator=(nrf_event const&) = delete;
static nrf_event& get_instance() {
static nrf_event instance;
return instance;
}
// class register/handle event
friend class nrf_app;
friend class task_manager;
bs2::connection subscribe_task_tick(const task_sig_t::slot_type &sig,
uint64_t period, uint64_t start = 0);
private:
task_sig_t task_tick;
};
}
}
}
#endif /* FILE_NRF_EVENT_HPP_SEEN */
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
*file except in compliance with the License. You may obtain a copy of the
*License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file nrf_event_sig.hpp
\brief
\author Tien-Thinh NGUYEN
\company Eurecom
\date 2020
\email: tien-thinh.nguyen@eurecom.fr
*/
#ifndef FILE_NRF_EVENT_SIG_HPP_SEEN
#define FILE_NRF_EVENT_SIG_HPP_SEEN
#include <boost/signals2.hpp>
namespace bs2 = boost::signals2;
namespace oai {
namespace nrf {
namespace app {
typedef bs2::signal_type<void(uint64_t),
bs2::keywords::mutex_type<bs2::dummy_mutex>>::type task_sig_t;
}
}
}
#endif /* FILE_NRF_EVENT_SIG_HPP_SEEN */
......@@ -40,8 +40,6 @@
#include <nlohmann/json.hpp>
#include "nrf.h"
#include "nrf.h"
namespace oai {
namespace nrf {
namespace app {
......
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file task_manager.cpp
\brief
\author
\company Eurecom
\date 2020
\email: tien-thinh.nguyen@eurecom.fr
*/
#include "task_manager.hpp"
#include <thread>
#include <unistd.h>
#include <iostream>
#include "logger.hpp"
using namespace oai::nrf::app;
//------------------------------------------------------------------------------
task_manager::task_manager(nrf_event &ev)
:
event_sub_(ev) {
struct itimerspec its;
sfd = timerfd_create(CLOCK_MONOTONIC, 0);
/* Start the timer */
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 1000 * 1000;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
if (timerfd_settime(sfd, TFD_TIMER_ABSTIME, &its, NULL) == -1) {
Logger::nrf_app().error("Failed to set timer for task manager");
}
}
//------------------------------------------------------------------------------
void task_manager::run() {
manage_tasks();
}
//------------------------------------------------------------------------------
void task_manager::manage_tasks() {
uint64_t t = 0;
while (1) {
event_sub_.task_tick(t);
wait_for_cycle();
}
}
//------------------------------------------------------------------------------
void task_manager::wait_for_cycle() {
uint64_t exp;
ssize_t res;
if (sfd > 0) {
res = read(sfd, &exp, sizeof(exp));
if ((res < 0) || (res != sizeof(exp))) {
Logger::nrf_app().error("Failed in task manager timer wait");
}
}
}
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file task_manager.hpp
\brief
\author
\company Eurecom
\date 2020
\email: tien-thinh.nguyen@eurecom.fr
*/
#ifndef TASK_MANAGER_H_
#define TASK_MANAGER_H_
#include "nrf_event.hpp"
#include <linux/types.h>
#include <sys/timerfd.h>
using namespace oai::nrf::app;
namespace oai {
namespace nrf {
namespace app {
class nrf_event;
class task_manager {
public:
task_manager(nrf_event& ev);
void manage_tasks();
void run();
private:
void wait_for_cycle();
nrf_event& event_sub_;
int sfd;
};
}
}
}
#endif
......@@ -18,6 +18,7 @@
#include "nrf-api-server.h"
#include "options.hpp"
#include "pid_file.hpp"
#include "logger.hpp"
#include "pistache/endpoint.h"
#include "pistache/http.h"
......@@ -30,8 +31,6 @@
#include <stdlib.h> // srand
#include <unistd.h> // get_pid(), pause()
#include "logger.hpp"
using namespace oai::nrf::app;
using namespace util;
using namespace std;
......@@ -87,8 +86,15 @@ if ( !Options::parse( argc, argv ) )
nrf_cfg.load(Options::getlibconfigConfig());
nrf_cfg.display();
//Event subsystem
nrf_event ev;
// NRF application layer
nrf_app_inst = new nrf_app(Options::getlibconfigConfig());
nrf_app_inst = new nrf_app(Options::getlibconfigConfig(), ev);
//Task Manager
task_manager tm(ev);
std::thread task_manager_thread(&task_manager::run, &tm);
// PID file
// Currently hard-coded value. TODO: add as config option.
......
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