LpdMessageDispatcher.h 3.0 KB

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