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/PeerGateway.hpp>
23 #include <ableton/link/MeasurementService.hpp>
24 #include <ableton/link/PeerState.hpp>
25 
26 namespace ableton
27 {
28 namespace link
29 {
30 
31 template <typename PeerObserver, typename Clock, typename IoContext>
32 class Gateway
33 {
34 public:
Gateway(util::Injected<IoContext> io,asio::ip::address_v4 addr,util::Injected<PeerObserver> observer,NodeState nodeState,GhostXForm ghostXForm,Clock clock)35   Gateway(util::Injected<IoContext> io,
36     asio::ip::address_v4 addr,
37     util::Injected<PeerObserver> observer,
38     NodeState nodeState,
39     GhostXForm ghostXForm,
40     Clock clock)
41     // TODO: Measurement should have an IoContext injected
42     : mIo(std::move(io))
43     , mMeasurement(addr,
44         nodeState.sessionId,
45         std::move(ghostXForm),
46         std::move(clock),
47         util::injectVal(channel(mIo->log(), "gateway@" + addr.to_string())))
48     , mPeerGateway(discovery::makeIpV4Gateway(util::injectRef(*mIo),
49         std::move(addr),
50         std::move(observer),
51         PeerState{std::move(nodeState), mMeasurement.endpoint()}))
52   {
53   }
54 
55   Gateway(const Gateway& rhs) = delete;
56   Gateway& operator=(const Gateway& rhs) = delete;
57 
Gateway(Gateway && rhs)58   Gateway(Gateway&& rhs)
59     : mIo(std::move(rhs.mIo))
60     , mMeasurement(std::move(rhs.mMeasurement))
61     , mPeerGateway(std::move(rhs.mPeerGateway))
62   {
63   }
64 
operator =(Gateway && rhs)65   Gateway& operator=(Gateway&& rhs)
66   {
67     mIo = std::move(rhs.mIo);
68     mMeasurement = std::move(rhs.mMeasurement);
69     mPeerGateway = std::move(rhs.mPeerGateway);
70     return *this;
71   }
72 
updateNodeState(std::pair<NodeState,GhostXForm> state)73   void updateNodeState(std::pair<NodeState, GhostXForm> state)
74   {
75     mMeasurement.updateNodeState(state.first.sessionId, state.second);
76     mPeerGateway.updateState(PeerState{std::move(state.first), mMeasurement.endpoint()});
77   }
78 
79   template <typename Handler>
measurePeer(const PeerState & peer,Handler handler)80   void measurePeer(const PeerState& peer, Handler handler)
81   {
82     mMeasurement.measurePeer(peer, std::move(handler));
83   }
84 
85 private:
86   util::Injected<IoContext> mIo;
87   MeasurementService<Clock, typename util::Injected<IoContext>::type::Log> mMeasurement;
88   discovery::
89     IpV4Gateway<PeerObserver, PeerState, typename util::Injected<IoContext>::type&>
90       mPeerGateway;
91 };
92 
93 } // namespace link
94 } // namespace ableton
95