/JmDNS/src/javax/jmdns/JmmDNS.java

https://github.com/hchapman/dmix · Java · 396 lines · 69 code · 40 blank · 287 comment · 7 complexity · 6386635a93a314ea2bef57c1e81e9482 MD5 · raw file

  1. /**
  2. *
  3. */
  4. package javax.jmdns;
  5. import java.io.Closeable;
  6. import java.io.IOException;
  7. import java.net.InetAddress;
  8. import java.util.Map;
  9. import java.util.concurrent.atomic.AtomicReference;
  10. import javax.jmdns.impl.JmmDNSImpl;
  11. /**
  12. * <p>
  13. * Java Multihomed Multicast DNS
  14. * </p>
  15. * Uses an underlying {@link JmDNS} instance for each {@link InetAddress} found on this computer.<br/>
  16. * This class will monitor network topology changes, and will create or destroy JmDNS instances as required. It is your responsibility to maintain services registration (hint: use a {@link NetworkTopologyListener}).<br/>
  17. * Most of this class methods have no notion of transaction: if an Exception is raised in the middle of execution, you may be in an incoherent state.
  18. * <p>
  19. * <b>Note:</b> This API is experimental and may change in the future please let us know what work and what does not work in you application.
  20. * </p>
  21. *
  22. * @author C&eacute;drik Lime, Pierre Frisch
  23. */
  24. public interface JmmDNS extends Closeable {
  25. /**
  26. * JmmDNS.Factory enable the creation of new instance of JmmDNS.
  27. */
  28. public static final class Factory {
  29. private static volatile JmmDNS _instance;
  30. /**
  31. * This interface defines a delegate to the EOClassDescriptionRegister class to enable subclassing.
  32. */
  33. public static interface ClassDelegate {
  34. /**
  35. * Allows the delegate the opportunity to construct and return a different JmmDNS.
  36. *
  37. * @return Should return a new JmmDNS.
  38. * @see #classDelegate()
  39. * @see #setClassDelegate(ClassDelegate anObject)
  40. */
  41. public JmmDNS newJmmDNS();
  42. }
  43. private static final AtomicReference<ClassDelegate> _databaseClassDelegate = new AtomicReference<ClassDelegate>();
  44. private Factory() {
  45. super();
  46. }
  47. /**
  48. * Assigns <code>delegate</code> as JmmDNS's class delegate. The class delegate is optional.
  49. *
  50. * @param delegate
  51. * The object to set as JmmDNS's class delegate.
  52. * @see #classDelegate()
  53. * @see JmmDNS.Factory.ClassDelegate
  54. */
  55. public static void setClassDelegate(ClassDelegate delegate) {
  56. _databaseClassDelegate.set(delegate);
  57. }
  58. /**
  59. * Returns JmmDNS's class delegate.
  60. *
  61. * @return JmmDNS's class delegate.
  62. * @see #setClassDelegate(ClassDelegate anObject)
  63. * @see JmmDNS.Factory.ClassDelegate
  64. */
  65. public static ClassDelegate classDelegate() {
  66. return _databaseClassDelegate.get();
  67. }
  68. /**
  69. * Returns a new instance of JmmDNS using the class delegate if it exists.
  70. *
  71. * @return new instance of JmmDNS
  72. */
  73. protected static JmmDNS newJmmDNS() {
  74. JmmDNS dns = null;
  75. ClassDelegate delegate = _databaseClassDelegate.get();
  76. if (delegate != null) {
  77. dns = delegate.newJmmDNS();
  78. }
  79. return (dns != null ? dns : new JmmDNSImpl());
  80. }
  81. /**
  82. * Return the instance of the Multihommed Multicast DNS.
  83. *
  84. * @return the JmmDNS
  85. */
  86. public static JmmDNS getInstance() {
  87. if (_instance == null) {
  88. synchronized (Factory.class) {
  89. if (_instance == null) {
  90. _instance = JmmDNS.Factory.newJmmDNS();
  91. }
  92. }
  93. }
  94. return _instance;
  95. }
  96. }
  97. /**
  98. * Return the names of the JmDNS instances.
  99. *
  100. * @return list of name of the JmDNS
  101. * @see javax.jmdns.JmDNS#getName()
  102. */
  103. public abstract String[] getNames();
  104. /**
  105. * Return the list HostName associated with this JmmDNS instance.
  106. *
  107. * @return list of host names
  108. * @see javax.jmdns.JmDNS#getHostName()
  109. */
  110. public abstract String[] getHostNames();
  111. /**
  112. * Return the list of addresses of the interface to which this instance of JmmDNS is bound.
  113. *
  114. * @return list of Internet Address
  115. * @exception IOException
  116. * @see javax.jmdns.JmDNS#getInterface()
  117. */
  118. public abstract InetAddress[] getInterfaces() throws IOException;
  119. /**
  120. * Get service information. If the information is not cached, the method will block until updated information is received on all DNS.
  121. * <p/>
  122. * Usage note: Do not call this method from the AWT event dispatcher thread. You will make the user interface unresponsive.
  123. *
  124. * @param type
  125. * fully qualified service type, such as <code>_http._tcp.local.</code> .
  126. * @param name
  127. * unqualified service name, such as <code>foobar</code> .
  128. * @return list of service info. If no service info is found the list is empty.
  129. * @see javax.jmdns.JmDNS#getServiceInfo(java.lang.String, java.lang.String)
  130. */
  131. public abstract ServiceInfo[] getServiceInfos(String type, String name);
  132. /**
  133. * Get service information. If the information is not cached, the method will block until updated information is received on all DNS.
  134. * <p/>
  135. * 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.
  136. *
  137. * @param type
  138. * full qualified service type, such as <code>_http._tcp.local.</code> .
  139. * @param name
  140. * unqualified service name, such as <code>foobar</code> .
  141. * @param timeout
  142. * timeout in milliseconds. Typical timeout should be 5s.
  143. * @return list of service info. If no service info is found the list is empty.
  144. * @see javax.jmdns.JmDNS#getServiceInfo(java.lang.String, java.lang.String, long)
  145. */
  146. public abstract ServiceInfo[] getServiceInfos(String type, String name, long timeout);
  147. /**
  148. * Get service information. If the information is not cached, the method will block until updated information is received on all DNS.
  149. * <p/>
  150. * 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.
  151. *
  152. * @param type
  153. * full qualified service type, such as <code>_http._tcp.local.</code> .
  154. * @param name
  155. * unqualified service name, such as <code>foobar</code> .
  156. * @param persistent
  157. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  158. * @return list of service info. If no service info is found the list is empty.
  159. * @see javax.jmdns.JmDNS#getServiceInfo(java.lang.String, java.lang.String, boolean)
  160. */
  161. public abstract ServiceInfo[] getServiceInfos(String type, String name, boolean persistent);
  162. /**
  163. * Get service information. If the information is not cached, the method will block until updated information is received on all DNS.
  164. * <p/>
  165. * 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.
  166. *
  167. * @param type
  168. * full qualified service type, such as <code>_http._tcp.local.</code> .
  169. * @param name
  170. * unqualified service name, such as <code>foobar</code> .
  171. * @param timeout
  172. * timeout in milliseconds. Typical timeout should be 5s.
  173. * @param persistent
  174. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  175. * @return list of service info. If no service info is found the list is empty.
  176. * @see javax.jmdns.JmDNS#getServiceInfo(java.lang.String, java.lang.String, boolean, long)
  177. */
  178. public abstract ServiceInfo[] getServiceInfos(String type, String name, boolean persistent, long timeout);
  179. /**
  180. * Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.
  181. *
  182. * @param type
  183. * full qualified service type, such as <code>_http._tcp.local.</code> .
  184. * @param name
  185. * unqualified service name, such as <code>foobar</code> .
  186. * @see javax.jmdns.JmDNS#requestServiceInfo(java.lang.String, java.lang.String)
  187. */
  188. public abstract void requestServiceInfo(String type, String name);
  189. /**
  190. * Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.
  191. *
  192. * @param type
  193. * full qualified service type, such as <code>_http._tcp.local.</code> .
  194. * @param name
  195. * unqualified service name, such as <code>foobar</code> .
  196. * @param persistent
  197. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  198. * @see javax.jmdns.JmDNS#requestServiceInfo(java.lang.String, java.lang.String, boolean)
  199. */
  200. public abstract void requestServiceInfo(String type, String name, boolean persistent);
  201. /**
  202. * Request service information. The information about the service is requested and the ServiceListener.resolveService method is called as soon as it is available.
  203. *
  204. * @param type
  205. * full qualified service type, such as <code>_http._tcp.local.</code> .
  206. * @param name
  207. * unqualified service name, such as <code>foobar</code> .
  208. * @param timeout
  209. * timeout in milliseconds
  210. * @see javax.jmdns.JmDNS#requestServiceInfo(java.lang.String, java.lang.String, long)
  211. */
  212. public abstract void requestServiceInfo(String type, String name, 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. *
  216. * @param type
  217. * full qualified service type, such as <code>_http._tcp.local.</code> .
  218. * @param name
  219. * unqualified service name, such as <code>foobar</code> .
  220. * @param persistent
  221. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  222. * @param timeout
  223. * timeout in milliseconds
  224. * @see javax.jmdns.JmDNS#requestServiceInfo(java.lang.String, java.lang.String, boolean, long)
  225. */
  226. public abstract void requestServiceInfo(String type, String name, boolean persistent, long timeout);
  227. /**
  228. * Listen for service types.
  229. *
  230. * @param listener
  231. * listener for service types
  232. * @exception IOException
  233. * @see javax.jmdns.JmDNS#addServiceTypeListener(javax.jmdns.ServiceTypeListener)
  234. */
  235. public abstract void addServiceTypeListener(ServiceTypeListener listener) throws IOException;
  236. /**
  237. * Remove listener for service types.
  238. *
  239. * @param listener
  240. * listener for service types
  241. * @see javax.jmdns.JmDNS#removeServiceTypeListener(javax.jmdns.ServiceTypeListener)
  242. */
  243. public abstract void removeServiceTypeListener(ServiceTypeListener listener);
  244. /**
  245. * Listen for services of a given type. The type has to be a fully qualified type name such as <code>_http._tcp.local.</code>.
  246. *
  247. * @param type
  248. * full qualified service type, such as <code>_http._tcp.local.</code>.
  249. * @param listener
  250. * listener for service updates
  251. * @see javax.jmdns.JmDNS#addServiceListener(java.lang.String, javax.jmdns.ServiceListener)
  252. */
  253. public abstract void addServiceListener(String type, ServiceListener listener);
  254. /**
  255. * Remove listener for services of a given type.
  256. *
  257. * @param type
  258. * full qualified service type, such as <code>_http._tcp.local.</code>.
  259. * @param listener
  260. * listener for service updates
  261. * @see javax.jmdns.JmDNS#removeServiceListener(java.lang.String, javax.jmdns.ServiceListener)
  262. */
  263. public abstract void removeServiceListener(String type, ServiceListener listener);
  264. /**
  265. * 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>
  266. * <b>Note</b> the Service info is cloned for each network interface.
  267. *
  268. * @param info
  269. * service info to register
  270. * @exception IOException
  271. * @see javax.jmdns.JmDNS#registerService(javax.jmdns.ServiceInfo)
  272. */
  273. public abstract void registerService(ServiceInfo info) throws IOException;
  274. /**
  275. * Unregister a service. The service should have been registered.
  276. *
  277. * @param info
  278. * service info to remove
  279. * @see javax.jmdns.JmDNS#unregisterService(javax.jmdns.ServiceInfo)
  280. */
  281. public abstract void unregisterService(ServiceInfo info);
  282. /**
  283. * Unregister all services.
  284. *
  285. * @see javax.jmdns.JmDNS#unregisterAllServices()
  286. */
  287. public abstract void unregisterAllServices();
  288. /**
  289. * Register a service type. If this service type was not already known, all service listeners will be notified of the new service type. Service types are automatically registered as they are discovered.
  290. *
  291. * @param type
  292. * full qualified service type, such as <code>_http._tcp.local.</code>.
  293. * @see javax.jmdns.JmDNS#registerServiceType(java.lang.String)
  294. */
  295. public abstract void registerServiceType(String type);
  296. /**
  297. * Returns a list of service infos of the specified type.
  298. *
  299. * @param type
  300. * Service type name, such as <code>_http._tcp.local.</code>.
  301. * @return An array of service instance.
  302. * @see javax.jmdns.JmDNS#list(java.lang.String)
  303. */
  304. public abstract ServiceInfo[] list(String type);
  305. /**
  306. * Returns a list of service infos of the specified type.
  307. *
  308. * @param type
  309. * Service type name, such as <code>_http._tcp.local.</code>.
  310. * @param timeout
  311. * timeout in milliseconds. Typical timeout should be 6s.
  312. * @return An array of service instance.
  313. * @see javax.jmdns.JmDNS#list(java.lang.String, long)
  314. */
  315. public abstract ServiceInfo[] list(String type, long timeout);
  316. /**
  317. * 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.
  318. *
  319. * @param type
  320. * Service type name, such as <code>_http._tcp.local.</code>.
  321. * @return A dictionary of service info by subtypes.
  322. * @see javax.jmdns.JmDNS#listBySubtype(java.lang.String)
  323. */
  324. public abstract Map<String, ServiceInfo[]> listBySubtype(String type);
  325. /**
  326. * 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.
  327. *
  328. * @param type
  329. * Service type name, such as <code>_http._tcp.local.</code>.
  330. * @param timeout
  331. * timeout in milliseconds. Typical timeout should be 6s.
  332. * @return A dictionary of service info by subtypes.
  333. * @see javax.jmdns.JmDNS#listBySubtype(java.lang.String, long)
  334. */
  335. public abstract Map<String, ServiceInfo[]> listBySubtype(String type, long timeout);
  336. /**
  337. * Listen to network changes.
  338. *
  339. * @param listener
  340. * listener for network changes
  341. */
  342. public abstract void addNetworkTopologyListener(NetworkTopologyListener listener);
  343. /**
  344. * Remove listener for network changes.
  345. *
  346. * @param listener
  347. * listener for network changes
  348. */
  349. public abstract void removeNetworkTopologyListener(NetworkTopologyListener listener);
  350. /**
  351. * Returns list of network change listeners
  352. *
  353. * @return list of network change listeners
  354. */
  355. public abstract NetworkTopologyListener[] networkListeners();
  356. }