Class NetworkTableInstance
- All Implemented Interfaces:
AutoCloseable
public final class NetworkTableInstance extends Object implements AutoCloseable
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.
Additional instances can be created with the create()
function. A reference must be
kept to the NetworkTableInstance returned by this function to keep it from being garbage
collected.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
NetworkTableInstance.NetworkMode
Client/server mode flag values (as returned bygetNetworkMode()
). -
Field Summary
Fields Modifier and Type Field Description static int
kDefaultPort3
The default port that network tables operates on for NT3.static int
kDefaultPort4
The default port that network tables operates on for NT4. -
Method Summary
Modifier and Type Method Description int
addConnectionListener(boolean immediateNotify, Consumer<NetworkTableEvent> listener)
Add a connection listener.int
addListener(MultiSubscriber subscriber, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> listener)
Add a listener for changes on a subscriber.int
addListener(NetworkTableEntry entry, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> listener)
Add a listener for changes on an entry.int
addListener(Subscriber subscriber, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> listener)
Add a listener for changes on a subscriber.int
addListener(Topic topic, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> listener)
Add a listener for changes on a particular topic.int
addListener(String[] prefixes, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> listener)
Add a listener for changes to topics with names that start with any of the given prefixes.int
addLogger(int minLevel, int maxLevel, Consumer<NetworkTableEvent> func)
Add logger callback function.void
close()
Destroys the instance (if created bycreate()
).static NetworkTableInstance
create()
Create an instance.boolean
equals(Object other)
void
flush()
Flushes all updated values immediately to the network.void
flushLocal()
Flushes all updated values immediately to the local client/server.BooleanArrayTopic
getBooleanArrayTopic(String name)
Get boolean[] topic.BooleanTopic
getBooleanTopic(String name)
Get boolean topic.ConnectionInfo[]
getConnections()
Gets information on the currently established network connections.static NetworkTableInstance
getDefault()
Get global default instance.DoubleArrayTopic
getDoubleArrayTopic(String name)
Get double[] topic.DoubleTopic
getDoubleTopic(String name)
Get double topic.NetworkTableEntry
getEntry(String name)
Gets the entry for a key.FloatArrayTopic
getFloatArrayTopic(String name)
Get float[] topic.FloatTopic
getFloatTopic(String name)
Get float topic.int
getHandle()
Gets the native handle for the instance.IntegerArrayTopic
getIntegerArrayTopic(String name)
Get long[] topic.IntegerTopic
getIntegerTopic(String name)
Get long topic.EnumSet<NetworkTableInstance.NetworkMode>
getNetworkMode()
Get the current network mode.RawTopic
getRawTopic(String name)
Get byte[] topic.StringArrayTopic
getStringArrayTopic(String name)
Get String[] topic.StringTopic
getStringTopic(String name)
Get String topic.NetworkTable
getTable(String key)
Gets the table with the specified key.Topic
getTopic(String name)
Get (generic) topic.TopicInfo[]
getTopicInfo()
Get information about all topics.TopicInfo[]
getTopicInfo(String prefix)
Get information about topics starting with the given prefix.TopicInfo[]
getTopicInfo(String prefix, int types)
Get information about topics starting with the given prefix.TopicInfo[]
getTopicInfo(String prefix, String[] types)
Get information about topics starting with the given prefix.Topic[]
getTopics()
Get all published topics.Topic[]
getTopics(String prefix)
Get published topics starting with the given prefix.Topic[]
getTopics(String prefix, int types)
Get published topics starting with the given prefix.Topic[]
getTopics(String prefix, String[] types)
Get published topics starting with the given prefix.int
hashCode()
boolean
isConnected()
Return whether or not the instance is connected to another node.boolean
isValid()
Determines if the native handle is valid.void
removeListener(int listener)
Remove a connection listener.void
setServer(String serverName)
Sets server address and port for client (without restarting client).void
setServer(String[] serverNames)
Sets server addresses and port for client (without restarting client).void
setServer(String[] serverNames, int port)
Sets server addresses and port for client (without restarting client).void
setServer(String[] serverNames, int[] ports)
Sets server addresses and ports for client (without restarting client).void
setServer(String serverName, int port)
Sets server address and port for client (without restarting client).void
setServerTeam(int team)
Sets server addresses and port for client (without restarting client).void
setServerTeam(int team, int port)
Sets server addresses and port for client (without restarting client).void
startClient3(String identity)
Starts a NT3 client.void
startClient4(String identity)
Starts a NT4 client.int
startConnectionDataLog(DataLog log, String name)
Starts logging connection changes to a DataLog.void
startDSClient()
Starts requesting server address from Driver Station.void
startDSClient(int port)
Starts requesting server address from Driver Station.int
startEntryDataLog(DataLog log, String prefix, String logPrefix)
Starts logging entry changes to a DataLog.void
startLocal()
Starts local-only operation.void
startServer()
Starts a server using the networktables.json as the persistent file, using the default listening address and port.void
startServer(String persistFilename)
Starts a server using the specified persistent filename, using the default listening address and port.void
startServer(String persistFilename, String listenAddress)
Starts a server using the specified filename and listening address, using the default port.void
startServer(String persistFilename, String listenAddress, int port3)
Starts a server using the specified filename, listening address, and port.void
startServer(String persistFilename, String listenAddress, int port3, int port4)
Starts a server using the specified filename, listening address, and port.void
stopClient()
Stops the client if it is running.static void
stopConnectionDataLog(int logger)
Stops logging connection changes to a DataLog.void
stopDSClient()
Stops requesting server address from Driver Station.static void
stopEntryDataLog(int logger)
Stops logging entry changes to a DataLog.void
stopLocal()
Stops local-only operation.void
stopServer()
Stops the server if it is running.boolean
waitForListenerQueue(double timeout)
Wait for the listener queue to be empty.
-
Field Details
-
kDefaultPort3
The default port that network tables operates on for NT3.- See Also:
- Constant Field Values
-
kDefaultPort4
The default port that network tables operates on for NT4.- See Also:
- Constant Field Values
-
-
Method Details
-
close
Destroys the instance (if created bycreate()
).- Specified by:
close
in interfaceAutoCloseable
-
isValid
Determines if the native handle is valid.- Returns:
- True if the native handle is valid, false otherwise.
-
getDefault
Get global default instance.- Returns:
- Global default instance
-
create
Create an instance. Note: A reference to the returned instance must be retained to ensure the instance is not garbage collected.- Returns:
- Newly created instance
-
getHandle
Gets the native handle for the instance.- Returns:
- Native handle
-
getTopic
Get (generic) topic.- Parameters:
name
- topic name- Returns:
- Topic
-
getBooleanTopic
Get boolean topic.- Parameters:
name
- topic name- Returns:
- BooleanTopic
-
getIntegerTopic
Get long topic.- Parameters:
name
- topic name- Returns:
- IntegerTopic
-
getFloatTopic
Get float topic.- Parameters:
name
- topic name- Returns:
- FloatTopic
-
getDoubleTopic
Get double topic.- Parameters:
name
- topic name- Returns:
- DoubleTopic
-
getStringTopic
Get String topic.- Parameters:
name
- topic name- Returns:
- StringTopic
-
getRawTopic
Get byte[] topic.- Parameters:
name
- topic name- Returns:
- RawTopic
-
getBooleanArrayTopic
Get boolean[] topic.- Parameters:
name
- topic name- Returns:
- BooleanArrayTopic
-
getIntegerArrayTopic
Get long[] topic.- Parameters:
name
- topic name- Returns:
- IntegerArrayTopic
-
getFloatArrayTopic
Get float[] topic.- Parameters:
name
- topic name- Returns:
- FloatArrayTopic
-
getDoubleArrayTopic
Get double[] topic.- Parameters:
name
- topic name- Returns:
- DoubleArrayTopic
-
getStringArrayTopic
Get String[] topic.- Parameters:
name
- topic name- Returns:
- StringArrayTopic
-
getTopics
Get all published topics.- Returns:
- Array of topics.
-
getTopics
Get published topics starting with the given prefix. The results are optionally filtered by string prefix to only return a subset of all topics.- Parameters:
prefix
- topic name required prefix; only topics whose name starts with this string are returned- Returns:
- Array of topic information.
-
getTopics
Get published topics starting with the given prefix. The results are optionally filtered by string prefix and data type to only return a subset of all topics.- Parameters:
prefix
- topic name required prefix; only topics whose name starts with this string are returnedtypes
- bitmask of data types; 0 is treated as a "don't care"- Returns:
- Array of topic information.
-
getTopics
Get published topics starting with the given prefix. The results are optionally filtered by string prefix and data type to only return a subset of all topics.- Parameters:
prefix
- topic name required prefix; only topics whose name starts with this string are returnedtypes
- array of data type strings- Returns:
- Array of topic information.
-
getTopicInfo
Get information about all topics.- Returns:
- Array of topic information.
-
getTopicInfo
Get information about topics starting with the given prefix. The results are optionally filtered by string prefix to only return a subset of all topics.- Parameters:
prefix
- topic name required prefix; only topics whose name starts with this string are returned- Returns:
- Array of topic information.
-
getTopicInfo
Get information about topics starting with the given prefix. The results are optionally filtered by string prefix and data type to only return a subset of all topics.- Parameters:
prefix
- topic name required prefix; only topics whose name starts with this string are returnedtypes
- bitmask of data types; 0 is treated as a "don't care"- Returns:
- Array of topic information.
-
getTopicInfo
Get information about topics starting with the given prefix. The results are optionally filtered by string prefix and data type to only return a subset of all topics.- Parameters:
prefix
- topic name required prefix; only topics whose name starts with this string are returnedtypes
- array of data type strings- Returns:
- Array of topic information.
-
getEntry
Gets the entry for a key.- Parameters:
name
- Key- Returns:
- Network table entry.
-
getTable
Gets the table with the specified key.- Parameters:
key
- the key name- Returns:
- The network table
-
removeListener
Remove a connection listener.- Parameters:
listener
- Listener handle to remove
-
waitForListenerQueue
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.- Parameters:
timeout
- timeout, in seconds. Set to 0 for non-blocking behavior, or a negative value to block indefinitely- Returns:
- False if timed out, otherwise true.
-
addConnectionListener
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.- Parameters:
immediateNotify
- Notify listener of all existing connectionslistener
- Listener to add- Returns:
- Listener handle
-
addListener
public int addListener(Topic topic, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> 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.
- Parameters:
topic
- TopiceventKinds
- set of event kinds to listen tolistener
- Listener function- Returns:
- Listener handle
-
addListener
public int addListener(Subscriber subscriber, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> 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.- Parameters:
subscriber
- SubscribereventKinds
- set of event kinds to listen tolistener
- Listener function- Returns:
- Listener handle
-
addListener
public int addListener(MultiSubscriber subscriber, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> 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.- Parameters:
subscriber
- SubscribereventKinds
- set of event kinds to listen tolistener
- Listener function- Returns:
- Listener handle
-
addListener
public int addListener(NetworkTableEntry entry, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> 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.- Parameters:
entry
- EntryeventKinds
- set of event kinds to listen tolistener
- Listener function- Returns:
- Listener handle
-
addListener
public int addListener(String[] prefixes, EnumSet<NetworkTableEvent.Kind> eventKinds, Consumer<NetworkTableEvent> listener)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.
- Parameters:
prefixes
- Topic name string prefixeseventKinds
- set of event kinds to listen tolistener
- Listener function- Returns:
- Listener handle
-
getNetworkMode
Get the current network mode.- Returns:
- Enum set of NetworkMode.
-
startLocal
Starts local-only operation. Prevents calls to startServer or startClient from taking effect. Has no effect if startServer or startClient has already been called. -
stopLocal
Stops local-only operation. startServer or startClient can be called after this call to start a server or client. -
startServer
Starts a server using the networktables.json as the persistent file, using the default listening address and port. -
startServer
Starts a server using the specified persistent filename, using the default listening address and port.- Parameters:
persistFilename
- the name of the persist file to use
-
startServer
Starts a server using the specified filename and listening address, using the default port.- Parameters:
persistFilename
- the name of the persist file to uselistenAddress
- the address to listen on, or empty to listen on any address
-
startServer
Starts a server using the specified filename, listening address, and port.- Parameters:
persistFilename
- the name of the persist file to uselistenAddress
- the address to listen on, or empty to listen on any addressport3
- port to communicate over (NT3)
-
startServer
Starts a server using the specified filename, listening address, and port.- Parameters:
persistFilename
- the name of the persist file to uselistenAddress
- the address to listen on, or empty to listen on any addressport3
- port to communicate over (NT3)port4
- port to communicate over (NT4)
-
stopServer
Stops the server if it is running. -
startClient3
Starts a NT3 client. Use SetServer or SetServerTeam to set the server name and port.- Parameters:
identity
- network identity to advertise (cannot be empty string)
-
startClient4
Starts a NT4 client. Use SetServer or SetServerTeam to set the server name and port.- Parameters:
identity
- network identity to advertise (cannot be empty string)
-
stopClient
Stops the client if it is running. -
setServer
Sets server address and port for client (without restarting client). Changes the port to the default port.- Parameters:
serverName
- server name
-
setServer
Sets server address and port for client (without restarting client).- Parameters:
serverName
- server nameport
- port to communicate over (0=default)
-
setServer
Sets server addresses and port for client (without restarting client). Changes the port to the default port. The client will attempt to connect to each server in round robin fashion.- Parameters:
serverNames
- array of server names
-
setServer
Sets server addresses and port for client (without restarting client). The client will attempt to connect to each server in round robin fashion.- Parameters:
serverNames
- array of server namesport
- port to communicate over (0=default)
-
setServer
Sets server addresses and ports for client (without restarting client). The client will attempt to connect to each server in round robin fashion.- Parameters:
serverNames
- array of server namesports
- array of port numbers (0=default)
-
setServerTeam
Sets server addresses and port for client (without restarting client). Changes the port to the default port. The client will attempt to connect to each server in round robin fashion.- Parameters:
team
- team number
-
setServerTeam
Sets server addresses and port for client (without restarting client). Connects using commonly known robot addresses for the specified team.- Parameters:
team
- team numberport
- port to communicate over (0=default)
-
startDSClient
Starts requesting server address from Driver Station. This connects to the Driver Station running on localhost to obtain the server IP address, and connects with the default port. -
startDSClient
Starts requesting server address from Driver Station. This connects to the Driver Station running on localhost to obtain the server IP address.- Parameters:
port
- server port to use in combination with IP from DS (0=default)
-
stopDSClient
Stops requesting server address from Driver Station. -
flushLocal
Flushes all updated values immediately to the local client/server. This does not flush to the network. -
flush
Flushes all updated values immediately to the network. Note: This is rate-limited to protect the network from flooding. This is primarily useful for synchronizing network updates with user code. -
getConnections
Gets information on the currently established network connections. If operating as a client, this will return either zero or one values.- Returns:
- array of connection information
-
isConnected
Return whether or not the instance is connected to another node.- Returns:
- True if connected.
-
startEntryDataLog
Starts logging entry changes to a DataLog.- Parameters:
log
- data log object; lifetime must extend until StopEntryDataLog is called or the instance is destroyedprefix
- only store entries with names that start with this prefix; the prefix is not included in the data log entry namelogPrefix
- prefix to add to data log entry names- Returns:
- Data logger handle
-
stopEntryDataLog
Stops logging entry changes to a DataLog.- Parameters:
logger
- data logger handle
-
startConnectionDataLog
Starts logging connection changes to a DataLog.- Parameters:
log
- data log object; lifetime must extend until StopConnectionDataLog is called or the instance is destroyedname
- data log entry name- Returns:
- Data logger handle
-
stopConnectionDataLog
Stops logging connection changes to a DataLog.- Parameters:
logger
- data logger handle
-
addLogger
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.- Parameters:
minLevel
- minimum log levelmaxLevel
- maximum log levelfunc
- callback function- Returns:
- Listener handle
-
equals
-
hashCode
-