mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Add PortForwarder class (#1890)
This class provides an easy way to forward local ports to another host/port. This is primarily useful to provide a way to access Ethernet-connected devices from a computer tethered to the RoboRIO USB port. The most natural spot to put the shared implementation of this class was into wpiutil, so a wpiutilJNI library has been added.
This commit is contained in:
56
wpiutil/src/main/java/edu/wpi/first/wpiutil/WPIUtilJNI.java
Normal file
56
wpiutil/src/main/java/edu/wpi/first/wpiutil/WPIUtilJNI.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 FIRST. 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.wpiutil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public final class WPIUtilJNI {
|
||||
static boolean libraryLoaded = false;
|
||||
static RuntimeLoader<WPIUtilJNI> loader = null;
|
||||
|
||||
public static class Helper {
|
||||
private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true);
|
||||
|
||||
public static boolean getExtractOnStaticLoad() {
|
||||
return extractOnStaticLoad.get();
|
||||
}
|
||||
|
||||
public static void setExtractOnStaticLoad(boolean load) {
|
||||
extractOnStaticLoad.set(load);
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
if (Helper.getExtractOnStaticLoad()) {
|
||||
try {
|
||||
loader = new RuntimeLoader<>("wpiutiljni", RuntimeLoader.getDefaultExtractionRoot(), WPIUtilJNI.class);
|
||||
loader.loadLibrary();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
libraryLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Force load the library.
|
||||
*/
|
||||
public static synchronized void forceLoad() throws IOException {
|
||||
if (libraryLoaded) {
|
||||
return;
|
||||
}
|
||||
loader = new RuntimeLoader<>("wpiutiljni", RuntimeLoader.getDefaultExtractionRoot(), WPIUtilJNI.class);
|
||||
loader.loadLibrary();
|
||||
libraryLoaded = true;
|
||||
}
|
||||
|
||||
public static native void addPortForwarder(int port, String remoteHost, int remotePort);
|
||||
public static native void removePortForwarder(int port);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 FIRST. 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.wpiutil;
|
||||
|
||||
/**
|
||||
* Forward ports to another host. This is primarily useful for accessing
|
||||
* Ethernet-connected devices from a computer tethered to the RoboRIO USB port.
|
||||
*/
|
||||
public final class PortForwarder {
|
||||
private PortForwarder() {
|
||||
throw new UnsupportedOperationException("This is a utility class!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward a local TCP port to a remote host and port.
|
||||
* Note that local ports less than 1024 won't work as a normal user.
|
||||
*
|
||||
* @param port local port number
|
||||
* @param remoteHost remote IP address / DNS name
|
||||
* @param remotePort remote port number
|
||||
*/
|
||||
public static void add(int port, String remoteHost, int remotePort) {
|
||||
WPIUtilJNI.addPortForwarder(port, remoteHost, remotePort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop TCP forwarding on a port.
|
||||
*
|
||||
* @param port local port number
|
||||
*/
|
||||
public static void remove(int port) {
|
||||
WPIUtilJNI.removePortForwarder(port);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user