Free Electron
BeaconClient.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __BeaconClient_h__
8 #define __BeaconClient_h__
9 
10 namespace beacon
11 {
12 
13 /// Max milliseconds between messages before we send a keep alive message
14 #define KEEP_ALIVE_REFRESH_TIME 4000
15 
16 /// Max milliseconds between messages from Beacon before it is considered dead
17 #define BEACON_ALIVE_TIME_LIMIT 6000
18 
19 class FE_DL_EXPORT BeaconClient : virtual public BeaconClientStartupI
20 {
21 public:
22  BeaconClient();
23  virtual ~BeaconClient();
24 
25  virtual bool startup(const char *fileName) { return true; }
26 
27  // Registers node with the Beacon
28  virtual bool registerWithBeacon(const BeaconConfig &config,
29  bool timeSync) override;
30 
31  virtual void shutdown() override;
32  virtual uint8_t getID() override { return m_myID; }
33  virtual int64_t getTimeOffset() override;
34  virtual int64_t getBeaconTime()override;
35  virtual int64_t getLatency() override;
36 
37  // Returns true if the list of nodes has changed since
38  // the last getList() call
39  bool isListUpdated() override;
40  // Get a list of the nodes registered with the Beacon
41  void getList(std::list<Node> &registeredNode) override;
42  // Returns true if the Beacon is alive
43  // (received a response from the Beacon within the timeout period)
44  bool isBeaconAlive() override;
45 
46  // Global Dictionary access functions
47  virtual bool dictionaryGet(
48  std::vector<std::pair<fe::String,fe::String>> &list) override;
49  virtual bool dictionarySet(
50  const std::vector<std::pair<fe::String,fe::String>> &list) override;
51  virtual bool dictionaryUnset(
52  const std::vector<std::pair<fe::String,fe::String>> &list) override;
53  virtual bool dictionaryClear() override;
54  virtual bool dictionaryGetRegex(const fe::String searchString,
55  std::vector<std::pair<fe::String,fe::String>> &list) override;
56  virtual uint64_t dictionaryGetUpdateCounter() override;
57 
58 protected:
59  BeaconConfig m_config;
60 
61 private:
62  bool m_initialized;
63  bool m_registered;
64  bool m_timeSynced;
65  bool m_done;
66  bool m_listUpdated;
67  uint8_t m_myID;
68  int64_t m_lastSendTime;
69  int64_t m_latency;
70  int64_t m_deltaTime;
71  uint16_t m_responsePort;
72 
73  std::atomic<bool> m_running;
74  std::atomic<int64_t> m_lastRecTime;
75  std::atomic<uint64_t> m_updateCounter;
76 
77  std::thread* m_monitorThread;
78  fe::sp<fe::SingleMaster> m_spSingleMaster;
79  fe::sp<fe::ext::MessageI> m_sendMsgSystem;
80  fe::sp<fe::ext::MessageI> m_recMsgSystem;
81  fe::sp<fe::ext::MessageReliableUDPI> m_gdMessageSystem;
82  fe::sp<beacon::BeaconTimeSyncI> m_beaconTimeSync;
83  std::list<Node> m_registeredNodes;
84  std::mutex m_nodeListMutex;
85 
86  void initVariables();
87  bool init();
88  bool syncTimeWithBeacon();
89  bool getBeaconMessage(fe::ext::Messagegram *msg);
90  bool registerNode();
91  void monitorThread();
92  int64_t getCurrentTime();
93 
94  void sendRegisterRequest();
95  void sendShutdown();
96  void sendKeepAlive();
97  bool sendRequest(const fe::ext::Messagegram &m);
98  bool sendGDRequest(
99  const std::vector<std::pair<fe::String,fe::String>> &list,
100  const bool includeValues,
101  const NodeMessageType requestType);
102  bool waitForResponse(uint8_t **msg, uint32_t &bytesRecv,
103  char *fromIPaddress);
104  void displayList();
105 };
106 
107 } // namespace beacon
108 
109 #endif // __BeaconClient_h__
Automatically reference-counted string container.
Definition: String.h:128
Definition: BeaconClient.cc:16