فهرست منبع

2010-09-26 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Sort usedHosts by least used and faster download speed.
	* src/Makefile.am
	* src/RequestGroupMan.cc
	* src/Triplet.h
	* test/Makefile.am
	* test/TripletTest.cc
Tatsuhiro Tsujikawa 15 سال پیش
والد
کامیت
aabd7b75f9
8فایلهای تغییر یافته به همراه267 افزوده شده و 16 حذف شده
  1. 9 0
      ChangeLog
  2. 2 1
      src/Makefile.am
  3. 2 2
      src/Makefile.in
  4. 18 7
      src/RequestGroupMan.cc
  5. 151 0
      src/Triplet.h
  6. 2 1
      test/Makefile.am
  7. 6 5
      test/Makefile.in
  8. 77 0
      test/TripletTest.cc

+ 9 - 0
ChangeLog

@@ -1,3 +1,12 @@
+2010-09-26  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Sort usedHosts by least used and faster download speed.
+	* src/Makefile.am
+	* src/RequestGroupMan.cc
+	* src/Triplet.h
+	* test/Makefile.am
+	* test/TripletTest.cc
+
 2010-09-23  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Made small optimization.

+ 2 - 1
src/Makefile.am

@@ -210,7 +210,8 @@ SRCS =  Socket.h\
 	SinkStreamFilter.cc SinkStreamFilter.h\
 	ChunkedDecodingStreamFilter.cc ChunkedDecodingStreamFilter.h\
 	NullSinkStreamFilter.cc NullSinkStreamFilter.h\
-	uri.cc uri.h
+	uri.cc uri.h\
+	Triplet.h
 
 if ENABLE_XML_RPC
 SRCS += XmlRpcRequestParserController.cc XmlRpcRequestParserController.h\

+ 2 - 2
src/Makefile.in

@@ -447,7 +447,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
 	StreamFilter.h SinkStreamFilter.cc SinkStreamFilter.h \
 	ChunkedDecodingStreamFilter.cc ChunkedDecodingStreamFilter.h \
 	NullSinkStreamFilter.cc NullSinkStreamFilter.h uri.cc uri.h \
-	XmlRpcRequestParserController.cc \
+	Triplet.h XmlRpcRequestParserController.cc \
 	XmlRpcRequestParserController.h \
 	XmlRpcRequestParserStateMachine.cc \
 	XmlRpcRequestParserStateMachine.h XmlRpcRequestParserState.h \
@@ -1241,7 +1241,7 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \
 	StreamFilter.h SinkStreamFilter.cc SinkStreamFilter.h \
 	ChunkedDecodingStreamFilter.cc ChunkedDecodingStreamFilter.h \
 	NullSinkStreamFilter.cc NullSinkStreamFilter.h uri.cc uri.h \
-	$(am__append_1) $(am__append_2) $(am__append_3) \
+	Triplet.h $(am__append_1) $(am__append_2) $(am__append_3) \
 	$(am__append_4) $(am__append_5) $(am__append_6) \
 	$(am__append_7) $(am__append_8) $(am__append_9) \
 	$(am__append_10) $(am__append_11) $(am__append_12) \

+ 18 - 7
src/RequestGroupMan.cc

@@ -72,6 +72,7 @@
 #include "Segment.h"
 #include "DlAbortEx.h"
 #include "uri.h"
+#include "Triplet.h"
 
 namespace aria2 {
 
@@ -898,6 +899,11 @@ bool RequestGroupMan::doesOverallUploadSpeedExceed()
 void RequestGroupMan::getUsedHosts
 (std::vector<std::pair<size_t, std::string> >& usedHosts)
 {
+  // vector of triplet which consists of use count, -download speed,
+  // hostname. We want to sort by least used and faster download
+  // speed. We use -download speed so that we can sort them using
+  // operator<().
+  std::vector<Triplet<size_t, int, std::string> > tempHosts;
   for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
         requestGroups_.begin(), eoi = requestGroups_.end(); i != eoi; ++i) {
     const std::deque<SharedHandle<Request> >& inFlightReqs =
@@ -906,22 +912,27 @@ void RequestGroupMan::getUsedHosts
           inFlightReqs.begin(), eoj = inFlightReqs.end(); j != eoj; ++j) {
       uri::UriStruct us;
       if(uri::parse(us, (*j)->getUri())) {
-        std::vector<std::pair<size_t, std::string> >::iterator k;
-        std::vector<std::pair<size_t, std::string> >::iterator eok =
-          usedHosts.end();
-        for(k =  usedHosts.begin(); k != eok; ++k) {
-          if((*k).second == us.host) {
+        std::vector<Triplet<size_t, int, std::string> >::iterator k;
+        std::vector<Triplet<size_t, int, std::string> >::iterator eok =
+          tempHosts.end();
+        for(k =  tempHosts.begin(); k != eok; ++k) {
+          if((*k).third == us.host) {
             ++(*k).first;
             break;
           }
         }
         if(k == eok) {
-          usedHosts.push_back(std::make_pair(1, us.host));
+          SharedHandle<ServerStat> ss = findServerStat(us.host, us.protocol);
+          int invDlSpeed = !ss.isNull() && ss->isOK()?
+            -(static_cast<int>(ss->getDownloadSpeed())):0;
+          tempHosts.push_back(makeTriplet(1, invDlSpeed, us.host));
         }
       }
     }
   }
-  std::sort(usedHosts.begin(), usedHosts.end());
+  std::sort(tempHosts.begin(), tempHosts.end());
+  std::transform(tempHosts.begin(), tempHosts.end(),
+                 std::back_inserter(usedHosts), Triplet2Pair<1, 3>());
 }
 
 } // namespace aria2

+ 151 - 0
src/Triplet.h

@@ -0,0 +1,151 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2010 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef D_TRIPLET_H
+#define D_TRIPLET_H
+
+#include <utility>
+
+namespace aria2 {
+
+template<typename T1, typename T2, typename T3>
+struct Triplet {
+  typedef T1 first_type;
+  typedef T2 second_type;
+  typedef T3 third_type;
+
+  T1 first;
+  T2 second;
+  T3 third;
+
+  Triplet() {}
+
+  Triplet(const T1& t1, const T2& t2, const T3& t3):
+    first(t1), second(t2), third(t3) {}
+
+  template<typename U1, typename U2, typename U3>
+  Triplet(const Triplet<U1, U2, U3>& t):
+    first(t.first), second(t.second), third(t.third) {}
+
+  Triplet& operator=(const Triplet& tri)
+  {
+    if(this != &tri) {
+      first = tri.first;
+      second = tri.second;
+      third = tri.third;
+    }
+    return *this;
+  }
+};
+
+template<typename T1, typename T2, typename T3>
+bool operator<(const Triplet<T1, T2, T3>& lhs, const Triplet<T1, T2, T3>& rhs)
+{
+  return lhs.first < rhs.first ||
+    (!(rhs.first < lhs.first) && (lhs.second < rhs.second ||
+                                  (!(rhs.second < lhs.second) &&
+                                   lhs.third < rhs.third)));
+}
+
+template<typename T1, typename T2, typename T3>
+Triplet<T1, T2, T3> makeTriplet(const T1& t1, const T2& t2, const T3& t3)
+{
+  return Triplet<T1, T2, T3>(t1, t2, t3);
+}
+
+template<typename T1, typename T2, typename T3, size_t N>
+struct TripletNthType;
+
+template<typename T1, typename T2, typename T3>
+struct TripletNthType<T1, T2, T3, 1> {
+  typedef T1 type;
+};
+
+template<typename T1, typename T2, typename T3>
+struct TripletNthType<T1, T2, T3, 2> {
+  typedef T2 type;
+};
+
+template<typename T1, typename T2, typename T3>
+struct TripletNthType<T1, T2, T3, 3> {
+  typedef T3 type;
+};
+
+template<size_t N>
+struct TripletGet;
+
+template<>
+struct TripletGet<1> {
+  template<typename T1, typename T2, typename T3>
+  static T1 get(const Triplet<T1, T2, T3>& tri)
+  {
+    return tri.first;
+  }
+};
+
+template<>
+struct TripletGet<2> {
+  template<typename T1, typename T2, typename T3>
+  static T2 get(const Triplet<T1, T2, T3>& tri)
+  {
+    return tri.second;
+  }
+};
+
+template<>
+struct TripletGet<3> {
+  template<typename T1, typename T2, typename T3>
+  static T3 get(const Triplet<T1, T2, T3>& tri)
+  {
+    return tri.third;
+  }
+};
+
+template<size_t N1, size_t N2>
+class Triplet2Pair {
+public:
+  template<typename T1, typename T2, typename T3>
+  std::pair<typename TripletNthType<T1, T2, T3, N1>::type,
+            typename TripletNthType<T1, T2, T3, N2>::type>
+  operator()(const Triplet<T1, T2, T3>& tri) const
+  {
+    return std::make_pair<typename TripletNthType<T1, T2, T3, N1>::type,
+                          typename TripletNthType<T1, T2, T3, N2>::type>
+      (TripletGet<N1>::get(tri), TripletGet<N2>::get(tri));
+  }
+};
+
+} // namespace aria2
+
+#endif // D_TRIPLET_H

+ 2 - 1
test/Makefile.am

@@ -74,7 +74,8 @@ aria2c_SOURCES = AllTest.cc\
 	ValueBaseTest.cc\
 	ChunkedDecodingStreamFilterTest.cc\
 	UriTest.cc\
-	MockSegment.h
+	MockSegment.h\
+	TripletTest.cc
 
 if ENABLE_XML_RPC
 aria2c_SOURCES += XmlRpcRequestParserControllerTest.cc\

+ 6 - 5
test/Makefile.in

@@ -215,7 +215,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
 	bitfieldTest.cc DownloadContextTest.cc \
 	SessionSerializerTest.cc ValueBaseTest.cc \
 	ChunkedDecodingStreamFilterTest.cc UriTest.cc MockSegment.h \
-	XmlRpcRequestParserControllerTest.cc \
+	TripletTest.cc XmlRpcRequestParserControllerTest.cc \
 	XmlRpcRequestProcessorTest.cc XmlRpcMethodTest.cc \
 	FallocFileAllocationIteratorTest.cc GZipDecoderTest.cc \
 	GZipEncoderTest.cc GZipDecodingStreamFilterTest.cc \
@@ -409,9 +409,9 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \
 	DownloadContextTest.$(OBJEXT) SessionSerializerTest.$(OBJEXT) \
 	ValueBaseTest.$(OBJEXT) \
 	ChunkedDecodingStreamFilterTest.$(OBJEXT) UriTest.$(OBJEXT) \
-	$(am__objects_1) $(am__objects_2) $(am__objects_3) \
-	$(am__objects_4) $(am__objects_5) $(am__objects_6) \
-	$(am__objects_7)
+	TripletTest.$(OBJEXT) $(am__objects_1) $(am__objects_2) \
+	$(am__objects_3) $(am__objects_4) $(am__objects_5) \
+	$(am__objects_6) $(am__objects_7)
 aria2c_OBJECTS = $(am_aria2c_OBJECTS)
 am__DEPENDENCIES_1 =
 aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1)
@@ -646,7 +646,7 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \
 	bitfieldTest.cc DownloadContextTest.cc \
 	SessionSerializerTest.cc ValueBaseTest.cc \
 	ChunkedDecodingStreamFilterTest.cc UriTest.cc MockSegment.h \
-	$(am__append_1) $(am__append_2) $(am__append_3) \
+	TripletTest.cc $(am__append_1) $(am__append_2) $(am__append_3) \
 	$(am__append_4) $(am__append_5) $(am__append_6) \
 	$(am__append_7)
 
@@ -892,6 +892,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TestUtil.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeSeedCriteriaTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeTest.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TripletTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UTMetadataDataExtensionMessageTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UTMetadataPostDownloadHandlerTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UTMetadataRejectExtensionMessageTest.Po@am__quote@

+ 77 - 0
test/TripletTest.cc

@@ -0,0 +1,77 @@
+#include "Triplet.h"
+
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace aria2 {
+
+class TripletTest:public CppUnit::TestFixture {
+
+  CPPUNIT_TEST_SUITE(TripletTest);
+  CPPUNIT_TEST(testLess);
+  CPPUNIT_TEST(testTripletGet);
+  CPPUNIT_TEST(testTripletNthType);
+  CPPUNIT_TEST(testTriplet2Pair);
+  CPPUNIT_TEST_SUITE_END();
+public:
+  void setUp() {}
+
+  void tearDown() {}
+
+  void testLess();
+  void testTripletGet();
+  void testTripletNthType();
+  void testTriplet2Pair();
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(TripletTest);
+
+void TripletTest::testLess()
+{
+  Triplet<int, int, int> tri1(0, 1, 1);
+  Triplet<int, int, int> tri2(1, 0, 0);
+  CPPUNIT_ASSERT(!(tri1 < tri1));
+  CPPUNIT_ASSERT(tri1 < tri2);
+  CPPUNIT_ASSERT(!(tri2 < tri1));
+
+  Triplet<int, int, int> tri3(0, 0, 1);
+  Triplet<int, int, int> tri4(0, 1, 0);
+  CPPUNIT_ASSERT(tri3 < tri4);
+  CPPUNIT_ASSERT(!(tri4 < tri3));
+
+  Triplet<int, int, int> tri5(0, 0, 0);
+  Triplet<int, int, int> tri6(0, 0, 1);
+  CPPUNIT_ASSERT(tri5 < tri6);
+  CPPUNIT_ASSERT(!(tri6 < tri5));
+}
+
+void TripletTest::testTripletGet()
+{
+  Triplet<int, double, std::string> x(1, 3.14, "foo");
+  CPPUNIT_ASSERT_EQUAL(1, (TripletGet<1>::get(x)));
+  CPPUNIT_ASSERT_EQUAL((double)3.14, (TripletGet<2>::get(x)));
+  CPPUNIT_ASSERT_EQUAL(std::string("foo"), (TripletGet<3>::get(x)));
+}
+
+void TripletTest::testTripletNthType()
+{
+  TripletNthType<int, double, std::string, 1>::type x = 1;
+  CPPUNIT_ASSERT_EQUAL(1, x);
+  TripletNthType<int, double, std::string, 2>::type y = 3.14;
+  CPPUNIT_ASSERT_EQUAL((double)3.14, y);
+  TripletNthType<int, double, std::string, 3>::type z = "foo";
+  CPPUNIT_ASSERT_EQUAL(std::string("foo"), z);
+}
+
+void TripletTest::testTriplet2Pair()
+{
+  Triplet<int, double, std::string> x(1, 3.14, "foo");
+  std::pair<int, double> p1 = Triplet2Pair<1, 2>()(x);
+  CPPUNIT_ASSERT_EQUAL(1, p1.first);
+  CPPUNIT_ASSERT_EQUAL((double)3.14, p1.second);
+
+  std::pair<double, std::string> p2 = Triplet2Pair<2, 3>()(x);
+  CPPUNIT_ASSERT_EQUAL((double)3.14, p2.first);
+  CPPUNIT_ASSERT_EQUAL(std::string("foo"), p2.second);
+}
+
+} // namespace aria2