| 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 |
| 11 #ifndef WEBRTC_BASE_FAKENETWORK_H_ | 11 #ifndef WEBRTC_BASE_FAKENETWORK_H_ |
| 12 #define WEBRTC_BASE_FAKENETWORK_H_ | 12 #define WEBRTC_BASE_FAKENETWORK_H_ |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 #include <utility> |
| 15 #include <vector> | 16 #include <vector> |
| 16 | 17 |
| 17 #include "webrtc/base/network.h" | 18 #include "webrtc/base/network.h" |
| 18 #include "webrtc/base/messagehandler.h" | 19 #include "webrtc/base/messagehandler.h" |
| 19 #include "webrtc/base/socketaddress.h" | 20 #include "webrtc/base/socketaddress.h" |
| 20 #include "webrtc/base/stringencode.h" | 21 #include "webrtc/base/stringencode.h" |
| 21 #include "webrtc/base/thread.h" | 22 #include "webrtc/base/thread.h" |
| 22 | 23 |
| 23 namespace rtc { | 24 namespace rtc { |
| 24 | 25 |
| 25 const int kFakeIPv4NetworkPrefixLength = 24; | 26 const int kFakeIPv4NetworkPrefixLength = 24; |
| 26 const int kFakeIPv6NetworkPrefixLength = 64; | 27 const int kFakeIPv6NetworkPrefixLength = 64; |
| 27 | 28 |
| 28 // Fake network manager that allows us to manually specify the IPs to use. | 29 // Fake network manager that allows us to manually specify the IPs to use. |
| 29 class FakeNetworkManager : public NetworkManagerBase, | 30 class FakeNetworkManager : public NetworkManagerBase, |
| 30 public MessageHandler { | 31 public MessageHandler { |
| 31 public: | 32 public: |
| 32 FakeNetworkManager() : thread_(Thread::Current()) {} | 33 FakeNetworkManager() : thread_(Thread::Current()) {} |
| 33 | 34 |
| 34 typedef std::vector<SocketAddress> IfaceList; | 35 typedef std::vector<std::pair<SocketAddress, AdapterType>> IfaceList; |
| 35 | 36 |
| 36 void AddInterface(const SocketAddress& iface) { | 37 void AddInterface(const SocketAddress& iface) { |
| 37 // Ensure a unique name for the interface if its name is not given. | 38 // Ensure a unique name for the interface if its name is not given. |
| 38 AddInterface(iface, "test" + rtc::ToString(next_index_++)); | 39 AddInterface(iface, "test" + rtc::ToString(next_index_++)); |
| 39 } | 40 } |
| 40 | 41 |
| 41 void AddInterface(const SocketAddress& iface, const std::string& if_name) { | 42 void AddInterface(const SocketAddress& iface, const std::string& if_name) { |
| 43 AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN); |
| 44 } |
| 45 |
| 46 void AddInterface(const SocketAddress& iface, |
| 47 const std::string& if_name, |
| 48 AdapterType type) { |
| 42 SocketAddress address(if_name, 0); | 49 SocketAddress address(if_name, 0); |
| 43 address.SetResolvedIP(iface.ipaddr()); | 50 address.SetResolvedIP(iface.ipaddr()); |
| 44 ifaces_.push_back(address); | 51 ifaces_.push_back(std::make_pair(address, type)); |
| 45 DoUpdateNetworks(); | 52 DoUpdateNetworks(); |
| 46 } | 53 } |
| 47 | 54 |
| 48 void RemoveInterface(const SocketAddress& iface) { | 55 void RemoveInterface(const SocketAddress& iface) { |
| 49 for (IfaceList::iterator it = ifaces_.begin(); | 56 for (IfaceList::iterator it = ifaces_.begin(); |
| 50 it != ifaces_.end(); ++it) { | 57 it != ifaces_.end(); ++it) { |
| 51 if (it->EqualIPs(iface)) { | 58 if (it->first.EqualIPs(iface)) { |
| 52 ifaces_.erase(it); | 59 ifaces_.erase(it); |
| 53 break; | 60 break; |
| 54 } | 61 } |
| 55 } | 62 } |
| 56 DoUpdateNetworks(); | 63 DoUpdateNetworks(); |
| 57 } | 64 } |
| 58 | 65 |
| 59 virtual void StartUpdating() { | 66 virtual void StartUpdating() { |
| 60 ++start_count_; | 67 ++start_count_; |
| 61 if (start_count_ == 1) { | 68 if (start_count_ == 1) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 79 using NetworkManagerBase::set_default_local_addresses; | 86 using NetworkManagerBase::set_default_local_addresses; |
| 80 | 87 |
| 81 private: | 88 private: |
| 82 void DoUpdateNetworks() { | 89 void DoUpdateNetworks() { |
| 83 if (start_count_ == 0) | 90 if (start_count_ == 0) |
| 84 return; | 91 return; |
| 85 std::vector<Network*> networks; | 92 std::vector<Network*> networks; |
| 86 for (IfaceList::iterator it = ifaces_.begin(); | 93 for (IfaceList::iterator it = ifaces_.begin(); |
| 87 it != ifaces_.end(); ++it) { | 94 it != ifaces_.end(); ++it) { |
| 88 int prefix_length = 0; | 95 int prefix_length = 0; |
| 89 if (it->ipaddr().family() == AF_INET) { | 96 if (it->first.ipaddr().family() == AF_INET) { |
| 90 prefix_length = kFakeIPv4NetworkPrefixLength; | 97 prefix_length = kFakeIPv4NetworkPrefixLength; |
| 91 } else if (it->ipaddr().family() == AF_INET6) { | 98 } else if (it->first.ipaddr().family() == AF_INET6) { |
| 92 prefix_length = kFakeIPv6NetworkPrefixLength; | 99 prefix_length = kFakeIPv6NetworkPrefixLength; |
| 93 } | 100 } |
| 94 IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length); | 101 IPAddress prefix = TruncateIP(it->first.ipaddr(), prefix_length); |
| 95 scoped_ptr<Network> net(new Network(it->hostname(), | 102 scoped_ptr<Network> net(new Network(it->first.hostname(), |
| 96 it->hostname(), | 103 it->first.hostname(), prefix, |
| 97 prefix, | 104 prefix_length, it->second)); |
| 98 prefix_length)); | |
| 99 net->set_default_local_address_provider(this); | 105 net->set_default_local_address_provider(this); |
| 100 net->AddIP(it->ipaddr()); | 106 net->AddIP(it->first.ipaddr()); |
| 101 networks.push_back(net.release()); | 107 networks.push_back(net.release()); |
| 102 } | 108 } |
| 103 bool changed; | 109 bool changed; |
| 104 MergeNetworkList(networks, &changed); | 110 MergeNetworkList(networks, &changed); |
| 105 if (changed || !sent_first_update_) { | 111 if (changed || !sent_first_update_) { |
| 106 SignalNetworksChanged(); | 112 SignalNetworksChanged(); |
| 107 sent_first_update_ = true; | 113 sent_first_update_ = true; |
| 108 } | 114 } |
| 109 } | 115 } |
| 110 | 116 |
| 111 Thread* thread_; | 117 Thread* thread_; |
| 112 IfaceList ifaces_; | 118 IfaceList ifaces_; |
| 113 int next_index_ = 0; | 119 int next_index_ = 0; |
| 114 int start_count_ = 0; | 120 int start_count_ = 0; |
| 115 bool sent_first_update_ = false; | 121 bool sent_first_update_ = false; |
| 116 | 122 |
| 117 IPAddress default_local_ipv4_address_; | 123 IPAddress default_local_ipv4_address_; |
| 118 IPAddress default_local_ipv6_address_; | 124 IPAddress default_local_ipv6_address_; |
| 119 }; | 125 }; |
| 120 | 126 |
| 121 } // namespace rtc | 127 } // namespace rtc |
| 122 | 128 |
| 123 #endif // WEBRTC_BASE_FAKENETWORK_H_ | 129 #endif // WEBRTC_BASE_FAKENETWORK_H_ |
| OLD | NEW |