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