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"
28 SubscriptionHandler::SubscriptionHandler(unsigned int timeout_seconds):_time_out(std::chrono::seconds(timeout_seconds)){
29 _data_lock = std::make_unique<std::mutex>();
30 _cv = std::make_unique<std::condition_variable>();
33 void SubscriptionHandler::clear(void){
35 std::lock_guard<std::mutex> lock(*(_data_lock).get());
42 bool SubscriptionHandler::add_request_entry(transaction_identifier id, transaction_status status){
44 // add entry in hash table if it does not exist
45 auto search = status_table.find(id);
46 if(search != status_table.end()){
50 status_table[id] = status;
57 bool SubscriptionHandler::delete_request_entry(transaction_identifier id){
59 auto search = status_table.find(id);
61 if (!trans_table.empty()) {
62 auto search2 = trans_table.find(id);
63 if(search2 !=trans_table.end()){
64 trans_table.erase(search2);
68 if (search != status_table.end()){
69 status_table.erase(search);
70 mdclog_write(MDCLOG_INFO,"Entry for Transaction ID deleted: %d",id);
73 mdclog_write(MDCLOG_INFO,"Entry not found in SubscriptionHandler for Transaction ID: %d",id);
79 bool SubscriptionHandler::set_request_status(transaction_identifier id, transaction_status status){
81 // change status of a request only if it exists.
82 auto search = status_table.find(id);
83 if(search != status_table.end()){
84 status_table[id] = status;
93 int const SubscriptionHandler::get_request_status(transaction_identifier id){
94 auto search = status_table.find(id);
95 if (search == status_table.end()){
99 return search->second;
104 bool SubscriptionHandler::is_request_entry(transaction_identifier id){
105 auto search = status_table.find(id);
106 if (search != status_table.end())
112 // Handles subscription responses
113 void SubscriptionHandler::manage_subscription_response(int message_type, transaction_identifier id){
116 std::cout << "In Manage subscription" << std::endl;
118 // wake up all waiting users ...
119 if(is_request_entry(id)){
120 std::cout << "In Manage subscription" << std::endl;
121 set_request_status(id, request_success);
122 _cv.get()->notify_all();