Commit 93f5a0ee authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen

Code cleanup

parent 4242ad39
......@@ -62,11 +62,11 @@ nrf_client::nrf_client(nrf_event& ev) : m_event_sub(ev) {
curl_global_init(CURL_GLOBAL_DEFAULT);
curl_multi = curl_multi_init();
handles = {};
subscribe_task_curl();
headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
// subscribe_task_curl();
}
//------------------------------------------------------------------------------
......@@ -262,203 +262,6 @@ void nrf_client::notify_subscribed_event(
0); // TODO: current time as parameter if curl is performed per event
}
//------------------------------------------------------------------------------
void nrf_client::notify_subscribed_event_multi(
const std::shared_ptr<nrf_profile>& profile, const uint8_t& event_type,
const std::vector<std::string>& uris) {
Logger::nrf_app().debug(
"Send notification for the subscribed event to the subscriptions");
int still_running = 0, numfds = 0, res = 0, msgs_left = 0;
CURLMsg* curl_msg = nullptr;
CURL* curl = nullptr;
CURLcode return_code = {};
int http_status_code = 0;
int index = 0;
char* curl_url = nullptr;
std::unique_ptr<std::string> httpData(new std::string());
// Fill the json part
nlohmann::json json_data = {};
json_data["event"] = notification_event_type_e2str[event_type];
std::vector<struct in_addr> instance_addrs = {};
profile.get()->get_nf_ipv4_addresses(instance_addrs);
// TODO: use the first IPv4 addr for now
std::string instance_uri =
std::string(inet_ntoa(*((struct in_addr*) &(instance_addrs[0]))));
Logger::nrf_app().debug("NF instance URI: %s", instance_uri.c_str());
json_data["nfInstanceUri"] = instance_uri;
// NF profile
if ((event_type == NOTIFICATION_TYPE_NF_REGISTERED) or
(event_type == NOTIFICATION_TYPE_NF_PROFILE_CHANGED)) {
nlohmann::json json_profile = {};
switch (profile.get()->get_nf_type()) {
case NF_TYPE_AMF: {
std::static_pointer_cast<amf_profile>(profile).get()->to_json(
json_profile);
} break;
case NF_TYPE_SMF: {
std::static_pointer_cast<smf_profile>(profile).get()->to_json(
json_profile);
} break;
case NF_TYPE_UPF: {
std::static_pointer_cast<upf_profile>(profile).get()->to_json(
json_profile);
} break;
default: {
profile.get()->to_json(json_profile);
}
}
json_data["nfProfile"] = json_profile;
}
std::string body = json_data.dump();
// create and add an easy handle to a multi curl request
for (auto uri : uris) {
curl = curl_easy_init();
if (curl) {
Logger::nrf_app().debug(
"Send notification to NF with URI: %s", uri.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, uri.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, NF_CURL_TIMEOUT_MS);
// Hook up data handling function.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, body.length());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
}
curl_multi_add_handle(curl_multi, curl);
index++;
handles.push_back(curl);
}
curl_multi_perform(curl_multi, &still_running);
// block until activity is detected on at least one of the handles or
// MAX_WAIT_MSECS has passed.
do {
res = curl_multi_wait(curl_multi, NULL, 0, 1000, &numfds);
if (res != CURLM_OK) {
Logger::nrf_app().debug("curl_multi_wait() returned %d!", res);
}
curl_multi_perform(curl_multi, &still_running);
} while (still_running);
// process multiple curl
// read the messages
while ((curl_msg = curl_multi_info_read(curl_multi, &msgs_left))) {
Logger::nrf_app().debug("Process message for multiple curl");
if (curl_msg->msg == CURLMSG_DONE) {
curl = curl_msg->easy_handle;
return_code = curl_msg->data.result;
res = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &curl_url);
if (return_code != CURLE_OK) {
Logger::nrf_app().debug("CURL error code %d!", curl_msg->data.result);
continue;
}
// Get HTTP status code
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status_code);
Logger::nrf_app().debug("HTTP status code %d!", http_status_code);
// TODO: remove handle from the multi session and end this handle now, or
// later
curl_multi_remove_handle(curl_multi, curl);
curl_easy_cleanup(curl);
std::vector<CURL*>::iterator it;
it = find(handles.begin(), handles.end(), curl);
if (it != handles.end()) {
handles.erase(it);
index--;
Logger::nrf_app().debug("Erase curl handle");
}
} else {
Logger::nrf_app().debug(
"Error after curl_multi_info_read(), CURLMsg %s", curl_msg->msg);
}
}
// Remove handle, free memory
for (int i = 0; i < index; i++) {
curl_multi_remove_handle(curl_multi, handles[i]);
curl_easy_cleanup(handles[i]);
}
curl_multi_cleanup(curl_multi);
curl_global_cleanup();
curl_slist_free_all(headers);
}
//------------------------------------------------------------------------------
void nrf_client::notify_subscribed_event(
const std::shared_ptr<nrf_profile>& profile, const std::string& uri) {
Logger::nrf_app().debug(
"Send notification to the subscribed NF (URI %s)", uri.c_str());
// Fill the json part
nlohmann::json json_data = {};
json_data["event"] = "NF_REGISTERED";
std::vector<struct in_addr> instance_addrs = {};
profile.get()->get_nf_ipv4_addresses(instance_addrs);
// TODO: use the first IPv4 addr for now
std::string instance_uri =
std::string(inet_ntoa(*((struct in_addr*) &(instance_addrs[0]))));
Logger::nrf_app().debug("NF instance URI: %s", instance_uri.c_str());
json_data["nfInstanceUri"] = instance_uri;
std::string body = json_data.dump();
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl = curl = curl_easy_init();
struct curl_slist* headers = nullptr;
if (curl) {
CURLcode res = {};
// headers = curl_slist_append(headers, "charsets: utf-8");
headers = curl_slist_append(headers, "content-type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, uri.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, NF_CURL_TIMEOUT_MS);
// Response information.
long httpCode = {0};
std::unique_ptr<std::string> httpData(new std::string());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, body.length());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
Logger::nrf_app().debug("Response from NF, Http Code: %d", httpCode);
if (httpCode == HTTP_STATUS_CODE_204_NO_CONTENT) {
} else {
// get cause from the response
json response_data = {};
try {
response_data = json::parse(*httpData.get());
} catch (json::exception& e) {
Logger::nrf_app().warn("Could not get the cause from the response");
}
Logger::nrf_app().debug(
"Response from NF, Http Code: %d, problem details %s", httpCode,
response_data.dump().c_str());
}
curl_easy_cleanup(curl);
}
curl_slist_free_all(headers);
curl_global_cleanup();
}
//------------------------------------------------------------------------------
void nrf_client::subscribe_task_curl() {
struct itimerspec its;
......
......@@ -55,25 +55,6 @@ class nrf_client {
nrf_client(nrf_client const&) = delete;
void operator=(nrf_client const&) = delete;
/*
* Send Notification for the associated event to the subscriber
* @param [const std::shared_ptr<nrf_profile> &] profile: NF profile
* @param [const std::string &] uri: URI of the subscribed NF
* @return void
*/
void notify_subscribed_event(
const std::shared_ptr<nrf_profile>& profile, const std::string& uri);
/*
* Send Notification for the associated event to the subscriber
* @param [const std::shared_ptr<nrf_profile> &] profile: NF profile
* @param [const std::string &] uri: URI of the subscribed NF
* @return void
*/
void notify_subscribed_event_multi(
const std::shared_ptr<nrf_profile>& profile, const uint8_t& event_type,
const std::vector<std::string>& uris);
/*
* Send Notification for the associated event to the subscribers
* @param [const std::shared_ptr<nrf_profile> &] profile: NF profile
......
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