001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.net;
006
007import edu.wpi.first.util.WPICleaner;
008import java.lang.ref.Cleaner.Cleanable;
009
010/** Class to resolve a service over mDNS. */
011public class MulticastServiceResolver implements AutoCloseable {
012  private final int m_handle;
013  private final Cleanable m_cleanable;
014
015  private static Runnable cleanupAction(int handle) {
016    return () -> WPINetJNI.freeMulticastServiceResolver(handle);
017  }
018
019  /**
020   * Creates a MulticastServiceResolver.
021   *
022   * @param serviceType service type to look for
023   */
024  @SuppressWarnings("this-escape")
025  public MulticastServiceResolver(String serviceType) {
026    m_handle = WPINetJNI.createMulticastServiceResolver(serviceType);
027    m_cleanable = WPICleaner.register(this, cleanupAction(m_handle));
028  }
029
030  @Override
031  public void close() {
032    m_cleanable.clean();
033  }
034
035  public void start() {
036    WPINetJNI.startMulticastServiceResolver(m_handle);
037  }
038
039  public void stop() {
040    WPINetJNI.stopMulticastServiceResolver(m_handle);
041  }
042
043  public boolean hasImplementation() {
044    return WPINetJNI.getMulticastServiceResolverHasImplementation(m_handle);
045  }
046
047  public int getEventHandle() {
048    return WPINetJNI.getMulticastServiceResolverEventHandle(m_handle);
049  }
050
051  public ServiceData[] getData() {
052    return WPINetJNI.getMulticastServiceResolverData(m_handle);
053  }
054}