-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkMapper.h
More file actions
185 lines (154 loc) · 4.89 KB
/
NetworkMapper.h
File metadata and controls
185 lines (154 loc) · 4.89 KB
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
// This file is part of the Open Audio Live System project, a live audio environment
// Copyright (c) 2026 - Mathis DELGADO
//
// This project is distributed under the Creative Commons CC-BY-NC-SA licence. https://creativecommons.org/licenses/by-nc-sa/4.0
#ifndef OPENAUDIONETWORK_NETWORKMAPPER_H
#define OPENAUDIONETWORK_NETWORKMAPPER_H
#ifndef NO_THREADS
#include <thread>
#include <mutex>
#endif // NO_THREADS
#include <memory>
#include <cstring>
#include <unordered_map>
#include <algorithm>
#include <optional>
#include <functional>
#include "netutils/LowLatSocket.h"
#include "packet_structs.h"
#include "peer/peer_conf.h"
/**
* @struct PeerInfos
* @brief Stores other visible devices infos
*/
struct PeerInfos {
MappingData peer_data; /**< Device infos */
uint64_t alive_stamp; /**< Last alive message timestamp */
};
/**
* @class NetworkMapper
* @brief Establishes a network map of all the visible OAN devices in LAN.
*/
class NetworkMapper {
public:
/**
* Constructor
* @param pconf Default self configuration
*/
NetworkMapper(const PeerConf& pconf);
~NetworkMapper();
/**
* Network Mapper initialization
* @param iface Physical network interface name to start the mapping on
* @return true if initialization succeeds
*/
bool init_mapper(const std::string& iface);
/**
* Launch two threads which scan and map the network
*/
void launch_mapping_process();
/**
* Find a device MAC address based on its UID.
* @param uid UID to find
* @return If found, the corresponding MAC address.
*/
std::optional<uint64_t> get_mac_by_uid(uint16_t uid);
/**
* Update self resource mapping
* @param topo New topology
*/
void update_resource_mapping(NodeTopology topo);
/**
* Update local memory about pipe resource mapping
* @param topo Temporary deduced topology of peer
*/
void update_peer_resource_mapping(NodeTopology topo, uint16_t peer_uid);
/**
* Finds a DSP Device in the network that has space for new pipes
* @return If found, DSP ID
*/
std::optional<uint16_t> find_free_dsp() const;
/**
* Finds a free processing channel in a given device
* @param uid Device to search on
* @return If found, channel index
*/
std::optional<uint8_t> first_free_processing_channel(uint16_t uid);
/**
* Finds given device topology
* @param peer_uid Device to search
* @return If found, device topology
*/
std::optional<NodeTopology> get_device_topo(uint16_t peer_uid);
/**
* Finds all known control surfaces
* @return The list of known control surfaces
*/
std::vector<uint16_t> find_all_control_surfaces();
/**
* Install a callback called whenever a peer changes
*
* Callback signature void callback(PeerInfos& peer, bool peer_state)
*
* peer : The peer that has changed
*
* peer_state : true if still in network, false is it is gone from the network
*
* @warning The callback function MUST be thread-safe
* @param callback Function to be called
*/
void set_peer_change_callback(std::function<void(PeerInfos&, bool)> callback);
/**
* Adds a temporary peer that we don't know much about. Essentially here to avoid packets
* to be dropped when a device is still not discovered but has already started to communicate with us
* @param infos PeerInfos with at least its MAC address
* @param uid Peer uid address
*/
void add_temp_peer(uint16_t uid, const PeerInfos& infos);
/**
* Retreive all known clock slaves
* @return List of the clock slaves on the network
*/
std::vector<PeerInfos> get_clock_slaves();
/**
* Get the local UNIX time in ms
* @return Local UNIX time in ms
*/
static uint64_t local_now();
/**
* Get the local UNIX time in us
* @return Local UNIX time in us
*/
static uint64_t local_now_us();
void mapper_update();
void packet_send_update();
void packet_recv_update();
private:
/**
* Main mapper process
*/
void mapper_process();
void packet_sender();
void packet_receiver();
/**
* Updated the default configuration
* @param pconf New default configuration
*/
void update_packet(const PeerConf& pconf);
void process_packet(MappingPacket pck);
MappingPacket m_packet;
uint32_t m_netmask;
uint16_t m_mapping_port;
std::unique_ptr<LowLatSocket> m_map_socket;
std::unordered_map<int, PeerInfos> m_peers;
std::unordered_map<int, PeerInfos> m_temp_peers;
std::vector<PeerInfos> m_ck_slaves;
std::function<void(PeerInfos&, bool)> m_peer_change_callback;
#ifndef NO_THREADS
std::thread m_tx_thread;
std::thread m_rx_thread;
std::thread m_mapper_thread;
std::mutex m_mapper_mutex;
#endif // NO_THREADS
};
#endif //OPENAUDIONETWORK_NETWORKMAPPER_H