2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
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
|
|
|
|
|
|
|
|
#include "InstanceImpl.h"
|
|
|
|
|
|
|
|
|
|
using namespace nt;
|
|
|
|
|
|
|
|
|
|
std::atomic<int> InstanceImpl::s_default{-1};
|
2021-09-17 12:11:00 -07:00
|
|
|
std::atomic<InstanceImpl*> InstanceImpl::s_instances[kNumInstances];
|
2017-11-13 09:51:05 -08:00
|
|
|
wpi::mutex InstanceImpl::s_mutex;
|
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
|
|
|
|
|
|
|
|
using namespace std::placeholders;
|
|
|
|
|
|
|
|
|
|
InstanceImpl::InstanceImpl(int inst)
|
2022-10-31 21:52:14 -07:00
|
|
|
: listenerStorage{inst},
|
|
|
|
|
logger_impl{listenerStorage},
|
|
|
|
|
logger{
|
|
|
|
|
std::bind(&LoggerImpl::Log, &logger_impl, _1, _2, _3, _4)}, // NOLINT
|
|
|
|
|
connectionList{inst, listenerStorage},
|
|
|
|
|
localStorage{inst, listenerStorage, logger},
|
2022-10-08 10:01:31 -07:00
|
|
|
m_inst{inst} {
|
2017-10-05 23:13:04 -07:00
|
|
|
logger.set_min_level(logger_impl.GetMinLevel());
|
|
|
|
|
}
|
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
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
InstanceImpl* InstanceImpl::GetDefault() {
|
|
|
|
|
return Get(GetDefaultIndex());
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
InstanceImpl* InstanceImpl::Get(int inst) {
|
2021-09-17 12:11:00 -07:00
|
|
|
if (inst < 0 || inst >= kNumInstances) {
|
2020-12-28 12:58:06 -08:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-09-17 12:11:00 -07:00
|
|
|
return s_instances[inst];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int InstanceImpl::GetDefaultIndex() {
|
|
|
|
|
int inst = s_default;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (inst >= 0) {
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
// slow path
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(s_mutex);
|
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
|
|
|
|
|
|
|
|
// double-check
|
|
|
|
|
inst = s_default;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (inst >= 0) {
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
// alloc and save
|
|
|
|
|
inst = AllocImpl();
|
|
|
|
|
s_default = inst;
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int InstanceImpl::Alloc() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(s_mutex);
|
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
|
|
|
return AllocImpl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int InstanceImpl::AllocImpl() {
|
2021-09-17 12:11:00 -07:00
|
|
|
int inst = 0;
|
|
|
|
|
for (; inst < kNumInstances; ++inst) {
|
|
|
|
|
if (!s_instances[inst]) {
|
|
|
|
|
s_instances[inst] = new InstanceImpl(inst);
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
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
|
|
|
}
|
2021-09-17 12:11:00 -07:00
|
|
|
return -1;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstanceImpl::Destroy(int inst) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(s_mutex);
|
2021-09-17 12:11:00 -07:00
|
|
|
if (inst < 0 || inst >= kNumInstances) {
|
2020-12-28 12:58:06 -08:00
|
|
|
return;
|
|
|
|
|
}
|
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
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
delete s_instances[inst].exchange(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstanceImpl::StartLocal() {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
if (networkMode != NT_NET_MODE_NONE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
networkMode = NT_NET_MODE_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstanceImpl::StopLocal() {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
if ((networkMode & NT_NET_MODE_LOCAL) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
networkMode = NT_NET_MODE_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstanceImpl::StartServer(std::string_view persistFilename,
|
|
|
|
|
std::string_view listenAddress,
|
|
|
|
|
unsigned int port3, unsigned int port4) {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
if (networkMode != NT_NET_MODE_NONE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_networkServer = std::make_shared<NetworkServer>(
|
|
|
|
|
persistFilename, listenAddress, port3, port4, localStorage,
|
|
|
|
|
connectionList, logger, [this] {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
networkMode &= ~NT_NET_MODE_STARTING;
|
|
|
|
|
});
|
|
|
|
|
networkMode = NT_NET_MODE_SERVER | NT_NET_MODE_STARTING;
|
2022-12-30 20:15:57 -08:00
|
|
|
listenerStorage.NotifyTimeSync({}, NT_EVENT_TIMESYNC, 0, 0, true);
|
|
|
|
|
m_serverTimeOffset = 0;
|
|
|
|
|
m_rtt2 = 0;
|
2022-10-08 10:01:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstanceImpl::StopServer() {
|
2022-12-27 10:25:48 -08:00
|
|
|
std::shared_ptr<NetworkServer> server;
|
|
|
|
|
{
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
if ((networkMode & NT_NET_MODE_SERVER) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
server = std::move(m_networkServer);
|
|
|
|
|
networkMode = NT_NET_MODE_NONE;
|
2022-12-30 20:15:57 -08:00
|
|
|
listenerStorage.NotifyTimeSync({}, NT_EVENT_TIMESYNC, 0, 0, false);
|
|
|
|
|
m_serverTimeOffset.reset();
|
|
|
|
|
m_rtt2 = 0;
|
2022-10-08 10:01:31 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 22:04:14 -07:00
|
|
|
void InstanceImpl::StartClient3(std::string_view identity) {
|
2022-10-08 10:01:31 -07:00
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
if (networkMode != NT_NET_MODE_NONE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_networkClient = std::make_shared<NetworkClient3>(
|
2022-10-21 22:04:14 -07:00
|
|
|
m_inst, identity, localStorage, connectionList, logger);
|
|
|
|
|
if (!m_servers.empty()) {
|
|
|
|
|
m_networkClient->SetServers(m_servers);
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
networkMode = NT_NET_MODE_CLIENT3;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 22:04:14 -07:00
|
|
|
void InstanceImpl::StartClient4(std::string_view identity) {
|
2022-10-08 10:01:31 -07:00
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
if (networkMode != NT_NET_MODE_NONE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_networkClient = std::make_shared<NetworkClient>(
|
2022-12-30 20:15:57 -08:00
|
|
|
m_inst, identity, localStorage, connectionList, logger,
|
|
|
|
|
[this](int64_t serverTimeOffset, int64_t rtt2, bool valid) {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
listenerStorage.NotifyTimeSync({}, NT_EVENT_TIMESYNC, serverTimeOffset,
|
|
|
|
|
rtt2, valid);
|
|
|
|
|
if (valid) {
|
|
|
|
|
m_serverTimeOffset = serverTimeOffset;
|
|
|
|
|
m_rtt2 = rtt2;
|
|
|
|
|
} else {
|
|
|
|
|
m_serverTimeOffset.reset();
|
|
|
|
|
m_rtt2 = 0;
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-10-21 22:04:14 -07:00
|
|
|
if (!m_servers.empty()) {
|
|
|
|
|
m_networkClient->SetServers(m_servers);
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
networkMode = NT_NET_MODE_CLIENT4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstanceImpl::StopClient() {
|
2022-12-30 20:15:57 -08:00
|
|
|
std::shared_ptr<INetworkClient> client;
|
|
|
|
|
{
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
if ((networkMode & (NT_NET_MODE_CLIENT3 | NT_NET_MODE_CLIENT4)) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
client = std::move(m_networkClient);
|
|
|
|
|
networkMode = NT_NET_MODE_NONE;
|
|
|
|
|
}
|
|
|
|
|
client.reset();
|
|
|
|
|
{
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
listenerStorage.NotifyTimeSync({}, NT_EVENT_TIMESYNC, 0, 0, false);
|
|
|
|
|
m_serverTimeOffset.reset();
|
|
|
|
|
m_rtt2 = 0;
|
2022-10-08 10:01:31 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 22:04:14 -07:00
|
|
|
void InstanceImpl::SetServers(
|
|
|
|
|
std::span<const std::pair<std::string, unsigned int>> servers) {
|
2022-10-08 10:01:31 -07:00
|
|
|
std::scoped_lock lock{m_mutex};
|
2022-10-21 22:04:14 -07:00
|
|
|
m_servers = {servers.begin(), servers.end()};
|
|
|
|
|
if (m_networkClient) {
|
|
|
|
|
m_networkClient->SetServers(servers);
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<NetworkServer> InstanceImpl::GetServer() {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
return m_networkServer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<INetworkClient> InstanceImpl::GetClient() {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
return m_networkClient;
|
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
|
|
|
}
|
2022-11-18 13:21:05 -05:00
|
|
|
|
2022-12-30 20:15:57 -08:00
|
|
|
std::optional<int64_t> InstanceImpl::GetServerTimeOffset() {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
return m_serverTimeOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstanceImpl::AddTimeSyncListener(NT_Listener listener,
|
|
|
|
|
unsigned int eventMask) {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
eventMask &= (NT_EVENT_TIMESYNC | NT_EVENT_IMMEDIATE);
|
|
|
|
|
listenerStorage.Activate(listener, eventMask);
|
|
|
|
|
if ((eventMask & (NT_EVENT_TIMESYNC | NT_EVENT_IMMEDIATE)) ==
|
|
|
|
|
(NT_EVENT_TIMESYNC | NT_EVENT_IMMEDIATE) &&
|
|
|
|
|
m_serverTimeOffset) {
|
|
|
|
|
listenerStorage.NotifyTimeSync({&listener, 1},
|
|
|
|
|
NT_EVENT_TIMESYNC | NT_EVENT_IMMEDIATE,
|
|
|
|
|
*m_serverTimeOffset, m_rtt2, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 13:21:05 -05:00
|
|
|
void InstanceImpl::Reset() {
|
|
|
|
|
std::scoped_lock lock{m_mutex};
|
|
|
|
|
m_networkServer.reset();
|
|
|
|
|
m_networkClient.reset();
|
|
|
|
|
m_servers.clear();
|
|
|
|
|
networkMode = NT_NET_MODE_NONE;
|
2022-12-30 20:15:57 -08:00
|
|
|
m_serverTimeOffset.reset();
|
|
|
|
|
m_rtt2 = 0;
|
2022-11-18 13:21:05 -05:00
|
|
|
|
|
|
|
|
listenerStorage.Reset();
|
|
|
|
|
// connectionList should have been cleared by destroying networkClient/server
|
|
|
|
|
localStorage.Reset();
|
|
|
|
|
}
|