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 public MulticastServiceResolver(String serviceType) { 025 m_handle = WPINetJNI.createMulticastServiceResolver(serviceType); 026 m_cleanable = WPICleaner.register(this, cleanupAction(m_handle)); 027 } 028 029 @Override 030 public void close() { 031 m_cleanable.clean(); 032 } 033 034 public void start() { 035 WPINetJNI.startMulticastServiceResolver(m_handle); 036 } 037 038 public void stop() { 039 WPINetJNI.stopMulticastServiceResolver(m_handle); 040 } 041 042 public boolean hasImplementation() { 043 return WPINetJNI.getMulticastServiceResolverHasImplementation(m_handle); 044 } 045 046 public int getEventHandle() { 047 return WPINetJNI.getMulticastServiceResolverEventHandle(m_handle); 048 } 049 050 public ServiceData[] getData() { 051 return WPINetJNI.getMulticastServiceResolverData(m_handle); 052 } 053}