MADARA  3.4.1
EndpointClear.cpp
Go to the documentation of this file.
1 #include "EndpointClear.h"
4 
5 namespace utility = madara::utility;
7 
9  : initialized_(false), prefix_(prefix)
10 {
11 }
12 
14 
16  const transport::TransportContext& transport_context,
18 {
19  if (!initialized_)
20  {
22  endpoints_.set_settings(settings);
23  endpoints_.set_name(prefix_, vars);
24  initialized_ = true;
25  }
26 
27  endpoints_.sync_keys();
28 
30  "EndpointClear::filter:"
31  " Processing a new update from %s with %d records\n",
32  transport_context.get_originator().c_str(), (int)records.size());
33 
34  // if a trusted originator has been added, and this is the originator
35  if (trusted_originators_.size() != 0 &&
36  (trusted_originators_.find(transport_context.get_originator()) !=
37  trusted_originators_.end()))
38  {
40  "EndpointClear::filter:"
41  " Sender is trusted. Clearing endpoints.\n");
42 
43  // then clear the endpoints
44  endpoints_.clear();
45 
47  {
48  vars.print();
49  }
50  }
51  else
52  {
53  bool found = false;
54  const std::string prefix(endpoints_.get_name());
55 
56  if (prefix != "")
57  {
58  // iterate through all records looking for the common prefix
59  for (knowledge::KnowledgeMap::const_iterator i = records.begin();
60  i != records.end() && !found; ++i)
61  {
62  // if we've found the prefix, then clear the endpoints
63  if (utility::begins_with(i->first, prefix))
64  {
66  "EndpointClear::filter:"
67  " Prefix is match. Clearing endpoints.\n");
68 
69  endpoints_.clear();
70  found = true;
71 
72  if (vars.get_context()->get_logger().get_level() >=
74  {
75  vars.print();
76  }
77  }
78  // if we're past where the prefix would be, quit
79  else if (prefix[0] < i->first[0])
80  {
82  "EndpointClear::filter:"
83  " Prefix is not a match. Not clearing endpoints.\n");
84 
85  found = true;
86  }
87  }
88  }
89  }
90 }
91 
93  const std::vector<std::string>& originators)
94 {
95  for (std::vector<std::string>::const_iterator i = originators.begin();
96  i != originators.end(); ++i)
97  {
98  trusted_originators_[*i] = true;
99  }
100 }
101 
103  const std::string& originator)
104 {
105  trusted_originators_[originator] = true;
106 }
107 
109 {
110  trusted_originators_.clear();
111 }
112 
114 {
115  prefix_ = prefix;
116 }
madara::knowledge::KnowledgeRecord::Integer Integer
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
virtual ~EndpointClear()
Destructor.
EndpointClear(const std::string &prefix=".endpoints")
Constructor.
void clear_trusted_originators(void)
Clears all trusted originators who can prompt deleting.
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.
void set_prefix(const std::string &prefix)
Sets the prefix to use.
Settings for applying knowledge updates.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
void print(unsigned int level=0) const
Prints all variables and values in the context.
Definition: Variables.cpp:516
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
Definition: Variables.cpp:17
int get_level(void)
Gets the maximum logging detail level.
Definition: Logger.inl:99
Provides context about the transport.
const std::string & get_originator(void) const
Returns the current message originator.
constexpr string_t string
::std::map< std::string, KnowledgeRecord > KnowledgeMap
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:15
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