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