Files
allwpilib/src/main/native/cpp/NetworkConnection.h

120 lines
3.6 KiB
C
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* 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_NETWORKCONNECTION_H_
#define NT_NETWORKCONNECTION_H_
#include <atomic>
2015-09-08 23:15:16 -07:00
#include <chrono>
#include <memory>
#include <thread>
#include "support/ConcurrentQueue.h"
#include "INetworkConnection.h"
#include "Message.h"
#include "ntcore_cpp.h"
namespace wpi {
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
class Logger;
class NetworkStream;
}
namespace nt {
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
class IConnectionNotifier;
class NetworkConnection : public INetworkConnection {
public:
2015-07-31 20:32:52 -07:00
typedef std::function<bool(
NetworkConnection& conn,
std::function<std::shared_ptr<Message>()> get_msg,
std::function<void(llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs)>
HandshakeFunc;
typedef std::function<void(std::shared_ptr<Message> msg,
NetworkConnection* conn)>
ProcessIncomingFunc;
typedef std::vector<std::shared_ptr<Message>> Outgoing;
typedef wpi::ConcurrentQueue<Outgoing> OutgoingQueue;
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
NetworkConnection(unsigned int uid,
std::unique_ptr<wpi::NetworkStream> stream,
IConnectionNotifier& notifier, wpi::Logger& logger,
HandshakeFunc handshake,
2015-08-13 13:12:15 -07:00
Message::GetEntryTypeFunc get_entry_type);
~NetworkConnection();
2015-08-13 13:12:15 -07:00
// Set the input processor function. This must be called before Start().
void set_process_incoming(ProcessIncomingFunc func) {
m_process_incoming = func;
}
void Start();
void Stop();
ConnectionInfo info() const override;
bool active() const { return m_active; }
wpi::NetworkStream& stream() { return *m_stream; }
2015-08-13 13:12:15 -07:00
void QueueOutgoing(std::shared_ptr<Message> msg) override;
void PostOutgoing(bool keep_alive) override;
2015-08-13 13:12:15 -07:00
unsigned int uid() const { return m_uid; }
unsigned int proto_rev() const override;
void set_proto_rev(unsigned int proto_rev) override;
State state() const override;
void set_state(State state) override;
std::string remote_id() const;
void set_remote_id(StringRef remote_id);
2015-08-02 10:47:05 -07:00
unsigned long long last_update() const { return m_last_update; }
NetworkConnection(const NetworkConnection&) = delete;
NetworkConnection& operator=(const NetworkConnection&) = delete;
private:
void ReadThreadMain();
void WriteThreadMain();
2015-08-13 13:12:15 -07:00
unsigned int m_uid;
std::unique_ptr<wpi::NetworkStream> m_stream;
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
IConnectionNotifier& m_notifier;
wpi::Logger& m_logger;
OutgoingQueue m_outgoing;
2015-07-31 20:32:52 -07:00
HandshakeFunc m_handshake;
Message::GetEntryTypeFunc m_get_entry_type;
ProcessIncomingFunc m_process_incoming;
std::thread m_read_thread;
std::thread m_write_thread;
std::atomic_bool m_active;
std::atomic_uint m_proto_rev;
mutable std::mutex m_state_mutex;
State m_state;
mutable std::mutex m_remote_id_mutex;
std::string m_remote_id;
2015-08-02 10:47:05 -07:00
std::atomic_ullong m_last_update;
2015-09-08 23:15:16 -07:00
std::chrono::steady_clock::time_point m_last_post;
2015-08-13 13:12:15 -07:00
std::mutex m_pending_mutex;
Outgoing m_pending_outgoing;
std::vector<std::pair<std::size_t, std::size_t>> m_pending_update;
// Condition variables for shutdown
std::mutex m_shutdown_mutex;
std::condition_variable m_read_shutdown_cv;
std::condition_variable m_write_shutdown_cv;
bool m_read_shutdown = false;
bool m_write_shutdown = false;
};
} // namespace nt
#endif // NT_NETWORKCONNECTION_H_