LpdMessageDispatcher.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef _D_LPD_MESSAGE_DISPATCHER_H_
  34. #define _D_LPD_MESSAGE_DISPATCHER_H_
  35. #include "common.h"
  36. #include <string>
  37. #include "SharedHandle.h"
  38. #include "TimerA2.h"
  39. namespace aria2 {
  40. class SocketCore;
  41. class Logger;
  42. class LpdMessageDispatcher {
  43. private:
  44. SharedHandle<SocketCore> socket_;
  45. std::string infoHash_;
  46. uint16_t port_;
  47. std::string multicastAddress_;
  48. uint16_t multicastPort_;
  49. Timer timer_;
  50. time_t interval_;
  51. std::string request_;
  52. Logger* logger_;
  53. public:
  54. LpdMessageDispatcher
  55. (const std::string& infoHash, uint16_t port,
  56. const std::string& multicastAddr, uint16_t multicastPort,
  57. time_t interval = 5*60);
  58. // No throw
  59. bool init(const std::string& localAddr, unsigned char ttl,unsigned char loop);
  60. // Returns true if timer_ reached announce interval, which is by
  61. // default 5mins.
  62. bool isAnnounceReady() const;
  63. // Sends LPD message. If message is sent returns true. Otherwise
  64. // returns false.
  65. bool sendMessage();
  66. // Reset timer_ to the current time.
  67. void resetAnnounceTimer();
  68. const std::string& getInfoHash() const
  69. {
  70. return infoHash_;
  71. }
  72. uint16_t getPort() const
  73. {
  74. return port_;
  75. }
  76. };
  77. namespace bittorrent {
  78. std::string createLpdRequest
  79. (const std::string& multicastAddress, uint16_t multicastPort,
  80. const std::string& infoHash, uint16_t port);
  81. } // namespace bittorrent
  82. } // namespace aria2
  83. #endif // _D_LPD_MESSAGE_DISPATCHER_H_