/usr/include/ableton/discovery/PeerGateways.hpp is in ableton-link-dev 1.0.0+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you would like to incorporate Link into a proprietary software application,
* please contact <link-devs@ableton.com>.
*/
#pragma once
#include <ableton/discovery/InterfaceScanner.hpp>
#include <ableton/platforms/asio/AsioWrapper.hpp>
#include <map>
namespace ableton
{
namespace discovery
{
// GatewayFactory must have an operator()(NodeState, IoRef, asio::ip::address)
// that constructs a new PeerGateway on a given interface address.
template <typename NodeState, typename GatewayFactory, typename IoContext>
class PeerGateways
{
public:
using IoType = typename util::Injected<IoContext>::type;
using Gateway = typename std::result_of<GatewayFactory(
NodeState, util::Injected<IoType&>, asio::ip::address)>::type;
using GatewayMap = std::map<asio::ip::address, Gateway>;
PeerGateways(const std::chrono::seconds rescanPeriod,
NodeState state,
GatewayFactory factory,
util::Injected<IoContext> io)
: mIo(std::move(io))
{
mpScannerCallback =
std::make_shared<Callback>(std::move(state), std::move(factory), *mIo);
mpScanner = std::make_shared<Scanner>(
rescanPeriod, util::injectShared(mpScannerCallback), util::injectRef(*mIo));
}
~PeerGateways()
{
// Release the callback in the io thread so that gateway cleanup
// doesn't happen in the client thread
mIo->async(Deleter{*this});
}
PeerGateways(const PeerGateways&) = delete;
PeerGateways& operator=(const PeerGateways&) = delete;
PeerGateways(PeerGateways&&) = delete;
PeerGateways& operator=(PeerGateways&&) = delete;
void enable(const bool bEnable)
{
auto pCallback = mpScannerCallback;
auto pScanner = mpScanner;
if (pCallback && pScanner)
{
mIo->async([pCallback, pScanner, bEnable] {
pCallback->mGateways.clear();
pScanner->enable(bEnable);
});
}
}
template <typename Handler>
void withGatewaysAsync(Handler handler)
{
auto pCallback = mpScannerCallback;
if (pCallback)
{
mIo->async([pCallback, handler] {
handler(pCallback->mGateways.begin(), pCallback->mGateways.end());
});
}
}
void updateNodeState(const NodeState& state)
{
auto pCallback = mpScannerCallback;
if (pCallback)
{
mIo->async([pCallback, state] {
pCallback->mState = state;
for (const auto& entry : pCallback->mGateways)
{
entry.second->updateNodeState(state);
}
});
}
}
// If a gateway has become non-responsive or is throwing exceptions,
// this method can be invoked to either fix it or discard it.
void repairGateway(const asio::ip::address& gatewayAddr)
{
auto pCallback = mpScannerCallback;
auto pScanner = mpScanner;
if (pCallback && pScanner)
{
mIo->async([pCallback, pScanner, gatewayAddr] {
if (pCallback->mGateways.erase(gatewayAddr))
{
// If we erased a gateway, rescan again immediately so that
// we will re-initialize it if it's still present
pScanner->scan();
}
});
}
}
private:
struct Callback
{
Callback(NodeState state, GatewayFactory factory, IoType& io)
: mState(std::move(state))
, mFactory(std::move(factory))
, mIo(io)
{
}
template <typename AddrRange>
void operator()(const AddrRange& range)
{
using namespace std;
// Get the set of current addresses.
vector<asio::ip::address> curAddrs;
curAddrs.reserve(mGateways.size());
transform(std::begin(mGateways), std::end(mGateways), back_inserter(curAddrs),
[](const typename GatewayMap::value_type& vt) { return vt.first; });
// Now use set_difference to determine the set of addresses that
// are new and the set of cur addresses that are no longer there
vector<asio::ip::address> newAddrs;
set_difference(std::begin(range), std::end(range), std::begin(curAddrs),
std::end(curAddrs), back_inserter(newAddrs));
vector<asio::ip::address> staleAddrs;
set_difference(std::begin(curAddrs), std::end(curAddrs), std::begin(range),
std::end(range), back_inserter(staleAddrs));
// Remove the stale addresses
for (const auto& addr : staleAddrs)
{
mGateways.erase(addr);
}
// Add the new addresses
for (const auto& addr : newAddrs)
{
try
{
// Only handle v4 for now
if (addr.is_v4())
{
info(mIo.log()) << "initializing peer gateway on interface " << addr;
mGateways.emplace(addr, mFactory(mState, util::injectRef(mIo), addr.to_v4()));
}
}
catch (const runtime_error& e)
{
warning(mIo.log()) << "failed to init gateway on interface " << addr
<< " reason: " << e.what();
}
}
}
NodeState mState;
GatewayFactory mFactory;
IoType& mIo;
GatewayMap mGateways;
};
using Scanner = InterfaceScanner<std::shared_ptr<Callback>, IoType&>;
struct Deleter
{
Deleter(PeerGateways& gateways)
: mpScannerCallback(std::move(gateways.mpScannerCallback))
, mpScanner(std::move(gateways.mpScanner))
{
}
void operator()()
{
mpScanner.reset();
mpScannerCallback.reset();
}
std::shared_ptr<Callback> mpScannerCallback;
std::shared_ptr<Scanner> mpScanner;
};
std::shared_ptr<Callback> mpScannerCallback;
std::shared_ptr<Scanner> mpScanner;
util::Injected<IoContext> mIo;
};
// Factory function
template <typename NodeState, typename GatewayFactory, typename IoContext>
std::unique_ptr<PeerGateways<NodeState, GatewayFactory, IoContext>> makePeerGateways(
const std::chrono::seconds rescanPeriod,
NodeState state,
GatewayFactory factory,
util::Injected<IoContext> io)
{
using namespace std;
using Gateways = PeerGateways<NodeState, GatewayFactory, IoContext>;
return unique_ptr<Gateways>{
new Gateways{rescanPeriod, move(state), move(factory), move(io)}};
}
} // namespace discovery
} // namespace ableton
|