[wpinet] Move network portions of wpiutil into new wpinet library (#4077)

This commit is contained in:
Peter Johnson
2022-05-07 10:54:14 -07:00
committed by GitHub
parent b33715db15
commit d673ead481
327 changed files with 1783 additions and 1179 deletions

View File

@@ -0,0 +1,45 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.net;
import java.util.Map;
/** Class to announce over mDNS that a service is available. */
public class MulticastServiceAnnouncer implements AutoCloseable {
private final int m_handle;
/**
* Creates a MulticastServiceAnnouncer.
*
* @param serviceName service name
* @param serviceType service type
* @param port port
* @param txt txt
*/
public MulticastServiceAnnouncer(
String serviceName, String serviceType, int port, Map<String, String> txt) {
String[] keys = txt.keySet().toArray(String[]::new);
String[] values = txt.values().toArray(String[]::new);
m_handle =
WPINetJNI.createMulticastServiceAnnouncer(serviceName, serviceType, port, keys, values);
}
@Override
public void close() {
WPINetJNI.freeMulticastServiceAnnouncer(m_handle);
}
public void start() {
WPINetJNI.startMulticastServiceAnnouncer(m_handle);
}
public void stop() {
WPINetJNI.stopMulticastServiceAnnouncer(m_handle);
}
public boolean hasImplementation() {
return WPINetJNI.getMulticastServiceAnnouncerHasImplementation(m_handle);
}
}

View File

@@ -0,0 +1,44 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.net;
/** Class to resolve a service over mDNS. */
public class MulticastServiceResolver implements AutoCloseable {
private final int m_handle;
/**
* Creates a MulticastServiceResolver.
*
* @param serviceType service type to look for
*/
public MulticastServiceResolver(String serviceType) {
m_handle = WPINetJNI.createMulticastServiceResolver(serviceType);
}
@Override
public void close() {
WPINetJNI.freeMulticastServiceResolver(m_handle);
}
public void start() {
WPINetJNI.startMulticastServiceResolver(m_handle);
}
public void stop() {
WPINetJNI.stopMulticastServiceResolver(m_handle);
}
public boolean hasImplementation() {
return WPINetJNI.getMulticastServiceResolverHasImplementation(m_handle);
}
public int getEventHandle() {
return WPINetJNI.getMulticastServiceResolverEventHandle(m_handle);
}
public ServiceData[] getData() {
return WPINetJNI.getMulticastServiceResolverData(m_handle);
}
}

View File

@@ -0,0 +1,36 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.net;
/**
* 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) {
WPINetJNI.addPortForwarder(port, remoteHost, remotePort);
}
/**
* Stop TCP forwarding on a port.
*
* @param port local port number
*/
public static void remove(int port) {
WPINetJNI.removePortForwarder(port);
}
}

View File

@@ -0,0 +1,66 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.net;
import java.util.HashMap;
import java.util.Map;
/** Service data for MulticastServiceResolver. */
public class ServiceData {
/**
* Constructs a ServiceData.
*
* @param ipv4Address ipv4Address
* @param port port
* @param serviceName found service name
* @param hostName found host name
* @param keys txt keys
* @param values txt values
*/
public ServiceData(
long ipv4Address,
int port,
String serviceName,
String hostName,
String[] keys,
String[] values) {
this.m_serviceName = serviceName;
this.m_hostName = hostName;
this.m_port = port;
this.m_ipv4Address = ipv4Address;
m_txt = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
m_txt.put(keys[i], values[i]);
}
}
public Map<String, String> getTxt() {
return m_txt;
}
public String getHostName() {
return m_hostName;
}
public String getServiceName() {
return m_serviceName;
}
public int getPort() {
return m_port;
}
public long getIpv4Address() {
return m_ipv4Address;
}
private final Map<String, String> m_txt;
private final long m_ipv4Address;
private final int m_port;
private final String m_serviceName;
private final String m_hostName;
}

View File

@@ -0,0 +1,85 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.net;
import edu.wpi.first.util.RuntimeLoader;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
public class WPINetJNI {
static boolean libraryLoaded = false;
static RuntimeLoader<WPINetJNI> 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<>(
"wpinetjni", RuntimeLoader.getDefaultExtractionRoot(), WPINetJNI.class);
loader.loadLibrary();
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1);
}
libraryLoaded = true;
}
}
/**
* Force load the library.
*
* @throws IOException if the library failed to load
*/
public static synchronized void forceLoad() throws IOException {
if (libraryLoaded) {
return;
}
loader =
new RuntimeLoader<>("wpinetjni", RuntimeLoader.getDefaultExtractionRoot(), WPINetJNI.class);
loader.loadLibrary();
libraryLoaded = true;
}
public static native void addPortForwarder(int port, String remoteHost, int remotePort);
public static native void removePortForwarder(int port);
public static native int createMulticastServiceAnnouncer(
String serviceName, String serviceType, int port, String[] keys, String[] values);
public static native void freeMulticastServiceAnnouncer(int handle);
public static native void startMulticastServiceAnnouncer(int handle);
public static native void stopMulticastServiceAnnouncer(int handle);
public static native boolean getMulticastServiceAnnouncerHasImplementation(int handle);
public static native int createMulticastServiceResolver(String serviceType);
public static native void freeMulticastServiceResolver(int handle);
public static native void startMulticastServiceResolver(int handle);
public static native void stopMulticastServiceResolver(int handle);
public static native boolean getMulticastServiceResolverHasImplementation(int handle);
public static native int getMulticastServiceResolverEventHandle(int handle);
public static native ServiceData[] getMulticastServiceResolverData(int handle);
}