Hermes SDK Documentation
Hive.hpp
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include "protodefinitions/Hermes.pb.h"
6 
7 #include <functional>
8 #include <memory>
9 #include <chrono>
10 #include <vector>
11 #include <mutex>
12 
13 namespace HermesSDK
14 {
15  // Set up a Doxygen group.
20  typedef std::function<void(Hermes::Protocol::HiveNode _node)> hiveNodeCallback;
21 
22  class Hive
23  {
24 
25  private:
26  //Limit according to https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentvariablew
27  static const unsigned long s_BufferSize = 65535;
28 
29  public:
30  Hive(hiveNodeCallback& _onNodeFound, hiveNodeCallback& _onLocalNodeFound, hiveNodeCallback& _onNodeRemoved);
31  ~Hive();
32 
33  // delete the copy constructor and assignment operator to prevent this class from accidentally being copied
34  Hive(const Hive&) = delete;
35  Hive& operator=(const Hive&) = delete;
36 
37  void Configure(std::string _name, bool _useLoopback = true, int _beaconPort = (int)Hermes::Protocol::DefaultPorts::HivePort, int _broadcastIntervalInSeconds = 1, int _deadNodeTimeoutInSeconds = 10);
38  void Join();
39  void Stop();
40  static void CleanEnvironmentVariables();
41  static void CleanEnvironmentVariable(std::wstring p_EnvironmentVariable);
42  static bool DoesEnvironmentVariableExist(std::wstring p_EnvironmentVariable);
43  static std::unique_ptr<Hive> FindCoordinator(std::string _name, int _beaconPort, hiveNodeCallback& _onNodeFound, hiveNodeCallback& _onLocalNodeFound, hiveNodeCallback& _onNodeRemoved, bool _useLoopback);
44  static std::unique_ptr<Hive> FindLocalCoordinator(std::string _name, int _beaconPort, hiveNodeCallback& _onNodeFound, hiveNodeCallback& _onLocalNodeFound, hiveNodeCallback& _onNodeRemoved);
45  static std::unique_ptr<Hive> FindNetworkCoordinator(std::string _name, int _beaconPort, hiveNodeCallback& _onNodeFound, hiveNodeCallback& _onLocalNodeFound, hiveNodeCallback& _onNodeRemoved);
46 
47  private:
48  static std::unique_ptr<Hermes::Protocol::HiveNode> tryParseHiveNodeFromBytes(unsigned char*_data, int _size);
49  void InternalStart();
50  void NodeDiscovered(Hermes::Protocol::HiveNode _node);
51 
52  struct HiveNodeTimeStamped {
54  std::chrono::system_clock::time_point lastseen;
55  };
56  std::vector<HiveNodeTimeStamped> m_nodes; // for a map, you need to implement your own comparator or hash function, gave a lot of trouble
57  std::mutex m_nodes_mutex;
58 
59  //Threads
60  void subscriberThreadFunction();
61  void nodeCleanupThreadFunction();
62  std::unique_ptr<std::thread> m_subscriberThread;
63  std::unique_ptr<std::thread> m_nodeCleanupThread;
64 
65  //Callbacks
66  hiveNodeCallback& m_anyNodeFoundCallback;
67  hiveNodeCallback& m_localNodeFoundCallback;
68  hiveNodeCallback& m_nodeRemovedCallback;
69 
70  bool m_running = false;
71 
72  bool m_isConfigured = false;
73  std::string m_name = "N/A";
74  int m_beaconPort = 0;
75  std::chrono::seconds m_deadNodeTimeout = std::chrono::seconds(2);
76  int m_broadcastIntervalInSeconds = 1;
77 
78  std::vector<std::string> m_localIpAddresses;
79  };
80 
81  // Close the Doxygen group.
83 }
HermesSDK::Hive::CleanEnvironmentVariables
static void CleanEnvironmentVariables()
support function to remove environment variables from the runtime environment for running the SDK
Definition: Hive.cpp:98
HermesSDK::Hive::FindLocalCoordinator
static std::unique_ptr< Hive > FindLocalCoordinator(std::string _name, int _beaconPort, hiveNodeCallback &_onNodeFound, hiveNodeCallback &_onLocalNodeFound, hiveNodeCallback &_onNodeRemoved)
Definition: Hive.cpp:180
HermesSDK::Hive::FindCoordinator
static std::unique_ptr< Hive > FindCoordinator(std::string _name, int _beaconPort, hiveNodeCallback &_onNodeFound, hiveNodeCallback &_onLocalNodeFound, hiveNodeCallback &_onNodeRemoved, bool _useLoopback)
Definition: Hive.cpp:154
HermesSDK::Hive
Definition: Hive.hpp:23
HermesSDK::Hive::FindNetworkCoordinator
static std::unique_ptr< Hive > FindNetworkCoordinator(std::string _name, int _beaconPort, hiveNodeCallback &_onNodeFound, hiveNodeCallback &_onLocalNodeFound, hiveNodeCallback &_onNodeRemoved)
Definition: Hive.cpp:199
HermesSDK::Hive::CleanEnvironmentVariable
static void CleanEnvironmentVariable(std::wstring p_EnvironmentVariable)
support function to remove an environment variable from the runtime environment for running the SDK
Definition: Hive.cpp:108
Hermes::Protocol::HiveNode
Definition: Session.proto:22