2 ==================================================================================
3 Copyright (c) 2019-2020 AT&T Intellectual Property.
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================================
21 * Author: Ashwin Shridharan, Shraboni Jana
23 #include "subs_mgmt.hpp"
27 SubscriptionHandler::SubscriptionHandler(unsigned int timeout_seconds):_time_out(std::chrono::seconds(timeout_seconds)){
28 _data_lock = std::make_unique<std::mutex>();
29 _cv = std::make_unique<std::condition_variable>();
32 void SubscriptionHandler::clear(void){
34 std::lock_guard<std::mutex> lock(*(_data_lock).get());
41 bool SubscriptionHandler::add_request_entry(transaction_identifier id, transaction_status status){
43 // add entry in hash table if it does not exist
44 auto search = status_table.find(id);
45 if(search != status_table.end()){
49 status_table[id] = status;
56 bool SubscriptionHandler::delete_request_entry(transaction_identifier id){
58 auto search = status_table.find(id);
60 if (!trans_table.empty()) {
61 auto search2 = trans_table.find(id);
62 if(search2 !=trans_table.end()){
63 trans_table.erase(search2);
67 if (search != status_table.end()){
68 status_table.erase(search);
69 mdclog_write(MDCLOG_INFO,"Entry for Transaction ID deleted: %d",id);
72 mdclog_write(MDCLOG_INFO,"Entry not found in SubscriptionHandler for Transaction ID: %d",id);
78 bool SubscriptionHandler::set_request_status(transaction_identifier id, transaction_status status){
80 // change status of a request only if it exists.
81 for(auto &it:status_table){
82 if(strcmp(it.first.c_str(), id.c_str())==0) {
92 int SubscriptionHandler::get_request_status(transaction_identifier id){
94 for(auto it:status_table){
95 if(strcmp(it.first.c_str(), id.c_str())==0) {
106 bool SubscriptionHandler::is_request_entry(transaction_identifier id){
107 for(auto it:status_table){
108 if(strcmp(it.first.c_str(), id.c_str())==0) {
118 // Handles subscription responses
119 void SubscriptionHandler::manage_subscription_response(int message_type, transaction_identifier id)
121 // Make This Thread sleep for 1 Second
122 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
124 std::unique_lock<std::mutex> _local_lock(*(_data_lock.get()));
125 mdclog_write(MDCLOG_INFO,"Subscription Handler: Status for meid %s WAS: %d",id.c_str(),this->get_request_status(id));
127 //from the message type we can know if its a success/failure etc.
128 if(message_type==RIC_SUB_RESP)
129 this->set_request_status(id, request_success);
131 if(message_type==RIC_SUB_DEL_RESP)
132 this->set_request_status(id, request_success);
134 if(message_type==RIC_SUB_FAILURE)
135 this->set_request_status(id,request_failed);
137 if(message_type==RIC_SUB_DEL_FAILURE)
138 this->set_request_status(id,request_failed);
140 mdclog_write(MDCLOG_INFO,"Subscription Handler: Status for meid %s IS: %d",id.c_str(),this->get_request_status(id));
143 //this->print_subscription_status();
145 //_cv.get()->notify_all();