Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion glass/src/libnt/native/cpp/NetworkTablesSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void NetworkTablesSettings::Thread::Main() {
}
} else if (m_mode == 3) {
wpi::nt::StartServer(m_inst, m_iniName.c_str(), m_listenAddress.c_str(),
m_port);
"", m_port);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions ntcore/src/dev/native/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void bench() {
auto server = wpi::nt::CreateInstance();

// connect client and server
wpi::nt::StartServer(server, "bench.json", "127.0.0.1", 10000);
wpi::nt::StartServer(server, "bench.json", "127.0.0.1", "", 10000);
wpi::nt::StartClient(client, "client");
wpi::nt::SetServer(client, "127.0.0.1", 10000);

Expand Down Expand Up @@ -150,7 +150,7 @@ void bench2() {
auto server = wpi::nt::CreateInstance();

// connect client and server
wpi::nt::StartServer(server, "bench2.json", "127.0.0.1", 10000);
wpi::nt::StartServer(server, "bench2.json", "127.0.0.1", "", 10000);
wpi::nt::StartClient(client1, "client1");
wpi::nt::StartClient(client2, "client2");
wpi::nt::SetServer(client1, "127.0.0.1", 10000);
Expand Down Expand Up @@ -223,7 +223,7 @@ static std::uniform_real_distribution<double> dist;

void stress() {
auto server = wpi::nt::CreateInstance();
wpi::nt::StartServer(server, "stress.json", "127.0.0.1", 10000);
wpi::nt::StartServer(server, "stress.json", "127.0.0.1", "", 10000);
wpi::nt::SubscribeMultiple(server, {{std::string_view{}}});

using namespace std::chrono_literals;
Expand Down Expand Up @@ -344,7 +344,7 @@ void latency() {
auto server = wpi::nt::CreateInstance();

// connect client and server
wpi::nt::StartServer(server, "latency.json", "127.0.0.1", 10000);
wpi::nt::StartServer(server, "latency.json", "127.0.0.1", "", 10000);
wpi::nt::StartClient(client1, "client1");
wpi::nt::SetServer(client1, "127.0.0.1", 10000);
wpi::nt::StartClient(client2, "client2");
Expand Down
24 changes: 19 additions & 5 deletions ntcore/src/generate/main/java/NetworkTableInstance.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ public final class NetworkTableInstance implements AutoCloseable {
* @param persistFilename the name of the persist file to use
*/
public void startServer(String persistFilename) {
startServer(persistFilename, "");
startServer(persistFilename, "", "");
}

/**
Expand All @@ -901,18 +901,32 @@ public final class NetworkTableInstance implements AutoCloseable {
* @param listenAddress the address to listen on, or empty to listen on any address
*/
public void startServer(String persistFilename, String listenAddress) {
startServer(persistFilename, listenAddress, kDefaultPort);
startServer(persistFilename, listenAddress, "");
}

/**
* Starts a server using the specified filename, listening address, and port.
* Starts a server using the specified filename, listening address, and mdns service,
* using the default port.
*
* @param persistFilename the name of the persist file to use
* @param listenAddress the address to listen on, or empty to listen on any address
* @param mdnsService the mDNS service name to advertise, or empty to not advertise
*/
public void startServer(String persistFilename, String listenAddress, String mdnsService) {
startServer(persistFilename, listenAddress, mdnsService, kDefaultPort);
}

/**
* Starts a server using the specified filename, listening address, mdns service and port.
*
* @param persistFilename the name of the persist file to use
* @param listenAddress the address to listen on, or empty to listen on any address
* @param mdnsService the mDNS service name to advertise, or empty to not advertise
* @param port port to communicate over
*/
public void startServer(String persistFilename, String listenAddress, int port) {
NetworkTablesJNI.startServer(m_handle, persistFilename, listenAddress, port);
public void startServer(String persistFilename, String listenAddress, String mdnsService,
int port) {
NetworkTablesJNI.startServer(m_handle, persistFilename, listenAddress, mdnsService, port);
}

/** Stops the server if it is running. */
Expand Down
3 changes: 2 additions & 1 deletion ntcore/src/generate/main/java/NetworkTablesJNI.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,11 @@ public final class NetworkTablesJNI {
* @param inst NT instance handle.
* @param persistFilename the name of the persist file to use
* @param listenAddress the address to listen on, or empty to listen on any address
* @param mdnsService the mDNS service name to advertise, or empty to not advertise
* @param port port to communicate over
*/
public static native void startServer(
int inst, String persistFilename, String listenAddress, int port);
int inst, String persistFilename, String listenAddress, String mdnsService, int port);

/**
* Stops the server if it is running.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions ntcore/src/main/native/cpp/InstanceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,20 @@ void InstanceImpl::StopLocal() {

void InstanceImpl::StartServer(std::string_view persistFilename,
std::string_view listenAddress,
std::string_view mdnsService,
unsigned int port) {
std::scoped_lock lock{m_mutex};
if (networkMode != NT_NET_MODE_NONE) {
return;
}
m_networkServer = std::make_shared<NetworkServer>(
persistFilename, listenAddress, port, localStorage, connectionList,
logger, [this] {
persistFilename, listenAddress, mdnsService, port, localStorage,
connectionList, logger, [this](bool announcingmDNS) {
std::scoped_lock lock{m_mutex};
networkMode &= ~NT_NET_MODE_STARTING;
if (announcingmDNS) {
networkMode |= NT_NET_MODE_MDNS_ANNOUNCING;
}
});
networkMode = NT_NET_MODE_SERVER | NT_NET_MODE_STARTING;
listenerStorage.NotifyTimeSync({}, NT_EVENT_TIMESYNC, 0, 0, true);
Expand Down
3 changes: 2 additions & 1 deletion ntcore/src/main/native/cpp/InstanceImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class InstanceImpl {
void StartLocal();
void StopLocal();
void StartServer(std::string_view persistFilename,
std::string_view listenAddress, unsigned int port);
std::string_view listenAddress, std::string_view mdnsService,
unsigned int port);
void StopServer();
void StartClient(std::string_view identity);
void StopClient();
Expand Down
22 changes: 19 additions & 3 deletions ntcore/src/main/native/cpp/NetworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,19 @@ void NetworkServer::ServerConnection4::ProcessWsUpgrade() {
}

NetworkServer::NetworkServer(std::string_view persistentFilename,
std::string_view listenAddress, unsigned int port,
std::string_view listenAddress,
std::string_view mdnsService, unsigned int port,
net::ILocalStorage& localStorage,
IConnectionList& connList,
wpi::util::Logger& logger,
std::function<void()> initDone)
std::function<void(bool)> initDone)
: m_localStorage{localStorage},
m_connList{connList},
m_logger{logger},
m_initDone{std::move(initDone)},
m_persistentFilename{persistentFilename},
m_listenAddress{wpi::util::trim(listenAddress)},
m_mdnsService{wpi::util::trim(mdnsService)},
m_port{port},
m_serverImpl{logger},
m_localQueue{logger},
Expand Down Expand Up @@ -462,9 +464,23 @@ void NetworkServer::Init() {
tcp4->Listen();
}

bool announcingmDNS = false;
if (!m_mdnsService.empty()) {
m_mdnsAnnouncer.emplace(m_mdnsService, "_networktables._tcp", m_port);
if (!m_mdnsAnnouncer->HasImplementation()) {
WARN("mDNS service announcer not available; cannot announce '{}'",
m_mdnsService);
m_mdnsAnnouncer.reset();
} else {
m_mdnsAnnouncer->Start();
announcingmDNS = true;
INFO("mDNS announcing as service '{}' on port {}", m_mdnsService, m_port);
}
}

if (m_initDone) {
DEBUG4("NetworkServer initDone()");
m_initDone();
m_initDone(announcingmDNS);
m_initDone = nullptr;
}
}
Expand Down
13 changes: 9 additions & 4 deletions ntcore/src/main/native/cpp/NetworkServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <atomic>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand All @@ -15,6 +16,7 @@
#include "net/Message.hpp"
#include "server/ServerImpl.hpp"
#include "wpi/net/EventLoopRunner.hpp"
#include "wpi/net/MulticastServiceAnnouncer.h"
#include "wpi/net/uv/Async.hpp"
#include "wpi/net/uv/Idle.hpp"
#include "wpi/net/uv/Timer.hpp"
Expand All @@ -35,9 +37,10 @@ class IConnectionList;
class NetworkServer {
public:
NetworkServer(std::string_view persistentFilename,
std::string_view listenAddress, unsigned int port,
net::ILocalStorage& localStorage, IConnectionList& connList,
wpi::util::Logger& logger, std::function<void()> initDone);
std::string_view listenAddress, std::string_view mdnsService,
unsigned int port, net::ILocalStorage& localStorage,
IConnectionList& connList, wpi::util::Logger& logger,
std::function<void(bool)> initDone);
~NetworkServer();

void FlushLocal();
Expand All @@ -57,13 +60,15 @@ class NetworkServer {
net::ILocalStorage& m_localStorage;
IConnectionList& m_connList;
wpi::util::Logger& m_logger;
std::function<void()> m_initDone;
std::function<void(bool)> m_initDone;
std::string m_persistentData;
std::string m_persistentFilename;
std::string m_listenAddress;
std::string m_mdnsService;
unsigned int m_port;

// used only from loop
std::optional<wpi::net::MulticastServiceAnnouncer> m_mdnsAnnouncer;
std::shared_ptr<wpi::net::uv::Timer> m_readLocalTimer;
std::shared_ptr<wpi::net::uv::Timer> m_savePersistentTimer;
std::shared_ptr<wpi::net::uv::Async<>> m_flushLocal;
Expand Down
11 changes: 8 additions & 3 deletions ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,12 +1178,12 @@ Java_org_wpilib_networktables_NetworkTablesJNI_stopLocal
/*
* Class: org_wpilib_networktables_NetworkTablesJNI
* Method: startServer
* Signature: (ILjava/lang/String;Ljava/lang/String;I)V
* Signature: (ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_networktables_NetworkTablesJNI_startServer
(JNIEnv* env, jclass, jint inst, jstring persistFilename,
jstring listenAddress, jint port)
jstring listenAddress, jstring mdnsService, jint port)
{
if (!persistFilename) {
nullPointerEx.Throw(env, "persistFilename cannot be null");
Expand All @@ -1193,8 +1193,13 @@ Java_org_wpilib_networktables_NetworkTablesJNI_startServer
nullPointerEx.Throw(env, "listenAddress cannot be null");
return;
}
if (!mdnsService) {
nullPointerEx.Throw(env, "mdnsService cannot be null");
return;
}
wpi::nt::StartServer(inst, JStringRef{env, persistFilename}.str(),
JStringRef{env, listenAddress}.c_str(), port);
JStringRef{env, listenAddress}.c_str(),
JStringRef{env, mdnsService}.c_str(), port);
}

/*
Expand Down
5 changes: 3 additions & 2 deletions ntcore/src/main/native/cpp/ntcore_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,10 @@ void NT_StopLocal(NT_Inst inst) {

void NT_StartServer(NT_Inst inst, const struct WPI_String* persist_filename,
const struct WPI_String* listen_address,
unsigned int port) {
const struct WPI_String* mdns_service, unsigned int port) {
wpi::nt::StartServer(inst, wpi::util::to_string_view(persist_filename),
wpi::util::to_string_view(listen_address), port);
wpi::util::to_string_view(listen_address),
wpi::util::to_string_view(mdns_service), port);
}

void NT_StopServer(NT_Inst inst) {
Expand Down
5 changes: 3 additions & 2 deletions ntcore/src/main/native/cpp/ntcore_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,10 @@ void StopLocal(NT_Inst inst) {
}

void StartServer(NT_Inst inst, std::string_view persist_filename,
std::string_view listen_address, unsigned int port) {
std::string_view listen_address, std::string_view mdns_service,
unsigned int port) {
if (auto ii = InstanceImpl::GetTyped(inst, Handle::kInstance)) {
ii->StartServer(persist_filename, listen_address, port);
ii->StartServer(persist_filename, listen_address, mdns_service, port);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,12 +607,17 @@ class NetworkTableInstance final {
* @param listen_address the address to listen on, or an empty string to
* listen on any address. (UTF-8 string, null
* terminated)
* @param mdns_service the mDNS service name to announce, or an empty
* string to not announce via mDNS (UTF-8 string,
* null terminated)
* @param port port to communicate over
*/
void StartServer(std::string_view persist_filename = "networktables.json",
const char* listen_address = "",
const char* mdns_service = "",
unsigned int port = kDefaultPort) {
::wpi::nt::StartServer(m_handle, persist_filename, listen_address, port);
::wpi::nt::StartServer(m_handle, persist_filename, listen_address,
mdns_service, port);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion ntcore/src/main/native/include/wpi/nt/ntcore_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ enum NT_NetworkMode {
NT_NET_MODE_CLIENT = 0x04, /* running in client mode */
NT_NET_MODE_STARTING = 0x08, /* flag for starting (either client or server) */
NT_NET_MODE_LOCAL = 0x10, /* running in local-only mode */
NT_NET_MODE_MDNS_ANNOUNCING = 0x20 /* mDNS is enabled and announcing */
};

/** Event notification flags. */
Expand Down Expand Up @@ -1116,10 +1117,14 @@ void NT_StopLocal(NT_Inst inst);
* @param listen_address the address to listen on, or an empty string to
* listen on any address. (UTF-8 string, null
* terminated)
* @param mdns_service the mDNS service name to advertise, or an empty
* string to not advertise via mDNS. (UTF-8 string,
* null terminated)
* @param port port to communicate over
*/
void NT_StartServer(NT_Inst inst, const struct WPI_String* persist_filename,
const struct WPI_String* listen_address, unsigned int port);
const struct WPI_String* listen_address,
const struct WPI_String* mdns_service, unsigned int port);

/**
* Stops the server if it is running.
Expand Down
Loading
Loading