[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

@@ -1,45 +0,0 @@
// 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.util;
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 =
WPIUtilJNI.createMulticastServiceAnnouncer(serviceName, serviceType, port, keys, values);
}
@Override
public void close() {
WPIUtilJNI.freeMulticastServiceAnnouncer(m_handle);
}
public void start() {
WPIUtilJNI.startMulticastServiceAnnouncer(m_handle);
}
public void stop() {
WPIUtilJNI.stopMulticastServiceAnnouncer(m_handle);
}
public boolean hasImplementation() {
return WPIUtilJNI.getMulticastServiceAnnouncerHasImplementation(m_handle);
}
}

View File

@@ -1,44 +0,0 @@
// 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.util;
/** 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 = WPIUtilJNI.createMulticastServiceResolver(serviceType);
}
@Override
public void close() {
WPIUtilJNI.freeMulticastServiceResolver(m_handle);
}
public void start() {
WPIUtilJNI.startMulticastServiceResolver(m_handle);
}
public void stop() {
WPIUtilJNI.stopMulticastServiceResolver(m_handle);
}
public boolean hasImplementation() {
return WPIUtilJNI.getMulticastServiceResolverHasImplementation(m_handle);
}
public int getEventHandle() {
return WPIUtilJNI.getMulticastServiceResolverEventHandle(m_handle);
}
public ServiceData[] getData() {
return WPIUtilJNI.getMulticastServiceResolverData(m_handle);
}
}

View File

@@ -1,66 +0,0 @@
// 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.util;
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

@@ -64,10 +64,6 @@ public class WPIUtilJNI {
public static native long getSystemTime();
public static native void addPortForwarder(int port, String remoteHost, int remotePort);
public static native void removePortForwarder(int port);
public static native int createEvent(boolean manualReset, boolean initialState);
public static native void destroyEvent(int eventHandle);
@@ -126,29 +122,4 @@ public class WPIUtilJNI {
*/
public static native int[] waitForObjectsTimeout(int[] handles, double timeout)
throws InterruptedException;
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);
}

View File

@@ -1,38 +0,0 @@
// 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.util.net;
import edu.wpi.first.util.WPIUtilJNI;
/**
* 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);
}
}