MADARA  3.4.1
DynamicPrefixPrint.h
Go to the documentation of this file.
1 
2 
3 #ifndef _MADARA_FILTERS_DYNAMIC_PREFIX_PRINT_H_
4 #define _MADARA_FILTERS_DYNAMIC_PREFIX_PRINT_H_
5 
13 #include <sstream>
14 
15 #include "AggregateFilter.h"
17 #include "madara/utility/Utility.h"
19 
20 namespace madara
21 {
22 namespace filters
23 {
29 {
30 public:
36  DynamicPrefixPrint(const std::string& string_vector_name = ".prefixes")
37  : initialized_(false), name(string_vector_name), prefixes_()
38  {
39  }
40 
44  virtual ~DynamicPrefixPrint() = default;
45 
52  inline virtual void filter(knowledge::KnowledgeMap& records,
53  const transport::TransportContext& transport_context,
55  {
56  std::stringstream buffer;
57  buffer << "DynamicPrefixFilter Arguments:\n";
58 
59  // Operation Type
60  buffer << " Operation Type: ";
61  buffer << transport::get_operation_type(transport_context.get_operation());
62  buffer << "\n";
63 
64  // Send Bandwidth
65  buffer << " Send Bandwidth: ";
66  buffer << transport_context.get_send_bandwidth();
67  buffer << " B/s\n";
68 
69  // Receive Bandwidth
70  buffer << " Receive Bandwidth: ";
71  buffer << transport_context.get_receive_bandwidth();
72  buffer << " B/s\n";
73 
74  // Update Time
75  buffer << " Update Time: ";
76  buffer << transport_context.get_message_time();
77  buffer << "\n";
78 
79  // Current Time
80  buffer << " Current Time: ";
81  buffer << transport_context.get_current_time();
82  buffer << "\n";
83 
84  // Knowledge Domain
85  buffer << " Knowledge Domain: ";
86  buffer << transport_context.get_domain();
87  buffer << "\n";
88 
89  // Originator
90  buffer << " Originator: ";
91  buffer << transport_context.get_originator();
92  buffer << "\n";
93 
94  buffer << " Updates:\n";
95  // local copy (fast access to prefix vector)
96  std::vector<std::string> prefixes;
97 
98  if (!initialized_)
99  {
100  // set the initial vector up
101  prefixes_.set_name(name, vars);
102  initialized_ = true;
103  }
104  else
105  {
106  // resize for what the current vector is indicating
107  prefixes_.resize();
108  }
109 
110  // copy the knowledge base prefixes to a STL vector for speed
111  prefixes_.copy_to(prefixes);
112 
113  // because of the usage of erase, don't auto inc record in for loop
114  for (auto record : records)
115  {
116  // check for valid prefix
117  bool accepted_prefix = false;
118 
119  if (prefixes.size() > 0)
120  {
121  for (auto prefix : prefixes)
122  {
123  if (utility::begins_with(record.first, prefix))
124  {
125  accepted_prefix = true;
126  }
127  }
128  }
129  else
130  {
131  accepted_prefix = true;
132  }
133 
134  // if not valid, remove the record and update iterator
135  if (!accepted_prefix)
136  {
139  "DynamicPrefixPrint::filter: "
140  "not printing variable %s\n",
141  record.first.c_str())
142  } // end not valid prefix
143  else
144  {
145  // valid prefix so keep the record and proceed to next
146  buffer << " " << record.first << " = ";
147  buffer << record.second.to_string() << "\n";
148  } // end valid prefix
149  } // end iteration over all filter records
150 
151  vars.print(buffer.str(), 0);
152  } // end filter function
153 
154 private:
159 
160 public:
165 
166 protected:
171 };
172 }
173 }
174 
175 #endif // _MADARA_FILTERS_DYNAMIC_PREFIX_PRINT_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 printing variables as they arrive and excluding prefixes that are unwanted via a user-spec...
std::string name
The string vector name to bind to inside the knowledge base.
virtual ~DynamicPrefixPrint()=default
Destructor.
DynamicPrefixPrint(const std::string &string_vector_name=".prefixes")
Constructor.
knowledge::containers::StringVector prefixes_
A map of discovered peers.
bool initialized_
Tracks if the prefix vector has been initialized.
virtual void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &transport_context, knowledge::Variables &vars)
Prints records and transport context information.
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
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.
uint64_t get_send_bandwidth(void) const
Gets the send/rebroadcast bandwidth in bytes per second.
const std::string & get_domain(void) const
Returns the network domain.
int64_t get_operation(void) const
Get operation that the context is performing.
uint64_t get_current_time(void) const
Gets the current timestamp.
const std::string & get_originator(void) const
Returns the current message originator.
uint64_t get_receive_bandwidth(void) const
Gets the receive bandwidth in bytes per second.
uint64_t get_message_time(void) const
Gets the message timestamp.
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
std::string MADARA_EXPORT get_operation_type(int type)
Return the type of operation (usually from TransportContext)
Definition: Transport.h:227
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.