DelegatingPeerListProcessor.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * aria2 - The high speed download utility
  3. *
  4. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * In addition, as a special exception, the copyright holders give
  21. * permission to link the code of portions of this program with the
  22. * OpenSSL library under certain conditions as described in each
  23. * individual source file, and distribute linked combinations
  24. * including the two.
  25. * You must obey the GNU General Public License in all respects
  26. * for all of the code used other than OpenSSL. If you modify
  27. * file(s) with this exception, you may extend this exception to your
  28. * version of the file(s), but you are not obligated to do so. If you
  29. * do not wish to do so, delete this exception statement from your
  30. * version. If you delete this exception statement from all source
  31. * files in the program, then also delete it here.
  32. */
  33. /* copyright --> */
  34. #include "DelegatingPeerListProcessor.h"
  35. #include <algorithm>
  36. #include "DefaultPeerListProcessor.h"
  37. #include "CompactPeerListProcessor.h"
  38. #include "Peer.h"
  39. #include "bencode.h"
  40. namespace aria2 {
  41. DelegatingPeerListProcessor::DelegatingPeerListProcessor()
  42. {
  43. {
  44. SharedHandle<PeerListProcessor> proc(new DefaultPeerListProcessor);
  45. processors.push_back(proc);
  46. }
  47. {
  48. SharedHandle<PeerListProcessor> proc(new CompactPeerListProcessor);
  49. processors.push_back(proc);
  50. }
  51. }
  52. DelegatingPeerListProcessor::~DelegatingPeerListProcessor() {}
  53. void DelegatingPeerListProcessor::extractPeer
  54. (std::deque<SharedHandle<Peer> >& peers, const bencode::BDE& peerData)
  55. {
  56. for(std::deque<SharedHandle<PeerListProcessor> >::iterator itr =
  57. processors.begin(); itr != processors.end(); ++itr) {
  58. PeerListProcessorHandle processor = *itr;
  59. if(processor->canHandle(peerData)) {
  60. processor->extractPeer(peers, peerData);
  61. break;
  62. }
  63. }
  64. }
  65. bool DelegatingPeerListProcessor::canHandle(const bencode::BDE& peerData) const
  66. {
  67. for(std::deque<SharedHandle<PeerListProcessor> >::const_iterator itr =
  68. processors.begin(); itr != processors.end(); ++itr) {
  69. if((*itr)->canHandle(peerData)) {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. } // namespace aria2