diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java index 29813a57e3..06dad09f76 100644 --- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java +++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java @@ -44,6 +44,7 @@ public final class NetworkTableInstance implements AutoCloseable { public static final int kNetModeClient = 0x02; public static final int kNetModeStarting = 0x04; public static final int kNetModeFailure = 0x08; + public static final int kNetModeLocal = 0x10; /** * The default port that network tables operates on. @@ -675,6 +676,23 @@ public final class NetworkTableInstance implements AutoCloseable { return NetworkTablesJNI.getNetworkMode(m_handle); } + /** + * Starts local-only operation. Prevents calls to startServer or startClient + * from taking effect. Has no effect if startServer or startClient + * has already been called. + */ + public void startLocal() { + NetworkTablesJNI.startLocal(m_handle); + } + + /** + * Stops local-only operation. startServer or startClient can be called after + * this call to start a server or client. + */ + public void stopLocal() { + NetworkTablesJNI.stopLocal(m_handle); + } + /** * Starts a server using the networktables.ini as the persistent file, * using the default listening address and port. diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java index d24f06680b..742c0ca672 100644 --- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java +++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java @@ -139,6 +139,8 @@ public final class NetworkTablesJNI { public static native void setNetworkIdentity(int inst, String name); public static native int getNetworkMode(int inst); + public static native void startLocal(int inst); + public static native void stopLocal(int inst); public static native void startServer(int inst, String persistFilename, String listenAddress, int port); public static native void stopServer(int inst); public static native void startClient(int inst); diff --git a/ntcore/src/main/native/cpp/Dispatcher.cpp b/ntcore/src/main/native/cpp/Dispatcher.cpp index 8164f45662..6d1ead039d 100644 --- a/ntcore/src/main/native/cpp/Dispatcher.cpp +++ b/ntcore/src/main/native/cpp/Dispatcher.cpp @@ -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 acceptor) { diff --git a/ntcore/src/main/native/cpp/Dispatcher.h b/ntcore/src/main/native/cpp/Dispatcher.h index dfaf4d5136..bfddae7dfa 100644 --- a/ntcore/src/main/native/cpp/Dispatcher.h +++ b/ntcore/src/main/native/cpp/Dispatcher.h @@ -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 acceptor); void StartClient(); diff --git a/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp b/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp index 49f34f0743..ac00a3c392 100644 --- a/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp +++ b/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp @@ -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 diff --git a/ntcore/src/main/native/cpp/ntcore_c.cpp b/ntcore/src/main/native/cpp/ntcore_c.cpp index 7ca68f51dd..60745fea14 100644 --- a/ntcore/src/main/native/cpp/ntcore_c.cpp +++ b/ntcore/src/main/native/cpp/ntcore_c.cpp @@ -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); diff --git a/ntcore/src/main/native/cpp/ntcore_cpp.cpp b/ntcore/src/main/native/cpp/ntcore_cpp.cpp index 5a2b8afadb..ad8f68b814 100644 --- a/ntcore/src/main/native/cpp/ntcore_cpp.cpp +++ b/ntcore/src/main/native/cpp/ntcore_cpp.cpp @@ -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(); diff --git a/ntcore/src/main/native/include/networktables/NetworkTableInstance.h b/ntcore/src/main/native/include/networktables/NetworkTableInstance.h index ee0874545c..16fc5b2950 100644 --- a/ntcore/src/main/native/include/networktables/NetworkTableInstance.h +++ b/ntcore/src/main/native/include/networktables/NetworkTableInstance.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-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. */ @@ -61,7 +61,8 @@ class NetworkTableInstance final { kNetModeServer = NT_NET_MODE_SERVER, kNetModeClient = NT_NET_MODE_CLIENT, kNetModeStarting = NT_NET_MODE_STARTING, - kNetModeFailure = NT_NET_MODE_FAILURE + kNetModeFailure = NT_NET_MODE_FAILURE, + kNetModeLocal = NT_NET_MODE_LOCAL }; /** @@ -298,6 +299,19 @@ class NetworkTableInstance final { */ unsigned int GetNetworkMode() const; + /** + * Starts local-only operation. Prevents calls to StartServer or StartClient + * from taking effect. Has no effect if StartServer or StartClient + * has already been called. + */ + void StartLocal(); + + /** + * Stops local-only operation. StartServer or StartClient can be called after + * this call to start a server or client. + */ + void StopLocal(); + /** * Starts a server using the specified filename, listening address, and port. * diff --git a/ntcore/src/main/native/include/networktables/NetworkTableInstance.inl b/ntcore/src/main/native/include/networktables/NetworkTableInstance.inl index 83616d978f..2b6607f18f 100644 --- a/ntcore/src/main/native/include/networktables/NetworkTableInstance.inl +++ b/ntcore/src/main/native/include/networktables/NetworkTableInstance.inl @@ -81,6 +81,10 @@ inline unsigned int NetworkTableInstance::GetNetworkMode() const { return ::nt::GetNetworkMode(m_handle); } +inline void NetworkTableInstance::StartLocal() { ::nt::StartLocal(m_handle); } + +inline void NetworkTableInstance::StopLocal() { ::nt::StopLocal(m_handle); } + inline void NetworkTableInstance::StartServer(const Twine& persist_filename, const char* listen_address, unsigned int port) { diff --git a/ntcore/src/main/native/include/ntcore_c.h b/ntcore/src/main/native/include/ntcore_c.h index 60773ea777..1ed40250ee 100644 --- a/ntcore/src/main/native/include/ntcore_c.h +++ b/ntcore/src/main/native/include/ntcore_c.h @@ -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. */ @@ -95,6 +95,7 @@ enum NT_NetworkMode { NT_NET_MODE_CLIENT = 0x02, /* running in client mode */ NT_NET_MODE_STARTING = 0x04, /* flag for starting (either client or server) */ NT_NET_MODE_FAILURE = 0x08, /* flag for failure (either client or server) */ + NT_NET_MODE_LOCAL = 0x10, /* running in local-only mode */ }; /* @@ -1037,6 +1038,19 @@ void NT_SetNetworkIdentity(NT_Inst inst, const char* name, size_t name_len); */ unsigned int NT_GetNetworkMode(NT_Inst inst); +/** + * Starts local-only operation. Prevents calls to NT_StartServer or + * NT_StartClient from taking effect. Has no effect if NT_StartServer or + * NT_StartClient has already been called. + */ +void NT_StartLocal(NT_Inst inst); + +/** + * Stops local-only operation. NT_StartServer or NT_StartClient can be called + * after this call to start a server or client. + */ +void NT_StopLocal(NT_Inst inst); + /** * Starts a server using the specified filename, listening address, and port. * diff --git a/ntcore/src/main/native/include/ntcore_cpp.h b/ntcore/src/main/native/include/ntcore_cpp.h index d7e91ad442..af1ea12c67 100644 --- a/ntcore/src/main/native/include/ntcore_cpp.h +++ b/ntcore/src/main/native/include/ntcore_cpp.h @@ -1130,6 +1130,19 @@ unsigned int GetNetworkMode(); */ unsigned int GetNetworkMode(NT_Inst inst); +/** + * Starts local-only operation. Prevents calls to StartServer or StartClient + * from taking effect. Has no effect if StartServer or StartClient + * has already been called. + */ +void StartLocal(NT_Inst inst); + +/** + * Stops local-only operation. StartServer or StartClient can be called after + * this call to start a server or client. + */ +void StopLocal(NT_Inst inst); + /** * Starts a server using the specified filename, listening address, and port. *