2015-07-16 22:55:50 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#ifndef NT_DISPATCHER_H_
|
|
|
|
|
#define NT_DISPATCHER_H_
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
2015-07-17 22:39:36 -07:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <condition_variable>
|
2015-07-29 20:33:26 -07:00
|
|
|
#include <functional>
|
2015-07-16 22:55:50 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "llvm/StringRef.h"
|
|
|
|
|
|
2016-07-27 00:39:38 -07:00
|
|
|
#include "support/atomic_static.h"
|
2015-07-16 22:55:50 -07:00
|
|
|
#include "NetworkConnection.h"
|
2015-08-02 21:47:01 -07:00
|
|
|
#include "Notifier.h"
|
2015-07-19 23:17:14 -07:00
|
|
|
#include "Storage.h"
|
2015-07-16 22:55:50 -07:00
|
|
|
|
2016-07-27 00:39:38 -07:00
|
|
|
namespace wpi {
|
2015-08-02 00:33:41 -07:00
|
|
|
class NetworkAcceptor;
|
|
|
|
|
class NetworkStream;
|
2016-07-27 00:39:38 -07:00
|
|
|
}
|
2015-07-17 22:39:36 -07:00
|
|
|
|
2015-07-17 07:21:07 -07:00
|
|
|
namespace nt {
|
2015-07-16 22:55:50 -07:00
|
|
|
|
2015-08-02 00:33:41 -07:00
|
|
|
class DispatcherBase {
|
2015-08-01 11:15:04 -07:00
|
|
|
friend class DispatcherTest;
|
2016-11-03 21:03:45 -07:00
|
|
|
|
2015-07-16 22:55:50 -07:00
|
|
|
public:
|
2016-07-27 00:39:38 -07:00
|
|
|
typedef std::function<std::unique_ptr<wpi::NetworkStream>()> Connector;
|
2016-04-08 13:31:35 -07:00
|
|
|
|
2015-08-02 00:33:41 -07:00
|
|
|
virtual ~DispatcherBase();
|
2015-07-16 22:55:50 -07:00
|
|
|
|
2017-06-02 20:25:20 -04:00
|
|
|
unsigned int GetNetworkMode() const;
|
2016-07-27 00:39:38 -07:00
|
|
|
void StartServer(llvm::StringRef persist_filename,
|
|
|
|
|
std::unique_ptr<wpi::NetworkAcceptor> acceptor);
|
2016-11-04 16:01:42 -07:00
|
|
|
void StartClient();
|
2015-07-16 22:55:50 -07:00
|
|
|
void Stop();
|
|
|
|
|
void SetUpdateRate(double interval);
|
|
|
|
|
void SetIdentity(llvm::StringRef name);
|
2015-07-17 22:39:36 -07:00
|
|
|
void Flush();
|
2015-08-02 10:47:05 -07:00
|
|
|
std::vector<ConnectionInfo> GetConnections() const;
|
2015-08-28 00:13:56 -07:00
|
|
|
void NotifyConnections(ConnectionListenerCallback callback) const;
|
2015-07-17 22:39:36 -07:00
|
|
|
|
2016-11-04 16:01:42 -07:00
|
|
|
void SetConnector(Connector connector);
|
|
|
|
|
void SetConnector(std::vector<Connector>&& connectors);
|
|
|
|
|
|
|
|
|
|
void SetConnectorOverride(Connector connector);
|
|
|
|
|
void ClearConnectorOverride();
|
|
|
|
|
|
2015-07-17 22:39:36 -07:00
|
|
|
bool active() const { return m_active; }
|
2015-07-16 22:55:50 -07:00
|
|
|
|
2015-08-02 00:33:41 -07:00
|
|
|
DispatcherBase(const DispatcherBase&) = delete;
|
|
|
|
|
DispatcherBase& operator=(const DispatcherBase&) = delete;
|
2015-07-16 22:55:50 -07:00
|
|
|
|
2015-08-02 00:33:41 -07:00
|
|
|
protected:
|
2015-08-02 21:47:01 -07:00
|
|
|
DispatcherBase(Storage& storage, Notifier& notifier);
|
2015-07-16 22:55:50 -07:00
|
|
|
|
2015-08-02 00:33:41 -07:00
|
|
|
private:
|
2015-07-16 22:55:50 -07:00
|
|
|
void DispatchThreadMain();
|
2015-08-02 00:33:41 -07:00
|
|
|
void ServerThreadMain();
|
2016-04-08 13:31:35 -07:00
|
|
|
void ClientThreadMain();
|
2015-07-16 22:55:50 -07:00
|
|
|
|
2015-07-31 20:32:52 -07:00
|
|
|
bool ClientHandshake(
|
|
|
|
|
NetworkConnection& conn,
|
|
|
|
|
std::function<std::shared_ptr<Message>()> get_msg,
|
|
|
|
|
std::function<void(llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
|
|
|
|
|
bool ServerHandshake(
|
|
|
|
|
NetworkConnection& conn,
|
|
|
|
|
std::function<std::shared_ptr<Message>()> get_msg,
|
|
|
|
|
std::function<void(llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
|
2015-07-29 20:33:26 -07:00
|
|
|
|
2015-07-31 20:32:52 -07:00
|
|
|
void ClientReconnect(unsigned int proto_rev = 0x0300);
|
2015-07-17 22:39:36 -07:00
|
|
|
|
2015-07-29 23:45:04 -07:00
|
|
|
void QueueOutgoing(std::shared_ptr<Message> msg, NetworkConnection* only,
|
|
|
|
|
NetworkConnection* except);
|
2015-07-17 22:39:36 -07:00
|
|
|
|
2015-08-01 11:15:04 -07:00
|
|
|
Storage& m_storage;
|
2015-08-02 21:47:01 -07:00
|
|
|
Notifier& m_notifier;
|
2017-06-02 20:25:20 -04:00
|
|
|
unsigned int m_networkMode = NT_NET_MODE_NONE;
|
2015-08-19 19:09:25 -07:00
|
|
|
std::string m_persist_filename;
|
2015-07-16 22:55:50 -07:00
|
|
|
std::thread m_dispatch_thread;
|
|
|
|
|
std::thread m_clientserver_thread;
|
|
|
|
|
|
2016-07-27 00:39:38 -07:00
|
|
|
std::unique_ptr<wpi::NetworkAcceptor> m_server_acceptor;
|
2016-11-04 16:01:42 -07:00
|
|
|
Connector m_client_connector_override;
|
2016-04-08 13:31:35 -07:00
|
|
|
std::vector<Connector> m_client_connectors;
|
2015-07-17 22:39:36 -07:00
|
|
|
|
|
|
|
|
// Mutex for user-accessible items
|
2015-08-02 10:47:05 -07:00
|
|
|
mutable std::mutex m_user_mutex;
|
2015-08-13 13:12:15 -07:00
|
|
|
std::vector<std::shared_ptr<NetworkConnection>> m_connections;
|
2015-07-16 22:55:50 -07:00
|
|
|
std::string m_identity;
|
|
|
|
|
|
2016-11-03 21:03:45 -07:00
|
|
|
std::atomic_bool m_active; // set to false to terminate threads
|
2015-07-17 22:39:36 -07:00
|
|
|
std::atomic_uint m_update_rate; // periodic dispatch update rate, in ms
|
|
|
|
|
|
|
|
|
|
// Condition variable for forced dispatch wakeup (flush)
|
|
|
|
|
std::mutex m_flush_mutex;
|
|
|
|
|
std::condition_variable m_flush_cv;
|
|
|
|
|
std::chrono::steady_clock::time_point m_last_flush;
|
2015-08-19 21:16:44 -07:00
|
|
|
bool m_do_flush = false;
|
2015-07-17 22:39:36 -07:00
|
|
|
|
2015-07-31 23:14:26 -07:00
|
|
|
// Condition variable for client reconnect (uses user mutex)
|
2015-07-17 22:39:36 -07:00
|
|
|
std::condition_variable m_reconnect_cv;
|
2015-08-19 21:16:44 -07:00
|
|
|
unsigned int m_reconnect_proto_rev = 0x0300;
|
|
|
|
|
bool m_do_reconnect = true;
|
2015-08-02 00:33:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Dispatcher : public DispatcherBase {
|
|
|
|
|
friend class DispatcherTest;
|
2016-11-03 21:03:45 -07:00
|
|
|
|
2015-08-02 00:33:41 -07:00
|
|
|
public:
|
|
|
|
|
static Dispatcher& GetInstance() {
|
|
|
|
|
ATOMIC_STATIC(Dispatcher, instance);
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 19:09:25 -07:00
|
|
|
void StartServer(StringRef persist_filename, const char* listen_address,
|
|
|
|
|
unsigned int port);
|
2016-11-04 16:01:42 -07:00
|
|
|
|
|
|
|
|
void SetServer(const char* server_name, unsigned int port);
|
|
|
|
|
void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
|
|
|
|
|
|
|
|
|
|
void SetServerOverride(const char* server_name, unsigned int port);
|
|
|
|
|
void ClearServerOverride();
|
2015-08-02 00:33:41 -07:00
|
|
|
|
|
|
|
|
private:
|
2015-08-19 21:49:18 -07:00
|
|
|
Dispatcher();
|
2015-08-02 21:47:01 -07:00
|
|
|
Dispatcher(Storage& storage, Notifier& notifier)
|
|
|
|
|
: DispatcherBase(storage, notifier) {}
|
2015-07-16 22:55:50 -07:00
|
|
|
|
2015-07-20 23:36:22 -07:00
|
|
|
ATOMIC_STATIC_DECL(Dispatcher)
|
2015-07-16 22:55:50 -07:00
|
|
|
};
|
|
|
|
|
|
2015-07-17 07:21:07 -07:00
|
|
|
} // namespace nt
|
2015-07-16 22:55:50 -07:00
|
|
|
|
|
|
|
|
#endif // NT_DISPATCHER_H_
|