forked from mit-ll/LL-DLEP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfoBaseMgr.h
224 lines (177 loc) · 7.3 KB
/
InfoBaseMgr.h
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
/*
* Dynamic Link Exchange Protocol (DLEP)
*
* Copyright (C) 2013, 2015, 2016, 2019 Massachusetts Institute of Technology
*/
#ifndef _INFO_BASE_MGR_
#define _INFO_BASE_MGR_
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <iostream>
#include <netinet/in.h>
#include <string>
#include <strings.h>
#include <time.h>
#include <map>
#include <vector>
#include <iterator>
#include <algorithm>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <boost/thread/recursive_mutex.hpp>
#include "Dlep.h"
#include "DlepLogger.h"
#include "DataItem.h"
namespace LLDLEP
{
namespace internal
{
class Dlep;
class Peer;
// Map data item ID to its value
typedef std::map<LLDLEP::DataItemIdType, LLDLEP::DataItem> DataItemMap;
/// Information about a destination
class DestinationData
{
public:
/// Constructor.
/// @param[in] mac MAC address of new destination
/// @param[in] initial_data_items data items associated with mac
/// @param[in] dlep back-pointer to dlep instance
DestinationData(const LLDLEP::DlepMac & mac,
const LLDLEP::DataItems & initial_data_items,
DlepPtr dlep);
~DestinationData();
/// Record updated data items for this destination.
/// @param[in] updates new data items to record
/// @param[in] tell_peers should we send peers a DestinationUpdate signal
/// with these updates?
/// @return the number of updates recorded
unsigned int update(const LLDLEP::DataItems & updates, bool tell_peers);
/// Get a list of all of this destination's current data items.
/// @param[out] data_items_out vector to store the data items in
void get_all_data_items(LLDLEP::DataItems & data_items_out) const;
/// Get a list of all of this destination's data items containing
/// IP addresses.
LLDLEP::DataItems get_ip_data_items() const;
/// Log information about this destination.
/// @param[in] prefix string to put at the beginning of the log message
/// @param[in] log_level log level to use for LOG
void log(const std::string & prefix, int log_level) const;
void needs_response(const std::string & response_name);
std::string needs_response() const;
/// Search for an IP address on this destination.
/// @param[in] ip_data_item
/// data item containing an IP address to search for in this
/// destination's IP addresses
/// @return "" if not found, else a non-empty string identifying this
/// destination.
std::string find_ip_data_item(const DataItem & ip_data_item) const;
private:
/// MAC address of this destination
LLDLEP::DlepMac mac_address;
/// Metric data items associated with this destination. Only one
/// data item per metric type is allowed, so this is a map.
DataItemMap metric_data_items;
/// IP address data items associated with this destination.
/// There can be multiple IP addresses of each type, so this
/// is a vector.
DataItems ip_data_items;
/// Is the peer expecting a Destination Announce Response or Destination
/// Up Response for this destination?
std::string needs_response_;
/// back-pointer to dlep instance
DlepPtr dlep;
/// logger, for LOG macro
DlepLoggerPtr logger;
};
typedef boost::shared_ptr<DestinationData> DestinationDataPtr;
class PeerData
{
public:
/// Constructor.
/// @param[in] id peer id
/// @param[in] initial_data_items data items associated with this peer
/// @param[in] dlep back-pointer to dlep instance
PeerData(const std::string & id, const LLDLEP::DataItems & initial_data_items,
DlepPtr dlep);
~PeerData();
bool addDestination(const LLDLEP::DlepMac & mac,
const LLDLEP::DataItems & initial_data_items,
bool tell_peers);
void sendAllDestinations(boost::shared_ptr<Peer> peer);
bool updateDestination(const LLDLEP::DlepMac & mac,
const LLDLEP::DataItems & updates,
bool tell_peers);
bool removeDestination(const LLDLEP::DlepMac & mac, bool tell_peers,
const LLDLEP::DataItems & data_items = LLDLEP::DataItems());
bool getDestinationData(const LLDLEP::DlepMac & mac, DestinationDataPtr * ddpp);
void logDestinations(bool include_metrics);
void getDestinations(std::vector<LLDLEP::DlepMac> & destinations);
bool validDestination(const LLDLEP::DlepMac & mac);
std::string update_data_items(const LLDLEP::DataItems & updates,
bool tell_peers);
void remove_data_items(const DataItems & data_items);
LLDLEP::DataItems get_data_items();
/// Get a list of all of this peer's data items containing
/// IP addresses.
LLDLEP::DataItems get_ip_data_items() const;
void log_data_items();
void needs_response(const LLDLEP::DlepMac & mac, const std::string & response_name);
std::string needs_response(const LLDLEP::DlepMac & mac);
/// Search for an IP address on this peer.
/// @param[in] ip_data_item
/// data item containing an IP address to search for in this
/// peer's IP addresses
/// @return "" if not found, else a non-empty string identifying where the
/// IP address was found: this peer, or one of its destinations.
std::string find_ip_data_item(const DataItem & ip_data_item) const;
private:
DataItems update_data_items_helper(const DataItems & data_items,
DlepPtr dlep);
/// peer_id is a combination of the TCP remote IP and port of the session
std::string peer_id;
/// Map destination mac to Destination Data
std::map<LLDLEP::DlepMac, DestinationDataPtr> destination_data;
/// The combination of the metric_data_items, ip_data_items, and
/// other_data_items fields below constitute the set of session
/// initialization data items referred to in the DlepService API.
/// Metric data items that this peer supports, and their default
/// values. Only one data item per metric type is allowed, so
/// this is a map.
DataItemMap metric_data_items;
/// IP address data items associated with this peer. There can be
/// multiple IP addresses of each type, so this is a vector.
DataItems ip_data_items;
// Other data items associated with this peer.
DataItems other_data_items;
/// back-pointer to dlep instance
DlepPtr dlep;
/// logger, for LOG macro
DlepLoggerPtr logger;
};
typedef boost::shared_ptr<PeerData> PeerDataPtr;
class InfoBaseMgr
{
public:
explicit InfoBaseMgr(DlepPtr dlep);
~InfoBaseMgr();
PeerDataPtr addPeer(const std::string & peer_id,
const LLDLEP::DataItems & initial_values);
bool removePeer(const std::string & peer_id);
bool getPeerData(const std::string & peer_id, PeerDataPtr * pdpp);
void logInfoBase(bool include_metrics);
bool validPeer(const std::string & peer_id);
bool validDestination(const std::string & peer_id, const LLDLEP::DlepMac & mac);
private:
// Peers, by peer ID
std::map<std::string, PeerDataPtr> peer_data;
DlepPtr dlep;
DlepLoggerPtr logger;
};
typedef boost::shared_ptr<InfoBaseMgr> InfoBaseMgrPtr;
} // namespace internal
} // namespace LLDLEP
#endif // _INFO_BASE_MGR_