MADARA  3.1.8
EndpointClear.cpp
Go to the documentation of this file.
1 #include "EndpointClear.h"
4 
5 namespace utility = madara::utility;
7 
9  const std::string & prefix)
10  : initialized_ (false), prefix_ (prefix)
11 {
12 }
13 
15 {
16 }
17 
18 
19 void
21  knowledge::KnowledgeMap & records,
22  const transport::TransportContext & transport_context,
23  knowledge::Variables & vars)
24 {
25  if (!initialized_)
26  {
27  knowledge::KnowledgeUpdateSettings settings (true);
28  endpoints_.set_settings (settings);
29  endpoints_.set_name (prefix_, vars);
30  initialized_ = true;
31  }
32 
34 
37  "EndpointClear::filter:" \
38  " Processing a new update from %s with %d records\n",
39  transport_context.get_originator ().c_str (),
40  (int)records.size ());
41 
42  // if a trusted originator has been added, and this is the originator
43  if (trusted_originators_.size () != 0 &&
44  (trusted_originators_.find (transport_context.get_originator ()) !=
45  trusted_originators_.end ()))
46  {
49  "EndpointClear::filter:" \
50  " Sender is trusted. Clearing endpoints.\n");
51 
52  // then clear the endpoints
53  endpoints_.clear ();
54 
55  if (vars.get_context ()->get_logger ().get_level () >=
57  {
58  vars.print ();
59  }
60  }
61  else
62  {
63  bool found = false;
64  const std::string prefix (endpoints_.get_name ());
65 
66  if (prefix != "")
67  {
68  // iterate through all records looking for the common prefix
69  for (knowledge::KnowledgeMap::const_iterator i = records.begin ();
70  i != records.end () && !found; ++i)
71  {
72  // if we've found the prefix, then clear the endpoints
73  if (utility::begins_with (i->first, prefix))
74  {
77  "EndpointClear::filter:" \
78  " Prefix is match. Clearing endpoints.\n");
79 
80  endpoints_.clear ();
81  found = true;
82 
83  if (vars.get_context ()->get_logger ().get_level () >=
85  {
86  vars.print ();
87  }
88  }
89  // if we're past where the prefix would be, quit
90  else if (prefix[0] < i->first[0])
91  {
94  "EndpointClear::filter:" \
95  " Prefix is not a match. Not clearing endpoints.\n");
96 
97  found = true;
98  }
99  }
100  }
101  }
102 }
103 
104 void
106  const std::vector <std::string> & originators)
107 {
108  for (std::vector <std::string>::const_iterator i = originators.begin ();
109  i != originators.end (); ++i)
110  {
111  trusted_originators_[*i] = true;
112  }
113 }
114 
115 void
117  const std::string & originator)
118 {
119  trusted_originators_[originator] = true;
120 }
121 
122 void
124 {
125  trusted_originators_.clear ();
126 }
127 
128 void
130 {
131  prefix_ = prefix;
132 }
void clear_trusted_originators(void)
Clears all trusted originators who can prompt deleting.
EndpointClear(const std::string &prefix=".endpoints")
Constructor.
std::string get_name(void) const
Returns the name of the container.
void set_settings(const KnowledgeUpdateSettings &settings)
Sets the update settings for the container.
virtual ~EndpointClear()
Destructor.
std::vector< std::string > sync_keys(void)
Syncs the keys from the knowledge base.
Definition: Map.cpp:225
virtual void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &transport_context, knowledge::Variables &vars)
Calls the main logic of the filter.
void add_trusted_originator(const std::vector< std::string > &originators)
Adds trusted originators who will be able to prompt deleting.
knowledge::containers::Map endpoints_
A map of discovered peers.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
Provides context about the transport.
void set_prefix(const std::string &prefix)
Sets the prefix to use.
int get_level(void)
Gets the maximum logging detail level.
Definition: Logger.inl:68
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
Definition: Map.cpp:411
::std::map< std::string, KnowledgeRecord > KnowledgeMap
std::map< std::string, bool > trusted_originators_
originators that are trusted to prompt a clear of endpoints
static constexpr struct madara::knowledge::tags::string_t string
void clear(bool clear_knowledge=true)
Clears the map.
Definition: Map.cpp:372
bool initialized_
Tracks if the peers_ map has been initialized.
Definition: EndpointClear.h:93
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
madara::knowledge::KnowledgeRecord::Integer Integer
Settings for applying knowledge updates.
const std::string & get_originator(void) const
Returns the current message originator.
std::string prefix_
The context.
Definition: EndpointClear.h:98
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
MADARA_Export bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:7
Provides an interface for external functions into the MADARA KaRL variable settings.
void print(unsigned int level=0) const
Prints all variables and values in the context.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.