Rewrite async DNS portion without exception

pull/60/head
Tatsuhiro Tsujikawa 2013-04-01 02:03:22 +09:00
parent 497c1dd8c9
commit d575e3cc77
2 changed files with 34 additions and 31 deletions

View File

@ -94,30 +94,20 @@ bool DHTEntryPointNameResolveCommand::execute()
entryPoints_.front().second); entryPoints_.front().second);
addPingTask(p); addPingTask(p);
} else { } else {
try { std::vector<std::string> res;
if(resolveHostname(hostname)) { int rv = resolveHostname(res, hostname);
std::vector<std::string> addrs; if(rv == 0) {
asyncNameResolverMan_->getResolvedAddress(addrs); e_->addCommand(this);
if(addrs.empty()) { return false;
A2_LOG_ERROR(fmt("No address returned for %s", } else {
hostname.c_str())); if(rv == 1) {
} else { ++numSuccess_;
A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE, std::pair<std::string, uint16_t> p
getCuid(), hostname.c_str(), (res.front(), entryPoints_.front().second);
addrs.front().c_str())); addPingTask(p);
++numSuccess_;
std::pair<std::string, uint16_t> p
(addrs.front(), entryPoints_.front().second);
addPingTask(p);
}
} else {
e_->addCommand(this);
return false;
} }
} catch(RecoverableException& e) { asyncNameResolverMan_->reset(e_, this);
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
} }
asyncNameResolverMan_->reset(e_, this);
} }
entryPoints_.pop_front(); entryPoints_.pop_front();
} }
@ -165,25 +155,37 @@ void DHTEntryPointNameResolveCommand::addPingTask
#ifdef ENABLE_ASYNC_DNS #ifdef ENABLE_ASYNC_DNS
bool DHTEntryPointNameResolveCommand::resolveHostname int DHTEntryPointNameResolveCommand::resolveHostname
(const std::string& hostname) (std::vector<std::string>& res, const std::string& hostname)
{ {
if(!asyncNameResolverMan_->started()) { if(!asyncNameResolverMan_->started()) {
asyncNameResolverMan_->startAsync(hostname, e_, this); asyncNameResolverMan_->startAsync(hostname, e_, this);
return 0;
} else { } else {
switch(asyncNameResolverMan_->getStatus()) { switch(asyncNameResolverMan_->getStatus()) {
case -1: case -1:
throw DL_ABORT_EX2 A2_LOG_INFO
(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(), (fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
asyncNameResolverMan_->getLastError().c_str()), asyncNameResolverMan_->getLastError().c_str()));
error_code::NAME_RESOLVE_ERROR); return -1;
case 0: case 0:
return false; return 0;
case 1: case 1:
return true; asyncNameResolverMan_->getResolvedAddress(res);
if(res.empty()) {
A2_LOG_INFO
(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
"No address returned"));
return -1;
} else {
A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE,
getCuid(), hostname.c_str(), res.front().c_str()));
return 1;
}
} }
} }
return false; // Unreachable
return 0;
} }
#endif // ENABLE_ASYNC_DNS #endif // ENABLE_ASYNC_DNS

View File

@ -80,7 +80,8 @@ private:
void addPingTask(const std::pair<std::string, uint16_t>& addr); void addPingTask(const std::pair<std::string, uint16_t>& addr);
#ifdef ENABLE_ASYNC_DNS #ifdef ENABLE_ASYNC_DNS
bool resolveHostname(const std::string& hostname); int resolveHostname(std::vector<std::string>& res,
const std::string& hostname);
#endif // ENABLE_ASYNC_DNS #endif // ENABLE_ASYNC_DNS
public: public: