26 #include <unordered_map> 31 #include "aossl/consul/include/consul_interface.h" 32 #include "aossl/consul/include/factory_consul.h" 33 #include "aossl/profile/include/tiered_app_profile.h" 35 #ifndef AOSSL_PROFILE_INCLUDE_NETWORK_APP_PROFILE_H_ 36 #define AOSSL_PROFILE_INCLUDE_NETWORK_APP_PROFILE_H_ 46 std::atomic<int> last_returned_index{-1};
52 rapidjson::Document& doc, std::string service_identifier, \
53 std::string metadata_key, std::string metadata_value,
bool filter_metadata) {
57 bool service_found =
false;
58 bool loop_ended_early =
false;
59 int discovery_index = 0;
62 for (
auto& itr : doc.GetObject()) {
66 if (service_found && discovery_index > last_returned_index) {
67 last_returned_index = discovery_index - 1;
68 loop_ended_early =
true;
72 std::vector<std::string> current_obj_tags;
75 rapidjson::Value::ConstMemberIterator service_itr = \
76 itr.value.FindMember(
"Service");
77 if (service_itr != itr.value.MemberEnd()) {
78 if (!(service_itr->value.IsNull())) {
79 std::string service_name(service_itr->value.GetString());
80 if ((service_name == service_identifier)) {
83 rapidjson::Value::ConstMemberIterator tag_itr = \
84 itr.value.FindMember(
"Tags");
85 bool metadata_matches =
false;
86 if (tag_itr != itr.value.MemberEnd()) {
87 if (tag_itr->value.IsArray()) {
88 for (
auto& tagv : tag_itr->value.GetArray()) {
89 if (tagv.IsString()) {
90 if (tagv.GetString() == metadata_key + std::string(
"=") + metadata_value) {
91 metadata_matches =
true;
99 if (!filter_metadata || metadata_matches) {
100 service_found =
true;
101 for (
auto& tag : current_obj_tags) {
104 rapidjson::Value::ConstMemberIterator address_itr = \
105 itr.value.FindMember(
"Address");
106 rapidjson::Value::ConstMemberIterator port_itr = \
107 itr.value.FindMember(
"Port");
108 rapidjson::Value::ConstMemberIterator id_itr = \
109 itr.value.FindMember(
"ID");
110 return_service->
set_address(address_itr->value.GetString());
111 return_service->
set_port(std::to_string(port_itr->value.GetInt()));
112 return_service->
set_id(id_itr->value.GetString());
113 return_service->
set_name(service_identifier);
120 if (!loop_ended_early) {
121 last_returned_index = -1;
123 if (!service_found) {
124 if (return_service)
delete return_service;
125 throw std::invalid_argument(
"Unable to find Service Instance");
131 TieredApplicationProfile(argc, argv) {}
134 std::string prof_name) : \
135 TieredApplicationProfile(argc, argv, app_name, prof_name) {}
138 TieredApplicationProfile(args) {}
141 std::string app_name, std::string prof_name) : \
142 TieredApplicationProfile(args, app_name, prof_name) {}
145 TieredApplicationProfile(app_name, prof_name) {}
149 inline ServiceInterface* get_service_by_metadata(std::string service_identifier, \
150 std::string metadata_key, std::string metadata_value) {
153 ApplicationProfile::get_consul()->services();
156 rapidjson::Document doc;
157 doc.Parse<rapidjson::kParseStopWhenDoneFlag>(services_buf->
val.c_str());
158 if (doc.HasParseError()) {
159 delete return_service;
161 throw std::invalid_argument(GetParseError_En(doc.GetParseError()));
163 find_service_in_doc(return_service, doc, service_identifier, metadata_key, metadata_value,
true);
166 return return_service;
172 ApplicationProfile::get_consul()->services();
175 rapidjson::Document doc;
176 doc.Parse<rapidjson::kParseStopWhenDoneFlag>(services_buf->
val.c_str());
177 if (doc.HasParseError()) {
178 delete return_service;
180 throw std::invalid_argument(GetParseError_En(doc.GetParseError()));
182 find_service_in_doc(return_service, doc, service_identifier, std::string(
""), std::string(
""),
false);
185 return return_service;
191 #endif // AOSSL_PROFILE_INCLUDE_NETWORK_APP_PROFILE_H_ A Structure for holding a single value.
Definition: buffers.h:42
std::string val
Value stored.
Definition: buffers.h:44
Biased Application Session which implements tiered configuration.
Definition: tiered_app_profile.h:63
A Service class which can be registered with Consul for each app instance.
Definition: consul_interface.h:57
virtual void add_tag(std::string new_tag)=0
Add a tag.
The Consul Service Component Factory.
Definition: factory_consul.h:44
virtual void set_id(std::string new_id)=0
Set the Service ID.
virtual void set_name(std::string new_name)=0
Set the Service Name.
virtual void set_address(std::string new_address)=0
Set the Service Address.
Extends the TieredApplicationProfile, adding service discovery logic.
Definition: network_app_profile.h:44
virtual void set_port(std::string new_port)=0
Set the Service Port.