mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpiutil] Add mDNS resolver and announcer (#3733)
This commit is contained in:
@@ -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.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);
|
||||
}
|
||||
}
|
||||
@@ -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.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);
|
||||
}
|
||||
}
|
||||
66
wpiutil/src/main/java/edu/wpi/first/util/ServiceData.java
Normal file
66
wpiutil/src/main/java/edu/wpi/first/util/ServiceData.java
Normal 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.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;
|
||||
}
|
||||
@@ -122,4 +122,29 @@ public final 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user