/external/jmdns/src/javax/jmdns/JmDNS.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 433 lines · 54 code · 40 blank · 339 comment · 0 complexity · 6bddc1107cd25a3fffafa24071283932 MD5 · raw file

  1. // /Copyright 2003-2005 Arthur van Hoff, Rick Blair
  2. // Licensed under Apache License version 2.0
  3. // Original license LGPL
  4. package javax.jmdns;
  5. import java.io.Closeable;
  6. import java.io.IOException;
  7. import java.net.InetAddress;
  8. import java.util.Collection;
  9. import java.util.Map;
  10. import javax.jmdns.impl.JmDNSImpl;
  11. /**
  12. * mDNS implementation in Java.
  13. *
  14. * @author Arthur van Hoff, Rick Blair, Jeff Sonstein, Werner Randelshofer, Pierre Frisch, Scott Lewis, Scott Cytacki
  15. */
  16. public abstract class JmDNS implements Closeable {
  17. /**
  18. *
  19. */
  20. public static interface Delegate {
  21. /**
  22. * This method is called if JmDNS cannot recover from an I/O error.
  23. *
  24. * @param dns
  25. * target DNS
  26. * @param infos
  27. * service info registered with the DNS
  28. */
  29. public void cannotRecoverFromIOError(JmDNS dns, Collection<ServiceInfo> infos);
  30. }
  31. /**
  32. * The version of JmDNS.
  33. */
  34. public static final String VERSION = "3.4.2";
  35. /**
  36. * <p>
  37. * Create an instance of JmDNS.
  38. * </p>
  39. * <p>
  40. * <b>Note:</b> This is a convenience method. The preferred constructor is {@link #create(InetAddress, String)}.<br/>
  41. * Check that your platform correctly handle the default localhost IP address and the local hostname. In doubt use the explicit constructor.<br/>
  42. * This call is equivalent to <code>create(null, null)</code>.
  43. * </p>
  44. *
  45. * @see #create(InetAddress, String)
  46. * @return jmDNS instance
  47. * @exception IOException
  48. * if an exception occurs during the socket creation
  49. */
  50. public static JmDNS create() throws IOException {
  51. return new JmDNSImpl(null, null);
  52. }
  53. /**
  54. * <p>
  55. * Create an instance of JmDNS and bind it to a specific network interface given its IP-address.
  56. * </p>
  57. * <p>
  58. * <b>Note:</b> This is a convenience method. The preferred constructor is {@link #create(InetAddress, String)}.<br/>
  59. * Check that your platform correctly handle the default localhost IP address and the local hostname. In doubt use the explicit constructor.<br/>
  60. * This call is equivalent to <code>create(addr, null)</code>.
  61. * </p>
  62. *
  63. * @see #create(InetAddress, String)
  64. * @param addr
  65. * IP address to bind to.
  66. * @return jmDNS instance
  67. * @exception IOException
  68. * if an exception occurs during the socket creation
  69. */
  70. public static JmDNS create(final InetAddress addr) throws IOException {
  71. return new JmDNSImpl(addr, null);
  72. }
  73. /**
  74. * <p>
  75. * Create an instance of JmDNS.
  76. * </p>
  77. * <p>
  78. * <b>Note:</b> This is a convenience method. The preferred constructor is {@link #create(InetAddress, String)}.<br/>
  79. * Check that your platform correctly handle the default localhost IP address and the local hostname. In doubt use the explicit constructor.<br/>
  80. * This call is equivalent to <code>create(null, name)</code>.
  81. * </p>
  82. *
  83. * @see #create(InetAddress, String)
  84. * @param name
  85. * name of the newly created JmDNS
  86. * @return jmDNS instance
  87. * @exception IOException
  88. * if an exception occurs during the socket creation
  89. */
  90. public static JmDNS create(final String name) throws IOException {
  91. return new JmDNSImpl(null, name);
  92. }
  93. /**
  94. * <p>
  95. * Create an instance of JmDNS and bind it to a specific network interface given its IP-address.
  96. * </p>
  97. * If <code>addr</code> parameter is null this method will try to resolve to a local IP address of the machine using a network discovery:
  98. * <ol>
  99. * <li>Check the system property <code>net.mdns.interface</code></li>
  100. * <li>Check the JVM local host</li>
  101. * <li>Use the {@link NetworkTopologyDiscovery} to find a valid network interface and IP.</li>
  102. * <li>In the last resort bind to the loopback address. This is non functional in most cases.</li>
  103. * </ol>
  104. * If <code>name</code> parameter is null will use the hostname. The hostname is determined by the following algorithm:
  105. * <ol>
  106. * <li>Get the hostname from the InetAdress obtained before.</li>
  107. * <li>If the hostname is a reverse lookup default to <code>JmDNS name</code> or <code>computer</code> if null.</li>
  108. * <li>If the name contains <code>'.'</code> replace them by <code>'-'</code></li>
  109. * <li>Add <code>.local.</code> at the end of the name.</li>
  110. * </ol>
  111. * <p>
  112. * <b>Note:</b> If you need to use a custom {@link NetworkTopologyDiscovery} it must be setup before any call to this method. This is done by setting up a {@link NetworkTopologyDiscovery.Factory.ClassDelegate} and installing it using
  113. * {@link NetworkTopologyDiscovery.Factory#setClassDelegate(NetworkTopologyDiscovery.Factory.ClassDelegate)}. This must be done before creating a {@link JmDNS} or {@link JmmDNS} instance.
  114. * </p>
  115. *
  116. * @param addr
  117. * IP address to bind to.
  118. * @param name
  119. * name of the newly created JmDNS
  120. * @return jmDNS instance
  121. * @exception IOException
  122. * if an exception occurs during the socket creation
  123. */
  124. public static JmDNS create(final InetAddress addr, final String name) throws IOException {
  125. return new JmDNSImpl(addr, name);
  126. }
  127. /**
  128. * Return the name of the JmDNS instance. This is an arbitrary string that is useful for distinguishing instances.
  129. *
  130. * @return name of the JmDNS
  131. */
  132. public abstract String getName();
  133. /**
  134. * Return the HostName associated with this JmDNS instance. Note: May not be the same as what started. The host name is subject to negotiation.
  135. *
  136. * @return Host name
  137. */
  138. public abstract String getHostName();
  139. /**
  140. * Return the address of the interface to which this instance of JmDNS is bound.
  141. *
  142. * @return Internet Address
  143. * @exception IOException
  144. * if there is an error in the underlying protocol, such as a TCP error.
  145. */
  146. public abstract InetAddress getInetAddress() throws IOException;
  147. /**
  148. * Return the address of the interface to which this instance of JmDNS is bound.
  149. *
  150. * @return Internet Address
  151. * @exception IOException
  152. * if there is an error in the underlying protocol, such as a TCP error.
  153. * @deprecated do not use this implementation yields unpredictable results use {@link #getInetAddress()}
  154. */
  155. @Deprecated
  156. public abstract InetAddress getInterface() throws IOException;
  157. /**
  158. * Get service information. If the information is not cached, the method will block until updated information is received.
  159. * <p/>
  160. * Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.
  161. *
  162. * @param type
  163. * fully qualified service type, such as <code>_http._tcp.local.</code> .
  164. * @param name
  165. * unqualified service name, such as <code>foobar</code> .
  166. * @return null if the service information cannot be obtained
  167. */
  168. public abstract ServiceInfo getServiceInfo(String type, String name);
  169. /**
  170. * Get service information. If the information is not cached, the method will block for the given timeout until updated information is received.
  171. * <p/>
  172. * Usage note: If you call this method from the AWT event dispatcher thread, use a small timeout, or you will make the user interface unresponsive.
  173. *
  174. * @param type
  175. * full qualified service type, such as <code>_http._tcp.local.</code> .
  176. * @param name
  177. * unqualified service name, such as <code>foobar</code> .
  178. * @param timeout
  179. * timeout in milliseconds. Typical timeout should be 5s.
  180. * @return null if the service information cannot be obtained
  181. */
  182. public abstract ServiceInfo getServiceInfo(String type, String name, long timeout);
  183. /**
  184. * Get service information. If the information is not cached, the method will block until updated information is received.
  185. * <p/>
  186. * Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.
  187. *
  188. * @param type
  189. * fully qualified service type, such as <code>_http._tcp.local.</code> .
  190. * @param name
  191. * unqualified service name, such as <code>foobar</code> .
  192. * @param persistent
  193. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  194. * @return null if the service information cannot be obtained
  195. */
  196. public abstract ServiceInfo getServiceInfo(String type, String name, boolean persistent);
  197. /**
  198. * Get service information. If the information is not cached, the method will block for the given timeout until updated information is received.
  199. * <p/>
  200. * Usage note: If you call this method from the AWT event dispatcher thread, use a small timeout, or you will make the user interface unresponsive.
  201. *
  202. * @param type
  203. * full qualified service type, such as <code>_http._tcp.local.</code> .
  204. * @param name
  205. * unqualified service name, such as <code>foobar</code> .
  206. * @param timeout
  207. * timeout in milliseconds. Typical timeout should be 5s.
  208. * @param persistent
  209. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  210. * @return null if the service information cannot be obtained
  211. */
  212. public abstract ServiceInfo getServiceInfo(String type, String name, boolean persistent, long timeout);
  213. /**
  214. * Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.
  215. * <p/>
  216. * Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.
  217. *
  218. * @param type
  219. * full qualified service type, such as <code>_http._tcp.local.</code> .
  220. * @param name
  221. * unqualified service name, such as <code>foobar</code> .
  222. */
  223. public abstract void requestServiceInfo(String type, String name);
  224. /**
  225. * Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.
  226. * <p/>
  227. * Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.
  228. *
  229. * @param type
  230. * full qualified service type, such as <code>_http._tcp.local.</code> .
  231. * @param name
  232. * unqualified service name, such as <code>foobar</code> .
  233. * @param persistent
  234. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  235. */
  236. public abstract void requestServiceInfo(String type, String name, boolean persistent);
  237. /**
  238. * Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.
  239. *
  240. * @param type
  241. * full qualified service type, such as <code>_http._tcp.local.</code> .
  242. * @param name
  243. * unqualified service name, such as <code>foobar</code> .
  244. * @param timeout
  245. * timeout in milliseconds
  246. */
  247. public abstract void requestServiceInfo(String type, String name, long timeout);
  248. /**
  249. * Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.
  250. *
  251. * @param type
  252. * full qualified service type, such as <code>_http._tcp.local.</code> .
  253. * @param name
  254. * unqualified service name, such as <code>foobar</code> .
  255. * @param persistent
  256. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  257. * @param timeout
  258. * timeout in milliseconds
  259. */
  260. public abstract void requestServiceInfo(String type, String name, boolean persistent, long timeout);
  261. /**
  262. * Listen for service types.
  263. *
  264. * @param listener
  265. * listener for service types
  266. * @exception IOException
  267. * if there is an error in the underlying protocol, such as a TCP error.
  268. */
  269. public abstract void addServiceTypeListener(ServiceTypeListener listener) throws IOException;
  270. /**
  271. * Remove listener for service types.
  272. *
  273. * @param listener
  274. * listener for service types
  275. */
  276. public abstract void removeServiceTypeListener(ServiceTypeListener listener);
  277. /**
  278. * Listen for services of a given type. The type has to be a fully qualified type name such as <code>_http._tcp.local.</code>.
  279. *
  280. * @param type
  281. * full qualified service type, such as <code>_http._tcp.local.</code>.
  282. * @param listener
  283. * listener for service updates
  284. */
  285. public abstract void addServiceListener(String type, ServiceListener listener);
  286. /**
  287. * Remove listener for services of a given type.
  288. *
  289. * @param type
  290. * full qualified service type, such as <code>_http._tcp.local.</code>.
  291. * @param listener
  292. * listener for service updates
  293. */
  294. public abstract void removeServiceListener(String type, ServiceListener listener);
  295. /**
  296. * Register a service. The service is registered for access by other jmdns clients. The name of the service may be changed to make it unique.<br>
  297. * Note that the given {@code ServiceInfo} is bound to this {@code JmDNS} instance, and should not be reused for any other {@linkplain #registerService(ServiceInfo)}.
  298. *
  299. * @param info
  300. * service info to register
  301. * @exception IOException
  302. * if there is an error in the underlying protocol, such as a TCP error.
  303. */
  304. public abstract void registerService(ServiceInfo info) throws IOException;
  305. /**
  306. * Unregister a service. The service should have been registered.
  307. * <p>
  308. * <b>Note:</b> Unregistered services will not disappear form the list of services immediately. According to the specification, when unregistering services we send goodbye packets and then wait <b>1s</b> before purging the cache.<br/>
  309. * This is support for shared records that can be rescued by some other cooperation DNS.
  310. *
  311. * <pre>
  312. * Clients receiving a Multicast DNS Response with a TTL of zero SHOULD NOT immediately delete the record from the cache, but instead record a TTL of 1 and then delete the record one second later.
  313. * </pre>
  314. *
  315. * </p>
  316. *
  317. * @param info
  318. * service info to remove
  319. */
  320. public abstract void unregisterService(ServiceInfo info);
  321. /**
  322. * Unregister all services.
  323. */
  324. public abstract void unregisterAllServices();
  325. /**
  326. * Register a service type. If this service type was not already known, all service listeners will be notified of the new service type.
  327. * <p>
  328. * Service types are automatically registered as they are discovered.
  329. * </p>
  330. *
  331. * @param type
  332. * full qualified service type, such as <code>_http._tcp.local.</code>.
  333. * @return <code>true</code> if the type or subtype was added, <code>false</code> if the type was already registered.
  334. */
  335. public abstract boolean registerServiceType(String type);
  336. /**
  337. * List Services and serviceTypes. Debugging Only
  338. *
  339. * @deprecated since 3.2.2
  340. */
  341. @Deprecated
  342. public abstract void printServices();
  343. /**
  344. * Returns a list of service infos of the specified type.
  345. *
  346. * @param type
  347. * Service type name, such as <code>_http._tcp.local.</code>.
  348. * @return An array of service instance.
  349. */
  350. public abstract ServiceInfo[] list(String type);
  351. /**
  352. * Returns a list of service infos of the specified type.
  353. *
  354. * @param type
  355. * Service type name, such as <code>_http._tcp.local.</code>.
  356. * @param timeout
  357. * timeout in milliseconds. Typical timeout should be 6s.
  358. * @return An array of service instance.
  359. */
  360. public abstract ServiceInfo[] list(String type, long timeout);
  361. /**
  362. * Returns a list of service infos of the specified type sorted by subtype. Any service that do not register a subtype is listed in the empty subtype section.
  363. *
  364. * @param type
  365. * Service type name, such as <code>_http._tcp.local.</code>.
  366. * @return A dictionary of service info by subtypes.
  367. */
  368. public abstract Map<String, ServiceInfo[]> listBySubtype(String type);
  369. /**
  370. * Returns a list of service infos of the specified type sorted by subtype. Any service that do not register a subtype is listed in the empty subtype section.
  371. *
  372. * @param type
  373. * Service type name, such as <code>_http._tcp.local.</code>.
  374. * @param timeout
  375. * timeout in milliseconds. Typical timeout should be 6s.
  376. * @return A dictionary of service info by subtypes.
  377. */
  378. public abstract Map<String, ServiceInfo[]> listBySubtype(String type, long timeout);
  379. /**
  380. * Returns the instance delegate
  381. *
  382. * @return instance delegate
  383. */
  384. public abstract Delegate getDelegate();
  385. /**
  386. * Sets the instance delegate
  387. *
  388. * @param value
  389. * new instance delegate
  390. * @return previous instance delegate
  391. */
  392. public abstract Delegate setDelegate(Delegate value);
  393. }