Hermes SDK Documentation
Hive.hpp
Go to the documentation of this file.
1 #pragma once
3 
4 #include "protodefinitions/Hermes.pb.h"
5 
6 #include <functional>
7 #include <memory>
8 #include <chrono>
9 #include <vector>
10 #include <mutex>
11 
12 namespace HermesSDK
13 {
14  typedef std::function<void(Hermes::Protocol::HiveNode _node)> hiveNodeCallback;
15 
16  class Hive
17  {
18  public:
19  Hive(hiveNodeCallback& _onNodeFound, hiveNodeCallback& _onLocalNodeFound, hiveNodeCallback& _onNodeRemoved);
20  ~Hive();
21 
22  // delete the copy constructor and assignment operator to prevent this class from accidentally being copied
23  Hive(const Hive&) = delete;
24  Hive& operator=(const Hive&) = delete;
25 
26  void Configure(std::string _name, bool _useLoopback = true, int _beaconPort = (int)Hermes::Protocol::DefaultPorts::HivePort, int _broadcastIntervalInSeconds = 1, int _deadNodeTimeoutInSeconds = 10);
27  void Join();
28  void Stop();
29  static std::unique_ptr<Hive> FindCoordinator(std::string _name, int _beaconPort, hiveNodeCallback& _onNodeFound, hiveNodeCallback& _onLocalNodeFound, hiveNodeCallback& _onNodeRemoved, bool _useLoopback);
30  static std::unique_ptr<Hive> FindLocalCoordinator(std::string _name, int _beaconPort, hiveNodeCallback& _onNodeFound, hiveNodeCallback& _onLocalNodeFound, hiveNodeCallback& _onNodeRemoved);
31  static std::unique_ptr<Hive> FindNetworkCoordinator(std::string _name, int _beaconPort, hiveNodeCallback& _onNodeFound, hiveNodeCallback& _onLocalNodeFound, hiveNodeCallback& _onNodeRemoved);
32 
33  private:
34  static std::unique_ptr<Hermes::Protocol::HiveNode> tryParseHiveNodeFromBytes(unsigned char*_data, int _size);
35  void InternalStart();
36  void NodeDiscovered(Hermes::Protocol::HiveNode _node);
37 
38  struct HiveNodeTimeStamped {
40  std::chrono::system_clock::time_point lastseen;
41  };
42  std::vector<HiveNodeTimeStamped> m_nodes; // for a map, you need to implement your own comparator or hash function, gave a lot of trouble
43  std::mutex m_nodes_mutex;
44 
45  //Threads
46  void subscriberThreadFunction();
47  void nodeCleanupThreadFunction();
48  std::unique_ptr<std::thread> m_subscriberThread;
49  std::unique_ptr<std::thread> m_nodeCleanupThread;
50 
51  //Callbacks
52  hiveNodeCallback& m_anyNodeFoundCallback;
53  hiveNodeCallback& m_localNodeFoundCallback;
54  hiveNodeCallback& m_nodeRemovedCallback;
55 
56  bool m_running = false;
57 
58  bool m_isConfigured = false;
59  std::string m_name = "N/A";
60  int m_beaconPort = 0;
61  std::chrono::seconds m_deadNodeTimeout = std::chrono::seconds(2);
62  int m_broadcastIntervalInSeconds = 1;
63 
64  std::vector<std::string> m_localIpAddresses;
65  };
66 }
HermesSDK::Hive::FindLocalCoordinator
static std::unique_ptr< Hive > FindLocalCoordinator(std::string _name, int _beaconPort, hiveNodeCallback &_onNodeFound, hiveNodeCallback &_onLocalNodeFound, hiveNodeCallback &_onNodeRemoved)
Definition: Hive.cpp:123
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:100
HermesSDK::Hive
Definition: Hive.hpp:17
HermesSDK::Hive::FindNetworkCoordinator
static std::unique_ptr< Hive > FindNetworkCoordinator(std::string _name, int _beaconPort, hiveNodeCallback &_onNodeFound, hiveNodeCallback &_onLocalNodeFound, hiveNodeCallback &_onNodeRemoved)
Definition: Hive.cpp:141
Hermes::Protocol::HiveNode
Definition: Session.proto:21