PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/org.eclipse.paho.mqttv5.client/src/main/java/org/eclipse/paho/mqttv5/client/alpha/IMqttClient.java

http://github.com/eclipse/paho.mqtt.java
Java | 306 lines | 19 code | 15 blank | 272 comment | 0 complexity | 1d35937e5be900086f0d8392c7f24561 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2015 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0
  10. * and the Eclipse Distribution License is available at
  11. * https://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * Contributors:
  14. * Dave Locke - initial API and implementation and/or initial documentation
  15. * Ian Craggs - MQTT 3.1.1 support
  16. * Ian Craggs - per subscription message handlers (bug 466579)
  17. * Ian Craggs - ack control (bug 472172)
  18. */
  19. package org.eclipse.paho.mqttv5.client.alpha;
  20. import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
  21. import org.eclipse.paho.mqttv5.client.alpha.result.IMqttConnectionResult;
  22. import org.eclipse.paho.mqttv5.common.MqttException;
  23. import org.eclipse.paho.mqttv5.common.MqttSecurityException;
  24. /**
  25. * Enables an application to communicate with an MQTT server using blocking methods.
  26. * <p>
  27. * This interface allows applications to utilize all features of the MQTT version 3.1
  28. * specification including:</p>
  29. * <ul>
  30. * <li>connect
  31. * <li>publish
  32. * <li>subscribe
  33. * <li>unsubscribe
  34. * <li>disconnect
  35. * </ul>
  36. * <p>
  37. * There are two styles of MQTT client, this one and {@link IMqttAsyncClient}.</p>
  38. * <ul>
  39. * <li>IMqttClient provides a set of methods that block and return control to the application
  40. * program once the MQTT action has completed.</li>
  41. * <li>IMqttAsyncClient provides a set of non-blocking methods that return control to the
  42. * invoking application after initial validation of parameters and state. The main processing is
  43. * performed in the background so as not to block the application programs thread. This non
  44. * blocking approach is handy when the application wants to carry on processing while the
  45. * MQTT action takes place. For instance connecting to an MQTT server can take time, using
  46. * the non-blocking connect method allows an application to display a busy indicator while the
  47. * connect action is occurring. Non-blocking methods are particularly useful in event-oriented
  48. * programs and graphical programs where issuing methods that take time to complete on the the
  49. * main or GUI thread can cause problems.</li>
  50. * </ul>
  51. * <p>
  52. * The non-blocking client can also be used in a blocking form by turning a non-blocking
  53. * method into a blocking invocation using the following pattern:</p>
  54. * <pre>
  55. * IMqttToken token;
  56. * token = asyncClient.method(parms).getPromise().getValue();
  57. * </pre>
  58. * <p>
  59. * Using the non-blocking client allows an application to use a mixture of blocking and
  60. * non-blocking styles. Using the blocking client only allows an application to use one
  61. * style. The blocking client provides compatibility with earlier versions
  62. * of the MQTT client.</p>
  63. */
  64. public interface IMqttClient extends IMqttCommonClient {
  65. /**
  66. * Connects to an MQTT server using the default options.
  67. * <p>The default options are specified in {@link MqttConnectionOptions} class.
  68. * </p>
  69. *
  70. * @throws MqttSecurityException when the server rejects the connect for security
  71. * reasons
  72. * @throws MqttException for non security related problems
  73. * @see #connect(MqttConnectionOptions)
  74. */
  75. void connect() throws MqttSecurityException, MqttException;
  76. /**
  77. * Connects to an MQTT server using the specified options.
  78. *
  79. * <p>This is a blocking method that returns once connect completes</p>
  80. *
  81. * @param options a set of connection parameters that override the defaults.
  82. * @throws MqttSecurityException when the server rejects the connect for security
  83. * reasons
  84. * @throws MqttException for non security related problems including communication errors
  85. */
  86. void connect(MqttConnectionOptions options) throws MqttSecurityException, MqttException;
  87. /**
  88. * Connects to an MQTT server using the specified options.
  89. *
  90. * <p>This is a blocking method that returns once connect completes</p>
  91. *
  92. * @param options a set of connection parameters that override the defaults.
  93. * @return the MqttToken used for the call. Can be used to obtain the session present flag
  94. * @throws MqttSecurityException when the server rejects the connect for security
  95. * reasons
  96. * @throws MqttException for non security related problems including communication errors
  97. */
  98. IMqttConnectionResult<Void> connectWithResult(MqttConnectionOptions options) throws MqttSecurityException, MqttException;
  99. /**
  100. * Disconnects from the server.
  101. *
  102. * <p>This is a blocking method that returns once disconnect completes</p>
  103. *
  104. * @throws MqttException if a problem is encountered while disconnecting
  105. */
  106. void disconnect() throws MqttException;
  107. /**
  108. * Disconnects from the server.
  109. * <p>
  110. * The client will wait for all callback methods to
  111. * complete. It will then wait for up to the quiesce timeout to allow for
  112. * work which has already been initiated to complete - for example, it will
  113. * wait for the QoS 2 flows from earlier publications to complete. When work has
  114. * completed or after the quiesce timeout, the client will disconnect from
  115. * the server. If the cleanStart flag was set to false and is set to false the
  116. * next time a connection is made QoS 1 and 2 messages that
  117. * were not previously delivered will be delivered.</p>
  118. *
  119. * <p>This is a blocking method that returns once disconnect completes</p>
  120. *
  121. * @param quiesceTimeout the amount of time in milliseconds to allow for
  122. * existing work to finish before disconnecting. A value of zero or less
  123. * means the client will not quiesce.
  124. * @throws MqttException if a problem is encountered while disconnecting
  125. */
  126. void disconnect(long quiesceTimeout) throws MqttException;
  127. /**
  128. * Disconnects from the server forcibly to reset all the states. Could be useful when disconnect attempt failed.
  129. * <p>
  130. * Because the client is able to establish the TCP/IP connection to a none MQTT server and it will certainly fail to
  131. * send the disconnect packet. It will wait for a maximum of 30 seconds for work to quiesce before disconnecting and
  132. * wait for a maximum of 10 seconds for sending the disconnect packet to server.
  133. *
  134. * @throws MqttException if any unexpected error
  135. * @since 0.4.1
  136. */
  137. void disconnectForcibly() throws MqttException;
  138. /**
  139. * Disconnects from the server forcibly to reset all the states. Could be useful when disconnect attempt failed.
  140. * <p>
  141. * Because the client is able to establish the TCP/IP connection to a none MQTT server and it will certainly fail to
  142. * send the disconnect packet. It will wait for a maximum of 30 seconds for work to quiesce before disconnecting.
  143. *
  144. * @param disconnectTimeout the amount of time in milliseconds to allow send disconnect packet to server.
  145. * @throws MqttException if any unexpected error
  146. * @since 0.4.1
  147. */
  148. void disconnectForcibly(long disconnectTimeout) throws MqttException;
  149. /**
  150. * Disconnects from the server forcibly to reset all the states. Could be useful when disconnect attempt failed.
  151. * <p>
  152. * Because the client is able to establish the TCP/IP connection to a none MQTT server and it will certainly fail to
  153. * send the disconnect packet.
  154. *
  155. * @param quiesceTimeout the amount of time in milliseconds to allow for existing work to finish before
  156. * disconnecting. A value of zero or less means the client will not quiesce.
  157. * @param disconnectTimeout the amount of time in milliseconds to allow send disconnect packet to server.
  158. * @throws MqttException if any unexpected error
  159. * @since 0.4.1
  160. */
  161. void disconnectForcibly(long quiesceTimeout, long disconnectTimeout) throws MqttException;
  162. /**
  163. * Subscribe to a topic, which may include wildcards using a QoS of 1.
  164. *
  165. * @see #subscribe(String[], int[])
  166. *
  167. * @param topicFilter the topic to subscribe to, which can include wildcards.
  168. * @return The Subscription Token
  169. * @throws MqttException if there was an error registering the subscription.
  170. * @throws MqttSecurityException if the client is not authorized to register the subscription
  171. */
  172. IMqttSubscriptionToken<Void> subscribe(String topicFilter) throws MqttException, MqttSecurityException;
  173. /**
  174. * Subscribes to a one or more topics, which may include wildcards using a QoS of 1.
  175. *
  176. * @see #subscribe(String[], int[])
  177. *
  178. * @param topicFilters the topic to subscribe to, which can include wildcards.
  179. * @return The Subscription Token
  180. * @throws MqttException if there was an error registering the subscription.
  181. */
  182. IMqttSubscriptionToken<Void> subscribe(String[] topicFilters) throws MqttException;
  183. /**
  184. * Subscribe to a topic, which may include wildcards.
  185. *
  186. * @see #subscribe(String[], int[])
  187. *
  188. * @param topicFilter the topic to subscribe to, which can include wildcards.
  189. * @param qos the maximum quality of service at which to subscribe. Messages
  190. * published at a lower quality of service will be received at the published
  191. * QoS. Messages published at a higher quality of service will be received using
  192. * the QoS specified on the subscribe.
  193. * @return The Subscription Token
  194. * @throws MqttException if there was an error registering the subscription.
  195. */
  196. IMqttSubscriptionToken<Void> subscribe(String topicFilter, int qos) throws MqttException;
  197. /**
  198. * Subscribes to multiple topics, each of which may include wildcards.
  199. * <p>
  200. * If (@link MqttConnectOptions#setCleanStart(boolean)} was set to true
  201. * when when connecting to the server then the subscription remains in place
  202. * until either:
  203. * </p>
  204. * <ul>
  205. * <li>The client disconnects</li>
  206. * <li>An unsubscribe method is called to un-subscribe the topic</li>
  207. * </ul>
  208. * <p>
  209. * If (@link MqttConnectOptions#setCleanStart(boolean)} was set to false
  210. * when when connecting to the server then the subscription remains in place
  211. * until either:</p>
  212. * <ul>
  213. * <li>An unsubscribe method is called to unsubscribe the topic</li>
  214. * <li>The client connects with cleanStart set to true</li>
  215. * </ul>
  216. * <p>
  217. * With cleanStart set to false the MQTT server will store messages on
  218. * behalf of the client when the client is not connected. The next time the
  219. * client connects with the <b>same client ID</b> the server will
  220. * deliver the stored messages to the client.
  221. * </p>
  222. *
  223. * <p>The "topic filter" string used when subscribing
  224. * may contain special characters, which allow you to subscribe to multiple topics
  225. * at once.</p>
  226. * <p>The topic level separator is used to introduce structure into the topic, and
  227. * can therefore be specified within the topic for that purpose. The multi-level
  228. * wildcard and single-level wildcard can be used for subscriptions, but they
  229. * cannot be used within a topic by the publisher of a message.
  230. * <dl>
  231. * <dt>Topic level separator</dt>
  232. * <dd>The forward slash (/) is used to separate each level within
  233. * a topic tree and provide a hierarchical structure to the topic space. The
  234. * use of the topic level separator is significant when the two wildcard characters
  235. * are encountered in topics specified by subscribers.</dd>
  236. *
  237. * <dt>Multi-level wildcard</dt>
  238. * <dd><p>The number sign (#) is a wildcard character that matches
  239. * any number of levels within a topic. For example, if you subscribe to
  240. * <span><span class="filepath">finance/stock/ibm/#</span></span>, you receive
  241. * messages on these topics:</p>
  242. * <ul>
  243. * <li><pre>finance/stock/ibm</pre></li>
  244. * <li><pre>finance/stock/ibm/closingprice</pre></li>
  245. * <li><pre>finance/stock/ibm/currentprice</pre></li>
  246. * </ul>
  247. *
  248. * <p>The multi-level wildcard
  249. * can represent zero or more levels. Therefore, <em>finance/#</em> can also match
  250. * the singular <em>finance</em>, where <em>#</em> represents zero levels. The topic
  251. * level separator is meaningless in this context, because there are no levels
  252. * to separate.</p>
  253. *
  254. * <p>The <span>multi-level</span> wildcard can
  255. * be specified only on its own or next to the topic level separator character.
  256. * Therefore, <em>#</em> and <em>finance/#</em> are both valid, but <em>finance#</em> is
  257. * not valid. <span>The multi-level wildcard must be the last character
  258. * used within the topic tree. For example, <em>finance/#</em> is valid but
  259. * <em>finance/#/closingprice</em> is not valid.</span></p></dd>
  260. *
  261. * <dt>Single-level wildcard</dt>
  262. * <dd><p>The plus sign (+) is a wildcard character that matches only one topic
  263. * level. For example, <em>finance/stock/+</em> matches
  264. * <em>finance/stock/ibm</em> and <em>finance/stock/xyz</em>,
  265. * but not <em>finance/stock/ibm/closingprice</em>. Also, because the single-level
  266. * wildcard matches only a single level, <em>finance/+</em> does not match <em>finance</em>.</p>
  267. *
  268. * <p>Use
  269. * the single-level wildcard at any level in the topic tree, and in conjunction
  270. * with the multilevel wildcard. Specify the single-level wildcard next to the
  271. * topic level separator, except when it is specified on its own. Therefore,
  272. * <em>+</em> and <em>finance/+</em> are both valid, but <em>finance+</em> is
  273. * not valid. <span>The single-level wildcard can be used at the end of the
  274. * topic tree or within the topic tree.
  275. * For example, <em>finance/+</em> and <em>finance/+/ibm</em> are both valid.</span></p>
  276. * </dd>
  277. * </dl>
  278. *
  279. * <p>This is a blocking method that returns once subscribe completes</p>
  280. *
  281. * @param topicFilters one or more topics to subscribe to, which can include wildcards.
  282. * @param qos the maximum quality of service to subscribe each topic at.Messages
  283. * published at a lower quality of service will be received at the published
  284. * QoS. Messages published at a higher quality of service will be received using
  285. * the QoS specified on the subscribe.
  286. * @return The Subscription Token
  287. * @throws MqttException if there was an error registering the subscription.
  288. * @throws IllegalArgumentException if the two supplied arrays are not the same size.
  289. */
  290. IMqttSubscriptionToken<Void> subscribe(String[] topicFilters, int[] qos) throws MqttException;
  291. }