MQTTClientPersistence.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2012 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 v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. *******************************************************************************/
  16. /**
  17. * @file
  18. * \brief This structure represents a persistent data store, used to store
  19. * outbound and inbound messages, in order to achieve reliable messaging.
  20. *
  21. * The MQTT Client persists QoS1 and QoS2 messages in order to meet the
  22. * assurances of delivery associated with these @ref qos levels. The messages
  23. * are saved in persistent storage
  24. * The type and context of the persistence implementation are specified when
  25. * the MQTT client is created (see MQTTClient_create()). The default
  26. * persistence type (::MQTTCLIENT_PERSISTENCE_DEFAULT) uses a file system-based
  27. * persistence mechanism. The <i>persistence_context</i> argument passed to
  28. * MQTTClient_create() when using the default peristence is a string
  29. * representing the location of the persistence directory. If the context
  30. * argument is NULL, the working directory will be used.
  31. *
  32. * To use memory-based persistence, an application passes
  33. * ::MQTTCLIENT_PERSISTENCE_NONE as the <i>persistence_type</i> to
  34. * MQTTClient_create(). This can lead to message loss in certain situations,
  35. * but can be appropriate in some cases (see @ref qos).
  36. *
  37. * Client applications can provide their own persistence mechanism by passing
  38. * ::MQTTCLIENT_PERSISTENCE_USER as the <i>persistence_type</i>. To implement a
  39. * custom persistence mechanism, the application must pass an initialized
  40. * ::MQTTClient_persistence structure as the <i>persistence_context</i>
  41. * argument to MQTTClient_create().
  42. *
  43. * If the functions defined return an ::MQTTCLIENT_PERSISTENCE_ERROR then the
  44. * state of the persisted data should remain as it was prior to the function
  45. * being called. For example, if Persistence_put() returns
  46. * ::MQTTCLIENT_PERSISTENCE_ERROR, then it is assumed tha tthe persistent store
  47. * does not contain the data that was passed to the function. Similarly, if
  48. * Persistence_remove() returns ::MQTTCLIENT_PERSISTENCE_ERROR then it is
  49. * assumed that the data to be removed is still held in the persistent store.
  50. *
  51. * It is up to the persistence implementation to log any error information that
  52. * may be required to diagnose a persistence mechanism failure.
  53. */
  54. /*
  55. /// @cond EXCLUDE
  56. */
  57. #if !defined(MQTTCLIENTPERSISTENCE_H)
  58. #define MQTTCLIENTPERSISTENCE_H
  59. /*
  60. /// @endcond
  61. */
  62. /**
  63. * This <i>persistence_type</i> value specifies the default file system-based
  64. * persistence mechanism (see MQTTClient_create()).
  65. */
  66. #define MQTTCLIENT_PERSISTENCE_DEFAULT 0
  67. /**
  68. * This <i>persistence_type</i> value specifies a memory-based
  69. * persistence mechanism (see MQTTClient_create()).
  70. */
  71. #define MQTTCLIENT_PERSISTENCE_NONE 1
  72. /**
  73. * This <i>persistence_type</i> value specifies an application-specific
  74. * persistence mechanism (see MQTTClient_create()).
  75. */
  76. #define MQTTCLIENT_PERSISTENCE_USER 2
  77. /**
  78. * Application-specific persistence functions must return this error code if
  79. * there is a problem executing the function.
  80. */
  81. #define MQTTCLIENT_PERSISTENCE_ERROR -2
  82. /**
  83. * @brief Initialize the persistent store.
  84. *
  85. * Either open the existing persistent store for this client ID or create a new
  86. * one if one doesn't exist. If the persistent store is already open, return
  87. * without taking any action.
  88. *
  89. * An application can use the same client identifier to connect to many
  90. * different servers. The <i>clientid</i> in conjunction with the
  91. * <i>serverURI</i> uniquely identifies the persistence store required.
  92. *
  93. * @param handle The address of a pointer to a handle for this persistence
  94. * implementation. This function must set handle to a valid reference to the
  95. * persistence following a successful return.
  96. * The handle pointer is passed as an argument to all the other
  97. * persistence functions. It may include the context parameter and/or any other
  98. * data for use by the persistence functions.
  99. * @param clientID The client identifier for which the persistent store should
  100. * be opened.
  101. * @param serverURI The connection string specified when the MQTT client was
  102. * created (see MQTTClient_create()).
  103. * @param context A pointer to any data required to initialize the persistent
  104. * store (see ::MQTTClient_persistence).
  105. * @return Return 0 if the function completes successfully, otherwise return
  106. * ::MQTTCLIENT_PERSISTENCE_ERROR.
  107. */
  108. typedef int (*Persistence_open)(void** handle, const char* clientID, const char* serverURI, void* context);
  109. /**
  110. * @brief Close the persistent store referred to by the handle.
  111. *
  112. * @param handle The handle pointer from a successful call to
  113. * Persistence_open().
  114. * @return Return 0 if the function completes successfully, otherwise return
  115. * ::MQTTCLIENT_PERSISTENCE_ERROR.
  116. */
  117. typedef int (*Persistence_close)(void* handle);
  118. /**
  119. * @brief Put the specified data into the persistent store.
  120. *
  121. * @param handle The handle pointer from a successful call to
  122. * Persistence_open().
  123. * @param key A string used as the key for the data to be put in the store. The
  124. * key is later used to retrieve data from the store with Persistence_get().
  125. * @param bufcount The number of buffers to write to the persistence store.
  126. * @param buffers An array of pointers to the data buffers associated with
  127. * this <i>key</i>.
  128. * @param buflens An array of lengths of the data buffers. <i>buflen[n]</i>
  129. * gives the length of <i>buffer[n]</i>.
  130. * @return Return 0 if the function completes successfully, otherwise return
  131. * ::MQTTCLIENT_PERSISTENCE_ERROR.
  132. */
  133. typedef int (*Persistence_put)(void* handle, char* key, int bufcount, char* buffers[], int buflens[]);
  134. /**
  135. * @brief Retrieve the specified data from the persistent store.
  136. *
  137. * @param handle The handle pointer from a successful call to
  138. * Persistence_open().
  139. * @param key A string that is the key for the data to be retrieved. This is
  140. * the same key used to save the data to the store with Persistence_put().
  141. * @param buffer The address of a pointer to a buffer. This function sets the
  142. * pointer to point at the retrieved data, if successful.
  143. * @param buflen The address of an int that is set to the length of
  144. * <i>buffer</i> by this function if successful.
  145. * @return Return 0 if the function completes successfully, otherwise return
  146. * ::MQTTCLIENT_PERSISTENCE_ERROR.
  147. */
  148. typedef int (*Persistence_get)(void* handle, char* key, char** buffer, int* buflen);
  149. /**
  150. * @brief Remove the data for the specified key from the store.
  151. *
  152. * @param handle The handle pointer from a successful call to
  153. * Persistence_open().
  154. * @param key A string that is the key for the data to be removed from the
  155. * store. This is the same key used to save the data to the store with
  156. * Persistence_put().
  157. * @return Return 0 if the function completes successfully, otherwise return
  158. * ::MQTTCLIENT_PERSISTENCE_ERROR.
  159. */
  160. typedef int (*Persistence_remove)(void* handle, char* key);
  161. /**
  162. * @brief Returns the keys in this persistent data store.
  163. *
  164. * @param handle The handle pointer from a successful call to
  165. * Persistence_open().
  166. * @param keys The address of a pointer to pointers to strings. Assuming
  167. * successful execution, this function allocates memory to hold the returned
  168. * keys (strings used to store the data with Persistence_put()). It also
  169. * allocates memory to hold an array of pointers to these strings. <i>keys</i>
  170. * is set to point to the array of pointers to strings.
  171. * @param nkeys A pointer to the number of keys in this persistent data store.
  172. * This function sets the number of keys, if successful.
  173. * @return Return 0 if the function completes successfully, otherwise return
  174. * ::MQTTCLIENT_PERSISTENCE_ERROR.
  175. */
  176. typedef int (*Persistence_keys)(void* handle, char*** keys, int* nkeys);
  177. /**
  178. * @brief Clears the persistence store, so that it no longer contains any
  179. * persisted data.
  180. *
  181. * @param handle The handle pointer from a successful call to
  182. * Persistence_open().
  183. * @return Return 0 if the function completes successfully, otherwise return
  184. * ::MQTTCLIENT_PERSISTENCE_ERROR.
  185. */
  186. typedef int (*Persistence_clear)(void* handle);
  187. /**
  188. * @brief Returns whether any data has been persisted using the specified key.
  189. *
  190. * @param handle The handle pointer from a successful call to
  191. * Persistence_open().
  192. * @param key The string to be tested for existence in the store.
  193. * @return Return 0 if the key was found in the store, otherwise return
  194. * ::MQTTCLIENT_PERSISTENCE_ERROR.
  195. */
  196. typedef int (*Persistence_containskey)(void* handle, char* key);
  197. /**
  198. * @brief A structure containing the function pointers to a persistence
  199. * implementation and the context or state that will be shared across all
  200. * the persistence functions.
  201. */
  202. typedef struct {
  203. /**
  204. * A pointer to any data required to initialize the persistent store.
  205. */
  206. void* context;
  207. /**
  208. * A function pointer to an implementation of Persistence_open().
  209. */
  210. Persistence_open popen;
  211. /**
  212. * A function pointer to an implementation of Persistence_close().
  213. */
  214. Persistence_close pclose;
  215. /**
  216. * A function pointer to an implementation of Persistence_put().
  217. */
  218. Persistence_put pput;
  219. /**
  220. * A function pointer to an implementation of Persistence_get().
  221. */
  222. Persistence_get pget;
  223. /**
  224. * A function pointer to an implementation of Persistence_remove().
  225. */
  226. Persistence_remove premove;
  227. /**
  228. * A function pointer to an implementation of Persistence_keys().
  229. */
  230. Persistence_keys pkeys;
  231. /**
  232. * A function pointer to an implementation of Persistence_clear().
  233. */
  234. Persistence_clear pclear;
  235. /**
  236. * A function pointer to an implementation of Persistence_containskey().
  237. */
  238. Persistence_containskey pcontainskey;
  239. } MQTTClient_persistence;
  240. #endif