Use SegList<int> instead of IntSequence.

pull/2/head
Tatsuhiro Tsujikawa 2011-10-30 14:25:36 +09:00
parent e9b86f2f43
commit 4be395117d
4 changed files with 26 additions and 22 deletions

View File

@ -55,18 +55,17 @@ DHTConnectionImpl::DHTConnectionImpl(int family)
DHTConnectionImpl::~DHTConnectionImpl() {} DHTConnectionImpl::~DHTConnectionImpl() {}
bool DHTConnectionImpl::bind bool DHTConnectionImpl::bind
(uint16_t& port, const std::string& addr, IntSequence& ports) (uint16_t& port, const std::string& addr, SegList<int>& sgl)
{ {
std::vector<int32_t> randPorts = ports.flush(); std::vector<uint16_t> ports;
std::random_shuffle(randPorts.begin(), randPorts.end(), while(sgl.hasNext()) {
*SimpleRandomizer::getInstance().get()); ports.push_back(sgl.next());
for(std::vector<int32_t>::const_iterator portItr = randPorts.begin(),
eoi = randPorts.end(); portItr != eoi; ++portItr) {
if(!(0 < (*portItr) && (*portItr) <= 65535)) {
continue;
} }
port = (*portItr); std::random_shuffle(ports.begin(), ports.end(),
*SimpleRandomizer::getInstance().get());
for(std::vector<uint16_t>::const_iterator i = ports.begin(),
eoi = ports.end(); i != eoi; ++i) {
port = *i;
if(bind(port, addr)) { if(bind(port, addr)) {
return true; return true;
} }

View File

@ -37,7 +37,7 @@
#include "DHTConnection.h" #include "DHTConnection.h"
#include "SharedHandle.h" #include "SharedHandle.h"
#include "IntSequence.h" #include "SegList.h"
namespace aria2 { namespace aria2 {
@ -54,12 +54,13 @@ public:
virtual ~DHTConnectionImpl(); virtual ~DHTConnectionImpl();
/** /**
* Binds port. All number in ports are tried. If successful, the * Binds port. All number in sgl are tried. All numbers in sgl must
* binded port is assigned to port and returns true. Otherwise * be in range [1, 65535], inclusive. If successful, the binded port
* return false and port is undefined in this case. If non-empty * is assigned to port and returns true. Otherwise return false and
* string addr is given, the socket is associated to the address. * port is undefined in this case. If non-empty string addr is
* given, the socket is associated to the address.
*/ */
bool bind(uint16_t& port, const std::string& addr, IntSequence& ports); bool bind(uint16_t& port, const std::string& addr, SegList<int>& sgl);
/** /**
* Binds port. The port number specified by port is used to bind. * Binds port. The port number specified by port is used to bind.

View File

@ -112,13 +112,14 @@ void DHTSetup::setup
SharedHandle<DHTConnectionImpl> connection(new DHTConnectionImpl(family)); SharedHandle<DHTConnectionImpl> connection(new DHTConnectionImpl(family));
{ {
IntSequence seq = SegList<int> sgl;
util::parseIntRange(e->getOption()->get(PREF_DHT_LISTEN_PORT)); util::parseIntSegments(sgl, e->getOption()->get(PREF_DHT_LISTEN_PORT));
sgl.normalize();
uint16_t port; uint16_t port;
const std::string& addr = const std::string& addr =
e->getOption()->get(family == AF_INET?PREF_DHT_LISTEN_ADDR: e->getOption()->get(family == AF_INET?PREF_DHT_LISTEN_ADDR:
PREF_DHT_LISTEN_ADDR6); PREF_DHT_LISTEN_ADDR6);
if(!connection->bind(port, addr, seq)) { if(!connection->bind(port, addr, sgl)) {
throw DL_ABORT_EX("Error occurred while binding port for DHT"); throw DL_ABORT_EX("Error occurred while binding port for DHT");
} }
localNode->setPort(port); localNode->setPort(port);

View File

@ -57,6 +57,7 @@
#include "a2io.h" #include "a2io.h"
#include "LogFactory.h" #include "LogFactory.h"
#include "uri.h" #include "uri.h"
#include "SegList.h"
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
# include "MessageDigest.h" # include "MessageDigest.h"
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
@ -113,9 +114,11 @@ IntegerRangeOptionHandler::~IntegerRangeOptionHandler() {}
void IntegerRangeOptionHandler::parseArg void IntegerRangeOptionHandler::parseArg
(Option& option, const std::string& optarg) (Option& option, const std::string& optarg)
{ {
IntSequence seq = util::parseIntRange(optarg); SegList<int> sgl;
while(seq.hasNext()) { util::parseIntSegments(sgl, optarg);
int32_t v = seq.next(); sgl.normalize();
while(sgl.hasNext()) {
int v = sgl.next();
if(v < min_ || max_ < v) { if(v < min_ || max_ < v) {
std::string msg = pref_->k; std::string msg = pref_->k;
strappend(msg, " ", _("must be between %s and %s.")); strappend(msg, " ", _("must be between %s and %s."));