iclient_persistence.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // iclient_persistence.cpp
  2. /*******************************************************************************
  3. * Copyright (c) 2013-2016 Frank Pagliughi <fpagliughi@mindspring.com>
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * and Eclipse Distribution License v1.0 which accompany this distribution.
  8. *
  9. * The Eclipse Public License is available at
  10. * http://www.eclipse.org/legal/epl-v10.html
  11. * and the Eclipse Distribution License is available at
  12. * http://www.eclipse.org/org/documents/edl-v10.php.
  13. *
  14. * Contributors:
  15. * Frank Pagliughi - initial implementation and documentation
  16. *******************************************************************************/
  17. #include "mqtt/types.h"
  18. #include "mqtt/iclient_persistence.h"
  19. #include <cstring>
  20. #include <cstdlib>
  21. namespace mqtt {
  22. /////////////////////////////////////////////////////////////////////////////
  23. // Functions to transition C persistence calls to the C++ persistence object.
  24. // Upon the call to persistence_open(), the 'context' has the address of the
  25. // C++ persistence object, which is reassigned to the 'handle'. Subsequent
  26. // calls have the object address as the handle.
  27. int iclient_persistence::persistence_open(void** handle, const char* clientID,
  28. const char* serverURI, void* context)
  29. {
  30. try {
  31. if (handle && clientID && serverURI && context) {
  32. static_cast<iclient_persistence*>(context)->open(clientID, serverURI);
  33. *handle = context;
  34. return MQTTASYNC_SUCCESS;
  35. }
  36. }
  37. catch (...) {}
  38. return MQTTCLIENT_PERSISTENCE_ERROR;
  39. }
  40. int iclient_persistence::persistence_close(void* handle)
  41. {
  42. try {
  43. if (handle) {
  44. static_cast<iclient_persistence*>(handle)->close();
  45. return MQTTASYNC_SUCCESS;
  46. }
  47. }
  48. catch (...) {}
  49. return MQTTCLIENT_PERSISTENCE_ERROR;
  50. }
  51. int iclient_persistence::persistence_put(void* handle, char* key, int bufcount,
  52. char* buffers[], int buflens[])
  53. {
  54. try {
  55. if (handle && bufcount > 0 && buffers && buflens) {
  56. std::vector<string_view> vec;
  57. for (int i=0; i<bufcount; ++i)
  58. vec.push_back(string_view(buffers[i], buflens[i]));
  59. static_cast<iclient_persistence*>(handle)->put(key, vec);
  60. return MQTTASYNC_SUCCESS;
  61. }
  62. }
  63. catch (...) {}
  64. return MQTTCLIENT_PERSISTENCE_ERROR;
  65. }
  66. int iclient_persistence::persistence_get(void* handle, char* key,
  67. char** buffer, int* buflen)
  68. {
  69. try {
  70. if (handle && key && buffer && buflen) {
  71. auto sv = static_cast<iclient_persistence*>(handle)->get(key);
  72. *buffer = const_cast<char*>(sv.data());
  73. *buflen = (int) sv.length();
  74. return MQTTASYNC_SUCCESS;
  75. }
  76. }
  77. catch (...) {}
  78. return MQTTCLIENT_PERSISTENCE_ERROR;
  79. }
  80. int iclient_persistence::persistence_remove(void* handle, char* key)
  81. {
  82. try {
  83. if (handle && key) {
  84. static_cast<iclient_persistence*>(handle)->remove(key);
  85. return MQTTASYNC_SUCCESS;
  86. }
  87. }
  88. catch (...) {}
  89. return MQTTCLIENT_PERSISTENCE_ERROR;
  90. }
  91. int iclient_persistence::persistence_keys(void* handle, char*** keys, int* nkeys)
  92. {
  93. try {
  94. if (handle && keys && nkeys) {
  95. auto& k = static_cast<iclient_persistence*>(handle)->keys();
  96. size_t n = k.size();
  97. *nkeys = n;
  98. *keys = (n == 0) ? nullptr : const_cast<char**>(k.c_arr());
  99. return MQTTASYNC_SUCCESS;
  100. }
  101. }
  102. catch (...) {}
  103. return MQTTCLIENT_PERSISTENCE_ERROR;
  104. }
  105. int iclient_persistence::persistence_clear(void* handle)
  106. {
  107. try {
  108. if (handle) {
  109. static_cast<iclient_persistence*>(handle)->clear();
  110. return MQTTASYNC_SUCCESS;
  111. }
  112. }
  113. catch (...) {}
  114. return MQTTCLIENT_PERSISTENCE_ERROR;
  115. }
  116. int iclient_persistence::persistence_containskey(void* handle, char* key)
  117. {
  118. try {
  119. if (handle && key &&
  120. static_cast<iclient_persistence*>(handle)->contains_key(key))
  121. return MQTTASYNC_SUCCESS;
  122. }
  123. catch (...) {}
  124. return MQTTCLIENT_PERSISTENCE_ERROR;
  125. }
  126. /////////////////////////////////////////////////////////////////////////////
  127. // end namespace mqtt
  128. }