PeerGateways.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/discovery/InterfaceScanner.hpp>
  22. #include <map>
  23. namespace ableton
  24. {
  25. namespace discovery
  26. {
  27. // GatewayFactory must have an operator()(NodeState, IoRef, IpAddress)
  28. // that constructs a new PeerGateway on a given interface address.
  29. template <typename NodeState, typename GatewayFactory, typename IoContext>
  30. class PeerGateways
  31. {
  32. public:
  33. using IoType = typename util::Injected<IoContext>::type;
  34. using FactoryType = typename util::Injected<GatewayFactory>::type;
  35. using Gateway = std::invoke_result_t<FactoryType,
  36. NodeState,
  37. util::Injected<IoType&>,
  38. const discovery::IpAddress&>;
  39. using GatewayMap = std::map<IpAddress, Gateway>;
  40. PeerGateways(const std::chrono::seconds rescanPeriod,
  41. NodeState state,
  42. util::Injected<GatewayFactory> factory,
  43. util::Injected<IoContext> io)
  44. : mIo(std::move(io))
  45. {
  46. mpScannerCallback =
  47. std::make_shared<Callback>(std::move(state), std::move(factory), *mIo);
  48. mpScanner = std::make_shared<Scanner>(
  49. rescanPeriod, util::injectShared(mpScannerCallback), util::injectRef(*mIo));
  50. }
  51. ~PeerGateways()
  52. {
  53. mpScanner.reset();
  54. mpScannerCallback.reset();
  55. }
  56. PeerGateways(const PeerGateways&) = delete;
  57. PeerGateways& operator=(const PeerGateways&) = delete;
  58. PeerGateways(PeerGateways&&) = delete;
  59. PeerGateways& operator=(PeerGateways&&) = delete;
  60. void enable(const bool bEnable)
  61. {
  62. mpScannerCallback->mGateways.clear();
  63. mpScannerCallback->mFactory->gatewaysChanged();
  64. mpScanner->enable(bEnable);
  65. }
  66. template <typename Handler>
  67. void withGateways(Handler handler)
  68. {
  69. handler(mpScannerCallback->mGateways.begin(), mpScannerCallback->mGateways.end());
  70. }
  71. void updateNodeState(const NodeState& state)
  72. {
  73. mpScannerCallback->mState = state;
  74. for (const auto& entry : mpScannerCallback->mGateways)
  75. {
  76. entry.second->updateNodeState(state);
  77. }
  78. }
  79. // If a gateway has become non-responsive or is throwing exceptions,
  80. // this method can be invoked to either fix it or discard it.
  81. void repairGateway(const IpAddress& gatewayAddr)
  82. {
  83. if (mpScannerCallback->mGateways.erase(gatewayAddr))
  84. {
  85. mpScannerCallback->mFactory->gatewaysChanged();
  86. // If we erased a gateway, rescan again immediately so that
  87. // we will re-initialize it if it's still present
  88. mpScanner->scan();
  89. }
  90. }
  91. private:
  92. struct Callback
  93. {
  94. Callback(NodeState state, util::Injected<GatewayFactory> factory, IoType& io)
  95. : mState(std::move(state))
  96. , mFactory(std::move(factory))
  97. , mIo(io)
  98. {
  99. }
  100. template <typename AddrRange>
  101. void operator()(const AddrRange& range)
  102. {
  103. using namespace std;
  104. // Get the set of current addresses.
  105. vector<IpAddress> curAddrs;
  106. curAddrs.reserve(mGateways.size());
  107. transform(std::begin(mGateways),
  108. std::end(mGateways),
  109. back_inserter(curAddrs),
  110. [](const typename GatewayMap::value_type& vt) { return vt.first; });
  111. // Now use set_difference to determine the set of addresses that
  112. // are new and the set of cur addresses that are no longer there
  113. vector<IpAddress> newAddrs;
  114. set_difference(std::begin(range),
  115. std::end(range),
  116. std::begin(curAddrs),
  117. std::end(curAddrs),
  118. back_inserter(newAddrs));
  119. vector<IpAddress> staleAddrs;
  120. set_difference(std::begin(curAddrs),
  121. std::end(curAddrs),
  122. std::begin(range),
  123. std::end(range),
  124. back_inserter(staleAddrs));
  125. // Remove the stale addresses
  126. for (const auto& addr : staleAddrs)
  127. {
  128. mGateways.erase(addr);
  129. }
  130. // Add the new addresses
  131. for (const auto& addr : newAddrs)
  132. {
  133. try
  134. {
  135. info(mIo.log()) << "initializing peer gateway on interface " << addr;
  136. mGateways.emplace(addr, (*mFactory)(mState, util::injectRef(mIo), addr));
  137. }
  138. catch (const runtime_error& e)
  139. {
  140. warning(mIo.log()) << "failed to init gateway on interface " << addr
  141. << " reason: " << e.what();
  142. }
  143. }
  144. if (!staleAddrs.empty() || !newAddrs.empty())
  145. {
  146. mFactory->gatewaysChanged();
  147. }
  148. }
  149. NodeState mState;
  150. util::Injected<GatewayFactory> mFactory;
  151. IoType& mIo;
  152. GatewayMap mGateways;
  153. };
  154. using Scanner = InterfaceScanner<std::shared_ptr<Callback>, IoType&>;
  155. std::shared_ptr<Callback> mpScannerCallback;
  156. std::shared_ptr<Scanner> mpScanner;
  157. util::Injected<IoContext> mIo;
  158. };
  159. // Factory function
  160. template <typename NodeState, typename GatewayFactory, typename IoContext>
  161. std::unique_ptr<PeerGateways<NodeState, GatewayFactory, IoContext>> makePeerGateways(
  162. const std::chrono::seconds rescanPeriod,
  163. NodeState state,
  164. util::Injected<GatewayFactory> factory,
  165. util::Injected<IoContext> io)
  166. {
  167. using Gateways = PeerGateways<NodeState, GatewayFactory, IoContext>;
  168. return std::make_unique<Gateways>(
  169. rescanPeriod, std::move(state), std::move(factory), std::move(io));
  170. }
  171. } // namespace discovery
  172. } // namespace ableton