OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 const int kFakeIPv4NetworkPrefixLength = 24; | 25 const int kFakeIPv4NetworkPrefixLength = 24; |
26 const int kFakeIPv6NetworkPrefixLength = 64; | 26 const int kFakeIPv6NetworkPrefixLength = 64; |
27 | 27 |
28 // Fake network manager that allows us to manually specify the IPs to use. | 28 // Fake network manager that allows us to manually specify the IPs to use. |
29 class FakeNetworkManager : public NetworkManagerBase, | 29 class FakeNetworkManager : public NetworkManagerBase, |
30 public MessageHandler { | 30 public MessageHandler { |
31 public: | 31 public: |
32 FakeNetworkManager() | 32 FakeNetworkManager() |
33 : thread_(Thread::Current()), | 33 : thread_(Thread::Current()), |
34 next_index_(0), | 34 next_index_(0), |
35 started_(false), | 35 start_count_(0), |
36 sent_first_update_(false) { | 36 sent_first_update_(false) { |
37 } | 37 } |
38 | 38 |
39 typedef std::vector<SocketAddress> IfaceList; | 39 typedef std::vector<SocketAddress> IfaceList; |
40 | 40 |
41 void AddInterface(const SocketAddress& iface) { | 41 void AddInterface(const SocketAddress& iface) { |
42 // ensure a unique name for the interface | 42 // ensure a unique name for the interface |
43 SocketAddress address("test" + rtc::ToString(next_index_++), 0); | 43 SocketAddress address("test" + rtc::ToString(next_index_++), 0); |
44 address.SetResolvedIP(iface.ipaddr()); | 44 address.SetResolvedIP(iface.ipaddr()); |
45 ifaces_.push_back(address); | 45 ifaces_.push_back(address); |
46 DoUpdateNetworks(); | 46 DoUpdateNetworks(); |
47 } | 47 } |
48 | 48 |
49 void RemoveInterface(const SocketAddress& iface) { | 49 void RemoveInterface(const SocketAddress& iface) { |
50 for (IfaceList::iterator it = ifaces_.begin(); | 50 for (IfaceList::iterator it = ifaces_.begin(); |
51 it != ifaces_.end(); ++it) { | 51 it != ifaces_.end(); ++it) { |
52 if (it->EqualIPs(iface)) { | 52 if (it->EqualIPs(iface)) { |
53 ifaces_.erase(it); | 53 ifaces_.erase(it); |
54 break; | 54 break; |
55 } | 55 } |
56 } | 56 } |
57 DoUpdateNetworks(); | 57 DoUpdateNetworks(); |
58 } | 58 } |
59 | 59 |
60 virtual void StartUpdating() { | 60 virtual void StartUpdating() { |
61 if (started_) { | 61 if (start_count_ == 0) { |
| 62 ++start_count_; |
| 63 sent_first_update_ = false; |
| 64 thread_->Post(this); |
| 65 } else { |
| 66 ++start_count_; |
62 if (sent_first_update_) | 67 if (sent_first_update_) |
63 SignalNetworksChanged(); | 68 SignalNetworksChanged(); |
64 return; | |
65 } | 69 } |
66 | |
67 started_ = true; | |
68 sent_first_update_ = false; | |
69 thread_->Post(this); | |
70 } | 70 } |
71 | 71 |
72 virtual void StopUpdating() { | 72 virtual void StopUpdating() { |
73 started_ = false; | 73 --start_count_; |
74 } | 74 } |
75 | 75 |
76 // MessageHandler interface. | 76 // MessageHandler interface. |
77 virtual void OnMessage(Message* msg) { | 77 virtual void OnMessage(Message* msg) { |
78 DoUpdateNetworks(); | 78 DoUpdateNetworks(); |
79 } | 79 } |
80 | 80 |
81 private: | 81 private: |
82 void DoUpdateNetworks() { | 82 void DoUpdateNetworks() { |
83 if (!started_) | 83 if (start_count_ == 0) |
84 return; | 84 return; |
85 std::vector<Network*> networks; | 85 std::vector<Network*> networks; |
86 for (IfaceList::iterator it = ifaces_.begin(); | 86 for (IfaceList::iterator it = ifaces_.begin(); |
87 it != ifaces_.end(); ++it) { | 87 it != ifaces_.end(); ++it) { |
88 int prefix_length = 0; | 88 int prefix_length = 0; |
89 if (it->ipaddr().family() == AF_INET) { | 89 if (it->ipaddr().family() == AF_INET) { |
90 prefix_length = kFakeIPv4NetworkPrefixLength; | 90 prefix_length = kFakeIPv4NetworkPrefixLength; |
91 } else if (it->ipaddr().family() == AF_INET6) { | 91 } else if (it->ipaddr().family() == AF_INET6) { |
92 prefix_length = kFakeIPv6NetworkPrefixLength; | 92 prefix_length = kFakeIPv6NetworkPrefixLength; |
93 } | 93 } |
94 IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length); | 94 IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length); |
95 scoped_ptr<Network> net(new Network(it->hostname(), | 95 scoped_ptr<Network> net(new Network(it->hostname(), |
96 it->hostname(), | 96 it->hostname(), |
97 prefix, | 97 prefix, |
98 prefix_length)); | 98 prefix_length)); |
99 net->AddIP(it->ipaddr()); | 99 net->AddIP(it->ipaddr()); |
100 networks.push_back(net.release()); | 100 networks.push_back(net.release()); |
101 } | 101 } |
102 bool changed; | 102 bool changed; |
103 MergeNetworkList(networks, &changed); | 103 MergeNetworkList(networks, &changed); |
104 if (changed || !sent_first_update_) { | 104 if (changed || !sent_first_update_) { |
105 SignalNetworksChanged(); | 105 SignalNetworksChanged(); |
106 sent_first_update_ = true; | 106 sent_first_update_ = true; |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 Thread* thread_; | 110 Thread* thread_; |
111 IfaceList ifaces_; | 111 IfaceList ifaces_; |
112 int next_index_; | 112 int next_index_; |
113 bool started_; | 113 int start_count_; |
114 bool sent_first_update_; | 114 bool sent_first_update_; |
115 }; | 115 }; |
116 | 116 |
117 } // namespace rtc | 117 } // namespace rtc |
118 | 118 |
119 #endif // WEBRTC_BASE_FAKENETWORK_H_ | 119 #endif // WEBRTC_BASE_FAKENETWORK_H_ |
OLD | NEW |