LpdMessageDispatcher.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * aria2 - The high speed download utility
  3. *
  4. * Copyright (C) 2010 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. #include "LpdMessageDispatcher.h"
  34. #include "SocketCore.h"
  35. #include "A2STR.h"
  36. #include "util.h"
  37. #include "Logger.h"
  38. #include "LogFactory.h"
  39. #include "BtConstants.h"
  40. #include "RecoverableException.h"
  41. #include "wallclock.h"
  42. namespace aria2 {
  43. LpdMessageDispatcher::LpdMessageDispatcher
  44. (const std::string& infoHash, uint16_t port,
  45. const std::string& multicastAddress, uint16_t multicastPort,
  46. time_t interval):
  47. _infoHash(infoHash),
  48. _port(port),
  49. _multicastAddress(multicastAddress),
  50. _multicastPort(multicastPort),
  51. _timer(0),
  52. _interval(interval),
  53. _request(bittorrent::createLpdRequest(_multicastAddress, _multicastPort,
  54. _infoHash, _port)),
  55. _logger(LogFactory::getInstance()) {}
  56. bool LpdMessageDispatcher::init(const std::string& localAddr,
  57. unsigned char ttl, unsigned char loop)
  58. {
  59. try {
  60. _socket.reset(new SocketCore(SOCK_DGRAM));
  61. _socket->create(AF_INET);
  62. if(_logger->debug()) {
  63. _logger->debug("Setting multicast outgoing interface=%s",
  64. localAddr.c_str());
  65. }
  66. _socket->setMulticastInterface(localAddr);
  67. if(_logger->debug()) {
  68. _logger->debug("Setting multicast ttl=%u",static_cast<unsigned int>(ttl));
  69. }
  70. _socket->setMulticastTtl(ttl);
  71. if(_logger->debug()) {
  72. _logger->debug("Setting multicast loop=%u",
  73. static_cast<unsigned int>(loop));
  74. }
  75. _socket->setMulticastLoop(loop);
  76. return true;
  77. } catch(RecoverableException& e) {
  78. _logger->error("Failed to initialize LpdMessageDispatcher.", e);
  79. }
  80. return false;
  81. }
  82. bool LpdMessageDispatcher::sendMessage()
  83. {
  84. return
  85. _socket->writeData(_request.c_str(), _request.size(),
  86. _multicastAddress, _multicastPort)
  87. == (ssize_t)_request.size();
  88. }
  89. bool LpdMessageDispatcher::isAnnounceReady() const
  90. {
  91. return _timer.difference(global::wallclock) >= _interval;
  92. }
  93. void LpdMessageDispatcher::resetAnnounceTimer()
  94. {
  95. _timer = global::wallclock;
  96. }
  97. namespace bittorrent {
  98. std::string createLpdRequest
  99. (const std::string& multicastAddress, uint16_t multicastPort,
  100. const std::string& infoHash, uint16_t port)
  101. {
  102. std::string req = "BT-SEARCH * HTTP/1.1\r\n";
  103. strappend(req, "Host: ", multicastAddress, A2STR::COLON_C,
  104. util::uitos(multicastPort), A2STR::CRLF);
  105. strappend(req, "Port: ", util::uitos(port), A2STR::CRLF);
  106. strappend(req, "Infohash: ", util::toHex(infoHash), A2STR::CRLF);
  107. req += "\r\n\r\n";
  108. return req;
  109. }
  110. } // namespace bittorrent
  111. } // namespac aria2