PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/src/maidsafe/kademlia/contact.cc

http://maidsafe-dht.googlecode.com/
C++ | 247 lines | 201 code | 19 blank | 27 comment | 38 complexity | 15556d201e50a44af23e7e7d728ebb57 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. #include "maidsafe/kademlia/contact.h"
  25. #include <boost/lexical_cast.hpp>
  26. #include "maidsafe/base/utils.h"
  27. #include "maidsafe/protobuf/contact_info.pb.h"
  28. namespace kad {
  29. Contact::Contact()
  30. : node_id_(),
  31. host_ip_(),
  32. host_port_(0),
  33. failed_rpc_(0),
  34. rendezvous_ip_(),
  35. rendezvous_port_(0),
  36. last_seen_(base::GetEpochMilliseconds()),
  37. local_ip_(),
  38. local_port_(0) {}
  39. Contact::Contact(const std::string &node_id, const std::string &host_ip,
  40. const boost::uint16_t &host_port, const std::string &local_ip,
  41. const boost::uint16_t &local_port,
  42. const std::string &rendezvous_ip,
  43. const boost::uint16_t &rendezvous_port)
  44. : node_id_(node_id), host_ip_(host_ip), host_port_(host_port),
  45. failed_rpc_(0), rendezvous_ip_(rendezvous_ip),
  46. rendezvous_port_(rendezvous_port),
  47. last_seen_(base::GetEpochMilliseconds()), local_ip_(local_ip),
  48. local_port_(local_port) {
  49. if (host_ip.size() > 4)
  50. host_ip_ = base::IpAsciiToBytes(host_ip);
  51. if (local_ip.size() > 4)
  52. local_ip_ = base::IpAsciiToBytes(local_ip);
  53. if (rendezvous_ip.size() > 4)
  54. rendezvous_ip_ = base::IpAsciiToBytes(rendezvous_ip);
  55. }
  56. Contact::Contact(const std::string &node_id, const std::string &host_ip,
  57. const boost::uint16_t &host_port)
  58. : node_id_(node_id), host_ip_(host_ip), host_port_(host_port),
  59. failed_rpc_(0), rendezvous_ip_(), rendezvous_port_(0),
  60. last_seen_(base::GetEpochMilliseconds()), local_ip_(), local_port_(0) {
  61. if (host_ip.size() > 4)
  62. host_ip_ = base::IpAsciiToBytes(host_ip);
  63. }
  64. Contact::Contact(const std::string &node_id, const std::string &host_ip,
  65. const boost::uint16_t &host_port, const std::string &local_ip,
  66. const boost::uint16_t &local_port)
  67. : node_id_(node_id), host_ip_(host_ip), host_port_(host_port),
  68. failed_rpc_(0), rendezvous_ip_(), rendezvous_port_(0),
  69. last_seen_(base::GetEpochMilliseconds()), local_ip_(local_ip),
  70. local_port_(local_port) {
  71. if (host_ip.size() > 4)
  72. host_ip_ = base::IpAsciiToBytes(host_ip);
  73. if (local_ip.size() > 4)
  74. local_ip_ = base::IpAsciiToBytes(local_ip);
  75. }
  76. Contact::Contact(const KadId &node_id, const std::string &host_ip,
  77. const boost::uint16_t &host_port, const std::string &local_ip,
  78. const boost::uint16_t &local_port,
  79. const std::string &rendezvous_ip,
  80. const boost::uint16_t &rendezvous_port)
  81. : node_id_(node_id), host_ip_(host_ip), host_port_(host_port),
  82. failed_rpc_(0), rendezvous_ip_(rendezvous_ip),
  83. rendezvous_port_(rendezvous_port),
  84. last_seen_(base::GetEpochMilliseconds()), local_ip_(local_ip),
  85. local_port_(local_port) {
  86. if (host_ip.size() > 4)
  87. host_ip_ = base::IpAsciiToBytes(host_ip);
  88. if (local_ip.size() > 4)
  89. local_ip_ = base::IpAsciiToBytes(local_ip);
  90. if (rendezvous_ip.size() > 4)
  91. rendezvous_ip_ = base::IpAsciiToBytes(rendezvous_ip);
  92. }
  93. Contact::Contact(const KadId &node_id, const std::string &host_ip,
  94. const boost::uint16_t &host_port)
  95. : node_id_(node_id), host_ip_(host_ip), host_port_(host_port),
  96. failed_rpc_(0), rendezvous_ip_(), rendezvous_port_(0),
  97. last_seen_(base::GetEpochMilliseconds()), local_ip_(), local_port_(0) {
  98. if (host_ip.size() > 4)
  99. host_ip_ = base::IpAsciiToBytes(host_ip);
  100. }
  101. Contact::Contact(const KadId &node_id, const std::string &host_ip,
  102. const boost::uint16_t &host_port, const std::string &local_ip,
  103. const boost::uint16_t &local_port)
  104. : node_id_(node_id), host_ip_(host_ip), host_port_(host_port),
  105. failed_rpc_(0), rendezvous_ip_(), rendezvous_port_(0),
  106. last_seen_(base::GetEpochMilliseconds()), local_ip_(local_ip),
  107. local_port_(local_port) {
  108. if (host_ip.size() > 4)
  109. host_ip_ = base::IpAsciiToBytes(host_ip);
  110. if (local_ip.size() > 4)
  111. local_ip_ = base::IpAsciiToBytes(local_ip);
  112. }
  113. Contact::Contact(const ContactInfo &contact_info)
  114. : node_id_(contact_info.node_id()), host_ip_(contact_info.ip()),
  115. host_port_(contact_info.port()), failed_rpc_(0),
  116. rendezvous_ip_(contact_info.rendezvous_ip()),
  117. rendezvous_port_(contact_info.rendezvous_port()),
  118. last_seen_(base::GetEpochMilliseconds()),
  119. local_ip_(contact_info.local_ip()),
  120. local_port_(contact_info.local_port()) {
  121. if (contact_info.ip().size() > 4)
  122. host_ip_ = base::IpAsciiToBytes(contact_info.ip());
  123. if (contact_info.local_ip().size() > 4)
  124. local_ip_ = base::IpAsciiToBytes(contact_info.local_ip());
  125. if (contact_info.rendezvous_ip().size() > 4)
  126. rendezvous_ip_ = base::IpAsciiToBytes(contact_info.rendezvous_ip());
  127. }
  128. Contact::Contact(const Contact &other)
  129. : node_id_(other.node_id_), host_ip_(other.host_ip_),
  130. host_port_(other.host_port_), failed_rpc_(other.failed_rpc_),
  131. rendezvous_ip_(other.rendezvous_ip_),
  132. rendezvous_port_(other.rendezvous_port_),
  133. last_seen_(other.last_seen_), local_ip_(other.local_ip_),
  134. local_port_(other.local_port_) {}
  135. bool Contact::Equals(const Contact &other) const {
  136. if (node_id_ == other.node_id_)
  137. return (node_id_.String() != kClientId) ||
  138. (host_ip_ == other.host_ip_ && host_port_ == other.host_port_);
  139. return false;
  140. }
  141. Contact& Contact::operator=(const Contact &other) {
  142. this->node_id_ = other.node_id_;
  143. this->host_ip_ = other.host_ip_;
  144. this->host_port_ = other.host_port_;
  145. this->failed_rpc_ = other.failed_rpc_;
  146. this->rendezvous_ip_ = other.rendezvous_ip_;
  147. this->rendezvous_port_ = other.rendezvous_port_;
  148. this->last_seen_ = other.last_seen_;
  149. this->local_ip_ = other.local_ip_;
  150. this->local_port_ = other.local_port_;
  151. return *this;
  152. }
  153. bool Contact::SerialiseToString(std::string *serialised_output) {
  154. // do not serialise empty contacts
  155. if (host_port_ == 0 && host_ip_.empty()) {
  156. return false;
  157. }
  158. ContactInfo info;
  159. info.set_node_id(node_id_.String());
  160. info.set_ip(host_ip_);
  161. info.set_port(host_port_);
  162. info.set_rendezvous_ip(rendezvous_ip_);
  163. info.set_rendezvous_port(rendezvous_port_);
  164. info.set_local_ip(local_ip_);
  165. info.set_local_port(local_port_);
  166. info.SerializeToString(serialised_output);
  167. return true;
  168. }
  169. bool Contact::ParseFromString(const std::string &data) {
  170. kad::ContactInfo info;
  171. if (!info.ParseFromString(data))
  172. return false;
  173. node_id_ = KadId(info.node_id());
  174. if (!node_id_.IsValid())
  175. return false;
  176. if (info.ip().size() > 4)
  177. host_ip_ = base::IpAsciiToBytes(info.ip());
  178. else
  179. host_ip_ = info.ip();
  180. host_port_ = static_cast<boost::uint16_t>(info.port());
  181. if (info.has_rendezvous_ip()) {
  182. if (info.rendezvous_ip().size() > 4)
  183. rendezvous_ip_ = base::IpAsciiToBytes(info.rendezvous_ip());
  184. else
  185. rendezvous_ip_ = info.rendezvous_ip();
  186. rendezvous_port_ = static_cast<boost::uint16_t>(info.rendezvous_port());
  187. } else {
  188. rendezvous_ip_.clear();
  189. rendezvous_port_ = 0;
  190. }
  191. if (info.has_local_ip()) {
  192. if (info.local_ip().size() > 4)
  193. local_ip_ = base::IpAsciiToBytes(info.local_ip());
  194. else
  195. local_ip_ = info.local_ip();
  196. local_port_ = static_cast<boost::uint16_t>(info.local_port());
  197. } else {
  198. local_ip_.clear();
  199. local_port_ = 0;
  200. }
  201. last_seen_ = base::GetEpochMilliseconds();
  202. return true;
  203. }
  204. std::string Contact::DebugString() const {
  205. if (host_port_ == 0 && host_ip_.empty()) {
  206. return "Empty contact.\n";
  207. }
  208. std::string port(boost::lexical_cast<std::string>(host_port_));
  209. std::string debug_string = "Node_id: " + node_id_.ToStringEncoded(KadId::kHex)
  210. + "\n";
  211. std::string dec_ip(base::IpBytesToAscii(host_ip_));
  212. debug_string += ("IP address: " + dec_ip + ":" + port + "\n");
  213. if (!local_ip_.empty()) {
  214. std::string dec_lip(base::IpBytesToAscii(local_ip_));
  215. std::string lport(boost::lexical_cast<std::string>(local_port_));
  216. debug_string += ("Local IP address: " + dec_lip + ":" + lport + "\n");
  217. }
  218. if (!rendezvous_ip_.empty()) {
  219. std::string dec_rip(base::IpBytesToAscii(rendezvous_ip_));
  220. std::string rport(boost::lexical_cast<std::string>(rendezvous_port_));
  221. debug_string += ("RV IP address: " + dec_rip + ":" + rport + "\n");
  222. }
  223. return debug_string;
  224. }
  225. } // namespace kad