8 #ifndef NT_DISPATCHER_H_
9 #define NT_DISPATCHER_H_
13 #include <condition_variable>
20 #include "llvm/StringRef.h"
22 #include "support/atomic_static.h"
23 #include "NetworkConnection.h"
28 class NetworkAcceptor;
35 friend class DispatcherTest;
38 typedef std::function<std::unique_ptr<wpi::NetworkStream>()> Connector;
42 void StartServer(llvm::StringRef persist_filename,
43 std::unique_ptr<wpi::NetworkAcceptor> acceptor);
46 void SetUpdateRate(
double interval);
47 void SetIdentity(llvm::StringRef name);
49 std::vector<ConnectionInfo> GetConnections()
const;
50 void NotifyConnections(ConnectionListenerCallback callback)
const;
52 void SetConnector(Connector connector);
53 void SetConnector(std::vector<Connector>&& connectors);
55 void SetConnectorOverride(Connector connector);
56 void ClearConnectorOverride();
58 bool active()
const {
return m_active; }
67 void DispatchThreadMain();
68 void ServerThreadMain();
69 void ClientThreadMain();
73 std::function<std::shared_ptr<Message>()> get_msg,
74 std::function<
void(llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
77 std::function<std::shared_ptr<Message>()> get_msg,
78 std::function<
void(llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
80 void ClientReconnect(
unsigned int proto_rev = 0x0300);
87 bool m_server =
false;
88 std::string m_persist_filename;
89 std::thread m_dispatch_thread;
90 std::thread m_clientserver_thread;
92 std::unique_ptr<wpi::NetworkAcceptor> m_server_acceptor;
93 Connector m_client_connector_override;
94 std::vector<Connector> m_client_connectors;
97 mutable std::mutex m_user_mutex;
98 std::vector<std::shared_ptr<NetworkConnection>> m_connections;
99 std::string m_identity;
101 std::atomic_bool m_active;
102 std::atomic_uint m_update_rate;
105 std::mutex m_flush_mutex;
106 std::condition_variable m_flush_cv;
107 std::chrono::steady_clock::time_point m_last_flush;
108 bool m_do_flush =
false;
111 std::condition_variable m_reconnect_cv;
112 unsigned int m_reconnect_proto_rev = 0x0300;
113 bool m_do_reconnect =
true;
117 friend class DispatcherTest;
125 void StartServer(StringRef persist_filename,
const char* listen_address,
128 void SetServer(
const char* server_name,
unsigned int port);
129 void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
131 void SetServerOverride(
const char* server_name,
unsigned int port);
132 void ClearServerOverride();
144 #endif // NT_DISPATCHER_H_
Definition: Dispatcher.h:34
Definition: Dispatcher.h:116
Definition: NetworkConnection.h:28
Definition: Notifier.h:19