InterfaceScanner.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/discovery/AsioTypes.hpp>
  21. #include <ableton/util/Injected.hpp>
  22. #include <chrono>
  23. #include <vector>
  24. namespace ableton
  25. {
  26. namespace discovery
  27. {
  28. // Callback takes a range of IpAddress which is
  29. // guaranteed to be sorted and unique
  30. template <typename Callback, typename IoContext>
  31. class InterfaceScanner
  32. {
  33. public:
  34. using Timer = typename util::Injected<IoContext>::type::Timer;
  35. InterfaceScanner(const std::chrono::seconds period,
  36. util::Injected<Callback> callback,
  37. util::Injected<IoContext> io)
  38. : mPeriod(period)
  39. , mCallback(std::move(callback))
  40. , mIo(std::move(io))
  41. , mTimer(mIo->makeTimer())
  42. {
  43. }
  44. void enable(const bool bEnable)
  45. {
  46. if (bEnable)
  47. {
  48. scan();
  49. }
  50. else
  51. {
  52. mTimer.cancel();
  53. }
  54. }
  55. void scan()
  56. {
  57. using namespace std;
  58. debug(mIo->log()) << "Scanning network interfaces";
  59. // Rescan the hardware for available network interface addresses
  60. vector<IpAddress> addrs = mIo->scanNetworkInterfaces();
  61. // Sort and unique them to guarantee consistent comparison
  62. sort(begin(addrs), end(addrs));
  63. addrs.erase(unique(begin(addrs), end(addrs)), end(addrs));
  64. // Pass them to the callback
  65. (*mCallback)(std::move(addrs));
  66. // setup the next scanning
  67. mTimer.expires_from_now(mPeriod);
  68. using ErrorCode = typename Timer::ErrorCode;
  69. mTimer.async_wait(
  70. [this](const ErrorCode e)
  71. {
  72. if (!e)
  73. {
  74. scan();
  75. }
  76. });
  77. }
  78. private:
  79. const std::chrono::seconds mPeriod;
  80. util::Injected<Callback> mCallback;
  81. util::Injected<IoContext> mIo;
  82. Timer mTimer;
  83. };
  84. // Factory function
  85. template <typename Callback, typename IoContext>
  86. InterfaceScanner<Callback, IoContext> makeInterfaceScanner(
  87. const std::chrono::seconds period,
  88. util::Injected<Callback> callback,
  89. util::Injected<IoContext> io)
  90. {
  91. return {period, std::move(callback), std::move(io)};
  92. }
  93. } // namespace discovery
  94. } // namespace ableton