WPILibC++ 2023.4.3
|
NetworkTables Instance. More...
#include <networktables/NetworkTableInstance.h>
Public Types | |
enum | NetworkMode { kNetModeNone = NT_NET_MODE_NONE , kNetModeServer = NT_NET_MODE_SERVER , kNetModeClient3 = NT_NET_MODE_CLIENT3 , kNetModeClient4 = NT_NET_MODE_CLIENT4 , kNetModeLocal = NT_NET_MODE_LOCAL } |
Client/server mode flag values (as returned by GetNetworkMode()). More... | |
enum | LogLevel { kLogCritical = NT_LOG_CRITICAL , kLogError = NT_LOG_ERROR , kLogWarning = NT_LOG_WARNING , kLogInfo = NT_LOG_INFO , kLogDebug = NT_LOG_DEBUG , kLogDebug1 = NT_LOG_DEBUG1 , kLogDebug2 = NT_LOG_DEBUG2 , kLogDebug3 = NT_LOG_DEBUG3 , kLogDebug4 = NT_LOG_DEBUG4 } |
Logging levels (as used by SetLogger()). More... | |
Public Member Functions | |
NetworkTableInstance () noexcept | |
Construct invalid instance. More... | |
NetworkTableInstance (NT_Inst inst) noexcept | |
Construct from native handle. More... | |
operator bool () const | |
Determines if the native handle is valid. More... | |
NT_Inst | GetHandle () const |
Gets the native handle for the entry. More... | |
Topic | GetTopic (std::string_view name) const |
Gets a "generic" (untyped) topic. More... | |
BooleanTopic | GetBooleanTopic (std::string_view name) const |
Gets a boolean topic. More... | |
IntegerTopic | GetIntegerTopic (std::string_view name) const |
Gets an integer topic. More... | |
FloatTopic | GetFloatTopic (std::string_view name) const |
Gets a float topic. More... | |
DoubleTopic | GetDoubleTopic (std::string_view name) const |
Gets a double topic. More... | |
StringTopic | GetStringTopic (std::string_view name) const |
Gets a string topic. More... | |
RawTopic | GetRawTopic (std::string_view name) const |
Gets a raw topic. More... | |
BooleanArrayTopic | GetBooleanArrayTopic (std::string_view name) const |
Gets a boolean array topic. More... | |
IntegerArrayTopic | GetIntegerArrayTopic (std::string_view name) const |
Gets an integer array topic. More... | |
FloatArrayTopic | GetFloatArrayTopic (std::string_view name) const |
Gets a float array topic. More... | |
DoubleArrayTopic | GetDoubleArrayTopic (std::string_view name) const |
Gets a double array topic. More... | |
StringArrayTopic | GetStringArrayTopic (std::string_view name) const |
Gets a string array topic. More... | |
std::vector< Topic > | GetTopics () |
Get Published Topics. More... | |
std::vector< Topic > | GetTopics (std::string_view prefix) |
Get Published Topics. More... | |
std::vector< Topic > | GetTopics (std::string_view prefix, unsigned int types) |
Get Published Topics. More... | |
std::vector< Topic > | GetTopics (std::string_view prefix, std::span< std::string_view > types) |
Get Published Topics. More... | |
std::vector< TopicInfo > | GetTopicInfo () |
Get Topic Information about multiple topics. More... | |
std::vector< TopicInfo > | GetTopicInfo (std::string_view prefix) |
Get Topic Information about multiple topics. More... | |
std::vector< TopicInfo > | GetTopicInfo (std::string_view prefix, unsigned int types) |
Get Topic Information about multiple topics. More... | |
std::vector< TopicInfo > | GetTopicInfo (std::string_view prefix, std::span< std::string_view > types) |
Get Topic Information about multiple topics. More... | |
NetworkTableEntry | GetEntry (std::string_view name) |
Gets the entry for a key. More... | |
std::shared_ptr< NetworkTable > | GetTable (std::string_view key) const |
Gets the table with the specified key. More... | |
bool | operator== (const NetworkTableInstance &) const =default |
Equality operator. More... | |
Client/Server Functions | |
unsigned int | GetNetworkMode () const |
Get the current network mode. More... | |
void | StartLocal () |
Starts local-only operation. More... | |
void | StopLocal () |
Stops local-only operation. More... | |
void | StartServer (std::string_view persist_filename="networktables.json", const char *listen_address="", unsigned int port3=kDefaultPort3, unsigned int port4=kDefaultPort4) |
Starts a server using the specified filename, listening address, and port. More... | |
void | StopServer () |
Stops the server if it is running. More... | |
void | StartClient3 (std::string_view identity) |
Starts a NT3 client. More... | |
void | StartClient4 (std::string_view identity) |
Starts a NT4 client. More... | |
void | StopClient () |
Stops the client if it is running. More... | |
void | SetServer (const char *server_name, unsigned int port=0) |
Sets server address and port for client (without restarting client). More... | |
void | SetServer (std::span< const std::pair< std::string_view, unsigned int > > servers) |
Sets server addresses and ports for client (without restarting client). More... | |
void | SetServer (std::span< const std::string_view > servers, unsigned int port=0) |
Sets server addresses and port for client (without restarting client). More... | |
void | SetServerTeam (unsigned int team, unsigned int port=0) |
Sets server addresses and port for client (without restarting client). More... | |
void | Disconnect () |
Disconnects the client if it's running and connected. More... | |
void | StartDSClient (unsigned int port=0) |
Starts requesting server address from Driver Station. More... | |
void | StopDSClient () |
Stops requesting server address from Driver Station. More... | |
void | FlushLocal () const |
Flushes all updated values immediately to the local client/server. More... | |
void | Flush () const |
Flushes all updated values immediately to the network. More... | |
std::vector< ConnectionInfo > | GetConnections () const |
Get information on the currently established network connections. More... | |
bool | IsConnected () const |
Return whether or not the instance is connected to another node. More... | |
std::optional< int64_t > | GetServerTimeOffset () const |
Get the time offset between server time and local time. More... | |
Logger Functions | |
NT_Listener | AddLogger (unsigned int minLevel, unsigned int maxLevel, ListenerCallback func) |
Add logger callback function. More... | |
Static Public Member Functions | |
static NetworkTableInstance | GetDefault () |
Get global default instance. More... | |
static NetworkTableInstance | Create () |
Create an instance. More... | |
static void | Destroy (NetworkTableInstance &inst) |
Destroys an instance (note: this has global effect). More... | |
Static Public Attributes | |
static constexpr unsigned int | kDefaultPort3 = NT_DEFAULT_PORT3 |
The default port that network tables operates on for NT3. More... | |
static constexpr unsigned int | kDefaultPort4 = NT_DEFAULT_PORT4 |
The default port that network tables operates on for NT4. More... | |
Listener Functions | |
bool | WaitForListenerQueue (double timeout) |
Wait for the listener queue to be empty. More... | |
NT_Listener | AddConnectionListener (bool immediate_notify, ListenerCallback callback) const |
Add a connection listener. More... | |
NT_Listener | AddTimeSyncListener (bool immediate_notify, ListenerCallback callback) const |
Add a time synchronization listener. More... | |
NT_Listener | AddListener (Topic topic, unsigned int eventMask, ListenerCallback listener) |
Add a listener for changes on a particular topic. More... | |
NT_Listener | AddListener (Subscriber &subscriber, unsigned int eventMask, ListenerCallback listener) |
Add a listener for changes on a subscriber. More... | |
NT_Listener | AddListener (MultiSubscriber &subscriber, int eventMask, ListenerCallback listener) |
Add a listener for changes on a subscriber. More... | |
NT_Listener | AddListener (const NetworkTableEntry &entry, int eventMask, ListenerCallback listener) |
Add a listener for changes on an entry. More... | |
NT_Listener | AddListener (std::span< const std::string_view > prefixes, int eventMask, ListenerCallback listener) |
Add a listener for changes to topics with names that start with any of the given prefixes. More... | |
static void | RemoveListener (NT_Listener listener) |
Remove a listener. More... | |
Data Logger Functions | |
NT_DataLogger | StartEntryDataLog (wpi::log::DataLog &log, std::string_view prefix, std::string_view logPrefix) |
Starts logging entry changes to a DataLog. More... | |
NT_ConnectionDataLogger | StartConnectionDataLog (wpi::log::DataLog &log, std::string_view name) |
Starts logging connection changes to a DataLog. More... | |
static void | StopEntryDataLog (NT_DataLogger logger) |
Stops logging entry changes to a DataLog. More... | |
static void | StopConnectionDataLog (NT_ConnectionDataLogger logger) |
Stops logging connection changes to a DataLog. More... | |
NetworkTables Instance.
Instances are completely independent from each other. Table operations on one instance will not be visible to other instances unless the instances are connected via the network. The main limitation on instances is that you cannot have two servers on the same network port. The main utility of instances is for unit testing, but they can also enable one program to connect to two different NetworkTables networks.
The global "default" instance (as returned by GetDefault()) is always available, and is intended for the common case when there is only a single NetworkTables instance being used in the program. The default instance cannot be destroyed.
Additional instances can be created with the Create() function. Instances are not reference counted or RAII. Instead, they must be explicitly destroyed (with Destroy()).
Logging levels (as used by SetLogger()).
Enumerator | |
---|---|
kLogCritical | |
kLogError | |
kLogWarning | |
kLogInfo | |
kLogDebug | |
kLogDebug1 | |
kLogDebug2 | |
kLogDebug3 | |
kLogDebug4 |
Client/server mode flag values (as returned by GetNetworkMode()).
This is a bitmask.
Enumerator | |
---|---|
kNetModeNone | |
kNetModeServer | |
kNetModeClient3 | |
kNetModeClient4 | |
kNetModeLocal |
|
inlinenoexcept |
Construct invalid instance.
|
inlineexplicitnoexcept |
Construct from native handle.
inst | Native handle |
|
inline |
Add a connection listener.
The callback function is called asynchronously on a separate thread, so it's important to use synchronization or atomics when accessing any shared state from the callback function.
immediate_notify | notify listener of all existing connections |
callback | listener to add |
NT_Listener nt::NetworkTableInstance::AddListener | ( | const NetworkTableEntry & | entry, |
int | eventMask, | ||
ListenerCallback | listener | ||
) |
Add a listener for changes on an entry.
The callback function is called asynchronously on a separate thread, so it's important to use synchronization or atomics when accessing any shared state from the callback function.
entry | Entry |
eventMask | Bitmask of EventFlags values |
listener | Listener function |
NT_Listener nt::NetworkTableInstance::AddListener | ( | MultiSubscriber & | subscriber, |
int | eventMask, | ||
ListenerCallback | listener | ||
) |
Add a listener for changes on a subscriber.
The callback function is called asynchronously on a separate thread, so it's important to use synchronization or atomics when accessing any shared state from the callback function. This does NOT keep the subscriber active.
subscriber | Subscriber |
eventMask | Bitmask of EventFlags values |
listener | Listener function |
|
inline |
Add a listener for changes to topics with names that start with any of the given prefixes.
The callback function is called asynchronously on a separate thread, so it's important to use synchronization or atomics when accessing any shared state from the callback function.
This creates a corresponding internal subscriber with the lifetime of the listener.
prefixes | Topic name string prefixes |
eventMask | Bitmask of EventFlags values |
listener | Listener function |
NT_Listener nt::NetworkTableInstance::AddListener | ( | Subscriber & | subscriber, |
unsigned int | eventMask, | ||
ListenerCallback | listener | ||
) |
Add a listener for changes on a subscriber.
The callback function is called asynchronously on a separate thread, so it's important to use synchronization or atomics when accessing any shared state from the callback function. This does NOT keep the subscriber active.
subscriber | Subscriber |
eventMask | Bitmask of EventFlags values |
listener | Listener function |
NT_Listener nt::NetworkTableInstance::AddListener | ( | Topic | topic, |
unsigned int | eventMask, | ||
ListenerCallback | listener | ||
) |
Add a listener for changes on a particular topic.
The callback function is called asynchronously on a separate thread, so it's important to use synchronization or atomics when accessing any shared state from the callback function.
This creates a corresponding internal subscriber with the lifetime of the listener.
topic | Topic |
eventMask | Bitmask of EventFlags values |
listener | Listener function |
|
inline |
Add logger callback function.
By default, log messages are sent to stderr; this function sends log messages with the specified levels to the provided callback function instead. The callback function will only be called for log messages with level greater than or equal to minLevel and less than or equal to maxLevel; messages outside this range will be silently ignored.
minLevel | minimum log level |
maxLevel | maximum log level |
func | callback function |
|
inline |
Add a time synchronization listener.
The callback function is called asynchronously on a separate thread, so it's important to use synchronization or atomics when accessing any shared state from the callback function.
immediate_notify | notify listener of current time synchronization value |
callback | listener to add |
|
inlinestatic |
Create an instance.
|
inlinestatic |
Destroys an instance (note: this has global effect).
inst | Instance |
|
inline |
Disconnects the client if it's running and connected.
This will automatically start reconnection attempts to the current server list.
|
inline |
Flushes all updated values immediately to the network.
|
inline |
Flushes all updated values immediately to the local client/server.
This does not flush to the network.
BooleanArrayTopic nt::NetworkTableInstance::GetBooleanArrayTopic | ( | std::string_view | name | ) | const |
BooleanTopic nt::NetworkTableInstance::GetBooleanTopic | ( | std::string_view | name | ) | const |
|
inline |
Get information on the currently established network connections.
If operating as a client, this will return either zero or one values.
|
inlinestatic |
Get global default instance.
DoubleArrayTopic nt::NetworkTableInstance::GetDoubleArrayTopic | ( | std::string_view | name | ) | const |
DoubleTopic nt::NetworkTableInstance::GetDoubleTopic | ( | std::string_view | name | ) | const |
|
inline |
Gets the entry for a key.
name | Key |
FloatArrayTopic nt::NetworkTableInstance::GetFloatArrayTopic | ( | std::string_view | name | ) | const |
FloatTopic nt::NetworkTableInstance::GetFloatTopic | ( | std::string_view | name | ) | const |
|
inline |
Gets the native handle for the entry.
IntegerArrayTopic nt::NetworkTableInstance::GetIntegerArrayTopic | ( | std::string_view | name | ) | const |
IntegerTopic nt::NetworkTableInstance::GetIntegerTopic | ( | std::string_view | name | ) | const |
|
inline |
Get the current network mode.
RawTopic nt::NetworkTableInstance::GetRawTopic | ( | std::string_view | name | ) | const |
|
inline |
Get the time offset between server time and local time.
Add this value to local time to get the estimated equivalent server time. In server mode, this always returns 0. In client mode, this returns the time offset only if the client and server are connected and have exchanged synchronization messages. Note the time offset may change over time as it is periodically updated; to receive updates as events, add a listener to the "time sync" event.
StringArrayTopic nt::NetworkTableInstance::GetStringArrayTopic | ( | std::string_view | name | ) | const |
StringTopic nt::NetworkTableInstance::GetStringTopic | ( | std::string_view | name | ) | const |
std::shared_ptr< NetworkTable > nt::NetworkTableInstance::GetTable | ( | std::string_view | key | ) | const |
Gets the table with the specified key.
key | the key name |
Topic nt::NetworkTableInstance::GetTopic | ( | std::string_view | name | ) | const |
|
inline |
Get Topic Information about multiple topics.
Returns an array of topic information (handle, name, type, and properties).
|
inline |
Get Topic Information about multiple topics.
Returns an array of topic information (handle, name, type, and properties). The results are filtered by string prefix to only return a subset of all topics.
prefix | name required prefix; only topics whose name starts with this string are returned |
|
inline |
Get Topic Information about multiple topics.
Returns an array of topic information (handle, name, type, and properties). The results are filtered by string prefix and type to only return a subset of all topics.
prefix | name required prefix; only topics whose name starts with this string are returned |
types | array of type strings |
|
inline |
Get Topic Information about multiple topics.
Returns an array of topic information (handle, name, type, and properties). The results are filtered by string prefix and type to only return a subset of all topics.
prefix | name required prefix; only topics whose name starts with this string are returned |
types | bitmask of NT_Type values; 0 is treated specially as a "don't care" |
|
inline |
Get Published Topics.
Returns an array of topics.
|
inline |
Get Published Topics.
Returns an array of topics. The results are filtered by string prefix to only return a subset of all topics.
prefix | name required prefix; only topics whose name starts with this string are returned |
|
inline |
Get Published Topics.
Returns an array of topics. The results are filtered by string prefix and type to only return a subset of all topics.
prefix | name required prefix; only topics whose name starts with this string are returned |
types | array of type strings |
|
inline |
Get Published Topics.
Returns an array of topics. The results are filtered by string prefix and type to only return a subset of all topics.
prefix | name required prefix; only topics whose name starts with this string are returned |
types | bitmask of NT_Type values; 0 is treated specially as a "don't care" |
|
inline |
Return whether or not the instance is connected to another node.
|
inlineexplicit |
Determines if the native handle is valid.
|
default |
Equality operator.
Returns true if both instances refer to the same native handle.
|
inlinestatic |
Remove a listener.
listener | Listener handle to remove |
|
inline |
Sets server address and port for client (without restarting client).
server_name | server name (UTF-8 string, null terminated) |
port | port to communicate over (0 = default) |
|
inline |
Sets server addresses and ports for client (without restarting client).
The client will attempt to connect to each server in round robin fashion.
servers | array of server address and port pairs |
void nt::NetworkTableInstance::SetServer | ( | std::span< const std::string_view > | servers, |
unsigned int | port = 0 |
||
) |
Sets server addresses and port for client (without restarting client).
The client will attempt to connect to each server in round robin fashion.
servers | array of server names |
port | port to communicate over (0 = default) |
|
inline |
Sets server addresses and port for client (without restarting client).
Connects using commonly known robot addresses for the specified team.
team | team number |
port | port to communicate over (0 = default) |
|
inline |
Starts a NT3 client.
Use SetServer or SetServerTeam to set the server name and port.
identity | network identity to advertise (cannot be empty string) |
|
inline |
Starts a NT4 client.
Use SetServer or SetServerTeam to set the server name and port.
identity | network identity to advertise (cannot be empty string) |
|
inline |
Starts logging connection changes to a DataLog.
log | data log object; lifetime must extend until StopConnectionDataLog is called or the instance is destroyed |
name | data log entry name |
|
inline |
Starts requesting server address from Driver Station.
This connects to the Driver Station running on localhost to obtain the server IP address.
port | server port to use in combination with IP from DS (0 = default) |
|
inline |
Starts logging entry changes to a DataLog.
log | data log object; lifetime must extend until StopEntryDataLog is called or the instance is destroyed |
prefix | only store entries with names that start with this prefix; the prefix is not included in the data log entry name |
logPrefix | prefix to add to data log entry names |
|
inline |
Starts local-only operation.
Prevents calls to StartServer or StartClient from taking effect. Has no effect if StartServer or StartClient has already been called.
|
inline |
Starts a server using the specified filename, listening address, and port.
persist_filename | the name of the persist file to use (UTF-8 string, null terminated) |
listen_address | the address to listen on, or null to listen on any address (UTF-8 string, null terminated) |
port3 | port to communicate over (NT3) |
port4 | port to communicate over (NT4) |
|
inline |
Stops the client if it is running.
|
inlinestatic |
Stops logging connection changes to a DataLog.
logger | data logger handle |
|
inline |
Stops requesting server address from Driver Station.
|
inlinestatic |
Stops logging entry changes to a DataLog.
logger | data logger handle |
|
inline |
Stops local-only operation.
StartServer or StartClient can be called after this call to start a server or client.
|
inline |
Stops the server if it is running.
|
inline |
Wait for the listener queue to be empty.
This is primarily useful for deterministic testing. This blocks until either the listener queue is empty (e.g. there are no more events that need to be passed along to callbacks or poll queues) or the timeout expires.
timeout | timeout, in seconds. Set to 0 for non-blocking behavior, or a negative value to block indefinitely |
|
staticconstexpr |
The default port that network tables operates on for NT3.
|
staticconstexpr |
The default port that network tables operates on for NT4.