iclient_persistence.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 v1.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-v10.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. /**
  32. * Represents a persistent data store, used to store outbound and inbound
  33. * messages while they are in flight, enabling delivery to the QoS
  34. * specified. You can specify an implementation of this interface using
  35. * client::client(string, string, iclient_persistence), which the
  36. * client will use to persist QoS 1 and 2 messages.
  37. *
  38. * If the methods defined throw the MqttPersistenceException then the state
  39. * of the data persisted should remain as prior to the method being called.
  40. * For example, if put(string, persistable) throws an exception at any
  41. * point then the data will be assumed to not be in the persistent store.
  42. * Similarly if remove(string) throws an exception then the data will be
  43. * assumed to still be held in the persistent store.
  44. *
  45. * It is up to the persistence interface to log any exceptions or error
  46. * information which may be required when diagnosing a persistence failure.
  47. */
  48. class iclient_persistence
  49. {
  50. friend class async_client;
  51. friend class iclient_persistence_test;
  52. /** Callbacks from the C library */
  53. static int persistence_open(void** handle, const char* clientID, const char* serverURI, void* context);
  54. static int persistence_close(void* handle);
  55. static int persistence_put(void* handle, char* key, int bufcount, char* buffers[], int buflens[]);
  56. static int persistence_get(void* handle, char* key, char** buffer, int* buflen);
  57. static int persistence_remove(void* handle, char* key);
  58. static int persistence_keys(void* handle, char*** keys, int* nkeys);
  59. static int persistence_clear(void* handle);
  60. static int persistence_containskey(void* handle, char* key);
  61. public:
  62. /** Smart/shared pointer to an object of this class. */
  63. using ptr_t = std::shared_ptr<iclient_persistence>;
  64. /** Smart/shared pointer to a const object of this class. */
  65. using const_ptr_t = std::shared_ptr<const iclient_persistence>;
  66. /**
  67. * Virtual destructor.
  68. */
  69. virtual ~iclient_persistence() {}
  70. /**
  71. * Initialize the persistent store.
  72. * This uses the client ID and server name to create a unique location
  73. * for the data store.
  74. * @param clientId The identifier string for the client.
  75. * @param serverURI The server to which the client is connected.
  76. */
  77. virtual void open(const string& clientId, const string& serverURI) =0;
  78. /**
  79. * Close the persistent store that was previously opened.
  80. */
  81. virtual void close() =0;
  82. /**
  83. * Clears persistence, so that it no longer contains any persisted data.
  84. */
  85. virtual void clear() =0;
  86. /**
  87. * Returns whether or not data is persisted using the specified key.
  88. * @param key The key to find
  89. * @return @em true if the key exists, @em false if not.
  90. */
  91. virtual bool contains_key(const string& key) =0;
  92. /**
  93. * Returns a collection of keys in this persistent data store.
  94. * @return A collection of strings representing the keys in the store.
  95. */
  96. virtual const string_collection& keys() const =0;
  97. /**
  98. * Puts the specified data into the persistent store.
  99. * @param key The key.
  100. * @param bufs The data to store
  101. */
  102. virtual void put(const string& key, const std::vector<string_view>& bufs) =0;
  103. /**
  104. * Gets the specified data out of the persistent store.
  105. * @param key The key
  106. * @return A const view of the data associated with the key.
  107. */
  108. virtual string_view get(const string& key) const =0;
  109. /**
  110. * Remove the data for the specified key.
  111. * @param key The key
  112. */
  113. virtual void remove(const string& key) =0;
  114. };
  115. /** Smart/shared pointer to a persistence client */
  116. using iclient_persistence_ptr = iclient_persistence::ptr_t;
  117. /** Smart/shared pointer to a persistence client */
  118. using const_iclient_persistence_ptr = iclient_persistence::const_ptr_t;
  119. /////////////////////////////////////////////////////////////////////////////
  120. // end namespace mqtt
  121. }
  122. #endif // __mqtt_iclient_persistence_h