MADARA  3.2.3
EndpointDiscovery.cpp
Go to the documentation of this file.
1 #include "EndpointDiscovery.h"
4 
5 namespace utility = madara::utility;
7 
9  const std::string & prefix,
11  : initialized_ (false), prefix_ (prefix),
12  heart_beat_ (heart_beat * 1000000000), last_clear_ (0)
13 {
14 }
15 
17 {
18 }
19 
20 
21 void
23  knowledge::KnowledgeMap & records,
24  const transport::TransportContext & transport_context,
25  knowledge::Variables & vars)
26 {
27  if (!initialized_)
28  {
29  endpoints_.set_name (prefix_, vars);
30  initialized_ = true;
31  }
32 
35  "EndpointDiscovery::filter:" \
36  " Processing a new update with %d records\n",
37  (int)records.size ());
38 
39  std::string endpoint (transport_context.get_endpoint ());
40  Integer cur_time = (Integer)transport_context.get_current_time ();
41 
42  endpoints_.set (endpoint, cur_time);
43 
44  if (heart_beat_ > 0 && last_clear_ != cur_time)
45  {
46  std::vector <std::string> keys;
48 
49  endpoints_.keys (keys);
50 
51  for (size_t i = 0; i < keys.size (); ++i)
52  {
53  // if the current endpoint is old, erase it
54  if (cur_time - endpoints_[keys[i]].to_integer () > heart_beat_)
55  {
58  "EndpointDiscovery::filter:" \
59  " Erasing endpoint %s\n",
60  keys[i].c_str ());
61 
62  endpoints_.erase (keys[i]);
63  }
64  }
65 
66  last_clear_ = cur_time;
67  }
68 }
knowledge::containers::Map endpoints_
A map of discovered peers.
knowledge::KnowledgeRecord::Integer heart_beat_
The time to keep record of a peer.
void keys(std::vector< std::string > &curkeys) const
Returns the keys within the map.
Definition: Map.cpp:516
bool initialized_
Tracks if the peers_ map has been initialized.
std::vector< std::string > sync_keys(void)
Syncs the keys from the knowledge base.
Definition: Map.cpp:225
std::string prefix_
The context.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
Provides context about the transport.
void erase(const std::string &key)
Erases a variable from the map.
Definition: Map.cpp:397
int set(const std::string &key, madara::knowledge::KnowledgeRecord::Integer value=madara::knowledge::KnowledgeRecord::MODIFIED)
Sets a location within the map to the specified value.
Definition: Map.cpp:605
static struct madara::knowledge::tags::string_t string
virtual ~EndpointDiscovery()
Destructor.
uint64_t get_current_time(void) const
Gets the current timestamp.
virtual void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &transport_context, knowledge::Variables &vars)
Calls the main logic of the filter.
madara::knowledge::KnowledgeRecord::Integer Integer
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
Definition: Map.cpp:415
::std::map< std::string, KnowledgeRecord > KnowledgeMap
EndpointDiscovery(const std::string &prefix=".endpoints", knowledge::KnowledgeRecord::Integer heart_beat=-1)
Constructor.
const std::string & get_endpoint(void) const
Returns the current message endpoint.
madara::knowledge::KnowledgeRecord::Integer Integer
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
knowledge::KnowledgeRecord::Integer last_clear_
The time of the last clear of the peer_list.
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.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.