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() {}
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::random_shuffle(randPorts.begin(), randPorts.end(),
std::vector<uint16_t> ports;
while(sgl.hasNext()) {
ports.push_back(sgl.next());
}
std::random_shuffle(ports.begin(), ports.end(),
*SimpleRandomizer::getInstance().get());
for(std::vector<int32_t>::const_iterator portItr = randPorts.begin(),
eoi = randPorts.end(); portItr != eoi; ++portItr) {
if(!(0 < (*portItr) && (*portItr) <= 65535)) {
continue;
}
port = (*portItr);
for(std::vector<uint16_t>::const_iterator i = ports.begin(),
eoi = ports.end(); i != eoi; ++i) {
port = *i;
if(bind(port, addr)) {
return true;
}

View File

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

View File

@ -112,13 +112,14 @@ void DHTSetup::setup
SharedHandle<DHTConnectionImpl> connection(new DHTConnectionImpl(family));
{
IntSequence seq =
util::parseIntRange(e->getOption()->get(PREF_DHT_LISTEN_PORT));
SegList<int> sgl;
util::parseIntSegments(sgl, e->getOption()->get(PREF_DHT_LISTEN_PORT));
sgl.normalize();
uint16_t port;
const std::string& addr =
e->getOption()->get(family == AF_INET?PREF_DHT_LISTEN_ADDR:
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");
}
localNode->setPort(port);

View File

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