Add ITable/NetworkTable GetKeys and GetSubTables accessors.

This commit is contained in:
Peter Johnson
2015-09-16 00:50:31 -07:00
parent 6cbc219427
commit c5d456f3a6
7 changed files with 141 additions and 8 deletions

View File

@@ -15,6 +15,7 @@ static jclass booleanCls = nullptr;
static jclass doubleCls = nullptr;
static jclass stringCls = nullptr;
static jclass connectionInfoCls = nullptr;
static jclass entryInfoCls = nullptr;
static jclass keyNotDefinedEx = nullptr;
static jclass persistentEx = nullptr;
@@ -52,6 +53,12 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
if (!connectionInfoCls) return JNI_ERR;
env->DeleteLocalRef(local);
local = env->FindClass("edu/wpi/first/wpilibj/networktables/EntryInfo");
if (!local) return JNI_ERR;
entryInfoCls = static_cast<jclass>(env->NewGlobalRef(local));
if (!entryInfoCls) return JNI_ERR;
env->DeleteLocalRef(local);
local =
env->FindClass("edu/wpi/first/wpilibj/networktables/NetworkTableKeyNotDefined");
keyNotDefinedEx = static_cast<jclass>(env->NewGlobalRef(local));
@@ -76,6 +83,7 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
if (doubleCls) env->DeleteGlobalRef(doubleCls);
if (stringCls) env->DeleteGlobalRef(stringCls);
if (connectionInfoCls) env->DeleteGlobalRef(connectionInfoCls);
if (entryInfoCls) env->DeleteGlobalRef(entryInfoCls);
if (keyNotDefinedEx) env->DeleteGlobalRef(keyNotDefinedEx);
if (persistentEx) env->DeleteGlobalRef(persistentEx);
jvm = nullptr;
@@ -347,6 +355,14 @@ static jobject ToJavaObject(JNIEnv *env, const nt::ConnectionInfo &info) {
(jlong)info.last_update, (jint)info.protocol_version);
}
static jobject ToJavaObject(JNIEnv *env, const nt::EntryInfo &info) {
static jmethodID constructor =
env->GetMethodID(entryInfoCls, "<init>", "(Ljava/lang/String;IIJ)V");
JavaLocal<jstring> name(env, ToJavaString(env, info.name));
return env->NewObject(entryInfoCls, constructor, name.obj(), (jint)info.type,
(jint)info.flags, (jlong)info.last_change);
}
//
// Exception throwers
//
@@ -836,6 +852,24 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
nt::DeleteAllEntries();
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: getEntries
* Signature: (Ljava/lang/String;I)[Ledu/wpi/first/wpilibj/networktables/EntryInfo;
*/
JNIEXPORT jobjectArray JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI_getEntries
(JNIEnv *env, jclass, jstring prefix, jint types)
{
auto arr = nt::GetEntryInfo(JavaStringRef(env, prefix), types);
jobjectArray jarr = env->NewObjectArray(arr.size(), entryInfoCls, nullptr);
if (!jarr) return nullptr;
for (size_t i = 0; i < arr.size(); ++i) {
JavaLocal<jobject> jelem(env, ToJavaObject(env, arr[i]));
env->SetObjectArrayElement(jarr, i, jelem);
}
return jarr;
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: flush

View File

@@ -367,13 +367,47 @@ public class NetworkTable implements ITable, IRemote {
}
public boolean containsSubTable(String key) {
String subtablePrefix = path + key + PATH_SEPARATOR;
//List keys = node.getEntryStore().keys();
//for (int i = 0; i < keys.size(); ++i) {
// if (((String)keys.get(i)).startsWith(subtablePrefix))
// return true;
//}
return false;
EntryInfo[] entries = NetworkTablesJNI.getEntries(path + PATH_SEPARATOR + key + PATH_SEPARATOR, 0);
return entries.length != 0;
}
/**
* @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 entry : NetworkTablesJNI.getEntries(path + PATH_SEPARATOR, types)) {
String relativeKey = entry.name.substring(prefixLen);
if (relativeKey.indexOf(PATH_SEPARATOR) != -1)
continue;
keys.add(relativeKey);
}
return keys;
}
/**
* @return keys currently in the table
*/
public Set<String> getKeys() {
return getKeys(0);
}
/**
* @return subtables currently in the table
*/
public Set<String> getSubTables() {
Set<String> keys = new HashSet<String>();
int prefixLen = path.length() + 1;
for (EntryInfo entry : NetworkTablesJNI.getEntries(path + PATH_SEPARATOR, 0)) {
String relativeKey = entry.name.substring(prefixLen);
int endSubTable = relativeKey.indexOf(PATH_SEPARATOR);
if (endSubTable == -1)
continue;
keys.add(relativeKey.substring(0, endSubTable));
}
return keys;
}
/**

View File

@@ -98,7 +98,7 @@ public class NetworkTablesJNI {
public static native void deleteEntry(String key);
public static native void deleteAllEntries();
// public static native EntryInfo[] getEntryInfo(String prefix, int types);
public static native EntryInfo[] getEntries(String prefix, int types);
public static native void flush();

View File

@@ -1,6 +1,7 @@
package edu.wpi.first.wpilibj.tables;
import java.util.NoSuchElementException;
import java.util.Set;
/**
@@ -30,6 +31,22 @@ public interface ITable {
*/
public ITable getSubTable(String key);
/**
* @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);
/**
* @return keys currently in the table
*/
public Set<String> getKeys();
/**
* @return subtables currently in the table
*/
public Set<String> getSubTables();
/**
* Makes a key's value persistent through program restarts.
* The key cannot be null.