iclient_persistence.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file iclient_persistence.h
  3. /// Declaration of MQTT iclient_persistence interface
  4. /// @date May 1, 2013
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2013-2016 Frank Pagliughi <fpagliughi@mindspring.com>
  9. *
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v2.0
  12. * and Eclipse Distribution License v1.0 which accompany this distribution.
  13. *
  14. * The Eclipse Public License is available at
  15. * http://www.eclipse.org/legal/epl-v20.html
  16. * and the Eclipse Distribution License is available at
  17. * http://www.eclipse.org/org/documents/edl-v10.php.
  18. *
  19. * Contributors:
  20. * Frank Pagliughi - initial implementation and documentation
  21. *******************************************************************************/
  22. #ifndef __mqtt_iclient_persistence_h
  23. #define __mqtt_iclient_persistence_h
  24. #include "MQTTAsync.h"
  25. #include "mqtt/types.h"
  26. #include "mqtt/buffer_view.h"
  27. #include "mqtt/string_collection.h"
  28. #include <vector>
  29. namespace mqtt {
  30. /**
  31. * Allocate memory for use with user persistence.
  32. *
  33. * @param n The number of bytes for the buffer.
  34. * @return A pointer to the allocated memory
  35. */
  36. inline void* persistence_malloc(size_t n) {
  37. return MQTTAsync_malloc(n);
  38. }
  39. /**
  40. * Frees memory allocated with @ref persistence_malloc().
  41. * @param p Pointer to a buffer obtained by persistence_malloc.
  42. */
  43. inline void persistence_free(void* p) {
  44. MQTTAsync_free(p);
  45. }
  46. /////////////////////////////////////////////////////////////////////////////
  47. /**
  48. * Represents a persistent data store, used to store outbound and inbound
  49. * messages while they are in flight, enabling delivery to the QoS
  50. * specified. You can specify an implementation of this interface using
  51. * client::client(string, string, iclient_persistence), which the
  52. * client will use to persist QoS 1 and 2 messages.
  53. *
  54. * If the methods defined throw the MqttPersistenceException then the state
  55. * of the data persisted should remain as prior to the method being called.
  56. * For example, if put(string, persistable) throws an exception at any
  57. * point then the data will be assumed to not be in the persistent store.
  58. * Similarly if remove(string) throws an exception then the data will be
  59. * assumed to still be held in the persistent store.
  60. *
  61. * It is up to the persistence interface to log any exceptions or error
  62. * information which may be required when diagnosing a persistence failure.
  63. */
  64. class iclient_persistence
  65. {
  66. friend class async_client;
  67. friend class mock_persistence;
  68. /** Callbacks from the C library */
  69. static int persistence_open(void** handle, const char* clientID, const char* serverURI, void* context);
  70. static int persistence_close(void* handle);
  71. static int persistence_put(void* handle, char* key, int bufcount, char* buffers[], int buflens[]);
  72. static int persistence_get(void* handle, char* key, char** buffer, int* buflen);
  73. static int persistence_remove(void* handle, char* key);
  74. static int persistence_keys(void* handle, char*** keys, int* nkeys);
  75. static int persistence_clear(void* handle);
  76. static int persistence_containskey(void* handle, char* key);
  77. public:
  78. /** Smart/shared pointer to an object of this class. */
  79. using ptr_t = std::shared_ptr<iclient_persistence>;
  80. /** Smart/shared pointer to a const object of this class. */
  81. using const_ptr_t = std::shared_ptr<const iclient_persistence>;
  82. /**
  83. * Virtual destructor.
  84. */
  85. virtual ~iclient_persistence() {}
  86. /**
  87. * Initialize the persistent store.
  88. * This uses the client ID and server name to create a unique location
  89. * for the data store.
  90. * @param clientId The identifier string for the client.
  91. * @param serverURI The server to which the client is connected.
  92. */
  93. virtual void open(const string& clientId, const string& serverURI) =0;
  94. /**
  95. * Close the persistent store that was previously opened.
  96. */
  97. virtual void close() =0;
  98. /**
  99. * Clears persistence, so that it no longer contains any persisted data.
  100. */
  101. virtual void clear() =0;
  102. /**
  103. * Returns whether or not data is persisted using the specified key.
  104. * @param key The key to find
  105. * @return @em true if the key exists, @em false if not.
  106. */
  107. virtual bool contains_key(const string& key) =0;
  108. /**
  109. * Returns a collection of keys in this persistent data store.
  110. * @return A collection of strings representing the keys in the store.
  111. */
  112. virtual string_collection keys() const =0;
  113. /**
  114. * Puts the specified data into the persistent store.
  115. * @param key The key.
  116. * @param bufs The data to store
  117. */
  118. virtual void put(const string& key, const std::vector<string_view>& bufs) =0;
  119. /**
  120. * Gets the specified data out of the persistent store.
  121. * @param key The key
  122. * @return A const view of the data associated with the key.
  123. */
  124. virtual string get(const string& key) const =0;
  125. /**
  126. * Remove the data for the specified key.
  127. * @param key The key
  128. */
  129. virtual void remove(const string& key) =0;
  130. };
  131. /** Smart/shared pointer to a persistence client */
  132. using iclient_persistence_ptr = iclient_persistence::ptr_t;
  133. /** Smart/shared pointer to a persistence client */
  134. using const_iclient_persistence_ptr = iclient_persistence::const_ptr_t;
  135. /////////////////////////////////////////////////////////////////////////////
  136. // end namespace mqtt
  137. }
  138. #endif // __mqtt_iclient_persistence_h