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 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        WPINetJNI.createMulticastServiceAnnouncer(serviceName, serviceType, port, keys, values);
027  }
028
029  /**
030   * Creates a MulticastServiceAnnouncer.
031   *
032   * @param serviceName service name
033   * @param serviceType service type
034   * @param port port
035   */
036  public MulticastServiceAnnouncer(String serviceName, String serviceType, int port) {
037    m_handle =
038        WPINetJNI.createMulticastServiceAnnouncer(serviceName, serviceType, port, null, null);
039  }
040
041  @Override
042  public void close() {
043    WPINetJNI.freeMulticastServiceAnnouncer(m_handle);
044  }
045
046  public void start() {
047    WPINetJNI.startMulticastServiceAnnouncer(m_handle);
048  }
049
050  public void stop() {
051    WPINetJNI.stopMulticastServiceAnnouncer(m_handle);
052  }
053
054  public boolean hasImplementation() {
055    return WPINetJNI.getMulticastServiceAnnouncerHasImplementation(m_handle);
056  }
057}