Service.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/PeerGateways.hpp>
  21. namespace ableton
  22. {
  23. namespace discovery
  24. {
  25. template <typename NodeState, typename GatewayFactory, typename IoContext>
  26. class Service
  27. {
  28. public:
  29. using ServicePeerGateways = PeerGateways<NodeState, GatewayFactory, IoContext>;
  30. Service(NodeState state,
  31. util::Injected<GatewayFactory> factory,
  32. util::Injected<IoContext> io)
  33. : mEnabled(false)
  34. , mGateways(
  35. std::chrono::seconds(5), std::move(state), std::move(factory), std::move(io))
  36. {
  37. }
  38. void enable(const bool bEnable)
  39. {
  40. mEnabled = bEnable;
  41. mGateways.enable(bEnable);
  42. }
  43. bool isEnabled() const { return mEnabled; }
  44. // Asynchronously operate on the current set of peer gateways. The
  45. // handler will be invoked in the service's io context.
  46. template <typename Handler>
  47. void withGateways(Handler handler)
  48. {
  49. mGateways.withGateways(std::move(handler));
  50. }
  51. void updateNodeState(const NodeState& state) { mGateways.updateNodeState(state); }
  52. // Repair the gateway with the given address if possible. Its
  53. // sockets may have been closed, for example, and the gateway needs
  54. // to be regenerated.
  55. void repairGateway(const IpAddress& gatewayAddr)
  56. {
  57. mGateways.repairGateway(gatewayAddr);
  58. }
  59. private:
  60. bool mEnabled;
  61. ServicePeerGateways mGateways;
  62. };
  63. } // namespace discovery
  64. } // namespace ableton