/external/jmdns/src/javax/jmdns/ServiceInfo.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 727 lines · 115 code · 59 blank · 553 comment · 0 complexity · 29d1a8db136285f501ab5d9a90542da2 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.net.Inet4Address;
  6. import java.net.Inet6Address;
  7. import java.net.InetAddress;
  8. import java.util.Enumeration;
  9. import java.util.Map;
  10. import javax.jmdns.impl.ServiceInfoImpl;
  11. /**
  12. * <p>
  13. * The fully qualified service name is build using up to 5 components with the following structure:
  14. *
  15. * <pre>
  16. * &lt;app&gt;.&lt;protocol&gt;.&lt;servicedomain&gt;.&lt;parentdomain&gt;.<br/>
  17. * &lt;Instance&gt;.&lt;app&gt;.&lt;protocol&gt;.&lt;servicedomain&gt;.&lt;parentdomain&gt;.<br/>
  18. * &lt;sub&gt;._sub.&lt;app&gt;.&lt;protocol&gt;.&lt;servicedomain&gt;.&lt;parentdomain&gt;.
  19. * </pre>
  20. *
  21. * <ol>
  22. * <li>&lt;servicedomain&gt;.&lt;parentdomain&gt;: This is the domain scope of the service typically "local.", but this can also be something similar to "in-addr.arpa." or "ip6.arpa."</li>
  23. * <li>&lt;protocol&gt;: This is either "_tcp" or "_udp"</li>
  24. * <li>&lt;app&gt;: This define the application protocol. Typical example are "_http", "_ftp", etc.</li>
  25. * <li>&lt;Instance&gt;: This is the service name</li>
  26. * <li>&lt;sub&gt;: This is the subtype for the application protocol</li>
  27. * </ol>
  28. * </p>
  29. */
  30. public abstract class ServiceInfo implements Cloneable {
  31. /**
  32. * This is the no value text byte. According top the specification it is one byte with 0 value.
  33. */
  34. public static final byte[] NO_VALUE = new byte[0];
  35. /**
  36. * Fields for the fully qualified map.
  37. */
  38. public enum Fields {
  39. /**
  40. * Domain Field.
  41. */
  42. Domain,
  43. /**
  44. * Protocol Field.
  45. */
  46. Protocol,
  47. /**
  48. * Application Field.
  49. */
  50. Application,
  51. /**
  52. * Instance Field.
  53. */
  54. Instance,
  55. /**
  56. * Subtype Field.
  57. */
  58. Subtype
  59. }
  60. /**
  61. * Construct a service description for registering with JmDNS.
  62. *
  63. * @param type
  64. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  65. * @param name
  66. * unqualified service instance name, such as <code>foobar</code>
  67. * @param port
  68. * the local port on which the service runs
  69. * @param text
  70. * string describing the service
  71. * @return new service info
  72. */
  73. public static ServiceInfo create(final String type, final String name, final int port, final String text) {
  74. return new ServiceInfoImpl(type, name, "", port, 0, 0, false, text);
  75. }
  76. /**
  77. * Construct a service description for registering with JmDNS.
  78. *
  79. * @param type
  80. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  81. * @param name
  82. * unqualified service instance name, such as <code>foobar</code>
  83. * @param subtype
  84. * service subtype see draft-cheshire-dnsext-dns-sd-06.txt chapter 7.1 Selective Instance Enumeration
  85. * @param port
  86. * the local port on which the service runs
  87. * @param text
  88. * string describing the service
  89. * @return new service info
  90. */
  91. public static ServiceInfo create(final String type, final String name, final String subtype, final int port, final String text) {
  92. return new ServiceInfoImpl(type, name, subtype, port, 0, 0, false, text);
  93. }
  94. /**
  95. * Construct a service description for registering with JmDNS.
  96. *
  97. * @param type
  98. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  99. * @param name
  100. * unqualified service instance name, such as <code>foobar</code>
  101. * @param port
  102. * the local port on which the service runs
  103. * @param weight
  104. * weight of the service
  105. * @param priority
  106. * priority of the service
  107. * @param text
  108. * string describing the service
  109. * @return new service info
  110. */
  111. public static ServiceInfo create(final String type, final String name, final int port, final int weight, final int priority, final String text) {
  112. return new ServiceInfoImpl(type, name, "", port, weight, priority, false, text);
  113. }
  114. /**
  115. * Construct a service description for registering with JmDNS.
  116. *
  117. * @param type
  118. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  119. * @param name
  120. * unqualified service instance name, such as <code>foobar</code>
  121. * @param subtype
  122. * service subtype see draft-cheshire-dnsext-dns-sd-06.txt chapter 7.1 Selective Instance Enumeration
  123. * @param port
  124. * the local port on which the service runs
  125. * @param weight
  126. * weight of the service
  127. * @param priority
  128. * priority of the service
  129. * @param text
  130. * string describing the service
  131. * @return new service info
  132. */
  133. public static ServiceInfo create(final String type, final String name, final String subtype, final int port, final int weight, final int priority, final String text) {
  134. return new ServiceInfoImpl(type, name, subtype, port, weight, priority, false, text);
  135. }
  136. /**
  137. * Construct a service description for registering with JmDNS. The properties hashtable must map property names to either Strings or byte arrays describing the property values.
  138. *
  139. * @param type
  140. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  141. * @param name
  142. * unqualified service instance name, such as <code>foobar</code>
  143. * @param port
  144. * the local port on which the service runs
  145. * @param weight
  146. * weight of the service
  147. * @param priority
  148. * priority of the service
  149. * @param props
  150. * properties describing the service
  151. * @return new service info
  152. */
  153. public static ServiceInfo create(final String type, final String name, final int port, final int weight, final int priority, final Map<String, ?> props) {
  154. return new ServiceInfoImpl(type, name, "", port, weight, priority, false, props);
  155. }
  156. /**
  157. * Construct a service description for registering with JmDNS. The properties hashtable must map property names to either Strings or byte arrays describing the property values.
  158. *
  159. * @param type
  160. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  161. * @param name
  162. * unqualified service instance name, such as <code>foobar</code>
  163. * @param subtype
  164. * service subtype see draft-cheshire-dnsext-dns-sd-06.txt chapter 7.1 Selective Instance Enumeration
  165. * @param port
  166. * the local port on which the service runs
  167. * @param weight
  168. * weight of the service
  169. * @param priority
  170. * priority of the service
  171. * @param props
  172. * properties describing the service
  173. * @return new service info
  174. */
  175. public static ServiceInfo create(final String type, final String name, final String subtype, final int port, final int weight, final int priority, final Map<String, ?> props) {
  176. return new ServiceInfoImpl(type, name, subtype, port, weight, priority, false, props);
  177. }
  178. /**
  179. * Construct a service description for registering with JmDNS.
  180. *
  181. * @param type
  182. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  183. * @param name
  184. * unqualified service instance name, such as <code>foobar</code>
  185. * @param port
  186. * the local port on which the service runs
  187. * @param weight
  188. * weight of the service
  189. * @param priority
  190. * priority of the service
  191. * @param text
  192. * bytes describing the service
  193. * @return new service info
  194. */
  195. public static ServiceInfo create(final String type, final String name, final int port, final int weight, final int priority, final byte[] text) {
  196. return new ServiceInfoImpl(type, name, "", port, weight, priority, false, text);
  197. }
  198. /**
  199. * Construct a service description for registering with JmDNS.
  200. *
  201. * @param type
  202. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  203. * @param name
  204. * unqualified service instance name, such as <code>foobar</code>
  205. * @param subtype
  206. * service subtype see draft-cheshire-dnsext-dns-sd-06.txt chapter 7.1 Selective Instance Enumeration
  207. * @param port
  208. * the local port on which the service runs
  209. * @param weight
  210. * weight of the service
  211. * @param priority
  212. * priority of the service
  213. * @param text
  214. * bytes describing the service
  215. * @return new service info
  216. */
  217. public static ServiceInfo create(final String type, final String name, final String subtype, final int port, final int weight, final int priority, final byte[] text) {
  218. return new ServiceInfoImpl(type, name, subtype, port, weight, priority, false, text);
  219. }
  220. /**
  221. * Construct a service description for registering with JmDNS.
  222. *
  223. * @param type
  224. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  225. * @param name
  226. * unqualified service instance name, such as <code>foobar</code>
  227. * @param port
  228. * the local port on which the service runs
  229. * @param weight
  230. * weight of the service
  231. * @param priority
  232. * priority of the service
  233. * @param persistent
  234. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  235. * @param text
  236. * string describing the service
  237. * @return new service info
  238. */
  239. public static ServiceInfo create(final String type, final String name, final int port, final int weight, final int priority, final boolean persistent, final String text) {
  240. return new ServiceInfoImpl(type, name, "", port, weight, priority, persistent, text);
  241. }
  242. /**
  243. * Construct a service description for registering with JmDNS.
  244. *
  245. * @param type
  246. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  247. * @param name
  248. * unqualified service instance name, such as <code>foobar</code>
  249. * @param subtype
  250. * service subtype see draft-cheshire-dnsext-dns-sd-06.txt chapter 7.1 Selective Instance Enumeration
  251. * @param port
  252. * the local port on which the service runs
  253. * @param weight
  254. * weight of the service
  255. * @param priority
  256. * priority of the service
  257. * @param persistent
  258. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  259. * @param text
  260. * string describing the service
  261. * @return new service info
  262. */
  263. public static ServiceInfo create(final String type, final String name, final String subtype, final int port, final int weight, final int priority, final boolean persistent, final String text) {
  264. return new ServiceInfoImpl(type, name, subtype, port, weight, priority, persistent, text);
  265. }
  266. /**
  267. * Construct a service description for registering with JmDNS. The properties hashtable must map property names to either Strings or byte arrays describing the property values.
  268. *
  269. * @param type
  270. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  271. * @param name
  272. * unqualified service instance name, such as <code>foobar</code>
  273. * @param port
  274. * the local port on which the service runs
  275. * @param weight
  276. * weight of the service
  277. * @param priority
  278. * priority of the service
  279. * @param persistent
  280. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  281. * @param props
  282. * properties describing the service
  283. * @return new service info
  284. */
  285. public static ServiceInfo create(final String type, final String name, final int port, final int weight, final int priority, final boolean persistent, final Map<String, ?> props) {
  286. return new ServiceInfoImpl(type, name, "", port, weight, priority, persistent, props);
  287. }
  288. /**
  289. * Construct a service description for registering with JmDNS. The properties hashtable must map property names to either Strings or byte arrays describing the property values.
  290. *
  291. * @param type
  292. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  293. * @param name
  294. * unqualified service instance name, such as <code>foobar</code>
  295. * @param subtype
  296. * service subtype see draft-cheshire-dnsext-dns-sd-06.txt chapter 7.1 Selective Instance Enumeration
  297. * @param port
  298. * the local port on which the service runs
  299. * @param weight
  300. * weight of the service
  301. * @param priority
  302. * priority of the service
  303. * @param persistent
  304. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  305. * @param props
  306. * properties describing the service
  307. * @return new service info
  308. */
  309. public static ServiceInfo create(final String type, final String name, final String subtype, final int port, final int weight, final int priority, final boolean persistent, final Map<String, ?> props) {
  310. return new ServiceInfoImpl(type, name, subtype, port, weight, priority, persistent, props);
  311. }
  312. /**
  313. * Construct a service description for registering with JmDNS.
  314. *
  315. * @param type
  316. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  317. * @param name
  318. * unqualified service instance name, such as <code>foobar</code>
  319. * @param port
  320. * the local port on which the service runs
  321. * @param weight
  322. * weight of the service
  323. * @param priority
  324. * priority of the service
  325. * @param persistent
  326. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  327. * @param text
  328. * bytes describing the service
  329. * @return new service info
  330. */
  331. public static ServiceInfo create(final String type, final String name, final int port, final int weight, final int priority, final boolean persistent, final byte[] text) {
  332. return new ServiceInfoImpl(type, name, "", port, weight, priority, persistent, text);
  333. }
  334. /**
  335. * Construct a service description for registering with JmDNS.
  336. *
  337. * @param type
  338. * fully qualified service type name, such as <code>_http._tcp.local.</code>.
  339. * @param name
  340. * unqualified service instance name, such as <code>foobar</code>
  341. * @param subtype
  342. * service subtype see draft-cheshire-dnsext-dns-sd-06.txt chapter 7.1 Selective Instance Enumeration
  343. * @param port
  344. * the local port on which the service runs
  345. * @param weight
  346. * weight of the service
  347. * @param priority
  348. * priority of the service
  349. * @param persistent
  350. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  351. * @param text
  352. * bytes describing the service
  353. * @return new service info
  354. */
  355. public static ServiceInfo create(final String type, final String name, final String subtype, final int port, final int weight, final int priority, final boolean persistent, final byte[] text) {
  356. return new ServiceInfoImpl(type, name, subtype, port, weight, priority, persistent, text);
  357. }
  358. /**
  359. * Construct a service description for registering with JmDNS. The properties hashtable must map property names to either Strings or byte arrays describing the property values.
  360. *
  361. * @param qualifiedNameMap
  362. * dictionary of values to build the fully qualified service name. Mandatory keys are Application and Instance. The Domain default is local, the Protocol default is tcp and the subtype default is none.
  363. * @param port
  364. * the local port on which the service runs
  365. * @param weight
  366. * weight of the service
  367. * @param priority
  368. * priority of the service
  369. * @param persistent
  370. * if <code>true</code> ServiceListener.resolveService will be called whenever new new information is received.
  371. * @param props
  372. * properties describing the service
  373. * @return new service info
  374. */
  375. public static ServiceInfo create(final Map<Fields, String> qualifiedNameMap, final int port, final int weight, final int priority, final boolean persistent, final Map<String, ?> props) {
  376. return new ServiceInfoImpl(qualifiedNameMap, port, weight, priority, persistent, props);
  377. }
  378. /**
  379. * Returns true if the service info is filled with data.
  380. *
  381. * @return <code>true</code> if the service info has data, <code>false</code> otherwise.
  382. */
  383. public abstract boolean hasData();
  384. /**
  385. * Fully qualified service type name, such as <code>_http._tcp.local.</code>
  386. *
  387. * @return service type name
  388. */
  389. public abstract String getType();
  390. /**
  391. * Fully qualified service type name with the subtype if appropriate, such as <code>_printer._sub._http._tcp.local.</code>
  392. *
  393. * @return service type name
  394. */
  395. public abstract String getTypeWithSubtype();
  396. /**
  397. * Unqualified service instance name, such as <code>foobar</code> .
  398. *
  399. * @return service name
  400. */
  401. public abstract String getName();
  402. /**
  403. * The key is used to retrieve service info in hash tables.<br/>
  404. * The key is the lower case qualified name.
  405. *
  406. * @return the key
  407. */
  408. public abstract String getKey();
  409. /**
  410. * Fully qualified service name, such as <code>foobar._http._tcp.local.</code> .
  411. *
  412. * @return qualified service name
  413. */
  414. public abstract String getQualifiedName();
  415. /**
  416. * Get the name of the server.
  417. *
  418. * @return server name
  419. */
  420. public abstract String getServer();
  421. /**
  422. * Returns the host IP address string in textual presentation.<br/>
  423. * <b>Note:</b> This can be either an IPv4 or an IPv6 representation.
  424. *
  425. * @return the host raw IP address in a string format.
  426. * @deprecated since 3.2.3
  427. * @see #getHostAddresses()
  428. */
  429. @Deprecated
  430. public abstract String getHostAddress();
  431. /**
  432. * Returns the host IP addresses string in textual presentation.
  433. *
  434. * @return list of host raw IP address in a string format.
  435. */
  436. public abstract String[] getHostAddresses();
  437. /**
  438. * Get the host address of the service.<br/>
  439. *
  440. * @return host Internet address
  441. * @deprecated since 3.1.8
  442. * @see #getInetAddresses()
  443. */
  444. @Deprecated
  445. public abstract InetAddress getAddress();
  446. /**
  447. * Get the InetAddress of the service. This will return the IPv4 if it exist, otherwise it return the IPv6 if set.<br/>
  448. * <b>Note:</b> This return null if the service IP address cannot be resolved.
  449. *
  450. * @return Internet address
  451. * @deprecated since 3.2.3
  452. * @see #getInetAddresses()
  453. */
  454. @Deprecated
  455. public abstract InetAddress getInetAddress();
  456. /**
  457. * Get the IPv4 InetAddress of the service.<br/>
  458. * <b>Note:</b> This return null if the service IPv4 address cannot be resolved.
  459. *
  460. * @return Internet address
  461. * @deprecated since 3.2.3
  462. * @see #getInet4Addresses()
  463. */
  464. @Deprecated
  465. public abstract Inet4Address getInet4Address();
  466. /**
  467. * Get the IPv6 InetAddress of the service.<br/>
  468. * <b>Note:</b> This return null if the service IPv6 address cannot be resolved.
  469. *
  470. * @return Internet address
  471. * @deprecated since 3.2.3
  472. * @see #getInet6Addresses()
  473. */
  474. @Deprecated
  475. public abstract Inet6Address getInet6Address();
  476. /**
  477. * Returns a list of all InetAddresses that can be used for this service.
  478. * <p>
  479. * In a multi-homed environment service info can be associated with more than one address.
  480. * </p>
  481. *
  482. * @return list of InetAddress objects
  483. */
  484. public abstract InetAddress[] getInetAddresses();
  485. /**
  486. * Returns a list of all IPv4 InetAddresses that can be used for this service.
  487. * <p>
  488. * In a multi-homed environment service info can be associated with more than one address.
  489. * </p>
  490. *
  491. * @return list of InetAddress objects
  492. */
  493. public abstract Inet4Address[] getInet4Addresses();
  494. /**
  495. * Returns a list of all IPv6 InetAddresses that can be used for this service.
  496. * <p>
  497. * In a multi-homed environment service info can be associated with more than one address.
  498. * </p>
  499. *
  500. * @return list of InetAddress objects
  501. */
  502. public abstract Inet6Address[] getInet6Addresses();
  503. /**
  504. * Get the port for the service.
  505. *
  506. * @return service port
  507. */
  508. public abstract int getPort();
  509. /**
  510. * Get the priority of the service.
  511. *
  512. * @return service priority
  513. */
  514. public abstract int getPriority();
  515. /**
  516. * Get the weight of the service.
  517. *
  518. * @return service weight
  519. */
  520. public abstract int getWeight();
  521. /**
  522. * Get the text for the service as raw bytes.
  523. *
  524. * @return raw service text
  525. */
  526. public abstract byte[] getTextBytes();
  527. /**
  528. * Get the text for the service. This will interpret the text bytes as a UTF8 encoded string. Will return null if the bytes are not a valid UTF8 encoded string.<br/>
  529. * <b>Note:</b> Do not use. This method make the assumption that the TXT record is one string. This is false. The TXT record is a series of key value pairs.
  530. *
  531. * @return service text
  532. * @see #getPropertyNames()
  533. * @see #getPropertyBytes(String)
  534. * @see #getPropertyString(String)
  535. * @deprecated since 3.1.7
  536. */
  537. @Deprecated
  538. public abstract String getTextString();
  539. /**
  540. * Get the URL for this service. An http URL is created by combining the address, port, and path properties.
  541. *
  542. * @return service URL
  543. * @deprecated since 3.2.3
  544. * @see #getURLs()
  545. */
  546. @Deprecated
  547. public abstract String getURL();
  548. /**
  549. * Get the list of URL for this service. An http URL is created by combining the address, port, and path properties.
  550. *
  551. * @return list of service URL
  552. */
  553. public abstract String[] getURLs();
  554. /**
  555. * Get the URL for this service. An URL is created by combining the protocol, address, port, and path properties.
  556. *
  557. * @param protocol
  558. * requested protocol
  559. * @return service URL
  560. * @deprecated since 3.2.3
  561. * @see #getURLs()
  562. */
  563. @Deprecated
  564. public abstract String getURL(String protocol);
  565. /**
  566. * Get the list of URL for this service. An URL is created by combining the protocol, address, port, and path properties.
  567. *
  568. * @param protocol
  569. * requested protocol
  570. * @return list of service URL
  571. */
  572. public abstract String[] getURLs(String protocol);
  573. /**
  574. * Get a property of the service. This involves decoding the text bytes into a property list. Returns null if the property is not found or the text data could not be decoded correctly.
  575. *
  576. * @param name
  577. * property name
  578. * @return raw property text
  579. */
  580. public abstract byte[] getPropertyBytes(final String name);
  581. /**
  582. * Get a property of the service. This involves decoding the text bytes into a property list. Returns null if the property is not found, the text data could not be decoded correctly, or the resulting bytes are not a valid UTF8 string.
  583. *
  584. * @param name
  585. * property name
  586. * @return property text
  587. */
  588. public abstract String getPropertyString(final String name);
  589. /**
  590. * Enumeration of the property names.
  591. *
  592. * @return property name enumeration
  593. */
  594. public abstract Enumeration<String> getPropertyNames();
  595. /**
  596. * Returns a description of the service info suitable for printing.
  597. *
  598. * @return service info description
  599. */
  600. public abstract String getNiceTextString();
  601. /**
  602. * Set the text for the service. Setting the text will fore a re-announce of the service.
  603. *
  604. * @param text
  605. * the raw byte representation of the text field.
  606. * @exception IllegalStateException
  607. * if attempting to set the text for a non persistent service info.
  608. */
  609. public abstract void setText(final byte[] text) throws IllegalStateException;
  610. /**
  611. * Set the text for the service. Setting the text will fore a re-announce of the service.
  612. *
  613. * @param props
  614. * a key=value map that will be encoded into raw bytes.
  615. * @exception IllegalStateException
  616. * if attempting to set the text for a non persistent service info.
  617. */
  618. public abstract void setText(final Map<String, ?> props) throws IllegalStateException;
  619. /**
  620. * Returns <code>true</code> if ServiceListener.resolveService will be called whenever new new information is received.
  621. *
  622. * @return the persistent
  623. */
  624. public abstract boolean isPersistent();
  625. /**
  626. * Returns the domain of the service info suitable for printing.
  627. *
  628. * @return service domain
  629. */
  630. public abstract String getDomain();
  631. /**
  632. * Returns the protocol of the service info suitable for printing.
  633. *
  634. * @return service protocol
  635. */
  636. public abstract String getProtocol();
  637. /**
  638. * Returns the application of the service info suitable for printing.
  639. *
  640. * @return service application
  641. */
  642. public abstract String getApplication();
  643. /**
  644. * Returns the sub type of the service info suitable for printing.
  645. *
  646. * @return service sub type
  647. */
  648. public abstract String getSubtype();
  649. /**
  650. * Returns a dictionary of the fully qualified name component of this service.
  651. *
  652. * @return dictionary of the fully qualified name components
  653. */
  654. public abstract Map<Fields, String> getQualifiedNameMap();
  655. /*
  656. * (non-Javadoc)
  657. * @see java.lang.Object#clone()
  658. */
  659. @Override
  660. public ServiceInfo clone() {
  661. try {
  662. return (ServiceInfo) super.clone();
  663. } catch (CloneNotSupportedException exception) {
  664. // clone is supported
  665. return null;
  666. }
  667. }
  668. }