Selaa lähdekoodia

2009-04-23 Tatsuhiro Tsujikawa <[email protected]>

	Rewritten PeerListProcessor
	* src/PeerListProcessor.h
	* src/CompactPeerListProcessor.h: Removed
	* src/CompactPeerListProcessor.cc: Removed
	* src/DelegatingPeerListProcessor.cc: Removed.
	* src/DefaultBtAnnounce.cc
	* src/Makefile.am
	* src/UTPexExtensionMessage.cc
	* src/DefaultPeerListProcessor.h: Removed.
	* src/DefaultPeerListProcessor.cc: Removed.
	* src/DelegatingPeerListProcessor.h: Removed.
	* test/Makefile.am
	* test/PeerListProcessorTest.cc
	* test/DefaultPeerListProcessorTest.cc: Removed.
Tatsuhiro Tsujikawa 16 vuotta sitten
vanhempi
sitoutus
6703097652

+ 17 - 0
ChangeLog

@@ -1,3 +1,20 @@
+2009-04-23  Tatsuhiro Tsujikawa  <[email protected]>
+
+	Rewritten PeerListProcessor
+	* src/PeerListProcessor.h
+	* src/CompactPeerListProcessor.h: Removed
+	* src/CompactPeerListProcessor.cc: Removed
+	* src/DelegatingPeerListProcessor.cc: Removed.
+	* src/DefaultBtAnnounce.cc
+	* src/Makefile.am
+	* src/UTPexExtensionMessage.cc
+	* src/DefaultPeerListProcessor.h: Removed.
+	* src/DefaultPeerListProcessor.cc: Removed.
+	* src/DelegatingPeerListProcessor.h: Removed.
+	* test/Makefile.am
+	* test/PeerListProcessorTest.cc
+	* test/DefaultPeerListProcessorTest.cc: Removed.
+
 2009-04-23  Tatsuhiro Tsujikawa  <[email protected]>
 
 	Rewritten RequestGroupMan::calculateStat()

+ 0 - 69
src/CompactPeerListProcessor.cc

@@ -1,69 +0,0 @@
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 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 --> */
-#include "CompactPeerListProcessor.h"
-#include "a2netcompat.h"
-#include "bencode.h"
-#include "Peer.h"
-
-namespace aria2 {
-
-CompactPeerListProcessor::CompactPeerListProcessor() {}
-
-CompactPeerListProcessor::~CompactPeerListProcessor() {}
-
-bool CompactPeerListProcessor::canHandle(const bencode::BDE& peerData) const
-{
-  return peerData.isString();
-}
-
-void CompactPeerListProcessor::extractPeer
-(std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData)
-{
-  if(!canHandle(peerData)) {
-    return;
-  }
-  size_t length = peerData.s().size();
-  if(length%6 == 0) {
-    for(size_t i = 0; i < length; i += 6) {
-      struct in_addr in;
-      in.s_addr = *(uint32_t*)(peerData.s().c_str()+i);
-      std::string ipaddr = inet_ntoa(in);
-      uint16_t port = ntohs(*(uint16_t*)(peerData.s().c_str()+i+4));
-      PeerHandle peer(new Peer(ipaddr, port));
-      peers.push_back(peer);
-    }
-  }
-}
-
-} // namespace aria2

+ 0 - 55
src/CompactPeerListProcessor.h

@@ -1,55 +0,0 @@
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 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_COMPACT_PEER_LIST_PROCESSOR_H_
-#define _D_COMPACT_PEER_LIST_PROCESSOR_H_
-
-#include "PeerListProcessor.h"
-
-namespace aria2 {
-
-class CompactPeerListProcessor : public PeerListProcessor {
-public:
-  CompactPeerListProcessor();
-
-  virtual ~CompactPeerListProcessor();
-
-  virtual void extractPeer
-  (std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData);
-
-  virtual bool canHandle(const bencode::BDE& peerData) const;
-};
-
-} // namespace aria2
-
-#endif // _D_COMPACT_PEER_LIST_PROCESSOR_H_

+ 5 - 6
src/DefaultBtAnnounce.cc

@@ -35,7 +35,7 @@
 #include "DefaultBtAnnounce.h"
 #include "LogFactory.h"
 #include "Logger.h"
-#include "DelegatingPeerListProcessor.h"
+#include "PeerListProcessor.h"
 #include "Util.h"
 #include "prefs.h"
 #include "DlAbortEx.h"
@@ -250,15 +250,14 @@ DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse,
     logger->debug("Incomplete:%d", incomplete);
   }
   const bencode::BDE& peerData = dict[BtAnnounce::PEERS];
-  if(!peerData.isNone()) {
+  if(peerData.isNone()) {
+    logger->info(MSG_NO_PEER_LIST_RECEIVED);
+  } else {
     if(!btRuntime->isHalt() && btRuntime->lessThanMinPeers()) {
-      DelegatingPeerListProcessor proc;
       std::deque<SharedHandle<Peer> > peers;
-      proc.extractPeer(peers, peerData);
+      PeerListProcessor().extractPeer(peerData, std::back_inserter(peers));
       peerStorage->addPeer(peers);
     }
-  } else {
-    logger->info(MSG_NO_PEER_LIST_RECEIVED);
   }
 }
 

+ 0 - 77
src/DefaultPeerListProcessor.cc

@@ -1,77 +0,0 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 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 --> */
-#include "DefaultPeerListProcessor.h"
-#include "Peer.h"
-#include "bencode.h"
-
-namespace aria2 {
-
-const std::string DefaultPeerListProcessor::IP("ip");
-
-const std::string DefaultPeerListProcessor::PORT("port");
-
-DefaultPeerListProcessor::DefaultPeerListProcessor() {}
-
-DefaultPeerListProcessor::~DefaultPeerListProcessor() {}
-
-bool DefaultPeerListProcessor::canHandle(const bencode::BDE& peerData) const
-{
-  return peerData.isList();
-}
-
-void DefaultPeerListProcessor::extractPeer
-(std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData)
-{
-  if(!canHandle(peerData)) {
-    return;
-  }
-  for(bencode::BDE::List::const_iterator itr = peerData.listBegin();
-      itr != peerData.listEnd(); ++itr) {
-    const bencode::BDE& peerDict = *itr;
-    if(!peerDict.isDict()) {
-      continue;
-    }
-    const bencode::BDE& ip = peerDict[IP];
-    const bencode::BDE& port = peerDict[PORT];
-    if(!ip.isString() || !port.isInteger() ||
-       !(0 < port.i() && port.i() < 65536)) {
-      continue;
-    }
-    PeerHandle peer(new Peer(ip.s(), port.i()));
-    peers.push_back(peer);
-  }
-}
-
-} // namespace aria2

+ 0 - 60
src/DefaultPeerListProcessor.h

@@ -1,60 +0,0 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 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_DEFAULT_PEER_LIST_PROCESSOR_H_
-#define _D_DEFAULT_PEER_LIST_PROCESSOR_H_
-
-#include "PeerListProcessor.h"
-
-namespace aria2 {
-
-class DefaultPeerListProcessor : public PeerListProcessor {
-private:
-  static const std::string IP;
-
-  static const std::string PORT;
-public:
-  DefaultPeerListProcessor();
-
-  virtual ~DefaultPeerListProcessor();
-
-  virtual void extractPeer
-  (std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData);
-
-  virtual bool canHandle(const bencode::BDE& peerData) const;
-};
-
-} // namespace aria2
-
-#endif // _D_DEFAULT_PEER_LIST_PROCESSOR_H_

+ 0 - 83
src/DelegatingPeerListProcessor.cc

@@ -1,83 +0,0 @@
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 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 --> */
-#include "DelegatingPeerListProcessor.h"
-
-#include <algorithm>
-
-#include "DefaultPeerListProcessor.h"
-#include "CompactPeerListProcessor.h"
-#include "Peer.h"
-#include "bencode.h"
-
-namespace aria2 {
-
-DelegatingPeerListProcessor::DelegatingPeerListProcessor()
-{
-  {
-    SharedHandle<PeerListProcessor> proc(new DefaultPeerListProcessor);
-    processors.push_back(proc);
-  }
-  {
-    SharedHandle<PeerListProcessor> proc(new CompactPeerListProcessor);
-    processors.push_back(proc);
-  }
-}
-
-DelegatingPeerListProcessor::~DelegatingPeerListProcessor() {}
-
-void DelegatingPeerListProcessor::extractPeer
-(std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData)
-{
-  for(std::deque<SharedHandle<PeerListProcessor> >::iterator itr =
-	processors.begin(); itr != processors.end(); ++itr) {
-    PeerListProcessorHandle processor = *itr;
-    if(processor->canHandle(peerData)) {
-      processor->extractPeer(peers, peerData);
-      break;
-    }
-  }
-}
-
-bool DelegatingPeerListProcessor::canHandle(const bencode::BDE& peerData) const
-{
-  for(std::deque<SharedHandle<PeerListProcessor> >::const_iterator itr =
-	processors.begin(); itr != processors.end(); ++itr) {
-    if((*itr)->canHandle(peerData)) {
-      return true;
-    }
-  }
-  return false;
-}
-
-} // namespace aria2

+ 0 - 57
src/DelegatingPeerListProcessor.h

@@ -1,57 +0,0 @@
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 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_DELEGATING_PEER_LIST_PROCESSOR_H_
-#define _D_DELEGATING_PEER_LIST_PROCESSOR_H_
-
-#include "PeerListProcessor.h"
-
-namespace aria2 {
-
-class DelegatingPeerListProcessor : public PeerListProcessor {
-private:
-  std::deque<SharedHandle<PeerListProcessor> > processors;
-public:
-  DelegatingPeerListProcessor();
-
-  virtual ~DelegatingPeerListProcessor();
-
-  virtual void extractPeer
-  (std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData);
-
-  virtual bool canHandle(const bencode::BDE& peerData) const;
-};
-
-} // namespace aria2
-
-#endif // _D_DELEGATING_PEER_LIST_PROCESSOR_H_

+ 0 - 3
src/Makefile.am

@@ -268,9 +268,6 @@ SRCS += PeerMessageUtil.cc PeerMessageUtil.h\
 	UnionSeedCriteria.h\
 	SeedCheckCommand.cc SeedCheckCommand.h\
 	PeerListProcessor.h\
-	DefaultPeerListProcessor.cc DefaultPeerListProcessor.h\
-	CompactPeerListProcessor.cc CompactPeerListProcessor.h\
-	DelegatingPeerListProcessor.cc DelegatingPeerListProcessor.h\
 	AnnounceTier.h\
 	AnnounceList.h AnnounceList.cc\
 	BtContext.cc BtContext.h\

+ 1 - 13
src/Makefile.in

@@ -68,9 +68,6 @@ bin_PROGRAMS = aria2c$(EXEEXT)
 @ENABLE_BITTORRENT_TRUE@	UnionSeedCriteria.h\
 @ENABLE_BITTORRENT_TRUE@	SeedCheckCommand.cc SeedCheckCommand.h\
 @ENABLE_BITTORRENT_TRUE@	PeerListProcessor.h\
-@ENABLE_BITTORRENT_TRUE@	DefaultPeerListProcessor.cc DefaultPeerListProcessor.h\
-@ENABLE_BITTORRENT_TRUE@	CompactPeerListProcessor.cc CompactPeerListProcessor.h\
-@ENABLE_BITTORRENT_TRUE@	DelegatingPeerListProcessor.cc DelegatingPeerListProcessor.h\
 @ENABLE_BITTORRENT_TRUE@	AnnounceTier.h\
 @ENABLE_BITTORRENT_TRUE@	AnnounceList.h AnnounceList.cc\
 @ENABLE_BITTORRENT_TRUE@	BtContext.cc BtContext.h\
@@ -438,10 +435,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
 	PeerChokeCommand.cc PeerChokeCommand.h SeedCriteria.h \
 	TimeSeedCriteria.h ShareRatioSeedCriteria.h \
 	UnionSeedCriteria.h SeedCheckCommand.cc SeedCheckCommand.h \
-	PeerListProcessor.h DefaultPeerListProcessor.cc \
-	DefaultPeerListProcessor.h CompactPeerListProcessor.cc \
-	CompactPeerListProcessor.h DelegatingPeerListProcessor.cc \
-	DelegatingPeerListProcessor.h AnnounceTier.h AnnounceList.h \
+	PeerListProcessor.h AnnounceTier.h AnnounceList.h \
 	AnnounceList.cc BtContext.cc BtContext.h DefaultBtContext.cc \
 	DefaultBtContext.h PeerStorage.h DefaultPeerStorage.cc \
 	DefaultPeerStorage.h BtAnnounce.cc BtAnnounce.h \
@@ -598,9 +592,6 @@ am__objects_3 =
 @ENABLE_BITTORRENT_TRUE@	TrackerWatcherCommand.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	PeerChokeCommand.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	SeedCheckCommand.$(OBJEXT) \
-@ENABLE_BITTORRENT_TRUE@	DefaultPeerListProcessor.$(OBJEXT) \
-@ENABLE_BITTORRENT_TRUE@	CompactPeerListProcessor.$(OBJEXT) \
-@ENABLE_BITTORRENT_TRUE@	DelegatingPeerListProcessor.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	AnnounceList.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	BtContext.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	DefaultBtContext.$(OBJEXT) \
@@ -1304,7 +1295,6 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ChecksumCheckIntegrityEntry.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ChunkedDecoder.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Command.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CompactPeerListProcessor.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConsoleStatCalc.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ContentTypeRequestGroupCriteria.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Cookie.Po@am__quote@
@@ -1371,11 +1361,9 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultDiskWriter.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultDiskWriterFactory.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultExtensionMessageFactory.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultPeerListProcessor.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultPeerStorage.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultPieceStorage.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultSegmentManFactory.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DelegatingPeerListProcessor.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DirectDiskAdaptor.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DiskAdaptor.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DownloadCommand.Po@am__quote@

+ 48 - 15
src/PeerListProcessor.h

@@ -36,30 +36,63 @@
 #define _D_PEER_LIST_PROCESSOR_H_
 
 #include "common.h"
-
-#include <deque>
-
-#include "SharedHandle.h"
+#include "a2netcompat.h"
+#include "bencode.h"
+#include "Peer.h"
 
 namespace aria2 {
 
-class Peer;
-namespace bencode {
-class BDE;
-}
-
 class PeerListProcessor {
 public:
-  virtual ~PeerListProcessor() {}
+  template<typename OutputIterator>
+  void extractPeer(const bencode::BDE& peerData, OutputIterator dest)
+  {
+    if(peerData.isList()) {
+      extractPeerFromList(peerData, dest);
+    } else if(peerData.isString()) {
+      extractPeerFromCompact(peerData, dest);
+    }
+  }
 
-  virtual void extractPeer
-  (std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData) = 0;
+  template<typename OutputIterator>
+  void extractPeerFromList(const bencode::BDE& peerData, OutputIterator dest)
+  {
+    for(bencode::BDE::List::const_iterator itr = peerData.listBegin();
+	itr != peerData.listEnd(); ++itr) {
+      const bencode::BDE& peerDict = *itr;
+      if(!peerDict.isDict()) {
+	continue;
+      }
+      static const std::string IP = "ip";
+      static const std::string PORT("port");
+      const bencode::BDE& ip = peerDict[IP];
+      const bencode::BDE& port = peerDict[PORT];
+      if(!ip.isString() || !port.isInteger() ||
+	 !(0 < port.i() && port.i() < 65536)) {
+	continue;
+      }
+      *dest = SharedHandle<Peer>(new Peer(ip.s(), port.i()));
+      ++dest;
+    }
+  }
 
-  virtual bool canHandle(const bencode::BDE& peerData) const = 0;
+  template<typename OutputIterator>
+  void extractPeerFromCompact(const bencode::BDE& peerData, OutputIterator dest)
+  {
+    size_t length = peerData.s().size();
+    if(length%6 == 0) {
+      for(size_t i = 0; i < length; i += 6) {
+	struct in_addr in;
+	in.s_addr = *(uint32_t*)(peerData.s().c_str()+i);
+	std::string ipaddr = inet_ntoa(in);
+	uint16_t port = ntohs(*(uint16_t*)(peerData.s().c_str()+i+4));
+	*dest = SharedHandle<Peer>(new Peer(ipaddr, port));
+	++dest;
+      }
+    }
+  }
 };
 
-typedef SharedHandle<PeerListProcessor> PeerListProcessorHandle;
-
 } // namespace aria2
 
 #endif // _D_PEER_LIST_PROCESSOR_H_

+ 5 - 4
src/UTPexExtensionMessage.cc

@@ -37,7 +37,7 @@
 #include "Util.h"
 #include "PeerMessageUtil.h"
 #include "PeerStorage.h"
-#include "CompactPeerListProcessor.h"
+#include "PeerListProcessor.h"
 #include "DlAbortEx.h"
 #include "message.h"
 #include "StringFormat.h"
@@ -174,14 +174,15 @@ UTPexExtensionMessage::create(const unsigned char* data, size_t len)
 
   const bencode::BDE dict = bencode::decode(data+1, len-1);
   if(dict.isDict()) {
-    CompactPeerListProcessor proc;
+    PeerListProcessor proc;
     const bencode::BDE& added = dict["added"];
     if(added.isString()) {
-      proc.extractPeer(msg->_freshPeers, added);
+      proc.extractPeerFromCompact(added, std::back_inserter(msg->_freshPeers));
     }
     const bencode::BDE& dropped = dict["dropped"];
     if(dropped.isString()) {
-      proc.extractPeer(msg->_droppedPeers, dropped);
+      proc.extractPeerFromCompact(dropped,
+				  std::back_inserter(msg->_droppedPeers));
     }
   }
   return msg;

+ 1 - 1
test/Makefile.am

@@ -114,7 +114,7 @@ aria2c_SOURCES += BtAllowedFastMessageTest.cc\
 	MockBtMessage.h\
 	MockBtMessageDispatcher.h\
 	MockBtMessageFactory.h\
-	DefaultPeerListProcessorTest.cc\
+	PeerListProcessorTest.cc\
 	AnnounceListTest.cc\
 	DefaultPeerStorageTest.cc\
 	MockPeerStorage.h\

+ 4 - 4
test/Makefile.in

@@ -66,7 +66,7 @@ check_PROGRAMS = $(am__EXEEXT_1)
 @ENABLE_BITTORRENT_TRUE@	MockBtMessage.h\
 @ENABLE_BITTORRENT_TRUE@	MockBtMessageDispatcher.h\
 @ENABLE_BITTORRENT_TRUE@	MockBtMessageFactory.h\
-@ENABLE_BITTORRENT_TRUE@	DefaultPeerListProcessorTest.cc\
+@ENABLE_BITTORRENT_TRUE@	PeerListProcessorTest.cc\
 @ENABLE_BITTORRENT_TRUE@	AnnounceListTest.cc\
 @ENABLE_BITTORRENT_TRUE@	DefaultPeerStorageTest.cc\
 @ENABLE_BITTORRENT_TRUE@	MockPeerStorage.h\
@@ -211,7 +211,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
 	DefaultBtContextTest.cc DefaultBtMessageDispatcherTest.cc \
 	DefaultBtRequestFactoryTest.cc MockBtMessage.h \
 	MockBtMessageDispatcher.h MockBtMessageFactory.h \
-	DefaultPeerListProcessorTest.cc AnnounceListTest.cc \
+	PeerListProcessorTest.cc AnnounceListTest.cc \
 	DefaultPeerStorageTest.cc MockPeerStorage.h \
 	ByteArrayDiskWriterTest.cc PeerTest.cc \
 	PeerSessionResourceTest.cc PeerMessageUtilTest.cc \
@@ -275,7 +275,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
 @ENABLE_BITTORRENT_TRUE@	DefaultBtContextTest.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	DefaultBtMessageDispatcherTest.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	DefaultBtRequestFactoryTest.$(OBJEXT) \
-@ENABLE_BITTORRENT_TRUE@	DefaultPeerListProcessorTest.$(OBJEXT) \
+@ENABLE_BITTORRENT_TRUE@	PeerListProcessorTest.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	AnnounceListTest.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	DefaultPeerStorageTest.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	ByteArrayDiskWriterTest.$(OBJEXT) \
@@ -751,7 +751,6 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtRequestFactoryTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultDiskWriterTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultExtensionMessageFactoryTest.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultPeerListProcessorTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultPeerStorageTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultPieceStorageTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DirectDiskAdaptorTest.Po@am__quote@
@@ -794,6 +793,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/OptionTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PStringBuildVisitorTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ParameterizedStringParserTest.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerListProcessorTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerMessageUtilTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerSessionResourceTest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerTest.Po@am__quote@

+ 18 - 16
test/DefaultPeerListProcessorTest.cc → test/PeerListProcessorTest.cc

@@ -1,18 +1,22 @@
-#include "DefaultPeerListProcessor.h"
+#include "PeerListProcessor.h"
+
+#include <cstdlib>
 
 #include <cppunit/extensions/HelperMacros.h>
 
 #include "Exception.h"
 #include "Peer.h"
 #include "bencode.h"
+#include "TimeA2.h"
+#include "PeerMessageUtil.h"
 
 namespace aria2 {
 
-class DefaultPeerListProcessorTest:public CppUnit::TestFixture {
+class PeerListProcessorTest:public CppUnit::TestFixture {
 
-  CPPUNIT_TEST_SUITE(DefaultPeerListProcessorTest);
-  CPPUNIT_TEST(testExtractPeer);
-  CPPUNIT_TEST(testExtract2Peers);
+  CPPUNIT_TEST_SUITE(PeerListProcessorTest);
+  CPPUNIT_TEST(testExtractPeerFromList);
+  CPPUNIT_TEST(testExtract2PeersFromList);
   CPPUNIT_TEST_SUITE_END();
 private:
 
@@ -20,37 +24,35 @@ public:
   void setUp() {
   }
 
-  void testExtractPeer();
-  void testExtract2Peers();
+  void testExtractPeerFromList();
+  void testExtract2PeersFromList();
 };
 
 
-CPPUNIT_TEST_SUITE_REGISTRATION( DefaultPeerListProcessorTest );
+CPPUNIT_TEST_SUITE_REGISTRATION( PeerListProcessorTest );
 
-void DefaultPeerListProcessorTest::testExtractPeer() {
-  DefaultPeerListProcessor proc;
+void PeerListProcessorTest::testExtractPeerFromList() {
+  PeerListProcessor proc;
   std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eeee";
 
   const bencode::BDE dict = bencode::decode(peersString);
   
-  CPPUNIT_ASSERT(proc.canHandle(dict["peers"]));
-
   std::deque<SharedHandle<Peer> > peers;
-  proc.extractPeer(peers, dict["peers"]);
+  proc.extractPeerFromList(dict["peers"], std::back_inserter(peers));
   CPPUNIT_ASSERT_EQUAL((size_t)1, peers.size());
   SharedHandle<Peer> peer = *peers.begin();
   CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->ipaddr);
   CPPUNIT_ASSERT_EQUAL((uint16_t)2006, peer->port);
 }
 
-void DefaultPeerListProcessorTest::testExtract2Peers() {
-  DefaultPeerListProcessor proc;
+void PeerListProcessorTest::testExtract2PeersFromList() {
+  PeerListProcessor proc;
   std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-000000000000004:porti2007eeee";
 
   const bencode::BDE dict = bencode::decode(peersString);
 
   std::deque<SharedHandle<Peer> > peers;
-  proc.extractPeer(peers, dict["peers"]);
+  proc.extractPeerFromList(dict["peers"], std::back_inserter(peers));
   CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
   SharedHandle<Peer> peer = *peers.begin();
   CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->ipaddr);