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 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace rtc { | 23 namespace rtc { |
24 | 24 |
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() : thread_(Thread::Current()) {} | 32 FakeNetworkManager() |
| 33 : thread_(Thread::Current()), |
| 34 next_index_(0), |
| 35 started_(false), |
| 36 sent_first_update_(false) { |
| 37 } |
33 | 38 |
34 typedef std::vector<SocketAddress> IfaceList; | 39 typedef std::vector<SocketAddress> IfaceList; |
35 | 40 |
36 void AddInterface(const SocketAddress& iface) { | 41 void AddInterface(const SocketAddress& iface) { |
37 // ensure a unique name for the interface | 42 // ensure a unique name for the interface |
38 SocketAddress address("test" + rtc::ToString(next_index_++), 0); | 43 SocketAddress address("test" + rtc::ToString(next_index_++), 0); |
39 address.SetResolvedIP(iface.ipaddr()); | 44 address.SetResolvedIP(iface.ipaddr()); |
40 ifaces_.push_back(address); | 45 ifaces_.push_back(address); |
41 DoUpdateNetworks(); | 46 DoUpdateNetworks(); |
42 } | 47 } |
43 | 48 |
44 void RemoveInterface(const SocketAddress& iface) { | 49 void RemoveInterface(const SocketAddress& iface) { |
45 for (IfaceList::iterator it = ifaces_.begin(); | 50 for (IfaceList::iterator it = ifaces_.begin(); |
46 it != ifaces_.end(); ++it) { | 51 it != ifaces_.end(); ++it) { |
47 if (it->EqualIPs(iface)) { | 52 if (it->EqualIPs(iface)) { |
48 ifaces_.erase(it); | 53 ifaces_.erase(it); |
49 break; | 54 break; |
50 } | 55 } |
51 } | 56 } |
52 DoUpdateNetworks(); | 57 DoUpdateNetworks(); |
53 } | 58 } |
54 | 59 |
55 virtual void StartUpdating() { | 60 virtual void StartUpdating() { |
56 ++start_count_; | 61 if (started_) { |
57 if (start_count_ == 1) { | 62 if (sent_first_update_) |
58 sent_first_update_ = false; | |
59 thread_->Post(this); | |
60 } else { | |
61 if (sent_first_update_) { | |
62 SignalNetworksChanged(); | 63 SignalNetworksChanged(); |
63 } | 64 return; |
64 } | 65 } |
| 66 |
| 67 started_ = true; |
| 68 sent_first_update_ = false; |
| 69 thread_->Post(this); |
65 } | 70 } |
66 | 71 |
67 virtual void StopUpdating() { --start_count_; } | 72 virtual void StopUpdating() { |
| 73 started_ = false; |
| 74 } |
68 | 75 |
69 // MessageHandler interface. | 76 // MessageHandler interface. |
70 virtual void OnMessage(Message* msg) { | 77 virtual void OnMessage(Message* msg) { |
71 DoUpdateNetworks(); | 78 DoUpdateNetworks(); |
72 } | 79 } |
73 | 80 |
74 using NetworkManagerBase::set_enumeration_permission; | 81 using NetworkManagerBase::set_enumeration_permission; |
75 | 82 |
76 private: | 83 private: |
77 void DoUpdateNetworks() { | 84 void DoUpdateNetworks() { |
78 if (start_count_ == 0) | 85 if (!started_) |
79 return; | 86 return; |
80 std::vector<Network*> networks; | 87 std::vector<Network*> networks; |
81 for (IfaceList::iterator it = ifaces_.begin(); | 88 for (IfaceList::iterator it = ifaces_.begin(); |
82 it != ifaces_.end(); ++it) { | 89 it != ifaces_.end(); ++it) { |
83 int prefix_length = 0; | 90 int prefix_length = 0; |
84 if (it->ipaddr().family() == AF_INET) { | 91 if (it->ipaddr().family() == AF_INET) { |
85 prefix_length = kFakeIPv4NetworkPrefixLength; | 92 prefix_length = kFakeIPv4NetworkPrefixLength; |
86 } else if (it->ipaddr().family() == AF_INET6) { | 93 } else if (it->ipaddr().family() == AF_INET6) { |
87 prefix_length = kFakeIPv6NetworkPrefixLength; | 94 prefix_length = kFakeIPv6NetworkPrefixLength; |
88 } | 95 } |
89 IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length); | 96 IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length); |
90 scoped_ptr<Network> net(new Network(it->hostname(), | 97 scoped_ptr<Network> net(new Network(it->hostname(), |
91 it->hostname(), | 98 it->hostname(), |
92 prefix, | 99 prefix, |
93 prefix_length)); | 100 prefix_length)); |
94 net->AddIP(it->ipaddr()); | 101 net->AddIP(it->ipaddr()); |
95 networks.push_back(net.release()); | 102 networks.push_back(net.release()); |
96 } | 103 } |
97 bool changed; | 104 bool changed; |
98 MergeNetworkList(networks, &changed); | 105 MergeNetworkList(networks, &changed); |
99 if (changed || !sent_first_update_) { | 106 if (changed || !sent_first_update_) { |
100 SignalNetworksChanged(); | 107 SignalNetworksChanged(); |
101 sent_first_update_ = true; | 108 sent_first_update_ = true; |
102 } | 109 } |
103 } | 110 } |
104 | 111 |
105 Thread* thread_; | 112 Thread* thread_; |
106 IfaceList ifaces_; | 113 IfaceList ifaces_; |
107 int next_index_ = 0; | 114 int next_index_; |
108 int start_count_ = 0; | 115 bool started_; |
109 bool sent_first_update_ = false; | 116 bool sent_first_update_; |
110 }; | 117 }; |
111 | 118 |
112 } // namespace rtc | 119 } // namespace rtc |
113 | 120 |
114 #endif // WEBRTC_BASE_FAKENETWORK_H_ | 121 #endif // WEBRTC_BASE_FAKENETWORK_H_ |
OLD | NEW |