Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include <stdio.h> | |
| 2 #include <iostream> | |
| 3 | |
| 4 #include "base/at_exit.h" | |
| 5 #include "base/command_line.h" | |
| 6 #if defined(OS_MACOSX) | |
| 7 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 8 #endif | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/string_number_conversions.h" | |
| 12 | |
| 13 #include "net/base/host_resolver_impl.h" | |
| 14 #include "net/base/net_errors.h" | |
| 15 #include "net/base/net_util.h" | |
| 16 #include "net/base/sys_addrinfo.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 class GDig; | |
| 23 | |
| 24 // This class is a wrapper around the real DnsConfigService. | |
| 25 // It is used to intercept the OnDnsConfig and notify the main program | |
|
szym
2012/05/18 18:15:05
Not clear which "OnDnsConfig." How about:
"It will
| |
| 26 // that a DnsConfig has been loaded. | |
| 27 class DnsConfigServiceMock : public DnsConfigService { | |
|
szym
2012/05/18 18:15:05
nit: in src/net MockXxx is much more common than X
| |
| 28 public: | |
| 29 explicit DnsConfigServiceMock(GDig* gdig); | |
| 30 | |
| 31 virtual ~DnsConfigServiceMock(); | |
| 32 | |
| 33 virtual void Watch(const CallbackType& callback) OVERRIDE; | |
| 34 void OnDnsConfig(const DnsConfig& dns_config_); | |
|
szym
2012/05/18 18:15:05
Can be private.
| |
| 35 private: | |
| 36 GDig* gdig_; | |
| 37 scoped_ptr<DnsConfigService> real_service_; | |
| 38 DnsConfigService::CallbackType callback_; | |
| 39 }; | |
| 40 | |
| 41 class GDig { | |
| 42 public: | |
| 43 GDig(); | |
| 44 | |
| 45 static void PrintUsage(const char* program_name); | |
| 46 | |
| 47 enum Result { | |
| 48 RESULT_NO_RESOLVE = -3, | |
| 49 RESULT_NO_CONFIG = -2, | |
| 50 RESULT_WRONG_USAGE = -1, | |
| 51 RESULT_OK = 0, | |
| 52 }; | |
| 53 | |
| 54 Result Main(int argc, const char* argv[]); | |
| 55 | |
| 56 private: | |
| 57 bool ParseCommandLine(int argc, const char* argv[]); | |
| 58 | |
| 59 void Start(); | |
| 60 | |
| 61 void OnDnsConfig(const DnsConfig& dns_config); | |
| 62 friend void DnsConfigServiceMock::OnDnsConfig(const DnsConfig&); | |
|
szym
2012/05/18 18:15:05
I think it's reasonable to friend the whole class.
| |
| 63 void OnResolveComplete(int val); | |
| 64 void OnTimeout(); | |
| 65 | |
| 66 Result result_; | |
| 67 | |
| 68 int resolve_name_ret_; | |
| 69 AddressList addrlist_; | |
| 70 | |
| 71 base::CancelableClosure timeout_; | |
| 72 DnsConfig dns_config_; | |
| 73 | |
| 74 int timeout_seconds_; | |
|
szym
2012/06/01 17:54:49
Use base::TimeDelta.
| |
| 75 std::string domain_name_; | |
| 76 | |
| 77 scoped_ptr<HostResolver> resolver_; | |
| 78 }; | |
| 79 | |
| 80 | |
| 81 DnsConfigServiceMock::DnsConfigServiceMock(GDig* gdig) : gdig_(gdig) { | |
| 82 } | |
| 83 | |
| 84 DnsConfigServiceMock::~DnsConfigServiceMock() {} | |
| 85 | |
| 86 void DnsConfigServiceMock::Watch(const CallbackType& callback) OVERRIDE { | |
| 87 DCHECK(CalledOnValidThread()); | |
| 88 DCHECK(!callback.is_null()); | |
| 89 callback_ = callback; | |
| 90 real_service_ = DnsConfigService::CreateSystemService(); | |
| 91 real_service_->Watch(base::Bind(&DnsConfigServiceMock::OnDnsConfig, | |
| 92 Unretained(this))); | |
| 93 } | |
| 94 | |
| 95 void DnsConfigServiceMock::OnDnsConfig(const DnsConfig& dns_config_) { | |
| 96 callback_.Run(dns_config_); | |
| 97 gdig_->OnDnsConfig(dns_config_); | |
| 98 real_service_.reset(); | |
| 99 } | |
| 100 | |
| 101 GDig::GDig() | |
| 102 : result_(GDig::RESULT_OK), | |
| 103 resolve_name_ret_(ERR_TIMED_OUT), | |
| 104 timeout_seconds_(5) { | |
| 105 } | |
| 106 | |
| 107 void GDig::PrintUsage(const char* program_name) { | |
| 108 std::cout << "usage: " << program_name << | |
|
szym
2012/05/18 18:15:05
This is so basic, could consider removing this fun
| |
| 109 " [--read_config_timeout=<seconds>] domain_name" << | |
| 110 std::endl; | |
| 111 } | |
| 112 | |
| 113 GDig::Result GDig::Main(int argc, const char* argv[]) { | |
| 114 if (!ParseCommandLine(argc, argv)) { | |
| 115 PrintUsage(argv[0]); | |
| 116 return RESULT_WRONG_USAGE; | |
| 117 } | |
| 118 | |
| 119 #if defined(OS_MACOSX) | |
| 120 // Without this there will be a mem leak on osx | |
| 121 base::mac::ScopedNSAutoreleasePool scoped_pool; | |
| 122 #endif | |
| 123 | |
| 124 base::AtExitManager exit_manager; | |
| 125 MessageLoop loop(MessageLoop::TYPE_IO); | |
| 126 | |
| 127 Start(); | |
| 128 | |
| 129 MessageLoop::current()->Run(); | |
| 130 return result_; | |
| 131 } | |
| 132 | |
| 133 void GDig::OnResolveComplete(int val) { | |
| 134 MessageLoop::current()->Quit(); | |
| 135 if (val < 0) { | |
|
szym
2012/05/18 18:15:05
val != OK
| |
| 136 std::cout << "Error trying to resolve hostname " << domain_name_ << | |
| 137 ":" << ErrorToString(resolve_name_ret_) << std::endl; | |
| 138 result_ = RESULT_NO_RESOLVE; | |
| 139 } else { | |
| 140 const struct addrinfo* addrinf = addrlist_.head(); | |
|
szym
2012/05/18 18:15:05
AddressList has been changed to std::vector<IPEndP
| |
| 141 while (addrinf) { | |
| 142 std::string ip = NetAddressToStringWithPort(addrinf); | |
| 143 std::cout << ip << std::endl; | |
| 144 addrinf = addrinf->ai_next; | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 void GDig::OnTimeout() { | |
| 150 MessageLoop::current()->Quit(); | |
| 151 std::cout << "Timed out waiting to load the dns config" << std::endl; | |
| 152 result_ = RESULT_NO_CONFIG; | |
| 153 } | |
| 154 | |
| 155 void GDig::Start() { | |
| 156 scoped_ptr<DnsConfigService> | |
| 157 dns_config_service(new DnsConfigServiceMock(this)); | |
| 158 | |
| 159 resolver_.reset( | |
| 160 new HostResolverImpl( | |
| 161 HostCache::CreateDefaultCache(), | |
| 162 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1), | |
| 163 HostResolverImpl::ProcTaskParams(NULL, 1), | |
| 164 dns_config_service.Pass(), | |
| 165 NULL)); | |
| 166 | |
| 167 timeout_.Reset(base::Bind(&GDig::OnTimeout, base::Unretained(this))); | |
| 168 | |
| 169 MessageLoop::current()->PostDelayedTask( | |
| 170 FROM_HERE, | |
| 171 timeout_.callback(), | |
| 172 base::TimeDelta::FromSeconds(timeout_seconds_)); | |
| 173 } | |
| 174 | |
| 175 void GDig::OnDnsConfig(const DnsConfig& dns_config) { | |
| 176 timeout_.Cancel(); | |
| 177 | |
| 178 DCHECK(dns_config.IsValid()); | |
| 179 dns_config_ = dns_config; | |
| 180 | |
| 181 HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80)); | |
| 182 | |
| 183 CompletionCallback callback = base::Bind(&GDig::OnResolveComplete, | |
| 184 base::Unretained(this)); | |
| 185 int ret = resolver_->Resolve(info, &addrlist_, callback, NULL, BoundNetLog()); | |
| 186 DCHECK(ret == ERR_IO_PENDING); | |
| 187 } | |
| 188 | |
| 189 bool GDig::ParseCommandLine(int argc, const char* argv[]) { | |
| 190 CommandLine::Init(argc, argv); | |
| 191 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | |
| 192 | |
| 193 if (parsed_command_line.GetArgs().size() != 1) { | |
| 194 return false; | |
| 195 } | |
| 196 domain_name_ = parsed_command_line.GetArgs().at(0); | |
| 197 | |
| 198 if (parsed_command_line.HasSwitch("read_config_timeout")) { | |
|
szym
2012/05/18 18:15:05
Perhaps just config_timeout
| |
| 199 base::StringToInt( | |
| 200 parsed_command_line.GetSwitchValueASCII("read_config_timeout"), | |
| 201 &timeout_seconds_); | |
| 202 } | |
| 203 | |
| 204 return true; | |
| 205 } | |
| 206 | |
| 207 } // empty namespace | |
| 208 | |
| 209 } // namespace net | |
| 210 | |
| 211 int main(int argc, const char* argv[]) { | |
| 212 net::GDig dig; | |
| 213 return dig.Main(argc, argv); | |
| 214 } | |
| OLD | NEW |