PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/maidsafe/tests/kademlia/fake_callbacks.h

http://maidsafe-dht.googlecode.com/
C Header | 209 lines | 168 code | 15 blank | 26 comment | 14 complexity | fd9c234de3d4c68437741851fb3fa7fa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /* Copyright (c) 2009 maidsafe.net limited
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification,
  4. are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of the maidsafe.net limited nor the names of its
  11. contributors may be used to endorse or promote products derived from this
  12. software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  17. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  21. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifndef MAIDSAFE_TESTS_KADEMLIA_FAKE_CALLBACKS_H_
  25. #define MAIDSAFE_TESTS_KADEMLIA_FAKE_CALLBACKS_H_
  26. #include <boost/thread/thread.hpp>
  27. #include <list>
  28. #include <vector>
  29. #include <string>
  30. #include "maidsafe/maidsafe-dht_config.h"
  31. #include "maidsafe/protobuf/general_messages.pb.h"
  32. #include "maidsafe/protobuf/kademlia_service_messages.pb.h"
  33. class FakeCallback {
  34. public:
  35. FakeCallback() : result_("") {
  36. }
  37. virtual ~FakeCallback() {
  38. }
  39. virtual void CallbackFunc(const std::string& res) = 0;
  40. virtual void Reset() = 0;
  41. std::string result() const {return result_;}
  42. protected:
  43. std::string result_;
  44. };
  45. class PingCallback : public FakeCallback {
  46. public:
  47. PingCallback() : FakeCallback(), result_msg() {
  48. }
  49. void CallbackFunc(const std::string &res) {
  50. if (!result_msg.ParseFromString(res))
  51. result_msg.set_result(kad::kRpcResultFailure);
  52. result_ = result_msg.result();
  53. }
  54. void Reset() {
  55. result_msg.Clear();
  56. result_ = "";
  57. }
  58. private:
  59. kad::PingResponse result_msg;
  60. };
  61. class StoreValueCallback :public FakeCallback {
  62. public:
  63. StoreValueCallback() : FakeCallback(), result_msg() {
  64. }
  65. void CallbackFunc(const std::string &res) {
  66. if (!result_msg.ParseFromString(res)) {
  67. result_msg.set_result(kad::kRpcResultFailure);
  68. }
  69. result_ = result_msg.result();
  70. };
  71. void Reset() {
  72. result_msg.Clear();
  73. result_ = "";
  74. };
  75. private:
  76. kad::StoreResponse result_msg;
  77. };
  78. class FindCallback : public FakeCallback {
  79. public:
  80. FindCallback() : FakeCallback(), result_msg(), values_(), closest_nodes_(),
  81. signed_values_() {
  82. }
  83. void CallbackFunc(const std::string &res) {
  84. if (!result_msg.ParseFromString(res)) {
  85. result_msg.set_result(kad::kRpcResultFailure);
  86. }
  87. for (int i = 0; i < result_msg.values_size(); i++)
  88. values_.push_back(result_msg.values(i));
  89. for (int i = 0; i < result_msg.closest_nodes_size(); i++)
  90. closest_nodes_.push_back(result_msg.closest_nodes(i));
  91. for (int i = 0; i < result_msg.signed_values_size(); i++)
  92. signed_values_.push_back(result_msg.signed_values(i));
  93. result_ = result_msg.result();
  94. };
  95. void Reset() {
  96. result_msg.Clear();
  97. result_ = "";
  98. values_.clear();
  99. closest_nodes_.clear();
  100. signed_values_.clear();
  101. };
  102. std::vector<std::string> values() const {return values_;}
  103. std::vector<std::string> closest_nodes() const {return closest_nodes_;}
  104. std::vector<kad::SignedValue> signed_values() {return signed_values_;}
  105. private:
  106. kad::FindResponse result_msg;
  107. std::vector<std::string> values_;
  108. std::vector<std::string> closest_nodes_;
  109. std::vector<kad::SignedValue> signed_values_;
  110. };
  111. class GetNodeContactDetailsCallback : public FakeCallback {
  112. public:
  113. GetNodeContactDetailsCallback() : result_msg(), contact_() {
  114. }
  115. void CallbackFunc(const std::string &res) {
  116. if (!result_msg.ParseFromString(res)) {
  117. result_msg.set_result(kad::kRpcResultFailure);
  118. }
  119. if (result_msg.has_contact())
  120. contact_ = result_msg.contact();
  121. result_ = result_msg.result();
  122. };
  123. void Reset() {
  124. result_msg.Clear();
  125. result_ = "";
  126. contact_ = "";
  127. };
  128. std::string contact() const {return contact_;}
  129. private:
  130. kad::FindNodeResult result_msg;
  131. std::string contact_;
  132. };
  133. class GeneralKadCallback : public FakeCallback {
  134. public:
  135. GeneralKadCallback() : FakeCallback(), result_msg() {
  136. }
  137. void CallbackFunc(const std::string &res) {
  138. if (!result_msg.ParseFromString(res)) {
  139. result_msg.set_result(kad::kRpcResultFailure);
  140. }
  141. result_ = result_msg.result();
  142. };
  143. void Reset() {
  144. result_msg.Clear();
  145. result_ = "";
  146. };
  147. private:
  148. base::GeneralResponse result_msg;
  149. };
  150. class DeleteValueCallback : public FakeCallback {
  151. public:
  152. DeleteValueCallback() : FakeCallback(), result_msg() {
  153. }
  154. void CallbackFunc(const std::string &res) {
  155. if (!result_msg.ParseFromString(res)) {
  156. result_msg.set_result(kad::kRpcResultFailure);
  157. }
  158. result_ = result_msg.result();
  159. };
  160. void Reset() {
  161. result_msg.Clear();
  162. result_ = "";
  163. };
  164. private:
  165. kad::DeleteResponse result_msg;
  166. };
  167. class UpdateValueCallback : public FakeCallback {
  168. public:
  169. UpdateValueCallback() : FakeCallback(), result_msg() { }
  170. void CallbackFunc(const std::string &res) {
  171. if (!result_msg.ParseFromString(res))
  172. result_msg.set_result(kad::kRpcResultFailure);
  173. result_ = result_msg.result();
  174. }
  175. void Reset() {
  176. result_msg.Clear();
  177. result_ = "";
  178. }
  179. private:
  180. kad::UpdateResponse result_msg;
  181. };
  182. inline void wait_result(FakeCallback *callback) {
  183. while (1) {
  184. {
  185. if (callback->result() != "")
  186. return;
  187. }
  188. boost::this_thread::sleep(boost::posix_time::milliseconds(500));
  189. }
  190. }
  191. #endif // MAIDSAFE_TESTS_KADEMLIA_FAKE_CALLBACKS_H_