Add FRC Driver Station connection support.

The 2017 FRC Driver Station supports getting the robot IP via a TCP
connection that returns JSON.  Use this to support overriding the
server IP address used for client connections.

Default to using this approach for client connections in the NetworkTable
interfaces.

Add support for setting the server address without stopping and
restarting the client.

SetTeam now also round-robins by default.
This commit is contained in:
Peter Johnson
2016-11-04 16:01:42 -07:00
parent 34acd9d47c
commit 77edf1e103
15 changed files with 538 additions and 66 deletions

View File

@@ -1147,6 +1147,17 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
nt::StopServer();
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: startClient
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI_startClient__
(JNIEnv *env, jclass)
{
nt::StartClient();
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: startClient
@@ -1205,6 +1216,75 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
nt::StopClient();
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: setServer
* Signature: (Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI_setServer__Ljava_lang_String_2I
(JNIEnv *env, jclass, jstring serverName, jint port)
{
nt::SetServer(JStringRef{env, serverName}.c_str(), port);
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: setServer
* Signature: ([Ljava/lang/String;[I)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI_setServer___3Ljava_lang_String_2_3I
(JNIEnv *env, jclass, jobjectArray serverNames, jintArray ports)
{
int len = env->GetArrayLength(serverNames);
if (len != env->GetArrayLength(ports)) {
env->ThrowNew(illegalArgEx,
"serverNames and ports arrays must be the same size");
return;
}
jint* portInts = env->GetIntArrayElements(ports, nullptr);
if (!portInts) return;
std::vector<std::string> names;
std::vector<std::pair<nt::StringRef, unsigned int>> servers;
names.reserve(len);
servers.reserve(len);
for (int i = 0; i < len; ++i) {
JLocal<jstring> elem{
env, static_cast<jstring>(env->GetObjectArrayElement(serverNames, i))};
if (!elem) {
env->ThrowNew(illegalArgEx, "null string in serverNames");
return;
}
names.emplace_back(JStringRef{env, elem}.str());
servers.emplace_back(std::make_pair(nt::StringRef(names.back()),
portInts[i]));
}
env->ReleaseIntArrayElements(ports, portInts, JNI_ABORT);
nt::SetServer(servers);
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: startDSClient
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI_startDSClient
(JNIEnv *env, jclass, jint port)
{
nt::StartDSClient(port);
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: stopDSClient
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJNI_stopDSClient
(JNIEnv *env, jclass)
{
nt::StopDSClient();
}
/*
* Class: edu_wpi_first_wpilibj_networktables_NetworkTablesJNI
* Method: setUpdateRate

View File

@@ -21,9 +21,9 @@ public class NetworkTable implements ITable, IRemote {
public static final int DEFAULT_PORT = 1735;
private static boolean client = false;
private static boolean enableDS = true;
private static boolean running = false;
private static int port = DEFAULT_PORT;
private static String[] ipAddresses = new String[0];
private static String persistentFilename = "networktables.ini";
private synchronized static void checkInit() {
@@ -39,10 +39,9 @@ public class NetworkTable implements ITable, IRemote {
if (running)
shutdown();
if (client) {
int[] ports = new int[ipAddresses.length];
for (int i=0; i<ipAddresses.length; i++)
ports[i] = port;
NetworkTablesJNI.startClient(ipAddresses, ports);
NetworkTablesJNI.startClient();
if (enableDS)
NetworkTablesJNI.startDSClient(port);
} else
NetworkTablesJNI.startServer(persistentFilename, "", port);
running = true;
@@ -54,9 +53,10 @@ public class NetworkTable implements ITable, IRemote {
public synchronized static void shutdown() {
if (!running)
return;
if (client)
if (client) {
NetworkTablesJNI.stopDSClient();
NetworkTablesJNI.stopClient();
else
} else
NetworkTablesJNI.stopServer();
running = false;
}
@@ -90,7 +90,12 @@ public class NetworkTable implements ITable, IRemote {
* @param team the team number
*/
public synchronized static void setTeam(int team) {
setIPAddress("roboRIO-" + team + "-FRC.local");
String[] addresses = new String[4];
addresses[0] = "10." + (int)(team / 100) + "." + (int)(team % 100) + ".2";
addresses[1] = "172.22.11.2";
addresses[2] = "roboRIO-" + team + "-FRC.local";
addresses[3] = "roboRIO-" + team + "-FRC.lan";
setIPAddress(addresses);
}
/**
@@ -98,11 +103,9 @@ public class NetworkTable implements ITable, IRemote {
* mode
*/
public synchronized static void setIPAddress(final String address) {
if (ipAddresses.length == 1 && ipAddresses[0].equals(address))
return;
checkInit();
ipAddresses = new String[1];
ipAddresses[0] = address;
String[] addresses = new String[1];
addresses[0] = address;
setIPAddress(addresses);
}
/**
@@ -110,19 +113,17 @@ public class NetworkTable implements ITable, IRemote {
* client mode (in round robin order)
*/
public synchronized static void setIPAddress(final String[] addresses) {
if (ipAddresses.length == addresses.length) {
boolean match = true;
for (int i=0; i<addresses.length; i++) {
if (!ipAddresses[i].equals(addresses[i])) {
match = false;
break;
}
}
if (match)
return;
}
checkInit();
ipAddresses = addresses;
int[] ports = new int[addresses.length];
for (int i=0; i<addresses.length; i++)
ports[i] = port;
NetworkTablesJNI.setServer(addresses, ports);
// Stop the DS client if we're explicitly connecting to localhost
if (addresses.length > 0 &&
(addresses[0].equals("localhost") || addresses[0].equals("127.0.0.1")))
NetworkTablesJNI.stopDSClient();
else if (enableDS)
NetworkTablesJNI.startDSClient(port);
}
/**
@@ -136,6 +137,18 @@ public class NetworkTable implements ITable, IRemote {
port = aport;
}
/**
* @param enabled whether to enable the connection to the local DS to get
* the robot IP address (defaults to enabled)
*/
public synchronized static void setDSClientEnabled(boolean enabled) {
enableDS = enabled;
if (enableDS)
NetworkTablesJNI.startDSClient(port);
else
NetworkTablesJNI.stopDSClient();
}
/**
* Sets the persistent filename.
* @param filename the filename that the network tables server uses for

View File

@@ -148,9 +148,14 @@ public class NetworkTablesJNI {
public static native void setNetworkIdentity(String name);
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();