ntcore: Add support for local-only operation (#2204)

StartLocal() causes future calls to StartServer() or StartClient() to have
no effect. StopLocal() re-enables these calls.
This commit is contained in:
Peter Johnson
2019-12-29 14:56:41 -06:00
committed by GitHub
parent 44bcf7fb4d
commit 6ea13ea8f3
11 changed files with 123 additions and 5 deletions

View File

@@ -115,6 +115,16 @@ DispatcherBase::~DispatcherBase() { Stop(); }
unsigned int DispatcherBase::GetNetworkMode() const { return m_networkMode; }
void DispatcherBase::StartLocal() {
{
std::scoped_lock lock(m_user_mutex);
if (m_active) return;
m_active = true;
}
m_networkMode = NT_NET_MODE_LOCAL;
m_storage.SetDispatcher(this, false);
}
void DispatcherBase::StartServer(
const Twine& persist_filename,
std::unique_ptr<wpi::NetworkAcceptor> acceptor) {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2015-2019 FIRST. 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. */
@@ -48,6 +48,7 @@ class DispatcherBase : public IDispatcher {
virtual ~DispatcherBase();
unsigned int GetNetworkMode() const;
void StartLocal();
void StartServer(const Twine& persist_filename,
std::unique_ptr<wpi::NetworkAcceptor> acceptor);
void StartClient();

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2018-2019 FIRST. 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. */
@@ -1368,6 +1368,30 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_getNetworkMode
return nt::GetNetworkMode(inst);
}
/*
* Class: edu_wpi_first_networktables_NetworkTablesJNI
* Method: startLocal
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_networktables_NetworkTablesJNI_startLocal
(JNIEnv*, jclass, jint inst)
{
nt::StartLocal(inst);
}
/*
* Class: edu_wpi_first_networktables_NetworkTablesJNI
* Method: stopLocal
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_networktables_NetworkTablesJNI_stopLocal
(JNIEnv*, jclass, jint inst)
{
nt::StopLocal(inst);
}
/*
* Class: edu_wpi_first_networktables_NetworkTablesJNI
* Method: startServer

View File

@@ -539,6 +539,10 @@ unsigned int NT_GetNetworkMode(NT_Inst inst) {
return nt::GetNetworkMode(inst);
}
void NT_StartLocal(NT_Inst inst) { nt::StartLocal(inst); }
void NT_StopLocal(NT_Inst inst) { nt::StopLocal(inst); }
void NT_StartServer(NT_Inst inst, const char* persist_filename,
const char* listen_address, unsigned int port) {
nt::StartServer(inst, persist_filename, listen_address, port);

View File

@@ -742,6 +742,20 @@ unsigned int GetNetworkMode(NT_Inst inst) {
return ii->dispatcher.GetNetworkMode();
}
void StartLocal(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
if (!ii) return;
ii->dispatcher.StartLocal();
}
void StopLocal(NT_Inst inst) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
if (!ii) return;
ii->dispatcher.Stop();
}
void StartServer(StringRef persist_filename, const char* listen_address,
unsigned int port) {
auto ii = InstanceImpl::GetDefault();