1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_P2P_BASE_CANDIDATE_H_
29#define TALK_P2P_BASE_CANDIDATE_H_
30
31#include <string>
32#include <sstream>
33#include "talk/base/socketaddress.h"
34
35namespace cricket {
36
37// Candidate for ICE based connection discovery.
38
39class Candidate {
40 public:
41  Candidate() : preference_(0), generation_(0) {}
42  Candidate(const std::string& name, const std::string& protocol,
43            const talk_base::SocketAddress& address, float preference,
44            const std::string& username, const std::string& password,
45            const std::string& type, const std::string& network_name,
46            uint32 generation)
47      : name_(name), protocol_(protocol), address_(address),
48        preference_(preference), username_(username), password_(password),
49        type_(type), network_name_(network_name), generation_(generation) {}
50
51  const std::string & name() const { return name_; }
52  void set_name(const std::string & name) { name_ = name; }
53
54  const std::string & protocol() const { return protocol_; }
55  void set_protocol(const std::string & protocol) { protocol_ = protocol; }
56
57  const talk_base::SocketAddress & address() const { return address_; }
58  void set_address(const talk_base::SocketAddress & address) {
59    address_ = address;
60  }
61
62  float preference() const { return preference_; }
63  void set_preference(const float preference) { preference_ = preference; }
64  const std::string preference_str() const {
65    std::ostringstream ost;
66    ost << preference_;
67    return ost.str();
68  }
69  void set_preference_str(const std::string & preference) {
70    std::istringstream ist(preference);
71    ist >> preference_;
72  }
73
74  const std::string & username() const { return username_; }
75  void set_username(const std::string & username) { username_ = username; }
76
77  const std::string & password() const { return password_; }
78  void set_password(const std::string & password) { password_ = password; }
79
80  const std::string & type() const { return type_; }
81  void set_type(const std::string & type) { type_ = type; }
82
83  const std::string & network_name() const { return network_name_; }
84  void set_network_name(const std::string & network_name) {
85    network_name_ = network_name;
86  }
87
88  // Candidates in a new generation replace those in the old generation.
89  uint32 generation() const { return generation_; }
90  void set_generation(uint32 generation) { generation_ = generation; }
91  const std::string generation_str() const {
92    std::ostringstream ost;
93    ost << generation_;
94    return ost.str();
95  }
96  void set_generation_str(const std::string& str) {
97    std::istringstream ist(str);
98    ist >> generation_;
99  }
100
101  // Determines whether this candidate is equivalent to the given one.
102  bool IsEquivalent(const Candidate& c) const {
103    // We ignore the network name, since that is just debug information, and
104    // the preference, since that should be the same if the rest is (and it's
105    // a float so equality checking is always worrisome).
106    return (name_ == c.name_) &&
107           (protocol_ == c.protocol_) &&
108           (address_ == c.address_) &&
109           (username_ == c.username_) &&
110           (password_ == c.password_) &&
111           (type_ == c.type_) &&
112           (generation_ == c.generation_);
113  }
114
115  std::string ToString() const {
116    std::ostringstream ost;
117    ost << "Cand[" << name_ << ":" << type_ << ":" << protocol_ << ":"
118        << network_name_ << ":" << address_.ToString() << ":"
119        << username_ << ":" << password_ << "]";
120    return ost.str();
121  }
122
123 private:
124  std::string name_;
125  std::string protocol_;
126  talk_base::SocketAddress address_;
127  float preference_;
128  std::string username_;
129  std::string password_;
130  std::string type_;
131  std::string network_name_;
132  uint32 generation_;
133};
134
135}  // namespace cricket
136
137#endif  // TALK_P2P_BASE_CANDIDATE_H_
138