MADARA  3.4.1
DynamicPrefixFilter.h
Go to the documentation of this file.
1 
2 
3 #ifndef _MADARA_FILTERS_DYNAMIC_PREFIX_FILTER_H_
4 #define _MADARA_FILTERS_DYNAMIC_PREFIX_FILTER_H_
5 
13 #include "AggregateFilter.h"
15 #include "madara/utility/Utility.h"
16 
17 namespace madara
18 {
19 namespace filters
20 {
25 {
26 public:
33  const std::string& string_vector_name = "prefixes.allowed")
34  : initialized_(false), name(string_vector_name), prefixes_()
35  {
36  }
37 
41  virtual ~DynamicPrefixFilter() = default;
42 
48  inline virtual void filter(knowledge::KnowledgeMap& records,
50  {
51  // local copy (fast access to prefix vector)
52  std::vector<std::string> prefixes;
53 
54  if (!initialized_)
55  {
56  // set the initial vector up
57  prefixes_.set_name(name, vars);
58  initialized_ = true;
59  }
60  else
61  {
62  // resize for what the current vector is indicating
63  prefixes_.resize();
64  }
65 
66  // copy the knowledge base prefixes to a STL vector for speed
67  prefixes_.copy_to(prefixes);
68 
69  // by default, the vector is empty and all prefixes are accepted
70  if (prefixes.size() > 0)
71  {
72  // because of the usage of erase, don't auto inc record in for loop
73  for (auto record = records.begin(); record != records.end();)
74  {
75  // check for valid prefix
76  bool accepted_prefix = false;
77  for (auto prefix : prefixes)
78  {
79  if (utility::begins_with(record->first, prefix))
80  {
81  accepted_prefix = true;
82  }
83  }
84 
85  // if not valid, remove the record and update iterator
86  if (!accepted_prefix)
87  {
90  "DynamicPrefixFilter::filter: "
91  "removing variable %s\n",
92  record->first.c_str())
93 
94  record = records.erase(record);
95  } // end not valid prefix
96  else
97  {
98  // valid prefix so keep the record and proceed to next
99  ++record;
100  } // end valid prefix
101  } // end iteration over records
102  } // end if there are prefixes to enforce
103  } // end filter method
104 
105 private:
110 
111 public:
116 
117 protected:
122 };
123 }
124 }
125 
126 #endif // _MADARA_FILTERS_DYNAMIC_PREFIX_FILTER_H_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
Abstract base class for implementing aggregate record filters via a functor interface.
Filter for removing variables with any unwanted prefixes.
knowledge::containers::StringVector prefixes_
A map of discovered peers.
std::string name
The string vector name to bind to inside the knowledge base.
virtual ~DynamicPrefixFilter()=default
Destructor.
virtual void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &, knowledge::Variables &vars)
Removes records not in a list of allowed prefixes.
bool initialized_
Tracks if the prefix vector has been initialized.
DynamicPrefixFilter(const std::string &string_vector_name="prefixes.allowed")
Constructor.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
This class stores a vector of strings inside of KaRL.
Definition: StringVector.h:32
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
Provides context about the transport.
constexpr string_t string
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
::std::map< std::string, KnowledgeRecord > KnowledgeMap
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:638
Copyright(c) 2020 Galois.