mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Implement independent instances.
Previously, most of the classes were implemented as singletons so only one
instance was possible.
This change adds an instance handle-based API. In Java, this API is located
in a different package than the old API (edu.wpi.first.networktables).
Backwards compatibility with ITable and the old NetworkTable API is largely
maintained, but a handful of classes have moved to the new package in Java
(ConnectionInfo and PersistentException), and the old JNI has been completed
replaced.
Also:
- Move SetTeam implementation to Dispatcher.
- Consistently pass time through Java and C++ Value API.
- Rename nt_Value.h to NetworkTableValue.h for consistency with Java.
- Improve documentation
- Make C++ and Java APIs more consistent
- Document RPC functions and support RPC in Java.
- Add polling features for entry and connection listeners and use them to
move callback threads to Java level.
- Remove thread start and stop hooks (as polling is available).
- Make Notifiers, RpcServer, Dispatcher, and Storage mockable.
- Set NOTIFY_NEW on immediate entry notifications.
- Make GetTable("/") and GetTable("") equivalent.
- Generate local notification for flags update when loading persistent file.
And many unit test updates/changes:
- Use InitGoogleMock instead of InitGoogleTest in test main.
- Move test printers to TestPrinter.h/cpp.
- Provide printers for StringRef, EntryNotifier, and Handle.
- StorageTest: Check notifications.
- Add entry notifier unit tests.
- Storage: Add test for incoming entry assignment.
- Update connection listener tests.
- Add entry listener unit tests.
Fixes #11, #140, #189, #190, #192, #193, #221
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* NetworkTables Connection information.
|
||||
*/
|
||||
public final class ConnectionInfo {
|
||||
/**
|
||||
* The remote identifier (as set on the remote node by
|
||||
* {@link NetworkTableInstance#setNetworkIdentity(String)}).
|
||||
*/
|
||||
public final String remote_id;
|
||||
|
||||
/**
|
||||
* The IP address of the remote node.
|
||||
*/
|
||||
public final String remote_ip;
|
||||
|
||||
/**
|
||||
* The port number of the remote node.
|
||||
*/
|
||||
public final int remote_port;
|
||||
|
||||
/**
|
||||
* The last time any update was received from the remote node (same scale as
|
||||
* returned by {@link NetworkTablesJNI#now()}).
|
||||
*/
|
||||
public final long last_update;
|
||||
|
||||
/**
|
||||
* The protocol version being used for this connection. This is in protocol
|
||||
* layer format, so 0x0200 = 2.0, 0x0300 = 3.0).
|
||||
*/
|
||||
public final int protocol_version;
|
||||
|
||||
/** Constructor.
|
||||
* This should generally only be used internally to NetworkTables.
|
||||
* @param remote_id Remote identifier
|
||||
* @param remote_ip Remote IP address
|
||||
* @param remote_port Remote port number
|
||||
* @param last_update Last time an update was received
|
||||
* @param protocol_version The protocol version used for the connection
|
||||
*/
|
||||
public ConnectionInfo(String remote_id, String remote_ip, int remote_port, long last_update, int protocol_version) {
|
||||
this.remote_id = remote_id;
|
||||
this.remote_ip = remote_ip;
|
||||
this.remote_port = remote_port;
|
||||
this.last_update = last_update;
|
||||
this.protocol_version = protocol_version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* NetworkTables Connection notification.
|
||||
*/
|
||||
public final class ConnectionNotification {
|
||||
/**
|
||||
* Listener that was triggered.
|
||||
*/
|
||||
public final int listener;
|
||||
|
||||
/**
|
||||
* True if event is due to connection being established.
|
||||
*/
|
||||
public final boolean connected;
|
||||
|
||||
/**
|
||||
* Connection information.
|
||||
*/
|
||||
public final ConnectionInfo conn;
|
||||
|
||||
/** Constructor.
|
||||
* This should generally only be used internally to NetworkTables.
|
||||
* @param inst Instance
|
||||
* @param listener Listener that was triggered
|
||||
* @param connected Connected if true
|
||||
* @param conn Connection information
|
||||
*/
|
||||
public ConnectionNotification(NetworkTableInstance inst, int listener, boolean connected, ConnectionInfo conn) {
|
||||
this.inst = inst;
|
||||
this.listener = listener;
|
||||
this.connected = connected;
|
||||
this.conn = conn;
|
||||
}
|
||||
|
||||
private final NetworkTableInstance inst;
|
||||
|
||||
public NetworkTableInstance getInstance() {
|
||||
return inst;
|
||||
}
|
||||
}
|
||||
63
src/main/java/edu/wpi/first/networktables/EntryInfo.java
Normal file
63
src/main/java/edu/wpi/first/networktables/EntryInfo.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* NetworkTables Entry information.
|
||||
*/
|
||||
public final class EntryInfo {
|
||||
/** Entry handle. */
|
||||
public final int entry;
|
||||
|
||||
/** Entry name. */
|
||||
public final String name;
|
||||
|
||||
/** Entry type. */
|
||||
public final NetworkTableType type;
|
||||
|
||||
/** Entry flags. */
|
||||
public final int flags;
|
||||
|
||||
/** Timestamp of last change to entry (type or value). */
|
||||
public final long last_change;
|
||||
|
||||
/** Constructor.
|
||||
* This should generally only be used internally to NetworkTables.
|
||||
* @param inst Instance
|
||||
* @param entry Entry handle
|
||||
* @param name Name
|
||||
* @param type Type (integer version of {@link NetworkTableType})
|
||||
* @param flags Flags
|
||||
* @param last_change Timestamp of last change
|
||||
*/
|
||||
public EntryInfo(NetworkTableInstance inst, int entry, String name, int type, int flags, long last_change) {
|
||||
this.inst = inst;
|
||||
this.entry = entry;
|
||||
this.name = name;
|
||||
this.type = NetworkTableType.getFromInt(type);
|
||||
this.flags = flags;
|
||||
this.last_change = last_change;
|
||||
}
|
||||
|
||||
/* Network table instance. */
|
||||
private final NetworkTableInstance inst;
|
||||
|
||||
/* Cached entry object. */
|
||||
private NetworkTableEntry entryObject;
|
||||
|
||||
/**
|
||||
* Get the entry as an object.
|
||||
* @return NetworkTableEntry for this entry.
|
||||
*/
|
||||
NetworkTableEntry getEntry() {
|
||||
if (entryObject == null) {
|
||||
entryObject = new NetworkTableEntry(inst, entry);
|
||||
}
|
||||
return entryObject;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* Flag values for use with entry listeners.
|
||||
*
|
||||
* The flags are a bitmask and must be OR'ed together to indicate the
|
||||
* combination of events desired to be received.
|
||||
*
|
||||
* The constants kNew, kDelete, kUpdate, and kFlags represent different events
|
||||
* that can occur to entries.
|
||||
*
|
||||
* By default, notifications are only generated for remote changes occurring
|
||||
* after the listener is created. The constants kImmediate and kLocal are
|
||||
* modifiers that cause notifications to be generated at other times.
|
||||
*/
|
||||
public interface EntryListenerFlags {
|
||||
/** Initial listener addition.
|
||||
* Set this flag to receive immediate notification of entries matching the
|
||||
* flag criteria (generally only useful when combined with kNew).
|
||||
*/
|
||||
public static final int kImmediate = 0x01;
|
||||
|
||||
/** Changed locally.
|
||||
* Set this flag to receive notification of both local changes and changes
|
||||
* coming from remote nodes. By default, notifications are only generated
|
||||
* for remote changes. Must be combined with some combination of kNew,
|
||||
* kDelete, kUpdate, and kFlags to receive notifications of those respective
|
||||
* events.
|
||||
*/
|
||||
public static final int kLocal = 0x02;
|
||||
|
||||
/** Newly created entry.
|
||||
* Set this flag to receive a notification when an entry is created.
|
||||
*/
|
||||
public static final int kNew = 0x04;
|
||||
|
||||
/** Entry was deleted.
|
||||
* Set this flag to receive a notification when an entry is deleted.
|
||||
*/
|
||||
public static final int kDelete = 0x08;
|
||||
|
||||
/** Entry's value changed.
|
||||
* Set this flag to receive a notification when an entry's value (or type)
|
||||
* changes.
|
||||
*/
|
||||
public static final int kUpdate = 0x10;
|
||||
|
||||
/** Entry's flags changed.
|
||||
* Set this flag to receive a notification when an entry's flags value
|
||||
* changes.
|
||||
*/
|
||||
public static final int kFlags = 0x20;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* NetworkTables Entry notification.
|
||||
*/
|
||||
public final class EntryNotification {
|
||||
/**
|
||||
* Listener that was triggered.
|
||||
*/
|
||||
public final int listener;
|
||||
|
||||
/**
|
||||
* Entry handle.
|
||||
*/
|
||||
public final int entry;
|
||||
|
||||
/**
|
||||
* Entry name.
|
||||
*/
|
||||
public final String name;
|
||||
|
||||
/**
|
||||
* The new value.
|
||||
*/
|
||||
public final NetworkTableValue value;
|
||||
|
||||
/**
|
||||
* Update flags. For example, {@link EntryListenerFlags#kNew} if the key did
|
||||
* not previously exist.
|
||||
*/
|
||||
public final int flags;
|
||||
|
||||
/** Constructor.
|
||||
* This should generally only be used internally to NetworkTables.
|
||||
* @param inst Instance
|
||||
* @param listener Listener that was triggered
|
||||
* @param entry Entry handle
|
||||
* @param name Entry name
|
||||
* @param value The new value
|
||||
* @param flags Update flags
|
||||
*/
|
||||
public EntryNotification(NetworkTableInstance inst, int listener, int entry, String name, NetworkTableValue value, int flags) {
|
||||
this.inst = inst;
|
||||
this.listener = listener;
|
||||
this.entry = entry;
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
/* Network table instance. */
|
||||
private final NetworkTableInstance inst;
|
||||
|
||||
/* Cached entry object. */
|
||||
NetworkTableEntry entryObject;
|
||||
|
||||
/**
|
||||
* Get the entry as an object.
|
||||
* @return NetworkTableEntry for this entry.
|
||||
*/
|
||||
public NetworkTableEntry getEntry() {
|
||||
if (entryObject == null) {
|
||||
entryObject = new NetworkTableEntry(inst, entry);
|
||||
}
|
||||
return entryObject;
|
||||
}
|
||||
}
|
||||
75
src/main/java/edu/wpi/first/networktables/LogMessage.java
Normal file
75
src/main/java/edu/wpi/first/networktables/LogMessage.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* NetworkTables log message.
|
||||
*/
|
||||
public final class LogMessage {
|
||||
/**
|
||||
* Logging levels.
|
||||
*/
|
||||
public static final int kCritical = 50;
|
||||
public static final int kError = 40;
|
||||
public static final int kWarning = 30;
|
||||
public static final int kInfo = 20;
|
||||
public static final int kDebug = 10;
|
||||
public static final int kDebug1 = 9;
|
||||
public static final int kDebug2 = 8;
|
||||
public static final int kDebug3 = 7;
|
||||
public static final int kDebug4 = 6;
|
||||
|
||||
/**
|
||||
* The logger that generated the message.
|
||||
*/
|
||||
public final int logger;
|
||||
|
||||
/**
|
||||
* Log level of the message.
|
||||
*/
|
||||
public final int level;
|
||||
|
||||
/**
|
||||
* The filename of the source file that generated the message.
|
||||
*/
|
||||
public final String filename;
|
||||
|
||||
/**
|
||||
* The line number in the source file that generated the message.
|
||||
*/
|
||||
public final int line;
|
||||
|
||||
/**
|
||||
* The message.
|
||||
*/
|
||||
public final String message;
|
||||
|
||||
/** Constructor.
|
||||
* This should generally only be used internally to NetworkTables.
|
||||
* @param inst Instance
|
||||
* @param logger Logger
|
||||
* @param level Log level
|
||||
* @param filename Filename
|
||||
* @param line Line number
|
||||
* @param message Message
|
||||
*/
|
||||
public LogMessage(NetworkTableInstance inst, int logger, int level, String filename, int line, String message) {
|
||||
this.inst = inst;
|
||||
this.logger = logger;
|
||||
this.level = level;
|
||||
this.filename = filename;
|
||||
this.line = line;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
private final NetworkTableInstance inst;
|
||||
|
||||
NetworkTableInstance getInstance() {
|
||||
return inst;
|
||||
}
|
||||
}
|
||||
283
src/main/java/edu/wpi/first/networktables/NetworkTable.java
Normal file
283
src/main/java/edu/wpi/first/networktables/NetworkTable.java
Normal file
@@ -0,0 +1,283 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* A network table that knows its subtable path.
|
||||
*/
|
||||
public final class NetworkTable {
|
||||
/**
|
||||
* The path separator for sub-tables and keys
|
||||
*
|
||||
*/
|
||||
public static final char PATH_SEPARATOR = '/';
|
||||
|
||||
private final String path;
|
||||
private final String pathWithSep;
|
||||
private final NetworkTableInstance inst;
|
||||
|
||||
public NetworkTable(NetworkTableInstance inst, String path) {
|
||||
this.path = path;
|
||||
this.pathWithSep = path + PATH_SEPARATOR;
|
||||
this.inst = inst;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the instance for the table.
|
||||
* @return Instance
|
||||
*/
|
||||
public NetworkTableInstance getInstance() { return inst; }
|
||||
|
||||
public String toString() { return "NetworkTable: " + path; }
|
||||
|
||||
private final ConcurrentMap<String, NetworkTableEntry> entries = new ConcurrentHashMap<String, NetworkTableEntry>();
|
||||
|
||||
/**
|
||||
* Gets the entry for a subkey.
|
||||
* @param key the key name
|
||||
* @return Network table entry.
|
||||
*/
|
||||
public NetworkTableEntry getEntry(String key) {
|
||||
NetworkTableEntry entry = entries.get(key);
|
||||
if (entry == null) {
|
||||
entry = inst.getEntry(pathWithSep + key);
|
||||
entries.putIfAbsent(key, entry);
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen to keys only within this table.
|
||||
* @param listener listener to add
|
||||
* @param flags {@link EntryListenerFlags} bitmask
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addEntryListener(TableEntryListener listener, int flags) {
|
||||
final int prefixLen = path.length() + 1;
|
||||
return inst.addEntryListener(pathWithSep, (event) -> {
|
||||
String relativeKey = event.name.substring(prefixLen);
|
||||
if (relativeKey.indexOf(PATH_SEPARATOR) != -1) // part of a subtable
|
||||
return;
|
||||
listener.valueChanged(this, relativeKey, event.getEntry(), event.value, event.flags);
|
||||
}, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen to a single key.
|
||||
* @param key the key name
|
||||
* @param listener listener to add
|
||||
* @param flags {@link EntryListenerFlags} bitmask
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addEntryListener(String key, TableEntryListener listener, int flags) {
|
||||
final NetworkTableEntry entry = getEntry(key);
|
||||
return inst.addEntryListener(entry, (event) -> {
|
||||
listener.valueChanged(this, key, entry, event.value, event.flags);
|
||||
}, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an entry listener.
|
||||
* @param listener listener handle
|
||||
*/
|
||||
public void removeEntryListener(int listener) {
|
||||
inst.removeEntryListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen for sub-table creation.
|
||||
* This calls the listener once for each newly created sub-table.
|
||||
* It immediately calls the listener for any existing sub-tables.
|
||||
* @param listener listener to add
|
||||
* @param localNotify notify local changes as well as remote
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addSubTableListener(TableListener listener, boolean localNotify) {
|
||||
int flags = EntryListenerFlags.kNew | EntryListenerFlags.kImmediate;
|
||||
if (localNotify)
|
||||
flags |= EntryListenerFlags.kLocal;
|
||||
|
||||
final int prefixLen = path.length() + 1;
|
||||
final NetworkTable parent = this;
|
||||
|
||||
return inst.addEntryListener(pathWithSep, new Consumer<EntryNotification>() {
|
||||
final Set<String> notifiedTables = new HashSet<String>();
|
||||
|
||||
@Override
|
||||
public void accept(EntryNotification event) {
|
||||
String relativeKey = event.name.substring(prefixLen);
|
||||
int endSubTable = relativeKey.indexOf(PATH_SEPARATOR);
|
||||
if (endSubTable == -1)
|
||||
return;
|
||||
String subTableKey = relativeKey.substring(0, endSubTable);
|
||||
if (notifiedTables.contains(subTableKey))
|
||||
return;
|
||||
notifiedTables.add(subTableKey);
|
||||
listener.tableCreated(parent, subTableKey, parent.getSubTable(subTableKey));
|
||||
}
|
||||
}, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a sub-table listener.
|
||||
* @param listener listener handle
|
||||
*/
|
||||
public void removeTableListener(int listener) {
|
||||
inst.removeEntryListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the table at the specified key. If there is no table at the
|
||||
* specified key, it will create a new table
|
||||
*
|
||||
* @param key the name of the table relative to this one
|
||||
* @return a sub table relative to this one
|
||||
*/
|
||||
public NetworkTable getSubTable(String key) {
|
||||
return new NetworkTable(inst, pathWithSep + key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the table and tells if it contains the specified key
|
||||
*
|
||||
* @param key the key to search for
|
||||
* @return true if the table as a value assigned to the given key
|
||||
*/
|
||||
public boolean containsKey(String key) {
|
||||
return getEntry(key).exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key the key to search for
|
||||
* @return true if there is a subtable with the key which contains at least
|
||||
* one key/subtable of its own
|
||||
*/
|
||||
public boolean containsSubTable(String key) {
|
||||
int[] handles = NetworkTablesJNI.getEntries(inst.getHandle(), pathWithSep + key + PATH_SEPARATOR, 0);
|
||||
return handles.length != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all keys in the table (not including sub-tables).
|
||||
* @param types bitmask of types; 0 is treated as a "don't care".
|
||||
* @return keys currently in the table
|
||||
*/
|
||||
public Set<String> getKeys(int types) {
|
||||
Set<String> keys = new HashSet<String>();
|
||||
int prefixLen = path.length() + 1;
|
||||
for (EntryInfo info : inst.getEntryInfo(pathWithSep, types)) {
|
||||
String relativeKey = info.name.substring(prefixLen);
|
||||
if (relativeKey.indexOf(PATH_SEPARATOR) != -1)
|
||||
continue;
|
||||
keys.add(relativeKey);
|
||||
// populate entries as we go
|
||||
if (entries.get(relativeKey) == null) {
|
||||
entries.putIfAbsent(relativeKey, new NetworkTableEntry(inst, info.entry));
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all keys in the table (not including sub-tables).
|
||||
* @return keys currently in the table
|
||||
*/
|
||||
public Set<String> getKeys() {
|
||||
return getKeys(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the names of all subtables in the table.
|
||||
* @return subtables currently in the table
|
||||
*/
|
||||
public Set<String> getSubTables() {
|
||||
Set<String> keys = new HashSet<String>();
|
||||
int prefixLen = path.length() + 1;
|
||||
for (EntryInfo info : inst.getEntryInfo(pathWithSep, 0)) {
|
||||
String relativeKey = info.name.substring(prefixLen);
|
||||
int endSubTable = relativeKey.indexOf(PATH_SEPARATOR);
|
||||
if (endSubTable == -1)
|
||||
continue;
|
||||
keys.add(relativeKey.substring(0, endSubTable));
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the specified key in this table. The key can
|
||||
* not be null.
|
||||
*
|
||||
* @param key the key name
|
||||
*/
|
||||
public void delete(String key) {
|
||||
getEntry(key).delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Put a value in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
boolean putValue(String key, NetworkTableValue value) {
|
||||
return getEntry(key).setValue(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
boolean setDefaultValue(String key, NetworkTableValue defaultValue) {
|
||||
return getEntry(key).setDefaultValue(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value associated with a key as an object
|
||||
*
|
||||
* @param key the key of the value to look up
|
||||
* @return the value associated with the given key, or nullptr if the key
|
||||
* does not exist
|
||||
*/
|
||||
NetworkTableValue getValue(String key) {
|
||||
return getEntry(key).getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof NetworkTable)) {
|
||||
return false;
|
||||
}
|
||||
NetworkTable other = (NetworkTable) o;
|
||||
return inst.equals(other.inst) && path.equals(other.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(inst, path);
|
||||
}
|
||||
}
|
||||
709
src/main/java/edu/wpi/first/networktables/NetworkTableEntry.java
Normal file
709
src/main/java/edu/wpi/first/networktables/NetworkTableEntry.java
Normal file
@@ -0,0 +1,709 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* NetworkTables Entry
|
||||
*/
|
||||
public final class NetworkTableEntry {
|
||||
/**
|
||||
* Flag values (as returned by {@link #getFlags()}).
|
||||
*/
|
||||
public static final int kPersistent = 0x01;
|
||||
|
||||
/**
|
||||
* Construct from native handle.
|
||||
* @param inst Instance
|
||||
* @param handle Native handle
|
||||
*/
|
||||
public NetworkTableEntry(NetworkTableInstance inst, int handle) {
|
||||
m_inst = inst;
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the native handle is valid.
|
||||
* @return True if the native handle is valid, false otherwise.
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return m_handle != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the native handle for the entry.
|
||||
* @return Native handle
|
||||
*/
|
||||
public int getHandle() {
|
||||
return m_handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the instance for the entry.
|
||||
* @return Instance
|
||||
*/
|
||||
public NetworkTableInstance getInstance() {
|
||||
return m_inst;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the entry currently exists.
|
||||
* @return True if the entry exists, false otherwise.
|
||||
*/
|
||||
public boolean exists() {
|
||||
return NetworkTablesJNI.getType(m_handle) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the entry (the key).
|
||||
* @return the entry's name
|
||||
*/
|
||||
public String getName() {
|
||||
return NetworkTablesJNI.getEntryName(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the type of the entry.
|
||||
* @return the entry's type
|
||||
*/
|
||||
public NetworkTableType getType() {
|
||||
return NetworkTableType.getFromInt(NetworkTablesJNI.getType(m_handle));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the flags.
|
||||
* @return the flags (bitmask)
|
||||
*/
|
||||
public int getFlags() {
|
||||
return NetworkTablesJNI.getEntryFlags(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last time the entry's value was changed.
|
||||
* @return Entry last change time
|
||||
*/
|
||||
public long getLastChange() {
|
||||
return NetworkTablesJNI.getEntryLastChange(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets combined information about the entry.
|
||||
* @return Entry information
|
||||
*/
|
||||
public EntryInfo getInfo() {
|
||||
return NetworkTablesJNI.getEntryInfoHandle(m_inst, m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value.
|
||||
* Returns a value with type NetworkTableType.kUnassigned if the value
|
||||
* does not exist.
|
||||
* @return the entry's value
|
||||
*/
|
||||
public NetworkTableValue getValue() {
|
||||
return NetworkTablesJNI.getValue(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a boolean. If the entry does not exist or is of
|
||||
* different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public boolean getBoolean(boolean defaultValue) {
|
||||
return NetworkTablesJNI.getBoolean(m_handle, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a double. If the entry does not exist or is of
|
||||
* different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public double getDouble(double defaultValue) {
|
||||
return NetworkTablesJNI.getDouble(m_handle, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a double. If the entry does not exist or is of
|
||||
* different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public Number getNumber(Number defaultValue) {
|
||||
return NetworkTablesJNI.getDouble(m_handle, defaultValue.doubleValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a string. If the entry does not exist or is of
|
||||
* different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public String getString(String defaultValue) {
|
||||
return NetworkTablesJNI.getString(m_handle, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a raw value (byte array). If the entry does not
|
||||
* exist or is of different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public byte[] getRaw(byte[] defaultValue) {
|
||||
return NetworkTablesJNI.getRaw(m_handle, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a boolean array. If the entry does not exist
|
||||
* or is of different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public boolean[] getBooleanArray(boolean[] defaultValue) {
|
||||
return NetworkTablesJNI.getBooleanArray(m_handle, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a boolean array. If the entry does not exist
|
||||
* or is of different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public Boolean[] getBooleanArray(Boolean[] defaultValue) {
|
||||
return NetworkTableValue.fromNative(NetworkTablesJNI.getBooleanArray(m_handle, NetworkTableValue.toNative(defaultValue)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a double array. If the entry does not exist
|
||||
* or is of different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public double[] getDoubleArray(double[] defaultValue) {
|
||||
return NetworkTablesJNI.getDoubleArray(m_handle, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a double array. If the entry does not exist
|
||||
* or is of different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public Double[] getDoubleArray(Double[] defaultValue) {
|
||||
return NetworkTableValue.fromNative(NetworkTablesJNI.getDoubleArray(m_handle, NetworkTableValue.toNative(defaultValue)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a double array. If the entry does not exist
|
||||
* or is of different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public Number[] getNumberArray(Number[] defaultValue) {
|
||||
return NetworkTableValue.fromNative(NetworkTablesJNI.getDoubleArray(m_handle, NetworkTableValue.toNative(defaultValue)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entry's value as a string array. If the entry does not exist
|
||||
* or is of different type, it will return the default value.
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the entry's value or the given default value
|
||||
*/
|
||||
public String[] getStringArray(String[] defaultValue) {
|
||||
return NetworkTablesJNI.getStringArray(m_handle, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultValue(NetworkTableValue defaultValue) {
|
||||
long time = defaultValue.getTime();
|
||||
Object o = defaultValue.getValue();
|
||||
switch (defaultValue.getType()) {
|
||||
case kBoolean:
|
||||
return NetworkTablesJNI.setDefaultBoolean(m_handle, time, ((Boolean)o).booleanValue());
|
||||
case kDouble:
|
||||
return NetworkTablesJNI.setDefaultDouble(m_handle, time, ((Number)o).doubleValue());
|
||||
case kString:
|
||||
return NetworkTablesJNI.setDefaultString(m_handle, time, (String)o);
|
||||
case kRaw:
|
||||
return NetworkTablesJNI.setDefaultRaw(m_handle, time, (byte[])o);
|
||||
case kBooleanArray:
|
||||
return NetworkTablesJNI.setDefaultBooleanArray(m_handle, time, (boolean[])o);
|
||||
case kDoubleArray:
|
||||
return NetworkTablesJNI.setDefaultDoubleArray(m_handle, time, (double[])o);
|
||||
case kStringArray:
|
||||
return NetworkTablesJNI.setDefaultStringArray(m_handle, time, (String[])o);
|
||||
case kRpc:
|
||||
// TODO
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultBoolean(boolean defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultBoolean(m_handle, 0, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultDouble(double defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultDouble(m_handle, 0, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultNumber(Number defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultDouble(m_handle, 0, defaultValue.doubleValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultString(String defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultString(m_handle, 0, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultRaw(byte[] defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultRaw(m_handle, 0, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultBooleanArray(boolean[] defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultBooleanArray(m_handle, 0, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultBooleanArray(Boolean[] defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultBooleanArray(m_handle, 0, NetworkTableValue.toNative(defaultValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultDoubleArray(double[] defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultDoubleArray(m_handle, 0, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultNumberArray(Number[] defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultDoubleArray(m_handle, 0, NetworkTableValue.toNative(defaultValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value if it does not exist.
|
||||
* @param defaultValue the default value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDefaultStringArray(String[] defaultValue) {
|
||||
return NetworkTablesJNI.setDefaultStringArray(m_handle, 0, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean setValue(NetworkTableValue value) {
|
||||
long time = value.getTime();
|
||||
Object o = value.getValue();
|
||||
switch (value.getType()) {
|
||||
case kBoolean:
|
||||
return NetworkTablesJNI.setBoolean(m_handle, time, ((Boolean)o).booleanValue(), false);
|
||||
case kDouble:
|
||||
return NetworkTablesJNI.setDouble(m_handle, time, ((Number)o).doubleValue(), false);
|
||||
case kString:
|
||||
return NetworkTablesJNI.setString(m_handle, time, (String)o, false);
|
||||
case kRaw:
|
||||
return NetworkTablesJNI.setRaw(m_handle, time, (byte[])o, false);
|
||||
case kBooleanArray:
|
||||
return NetworkTablesJNI.setBooleanArray(m_handle, time, (boolean[])o, false);
|
||||
case kDoubleArray:
|
||||
return NetworkTablesJNI.setDoubleArray(m_handle, time, (double[])o, false);
|
||||
case kStringArray:
|
||||
return NetworkTablesJNI.setStringArray(m_handle, time, (String[])o, false);
|
||||
case kRpc:
|
||||
// TODO
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setBoolean(boolean value) {
|
||||
return NetworkTablesJNI.setBoolean(m_handle, 0, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDouble(double value) {
|
||||
return NetworkTablesJNI.setDouble(m_handle, 0, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setNumber(Number value) {
|
||||
return NetworkTablesJNI.setDouble(m_handle, 0, value.doubleValue(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setString(String value) {
|
||||
return NetworkTablesJNI.setString(m_handle, 0, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setRaw(byte[] value) {
|
||||
return NetworkTablesJNI.setRaw(m_handle, 0, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @param len the length of the value
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setRaw(ByteBuffer value, int len) {
|
||||
if (!value.isDirect())
|
||||
throw new IllegalArgumentException("must be a direct buffer");
|
||||
if (value.capacity() < len)
|
||||
throw new IllegalArgumentException("buffer is too small, must be at least " + len);
|
||||
return NetworkTablesJNI.setRaw(m_handle, 0, value, len, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setBooleanArray(boolean[] value) {
|
||||
return NetworkTablesJNI.setBooleanArray(m_handle, 0, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setBooleanArray(Boolean[] value) {
|
||||
return NetworkTablesJNI.setBooleanArray(m_handle, 0, NetworkTableValue.toNative(value), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setDoubleArray(double[] value) {
|
||||
return NetworkTablesJNI.setDoubleArray(m_handle, 0, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setNumberArray(Number[] value) {
|
||||
return NetworkTablesJNI.setDoubleArray(m_handle, 0, NetworkTableValue.toNative(value), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value.
|
||||
* @param value the value to set
|
||||
* @return False if the entry exists with a different type
|
||||
*/
|
||||
public boolean setStringArray(String[] value) {
|
||||
return NetworkTablesJNI.setStringArray(m_handle, 0, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetValue(NetworkTableValue value) {
|
||||
long time = value.getTime();
|
||||
Object o = value.getValue();
|
||||
switch (value.getType()) {
|
||||
case kBoolean:
|
||||
NetworkTablesJNI.setBoolean(m_handle, time, ((Boolean)o).booleanValue(), true);
|
||||
return;
|
||||
case kDouble:
|
||||
NetworkTablesJNI.setDouble(m_handle, time, ((Number)o).doubleValue(), true);
|
||||
return;
|
||||
case kString:
|
||||
NetworkTablesJNI.setString(m_handle, time, (String)o, true);
|
||||
return;
|
||||
case kRaw:
|
||||
NetworkTablesJNI.setRaw(m_handle, time, (byte[])o, true);
|
||||
return;
|
||||
case kBooleanArray:
|
||||
NetworkTablesJNI.setBooleanArray(m_handle, time, (boolean[])o, true);
|
||||
return;
|
||||
case kDoubleArray:
|
||||
NetworkTablesJNI.setDoubleArray(m_handle, time, (double[])o, true);
|
||||
return;
|
||||
case kStringArray:
|
||||
NetworkTablesJNI.setStringArray(m_handle, time, (String[])o, true);
|
||||
return;
|
||||
case kRpc:
|
||||
// TODO
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetBoolean(boolean value) {
|
||||
NetworkTablesJNI.setBoolean(m_handle, 0, value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetDouble(double value) {
|
||||
NetworkTablesJNI.setDouble(m_handle, 0, value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetNumber(Number value) {
|
||||
NetworkTablesJNI.setDouble(m_handle, 0, value.doubleValue(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetString(String value) {
|
||||
NetworkTablesJNI.setString(m_handle, 0, value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetRaw(byte[] value) {
|
||||
NetworkTablesJNI.setRaw(m_handle, 0, value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetBooleanArray(boolean[] value) {
|
||||
NetworkTablesJNI.setBooleanArray(m_handle, 0, value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetBooleanArray(Boolean[] value) {
|
||||
NetworkTablesJNI.setBooleanArray(m_handle, 0, NetworkTableValue.toNative(value), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetDoubleArray(double[] value) {
|
||||
NetworkTablesJNI.setDoubleArray(m_handle, 0, value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetNumberArray(Number[] value) {
|
||||
NetworkTablesJNI.setDoubleArray(m_handle, 0, NetworkTableValue.toNative(value), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the entry's value. If the value is of different type, the type is
|
||||
* changed to match the new value.
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void forceSetStringArray(String[] value) {
|
||||
NetworkTablesJNI.setStringArray(m_handle, 0, value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets flags.
|
||||
* @param flags the flags to set (bitmask)
|
||||
*/
|
||||
public void setFlags(int flags) {
|
||||
NetworkTablesJNI.setEntryFlags(m_handle, getFlags() | flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears flags.
|
||||
* @param flags the flags to clear (bitmask)
|
||||
*/
|
||||
public void clearFlags(int flags) {
|
||||
NetworkTablesJNI.setEntryFlags(m_handle, getFlags() & ~flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make value persistent through program restarts.
|
||||
*/
|
||||
public void setPersistent() {
|
||||
setFlags(kPersistent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop making value persistent through program restarts.
|
||||
*/
|
||||
public void clearPersistent() {
|
||||
clearFlags(kPersistent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the value is persistent through program restarts.
|
||||
* @return True if the value is persistent.
|
||||
*/
|
||||
public boolean isPersistent() {
|
||||
return (getFlags() & kPersistent) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the entry.
|
||||
*/
|
||||
public void delete() {
|
||||
NetworkTablesJNI.deleteEntry(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a callback-based RPC entry point. Only valid to use on the server.
|
||||
* The callback function will be called when the RPC is called.
|
||||
* This function creates RPC version 0 definitions (raw data in and out).
|
||||
* @param callback callback function
|
||||
*/
|
||||
void createRpc(Consumer<RpcAnswer> callback) {
|
||||
m_inst.createRpc(this, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a RPC function. May be used on either the client or server.
|
||||
* This function is non-blocking. Either {@link RpcCall#GetResult()} or
|
||||
* {@link RpcCall#CancelResult()} must be called on the return value to either
|
||||
* get or ignore the result of the call.
|
||||
* @param params parameter
|
||||
* @return RPC call object.
|
||||
*/
|
||||
RpcCall callRpc(byte[] params) {
|
||||
return new RpcCall(this, NetworkTablesJNI.callRpc(m_handle, params));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a listener for changes to the entry
|
||||
* @param listener the listener to add
|
||||
* @param flags bitmask specifying desired notifications
|
||||
* @return listener handle
|
||||
*/
|
||||
public int addListener(Consumer<EntryNotification> listener, int flags) {
|
||||
return m_inst.addEntryListener(this, listener, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a listener from receiving entry events
|
||||
* @param listener the listener to be removed
|
||||
*/
|
||||
public void removeListener(int listener) {
|
||||
m_inst.removeEntryListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof NetworkTableEntry)) {
|
||||
return false;
|
||||
}
|
||||
NetworkTableEntry other = (NetworkTableEntry) o;
|
||||
return m_handle == other.m_handle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return m_handle;
|
||||
}
|
||||
|
||||
private NetworkTableInstance m_inst;
|
||||
private int m_handle;
|
||||
}
|
||||
1082
src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java
Normal file
1082
src/main/java/edu/wpi/first/networktables/NetworkTableInstance.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,47 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* Network table data types.
|
||||
*/
|
||||
public enum NetworkTableType {
|
||||
kUnassigned(0),
|
||||
kBoolean(0x01),
|
||||
kDouble(0x02),
|
||||
kString(0x04),
|
||||
kRaw(0x08),
|
||||
kBooleanArray(0x10),
|
||||
kDoubleArray(0x20),
|
||||
kStringArray(0x40),
|
||||
kRpc(0x80);
|
||||
|
||||
private final int value;
|
||||
|
||||
private NetworkTableType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static NetworkTableType getFromInt(int value) {
|
||||
switch (value) {
|
||||
case 0x01: return kBoolean;
|
||||
case 0x02: return kDouble;
|
||||
case 0x04: return kString;
|
||||
case 0x08: return kRaw;
|
||||
case 0x10: return kBooleanArray;
|
||||
case 0x20: return kDoubleArray;
|
||||
case 0x40: return kStringArray;
|
||||
case 0x80: return kRpc;
|
||||
default: return kUnassigned;
|
||||
}
|
||||
}
|
||||
}
|
||||
472
src/main/java/edu/wpi/first/networktables/NetworkTableValue.java
Normal file
472
src/main/java/edu/wpi/first/networktables/NetworkTableValue.java
Normal file
@@ -0,0 +1,472 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A network table entry value.
|
||||
*/
|
||||
public final class NetworkTableValue {
|
||||
NetworkTableValue(NetworkTableType type, Object value, long time) {
|
||||
m_type = type;
|
||||
m_value = value;
|
||||
m_time = time;
|
||||
}
|
||||
|
||||
NetworkTableValue(NetworkTableType type, Object value) {
|
||||
this(type, value, NetworkTablesJNI.now());
|
||||
}
|
||||
|
||||
NetworkTableValue(int type, Object value, long time) {
|
||||
this(NetworkTableType.getFromInt(type), value, time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data type.
|
||||
* @return The type.
|
||||
*/
|
||||
public NetworkTableType getType() {
|
||||
return m_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data value stored.
|
||||
* @return The type.
|
||||
*/
|
||||
public Object getValue() {
|
||||
return m_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the creation time of the value.
|
||||
* @return The time, in the units returned by NetworkTablesJNI.now().
|
||||
*/
|
||||
public long getTime() {
|
||||
return m_time;
|
||||
}
|
||||
|
||||
/*
|
||||
* Type Checkers
|
||||
*/
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a value or is unassigned.
|
||||
* @return True if the entry value contains a value.
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return m_type != NetworkTableType.kUnassigned;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a boolean.
|
||||
* @return True if the entry value is of boolean type.
|
||||
*/
|
||||
public boolean isBoolean() {
|
||||
return m_type == NetworkTableType.kBoolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a double.
|
||||
* @return True if the entry value is of double type.
|
||||
*/
|
||||
public boolean isDouble() {
|
||||
return m_type == NetworkTableType.kDouble;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a string.
|
||||
* @return True if the entry value is of string type.
|
||||
*/
|
||||
public boolean isString() {
|
||||
return m_type == NetworkTableType.kString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a raw.
|
||||
* @return True if the entry value is of raw type.
|
||||
*/
|
||||
public boolean isRaw() {
|
||||
return m_type == NetworkTableType.kRaw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a rpc definition.
|
||||
* @return True if the entry value is of rpc definition type.
|
||||
*/
|
||||
public boolean isRpc() {
|
||||
return m_type == NetworkTableType.kRpc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a boolean array.
|
||||
* @return True if the entry value is of boolean array type.
|
||||
*/
|
||||
public boolean isBooleanArray() {
|
||||
return m_type == NetworkTableType.kBooleanArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a double array.
|
||||
* @return True if the entry value is of double array type.
|
||||
*/
|
||||
public boolean isDoubleArray() {
|
||||
return m_type == NetworkTableType.kDoubleArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if entry value contains a string array.
|
||||
* @return True if the entry value is of string array type.
|
||||
*/
|
||||
public boolean isStringArray() {
|
||||
return m_type == NetworkTableType.kStringArray;
|
||||
}
|
||||
|
||||
/*
|
||||
* Type-Safe Getters
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the entry's boolean value.
|
||||
* @throws ClassCastException if the entry value is not of boolean type.
|
||||
* @return The boolean value.
|
||||
*/
|
||||
public boolean getBoolean() {
|
||||
if (m_type != NetworkTableType.kBoolean) {
|
||||
throw new ClassCastException("cannot convert " + m_type + " to boolean");
|
||||
}
|
||||
return ((Boolean)m_value).booleanValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entry's double value.
|
||||
* @throws ClassCastException if the entry value is not of double type.
|
||||
* @return The double value.
|
||||
*/
|
||||
public double getDouble() {
|
||||
if (m_type != NetworkTableType.kDouble) {
|
||||
throw new ClassCastException("cannot convert " + m_type + " to double");
|
||||
}
|
||||
return ((Number)m_value).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entry's string value.
|
||||
* @throws ClassCastException if the entry value is not of string type.
|
||||
* @return The string value.
|
||||
*/
|
||||
public String getString() {
|
||||
if (m_type != NetworkTableType.kString) {
|
||||
throw new ClassCastException("cannot convert " + m_type + " to string");
|
||||
}
|
||||
return (String)m_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entry's raw value.
|
||||
* @throws ClassCastException if the entry value is not of raw type.
|
||||
* @return The raw value.
|
||||
*/
|
||||
public byte[] getRaw() {
|
||||
if (m_type != NetworkTableType.kRaw) {
|
||||
throw new ClassCastException("cannot convert " + m_type + " to raw");
|
||||
}
|
||||
return (byte[])m_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entry's rpc definition value.
|
||||
* @throws ClassCastException if the entry value is not of rpc definition type.
|
||||
* @return The rpc definition value.
|
||||
*/
|
||||
public byte[] getRpc() {
|
||||
if (m_type != NetworkTableType.kRpc) {
|
||||
throw new ClassCastException("cannot convert " + m_type + " to rpc");
|
||||
}
|
||||
return (byte[])m_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entry's boolean array value.
|
||||
* @throws ClassCastException if the entry value is not of boolean array type.
|
||||
* @return The boolean array value.
|
||||
*/
|
||||
public boolean[] getBooleanArray() {
|
||||
if (m_type != NetworkTableType.kBooleanArray) {
|
||||
throw new ClassCastException("cannot convert " + m_type + " to boolean array");
|
||||
}
|
||||
return (boolean[])m_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entry's double array value.
|
||||
* @throws ClassCastException if the entry value is not of double array type.
|
||||
* @return The double array value.
|
||||
*/
|
||||
public double[] getDoubleArray() {
|
||||
if (m_type != NetworkTableType.kDoubleArray) {
|
||||
throw new ClassCastException("cannot convert " + m_type + " to double array");
|
||||
}
|
||||
return (double[])m_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entry's string array value.
|
||||
* @throws ClassCastException if the entry value is not of string array type.
|
||||
* @return The string array value.
|
||||
*/
|
||||
public String[] getStringArray() {
|
||||
if (m_type != NetworkTableType.kStringArray) {
|
||||
throw new ClassCastException("cannot convert " + m_type + " to string array");
|
||||
}
|
||||
return (String[])m_value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Factory functions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a boolean entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeBoolean(boolean value) {
|
||||
return new NetworkTableValue(NetworkTableType.kBoolean, new Boolean(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a boolean entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeBoolean(boolean value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kBoolean, new Boolean(value), time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a double entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeDouble(double value) {
|
||||
return new NetworkTableValue(NetworkTableType.kDouble, new Double(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a double entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeDouble(double value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kDouble, new Double(value), time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeString(String value) {
|
||||
return new NetworkTableValue(NetworkTableType.kString, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeString(String value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kString, value, time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a raw entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeRaw(byte[] value) {
|
||||
return new NetworkTableValue(NetworkTableType.kRaw, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a raw entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeRaw(byte[] value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kRaw, value, time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a rpc entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeRpc(byte[] value) {
|
||||
return new NetworkTableValue(NetworkTableType.kRpc, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a rpc entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeRpc(byte[] value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kRpc, value, time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a boolean array entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeBooleanArray(boolean[] value) {
|
||||
return new NetworkTableValue(NetworkTableType.kBooleanArray, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a boolean array entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeBooleanArray(boolean[] value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kBooleanArray, value, time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a boolean array entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeBooleanArray(Boolean[] value) {
|
||||
return new NetworkTableValue(NetworkTableType.kBooleanArray, toNative(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a boolean array entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeBooleanArray(Boolean[] value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kBooleanArray, toNative(value), time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a double array entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeDoubleArray(double[] value) {
|
||||
return new NetworkTableValue(NetworkTableType.kDoubleArray, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a double array entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeDoubleArray(double[] value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kDoubleArray, value, time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a double array entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeDoubleArray(Number[] value) {
|
||||
return new NetworkTableValue(NetworkTableType.kDoubleArray, toNative(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a double array entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeDoubleArray(Number[] value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kDoubleArray, toNative(value), time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string array entry value.
|
||||
* @param value the value
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeStringArray(String[] value) {
|
||||
return new NetworkTableValue(NetworkTableType.kStringArray, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string array entry value.
|
||||
* @param value the value
|
||||
* @param time the creation time to use (instead of the current time)
|
||||
* @return The entry value
|
||||
*/
|
||||
public static NetworkTableValue makeStringArray(String[] value, long time) {
|
||||
return new NetworkTableValue(NetworkTableType.kStringArray, value, time);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof NetworkTableValue)) {
|
||||
return false;
|
||||
}
|
||||
NetworkTableValue other = (NetworkTableValue) o;
|
||||
return m_type == other.m_type && m_value.equals(other.m_value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(m_type, m_value);
|
||||
}
|
||||
|
||||
static boolean[] toNative(Boolean[] arr) {
|
||||
boolean[] out = new boolean[arr.length];
|
||||
for (int i = 0; i < arr.length; i++)
|
||||
out[i] = arr[i];
|
||||
return out;
|
||||
}
|
||||
|
||||
static double[] toNative(Number[] arr) {
|
||||
double[] out = new double[arr.length];
|
||||
for (int i = 0; i < arr.length; i++)
|
||||
out[i] = arr[i].doubleValue();
|
||||
return out;
|
||||
}
|
||||
|
||||
static Boolean[] fromNative(boolean[] arr) {
|
||||
Boolean[] out = new Boolean[arr.length];
|
||||
for (int i = 0; i < arr.length; i++)
|
||||
out[i] = arr[i];
|
||||
return out;
|
||||
}
|
||||
|
||||
static Double[] fromNative(double[] arr) {
|
||||
Double[] out = new Double[arr.length];
|
||||
for (int i = 0; i < arr.length; i++)
|
||||
out[i] = arr[i];
|
||||
return out;
|
||||
}
|
||||
|
||||
private NetworkTableType m_type;
|
||||
private Object m_value;
|
||||
private long m_time;
|
||||
}
|
||||
182
src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java
Normal file
182
src/main/java/edu/wpi/first/networktables/NetworkTablesJNI.java
Normal file
@@ -0,0 +1,182 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import edu.wpi.first.wpiutil.RuntimeDetector;
|
||||
|
||||
public final class NetworkTablesJNI {
|
||||
static boolean libraryLoaded = false;
|
||||
static File jniLibrary = null;
|
||||
static {
|
||||
if (!libraryLoaded) {
|
||||
try {
|
||||
System.loadLibrary("ntcore");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
try {
|
||||
String resname = RuntimeDetector.getLibraryResource("ntcore");
|
||||
InputStream is = NetworkTablesJNI.class.getResourceAsStream(resname);
|
||||
if (is != null) {
|
||||
// create temporary file
|
||||
if (System.getProperty("os.name").startsWith("Windows"))
|
||||
jniLibrary = File.createTempFile("NetworkTablesJNI", ".dll");
|
||||
else if (System.getProperty("os.name").startsWith("Mac"))
|
||||
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".dylib");
|
||||
else
|
||||
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".so");
|
||||
// flag for delete on exit
|
||||
jniLibrary.deleteOnExit();
|
||||
OutputStream os = new FileOutputStream(jniLibrary);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int readBytes;
|
||||
try {
|
||||
while ((readBytes = is.read(buffer)) != -1) {
|
||||
os.write(buffer, 0, readBytes);
|
||||
}
|
||||
} finally {
|
||||
os.close();
|
||||
is.close();
|
||||
}
|
||||
System.load(jniLibrary.getAbsolutePath());
|
||||
} else {
|
||||
System.loadLibrary("ntcore");
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
libraryLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static native int getDefaultInstance();
|
||||
public static native int createInstance();
|
||||
public static native void destroyInstance(int inst);
|
||||
public static native int getInstanceFromHandle(int handle);
|
||||
|
||||
public static native int getEntry(int inst, String key);
|
||||
public static native int[] getEntries(int inst, String prefix, int types);
|
||||
public static native String getEntryName(int entry);
|
||||
public static native long getEntryLastChange(int entry);
|
||||
|
||||
public static native int getType(int entry);
|
||||
|
||||
public static native boolean setBoolean(int entry, long time, boolean value, boolean force);
|
||||
public static native boolean setDouble(int entry, long time, double value, boolean force);
|
||||
public static native boolean setString(int entry, long time, String value, boolean force);
|
||||
public static native boolean setRaw(int entry, long time, byte[] value, boolean force);
|
||||
public static native boolean setRaw(int entry, long time, ByteBuffer value, int len, boolean force);
|
||||
public static native boolean setBooleanArray(int entry, long time, boolean[] value, boolean force);
|
||||
public static native boolean setDoubleArray(int entry, long time, double[] value, boolean force);
|
||||
public static native boolean setStringArray(int entry, long time, String[] value, boolean force);
|
||||
|
||||
public static native NetworkTableValue getValue(int entry);
|
||||
|
||||
public static native boolean getBoolean(int entry, boolean defaultValue);
|
||||
public static native double getDouble(int entry, double defaultValue);
|
||||
public static native String getString(int entry, String defaultValue);
|
||||
public static native byte[] getRaw(int entry, byte[] defaultValue);
|
||||
public static native boolean[] getBooleanArray(int entry, boolean[] defaultValue);
|
||||
public static native double[] getDoubleArray(int entry, double[] defaultValue);
|
||||
public static native String[] getStringArray(int entry, String[] defaultValue);
|
||||
public static native boolean setDefaultBoolean(int entry, long time, boolean defaultValue);
|
||||
|
||||
public static native boolean setDefaultDouble(int entry, long time, double defaultValue);
|
||||
public static native boolean setDefaultString(int entry, long time, String defaultValue);
|
||||
public static native boolean setDefaultRaw(int entry, long time, byte[] defaultValue);
|
||||
public static native boolean setDefaultBooleanArray(int entry, long time, boolean[] defaultValue);
|
||||
public static native boolean setDefaultDoubleArray(int entry, long time, double[] defaultValue);
|
||||
public static native boolean setDefaultStringArray(int entry, long time, String[] defaultValue);
|
||||
|
||||
public static native void setEntryFlags(int entry, int flags);
|
||||
public static native int getEntryFlags(int entry);
|
||||
|
||||
public static native void deleteEntry(int entry);
|
||||
|
||||
public static native void deleteAllEntries(int inst);
|
||||
|
||||
public static native EntryInfo getEntryInfoHandle(NetworkTableInstance inst, int entry);
|
||||
public static native EntryInfo[] getEntryInfo(NetworkTableInstance instObject, int inst, String prefix, int types);
|
||||
|
||||
public static native int createEntryListenerPoller(int inst);
|
||||
public static native void destroyEntryListenerPoller(int poller);
|
||||
public static native int addPolledEntryListener(int poller, String prefix, int flags);
|
||||
public static native int addPolledEntryListener(int poller, int entry, int flags);
|
||||
public static native EntryNotification[] pollEntryListener(NetworkTableInstance inst, int poller) throws InterruptedException;
|
||||
public static native EntryNotification[] pollEntryListenerTimeout(NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
|
||||
public static native void cancelPollEntryListener(int poller);
|
||||
public static native void removeEntryListener(int entryListener);
|
||||
public static native boolean waitForEntryListenerQueue(int inst, double timeout);
|
||||
|
||||
public static native int createConnectionListenerPoller(int inst);
|
||||
public static native void destroyConnectionListenerPoller(int poller);
|
||||
public static native int addPolledConnectionListener(int poller, boolean immediateNotify);
|
||||
public static native ConnectionNotification[] pollConnectionListener(NetworkTableInstance inst, int poller) throws InterruptedException;
|
||||
public static native ConnectionNotification[] pollConnectionListenerTimeout(NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
|
||||
public static native void cancelPollConnectionListener(int poller);
|
||||
public static native void removeConnectionListener(int connListener);
|
||||
public static native boolean waitForConnectionListenerQueue(int inst, double timeout);
|
||||
|
||||
public static native int createRpcCallPoller(int inst);
|
||||
public static native void destroyRpcCallPoller(int poller);
|
||||
public static native void createPolledRpc(int entry, byte[] def, int poller);
|
||||
public static native RpcAnswer[] pollRpc(NetworkTableInstance inst, int poller) throws InterruptedException;
|
||||
public static native RpcAnswer[] pollRpcTimeout(NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
|
||||
public static native void cancelPollRpc(int poller);
|
||||
public static native boolean waitForRpcCallQueue(int inst, double timeout);
|
||||
public static native void postRpcResponse(int entry, int call, byte[] result);
|
||||
public static native int callRpc(int entry, byte[] params);
|
||||
public static native byte[] getRpcResult(int entry, int call);
|
||||
public static native byte[] getRpcResult(int entry, int call, double timeout);
|
||||
public static native void cancelRpcResult(int entry, int call);
|
||||
|
||||
public static native byte[] getRpc(int entry, byte[] defaultValue);
|
||||
|
||||
public static native void setNetworkIdentity(int inst, String name);
|
||||
public static native int getNetworkMode(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);
|
||||
public static native void startClient(int inst, String serverName, int port);
|
||||
public static native void startClient(int inst, String[] serverNames, int[] ports);
|
||||
public static native void startClientTeam(int inst, int team, int port);
|
||||
public static native void stopClient(int inst);
|
||||
public static native void setServer(int inst, String serverName, int port);
|
||||
public static native void setServer(int inst, String[] serverNames, int[] ports);
|
||||
public static native void setServerTeam(int inst, int team, int port);
|
||||
public static native void startDSClient(int inst, int port);
|
||||
public static native void stopDSClient(int inst);
|
||||
public static native void setUpdateRate(int inst, double interval);
|
||||
|
||||
public static native void flush(int inst);
|
||||
|
||||
public static native ConnectionInfo[] getConnections(int inst);
|
||||
|
||||
public static native boolean isConnected(int inst);
|
||||
|
||||
public static native void savePersistent(int inst, String filename) throws PersistentException;
|
||||
public static native String[] loadPersistent(int inst, String filename) throws PersistentException; // returns warnings
|
||||
|
||||
public static native long now();
|
||||
|
||||
public static native int createLoggerPoller(int inst);
|
||||
public static native void destroyLoggerPoller(int poller);
|
||||
public static native int addPolledLogger(int poller, int minLevel, int maxLevel);
|
||||
public static native LogMessage[] pollLogger(NetworkTableInstance inst, int poller) throws InterruptedException;
|
||||
public static native LogMessage[] pollLoggerTimeout(NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
|
||||
public static native void cancelPollLogger(int poller);
|
||||
public static native void removeLogger(int logger);
|
||||
public static native boolean waitForLoggerQueue(int inst, double timeout);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An exception thrown when persistent load/save fails in a {@link NetworkTable}
|
||||
*
|
||||
*/
|
||||
public final class PersistentException extends IOException {
|
||||
|
||||
public static final long serialVersionUID = 0;
|
||||
|
||||
/**
|
||||
* @param message The error message
|
||||
*/
|
||||
public PersistentException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
91
src/main/java/edu/wpi/first/networktables/RpcAnswer.java
Normal file
91
src/main/java/edu/wpi/first/networktables/RpcAnswer.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* NetworkTables Remote Procedure Call (Server Side).
|
||||
*/
|
||||
public final class RpcAnswer {
|
||||
/** Entry handle. */
|
||||
public final int entry;
|
||||
|
||||
/** Call handle. */
|
||||
public int call;
|
||||
|
||||
/** Entry name. */
|
||||
public final String name;
|
||||
|
||||
/** Call raw parameters. */
|
||||
public final String params;
|
||||
|
||||
/** Connection that called the RPC. */
|
||||
public final ConnectionInfo conn;
|
||||
|
||||
/** Constructor.
|
||||
* This should generally only be used internally to NetworkTables.
|
||||
* @param inst Instance
|
||||
* @param entry Entry handle
|
||||
* @param call Call handle
|
||||
* @param name Entry name
|
||||
* @param params Call raw parameters
|
||||
* @param conn Connection info
|
||||
*/
|
||||
public RpcAnswer(NetworkTableInstance inst, int entry, int call, String name, String params, ConnectionInfo conn) {
|
||||
this.inst = inst;
|
||||
this.entry = entry;
|
||||
this.call = call;
|
||||
this.name = name;
|
||||
this.params = params;
|
||||
this.conn = conn;
|
||||
}
|
||||
|
||||
static final byte[] emptyResponse = new byte[] {};
|
||||
|
||||
/**
|
||||
* Posts an empty response if one was not previously sent.
|
||||
*/
|
||||
public synchronized void free() {
|
||||
if (call != 0) {
|
||||
postResponse(emptyResponse);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the native handle is valid.
|
||||
* @return True if the native handle is valid, false otherwise.
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return call != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Post RPC response (return value) for a polled RPC.
|
||||
* @param result result raw data that will be provided to remote caller
|
||||
*/
|
||||
public void postResponse(byte[] result) {
|
||||
NetworkTablesJNI.postRpcResponse(entry, call, result);
|
||||
call = 0;
|
||||
}
|
||||
|
||||
/* Network table instance. */
|
||||
private final NetworkTableInstance inst;
|
||||
|
||||
/* Cached entry object. */
|
||||
NetworkTableEntry entryObject;
|
||||
|
||||
/**
|
||||
* Get the entry as an object.
|
||||
* @return NetworkTableEntry for the RPC.
|
||||
*/
|
||||
NetworkTableEntry getEntry() {
|
||||
if (entryObject == null) {
|
||||
entryObject = new NetworkTableEntry(inst, entry);
|
||||
}
|
||||
return entryObject;
|
||||
}
|
||||
}
|
||||
93
src/main/java/edu/wpi/first/networktables/RpcCall.java
Normal file
93
src/main/java/edu/wpi/first/networktables/RpcCall.java
Normal file
@@ -0,0 +1,93 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* NetworkTables Remote Procedure Call.
|
||||
*/
|
||||
public final class RpcCall {
|
||||
/** Constructor.
|
||||
* This should generally only be used internally to NetworkTables.
|
||||
* @param entry Entry
|
||||
* @param call Call handle
|
||||
*/
|
||||
public RpcCall(NetworkTableEntry entry, int call) {
|
||||
m_entry = entry;
|
||||
m_call = call;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the result if no other action taken.
|
||||
*/
|
||||
public synchronized void free() {
|
||||
if (m_call != 0) {
|
||||
cancelResult();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the native handle is valid.
|
||||
* @return True if the native handle is valid, false otherwise.
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return m_call != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the RPC entry.
|
||||
* @return NetworkTableEntry for the RPC.
|
||||
*/
|
||||
public NetworkTableEntry getEntry() {
|
||||
return m_entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the call native handle.
|
||||
* @return Native handle.
|
||||
*/
|
||||
public int getCall() {
|
||||
return m_call;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the result (return value). This function blocks until
|
||||
* the result is received.
|
||||
* @return Received result (output)
|
||||
*/
|
||||
public byte[] getResult() {
|
||||
byte[] result = NetworkTablesJNI.getRpcResult(m_entry.getHandle(), m_call);
|
||||
if (result.length != 0) {
|
||||
m_call = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the result (return value). This function blocks until
|
||||
* the result is received or it times out.
|
||||
* @param timeout timeout, in seconds
|
||||
* @return Received result (output)
|
||||
*/
|
||||
public byte[] getResult(double timeout) {
|
||||
byte[] result = NetworkTablesJNI.getRpcResult(m_entry.getHandle(), m_call, timeout);
|
||||
if (result.length != 0) {
|
||||
m_call = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ignore the result. This function is non-blocking.
|
||||
*/
|
||||
public void cancelResult() {
|
||||
NetworkTablesJNI.cancelRpcResult(m_entry.getHandle(), m_call);
|
||||
}
|
||||
|
||||
private final NetworkTableEntry m_entry;
|
||||
private int m_call;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* A listener that listens to changes in values in a {@link NetworkTable}
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface TableEntryListener extends EntryListenerFlags {
|
||||
/**
|
||||
* Called when a key-value pair is changed in a {@link NetworkTable}.
|
||||
*
|
||||
* @param table the table the key-value pair exists in
|
||||
* @param key the key associated with the value that changed
|
||||
* @param entry the entry associated with the value that changed
|
||||
* @param value the new value
|
||||
* @param flags update flags; for example, EntryListenerFlags.kNew if the key
|
||||
* did not previously exist in the table
|
||||
*/
|
||||
void valueChanged(NetworkTable table, String key, NetworkTableEntry entry, NetworkTableValue value, int flags);
|
||||
}
|
||||
23
src/main/java/edu/wpi/first/networktables/TableListener.java
Normal file
23
src/main/java/edu/wpi/first/networktables/TableListener.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2017. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
/**
|
||||
* A listener that listens to new tables in a {@link NetworkTable}
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface TableListener {
|
||||
/**
|
||||
* Called when a new table is created within a {@link NetworkTable}.
|
||||
*
|
||||
* @param parent the parent of the table
|
||||
* @param name the name of the new table
|
||||
* @param table the new table
|
||||
*/
|
||||
void tableCreated(NetworkTable parent, String name, NetworkTable table);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package edu.wpi.first.wpilibj.networktables;
|
||||
|
||||
public class ConnectionInfo {
|
||||
public final String remote_id;
|
||||
public final String remote_ip;
|
||||
public final int remote_port;
|
||||
public final long last_update;
|
||||
public final int protocol_version;
|
||||
|
||||
public ConnectionInfo(String remote_id, String remote_ip, int remote_port, long last_update, int protocol_version) {
|
||||
this.remote_id = remote_id;
|
||||
this.remote_ip = remote_ip;
|
||||
this.remote_port = remote_port;
|
||||
this.last_update = last_update;
|
||||
this.protocol_version = protocol_version;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package edu.wpi.first.wpilibj.networktables;
|
||||
|
||||
public class EntryInfo {
|
||||
public final String name;
|
||||
public final int type;
|
||||
public final int flags;
|
||||
public final long last_change;
|
||||
|
||||
public EntryInfo(String name, int type, int flags, long last_change) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.flags = flags;
|
||||
this.last_change = last_change;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,159 +0,0 @@
|
||||
package edu.wpi.first.wpilibj.networktables;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import edu.wpi.first.wpiutil.RuntimeDetector;
|
||||
|
||||
public class NetworkTablesJNI {
|
||||
static boolean libraryLoaded = false;
|
||||
static File jniLibrary = null;
|
||||
static {
|
||||
if (!libraryLoaded) {
|
||||
try {
|
||||
System.loadLibrary("ntcore");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
try {
|
||||
String resname = RuntimeDetector.getLibraryResource("ntcore");
|
||||
InputStream is = NetworkTablesJNI.class.getResourceAsStream(resname);
|
||||
if (is != null) {
|
||||
// create temporary file
|
||||
if (System.getProperty("os.name").startsWith("Windows"))
|
||||
jniLibrary = File.createTempFile("NetworkTablesJNI", ".dll");
|
||||
else if (System.getProperty("os.name").startsWith("Mac"))
|
||||
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".dylib");
|
||||
else
|
||||
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".so");
|
||||
// flag for delete on exit
|
||||
jniLibrary.deleteOnExit();
|
||||
OutputStream os = new FileOutputStream(jniLibrary);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int readBytes;
|
||||
try {
|
||||
while ((readBytes = is.read(buffer)) != -1) {
|
||||
os.write(buffer, 0, readBytes);
|
||||
}
|
||||
} finally {
|
||||
os.close();
|
||||
is.close();
|
||||
}
|
||||
System.load(jniLibrary.getAbsolutePath());
|
||||
} else {
|
||||
System.loadLibrary("ntcore");
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
libraryLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static final int NT_NET_MODE_NONE = 0x00;
|
||||
public static final int NT_NET_MODE_SERVER = 0x01;
|
||||
public static final int NT_NET_MODE_CLIENT = 0x02;
|
||||
public static final int NT_NET_MODE_STARTING = 0x04;
|
||||
public static final int NT_NET_MODE_FAILURE = 0x08;
|
||||
|
||||
public static native boolean containsKey(String key);
|
||||
public static native int getType(String key);
|
||||
|
||||
public static native boolean putBoolean(String key, boolean value);
|
||||
public static native boolean putDouble(String key, double value);
|
||||
public static native boolean putString(String key, String value);
|
||||
public static native boolean putRaw(String key, byte[] value);
|
||||
public static native boolean putRaw(String key, ByteBuffer value, int len);
|
||||
public static native boolean putBooleanArray(String key, boolean[] value);
|
||||
public static native boolean putDoubleArray(String key, double[] value);
|
||||
public static native boolean putStringArray(String key, String[] value);
|
||||
|
||||
public static native void forcePutBoolean(String key, boolean value);
|
||||
public static native void forcePutDouble(String key, double value);
|
||||
public static native void forcePutString(String key, String value);
|
||||
public static native void forcePutRaw(String key, byte[] value);
|
||||
public static native void forcePutRaw(String key, ByteBuffer value, int len);
|
||||
public static native void forcePutBooleanArray(String key, boolean[] value);
|
||||
public static native void forcePutDoubleArray(String key, double[] value);
|
||||
public static native void forcePutStringArray(String key, String[] value);
|
||||
|
||||
public static native Object getValue(String key, Object defaultValue);
|
||||
public static native boolean getBoolean(String key, boolean defaultValue);
|
||||
public static native double getDouble(String key, double defaultValue);
|
||||
public static native String getString(String key, String defaultValue);
|
||||
public static native byte[] getRaw(String key, byte[] defaultValue);
|
||||
public static native boolean[] getBooleanArray(String key, boolean[] defaultValue);
|
||||
public static native double[] getDoubleArray(String key, double[] defaultValue);
|
||||
public static native String[] getStringArray(String key, String[] defaultValue);
|
||||
|
||||
public static native boolean setDefaultBoolean(String key, boolean defaultValue);
|
||||
public static native boolean setDefaultDouble(String key, double defaultValue);
|
||||
public static native boolean setDefaultString(String key, String defaultValue);
|
||||
public static native boolean setDefaultRaw(String key, byte[] defaultValue);
|
||||
public static native boolean setDefaultBooleanArray(String key, boolean[] defaultValue);
|
||||
public static native boolean setDefaultDoubleArray(String key, double[] defaultValue);
|
||||
public static native boolean setDefaultStringArray(String key, String[] defaultValue);
|
||||
|
||||
public static native void setEntryFlags(String key, int flags);
|
||||
public static native int getEntryFlags(String key);
|
||||
|
||||
public static native void deleteEntry(String key);
|
||||
public static native void deleteAllEntries();
|
||||
|
||||
public static native EntryInfo[] getEntries(String prefix, int types);
|
||||
|
||||
public static native void flush();
|
||||
|
||||
@FunctionalInterface
|
||||
public interface EntryListenerFunction {
|
||||
void apply(int uid, String key, Object value, int flags);
|
||||
}
|
||||
public static native int addEntryListener(String prefix, EntryListenerFunction listener, int flags);
|
||||
public static native void removeEntryListener(int entryListenerUid);
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ConnectionListenerFunction {
|
||||
void apply(int uid, boolean connected, ConnectionInfo conn);
|
||||
}
|
||||
public static native int addConnectionListener(ConnectionListenerFunction listener, boolean immediateNotify);
|
||||
public static native void removeConnectionListener(int connListenerUid);
|
||||
|
||||
// public static native void createRpc(String key, byte[] def, IRpc rpc);
|
||||
// public static native void createRpc(String key, ByteBuffer def, int def_len, IRpc rpc);
|
||||
public static native byte[] getRpc(String key, byte[] defaultValue);
|
||||
public static native int callRpc(String key, byte[] params);
|
||||
public static native int callRpc(String key, ByteBuffer params, int params_len);
|
||||
// public static native byte[] getRpcResultBlocking(int callUid);
|
||||
// public static native byte[] getRpcResultNonblocking(int callUid) throws RpcNoResponseException;
|
||||
|
||||
public static native void setNetworkIdentity(String name);
|
||||
public static native int getNetworkMode();
|
||||
public static native void startServer(String persistFilename, String listenAddress, int port);
|
||||
public static native void stopServer();
|
||||
public static native void startClient();
|
||||
public static native void startClient(String serverName, int port);
|
||||
public static native void startClient(String[] serverNames, int[] ports);
|
||||
public static native void stopClient();
|
||||
public static native void setServer(String serverName, int port);
|
||||
public static native void setServer(String[] serverNames, int[] ports);
|
||||
public static native void startDSClient(int port);
|
||||
public static native void stopDSClient();
|
||||
public static native void setUpdateRate(double interval);
|
||||
|
||||
public static native ConnectionInfo[] getConnections();
|
||||
|
||||
public static native void savePersistent(String filename) throws PersistentException;
|
||||
public static native String[] loadPersistent(String filename) throws PersistentException; // returns warnings
|
||||
|
||||
public static native long now();
|
||||
|
||||
@FunctionalInterface
|
||||
public interface LoggerFunction {
|
||||
void apply(int level, String file, int line, String msg);
|
||||
}
|
||||
public static native void setLogger(LoggerFunction func, int minLevel);
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package edu.wpi.first.wpilibj.networktables;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An exception thrown when persistent load/save fails in a {@link NetworkTable}
|
||||
*
|
||||
*/
|
||||
public class PersistentException extends IOException {
|
||||
|
||||
/**
|
||||
* @param message The error message
|
||||
*/
|
||||
public PersistentException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,14 +3,13 @@ package edu.wpi.first.wpilibj.tables;
|
||||
|
||||
/**
|
||||
* Represents an object that has a remote connection
|
||||
*
|
||||
* @author Mitchell
|
||||
*
|
||||
* @deprecated Use {@link edu.wpi.first.networktables.NetworkTableInstance}.
|
||||
*/
|
||||
@Deprecated
|
||||
public interface IRemote {
|
||||
/**
|
||||
* Register an object to listen for connection and disconnection events
|
||||
*
|
||||
*
|
||||
* @param listener the listener to be register
|
||||
* @param immediateNotify if the listener object should be notified of the current connection state
|
||||
*/
|
||||
@@ -18,17 +17,17 @@ public interface IRemote {
|
||||
|
||||
/**
|
||||
* Unregister a listener from connection events
|
||||
*
|
||||
*
|
||||
* @param listener the listener to be unregistered
|
||||
*/
|
||||
public void removeConnectionListener(IRemoteConnectionListener listener);
|
||||
|
||||
|
||||
/**
|
||||
* Get the current state of the objects connection
|
||||
* @return the current connection state
|
||||
*/
|
||||
public boolean isConnected();
|
||||
|
||||
|
||||
/**
|
||||
* If the object is acting as a server
|
||||
* @return if the object is a server
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package edu.wpi.first.wpilibj.tables;
|
||||
|
||||
import edu.wpi.first.wpilibj.networktables.ConnectionInfo;
|
||||
import edu.wpi.first.networktables.ConnectionInfo;
|
||||
|
||||
/**
|
||||
* A listener that listens for connection changes in a {@link IRemote} object
|
||||
*
|
||||
* @author Mitchell
|
||||
*
|
||||
* @deprecated Use Consumer<{@link edu.wpi.first.networktables.ConnectionNotification}>.
|
||||
*/
|
||||
@Deprecated
|
||||
public interface IRemoteConnectionListener {
|
||||
/**
|
||||
* Called when an IRemote is connected
|
||||
|
||||
@@ -6,7 +6,9 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* A table whose values can be read and written to
|
||||
* @deprecated Use {@link edu.wpi.first.networktables.NetworkTable}.
|
||||
*/
|
||||
@Deprecated
|
||||
public interface ITable {
|
||||
|
||||
/**
|
||||
@@ -34,17 +36,20 @@ public interface ITable {
|
||||
public ITable getSubTable(String key);
|
||||
|
||||
/**
|
||||
* Gets all keys in the table (not including sub-tables).
|
||||
* @param types bitmask of types; 0 is treated as a "don't care".
|
||||
* @return keys currently in the table
|
||||
*/
|
||||
public Set<String> getKeys(int types);
|
||||
|
||||
/**
|
||||
* Gets all keys in the table (not including sub-tables).
|
||||
* @return keys currently in the table
|
||||
*/
|
||||
public Set<String> getKeys();
|
||||
|
||||
/**
|
||||
* Gets the names of all subtables in the table.
|
||||
* @return subtables currently in the table
|
||||
*/
|
||||
public Set<String> getSubTables();
|
||||
@@ -137,15 +142,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putNumber(String key, double value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultNumber(String key, double defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number the key maps to. If the key does not exist or is of
|
||||
* different type, it will return the default value.
|
||||
@@ -163,15 +168,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putString(String key, String value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultString(String key, String defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the string the key maps to. If the key does not exist or is of
|
||||
* different type, it will return the default value.
|
||||
@@ -189,15 +194,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putBoolean(String key, boolean value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultBoolean(String key, boolean defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the boolean the key maps to. If the key does not exist or is of
|
||||
* different type, it will return the default value.
|
||||
@@ -215,15 +220,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putBooleanArray(String key, boolean[] value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultBooleanArray(String key, boolean[] defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Put a boolean array in the table
|
||||
* @param key the key to be assigned to
|
||||
@@ -231,15 +236,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putBooleanArray(String key, Boolean[] value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultBooleanArray(String key, Boolean[] defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the boolean array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
@@ -266,15 +271,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putNumberArray(String key, double[] value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultNumberArray(String key, double[] defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Put a number array in the table
|
||||
* @param key the key to be assigned to
|
||||
@@ -282,15 +287,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putNumberArray(String key, Double[] value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultNumberArray(String key, Double[] defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
@@ -317,15 +322,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putStringArray(String key, String[] value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultStringArray(String key, String[] defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Returns the string array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
@@ -343,15 +348,15 @@ public interface ITable {
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
public boolean putRaw(String key, byte[] value);
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doens't exist.
|
||||
* @return False if the table key exists with a different type
|
||||
*/
|
||||
public boolean setDefaultRaw(String key, byte[] defaultValue);
|
||||
|
||||
|
||||
/**
|
||||
* Put a raw value (bytes from a byte buffer) in the table
|
||||
* @param key the key to be assigned to
|
||||
@@ -467,7 +472,8 @@ public interface ITable {
|
||||
public double getDouble(String key, double defaultValue);
|
||||
|
||||
/**
|
||||
* Gets the full path of this table.
|
||||
* Gets the full path of this table. Does not include the trailing "/".
|
||||
* @return The path to this table (e.g. "", "/foo").
|
||||
*/
|
||||
public String getPath();
|
||||
|
||||
|
||||
@@ -2,11 +2,12 @@ package edu.wpi.first.wpilibj.tables;
|
||||
|
||||
/**
|
||||
* A listener that listens to changes in values in a {@link ITable}
|
||||
*
|
||||
* @author Mitchell
|
||||
*
|
||||
* @deprecated Use Consumer<{@link edu.wpi.first.networktables.EntryNotification}>,
|
||||
* {@link edu.wpi.first.networktables.TableEntryListener}, or
|
||||
* {@link edu.wpi.first.networktables.TableListener} as appropriate.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
@Deprecated
|
||||
public interface ITableListener {
|
||||
/**
|
||||
* Called when a key-value pair is changed in a {@link ITable}
|
||||
|
||||
Reference in New Issue
Block a user