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.util;
006
007import java.util.Map;
008
009/** Class to announce over mDNS that a service is available. */
010public class MulticastServiceAnnouncer implements AutoCloseable {
011  private final int m_handle;
012
013  /**
014   * Creates a MulticastServiceAnnouncer.
015   *
016   * @param serviceName service name
017   * @param serviceType service type
018   * @param port port
019   * @param txt txt
020   */
021  public MulticastServiceAnnouncer(
022      String serviceName, String serviceType, int port, Map<String, String> txt) {
023    String[] keys = txt.keySet().toArray(String[]::new);
024    String[] values = txt.values().toArray(String[]::new);
025    m_handle =
026        WPIUtilJNI.createMulticastServiceAnnouncer(serviceName, serviceType, port, keys, values);
027  }
028
029  @Override
030  public void close() {
031    WPIUtilJNI.freeMulticastServiceAnnouncer(m_handle);
032  }
033
034  public void start() {
035    WPIUtilJNI.startMulticastServiceAnnouncer(m_handle);
036  }
037
038  public void stop() {
039    WPIUtilJNI.stopMulticastServiceAnnouncer(m_handle);
040  }
041
042  public boolean hasImplementation() {
043    return WPIUtilJNI.getMulticastServiceAnnouncerHasImplementation(m_handle);
044  }
045}