source: source/ariba/utility/addressing/endpoint_set.hpp@ 12768

Last change on this file since 12768 was 12060, checked in by hock@…, 11 years ago

Reintegrate branch: 20130111-hock-message_classes

improvements:

  • new message classes (reboost, zero-copy)
  • "fast path" for direct links (skip overlay layer)
  • link-properties accessible from the application
  • SystemQueue can call boost::bind functions
  • protlib compatibility removed (32bit overhead saved in every message)
  • addressing2
  • AddressDiscovery discoveres only addresses on which we're actually listening
  • ariba serialization usage reduced (sill used in OverlayMsg)
  • Node::connect, easier and cleaner interface to start-up ariba from the application
  • ariba configs via JSON, XML, etc (boost::property_tree)
  • keep-alive overhead greatly reduced
  • (relayed) overlay links can actually be closed now
  • lost messages are detected in most cases
  • notification to the application when link is transformed into direct-link
  • overlay routing: send message to second best hop if it would be dropped otherwise
  • SequenceNumbers (only mechanisms, so for: upward compatibility)
  • various small fixes


regressions:

  • bluetooth is not yet working again
  • bootstrap modules deactivated
  • liblog4xx is not working (use cout-logging)

This patch brings great performance and stability improvements at cost of backward compatibility.
Also bluetooth and the bootstrap modules have not been ported to the new interfaces, yet.

File size: 11.0 KB
RevLine 
[12060]1#ifndef ENDPOINT_SET_HPP_DEPRECATED_
2#define ENDPOINT_SET_HPP_DEPRECATED_
[5284]3
4#include "addressing.hpp"
5#include "tcpip_endpoint.hpp"
6
7#include <sstream>
8#include <boost/unordered_set.hpp>
9#include <boost/foreach.hpp>
10#include <boost/thread.hpp>
11
12namespace ariba {
13namespace addressing {
14
15using boost::unordered_set;
16
17/**
18 * This end-point set shelters known addresses of a device.
19 * Transport protocols use this class to address devices.
20 *
21 * Example of a string representation:
22 * "tcp{500|501};ip{10.11.12.13};bluetooth{01:02:03:04:05:06};rfcomm{1234}"
23 * Inside a address type specification, addresses are separated by a bar (|).
24 *
25 * @author Sebastian Mies <mies@tm.uka.de>
26 */
27class endpoint_set {
28public:
29 // layer 2
30 unordered_set<mac_address> bluetooth;
31
32 // layer 3
33 unordered_set<ip_address> ip;
34
35 // layer 4
36 unordered_set<tcp_port_address> tcp;
37 unordered_set<rfcomm_channel_address> rfcomm;
38
39 // mutex
40 boost::mutex io_mutex;
41 typedef boost::mutex::scoped_lock scoped_lock;
42
43private:
44 template<uint8_t type, class V>
45 size_t to_bytes_dynamic( const unordered_set<V>& set, uint8_t* bytes ) const {
46 size_t size = 0;
47 bytes[0] = type;
48 uint8_t* size_ptr = bytes+1;
49 bytes +=2;
50 size += 2;
51 BOOST_FOREACH( const V& value, set ) {
52 bytes[0] = (uint8_t)value.to_bytes_size();
53 bytes++;
54 size++;
55 value.to_bytes(bytes);
56 bytes += value.to_bytes_size();
57 size += value.to_bytes_size();
58 }
59 *size_ptr = size-2;
60 return size;
61 }
62
63 template<class V>
64 void from_bytes_dynamic( unordered_set<V>& set, const uint8_t* bytes, uint8_t size ) {
65 size_t pos = 0;
66 while (pos < size) {
67 uint8_t length = bytes[0];
68 bytes++; pos++;
69 V obj(bytes,length);
70 set.insert(obj);
71 bytes+=length; pos+=length;
72 }
73 }
74
75 template<uint8_t type, class V>
76 size_t to_bytes_fixed( const unordered_set<V>& set, uint8_t* bytes ) const {
77 size_t fixed_size = V().to_bytes_size();
78 bytes[0] = type;
79 bytes[1] = (uint8_t)(set.size()* fixed_size);
80 bytes+=2;
81 BOOST_FOREACH( const V& value, set ) {
82 value.to_bytes(bytes);
83 bytes += value.to_bytes_size();
84 }
85 return 2 + set.size() * fixed_size;
86 }
87
88 template<class V>
89 void from_bytes_fixed( unordered_set<V>& set, const uint8_t* bytes, uint8_t size ) {
90 size_t fixed_size = V().to_bytes_size();
91 uint8_t num = size/fixed_size;
92 for (uint8_t i=0; i<num; i++) {
93 V obj(bytes, fixed_size);
94 set.insert(obj);
95 bytes += fixed_size;
96 }
97 }
98
99 template<class V>
100 std::string to_string_set( const unordered_set<V>& set, const std::string& type ) const {
101 if (set.size()==0) return std::string("");
102 std::ostringstream buf;
103 buf << type << "{";
104 bool first = true;
105 BOOST_FOREACH( const V& value, set ) {
106 if (!first) {
107 buf << " | ";
108 } else
109 first = false;
110 buf << value.to_string();
111 }
112 buf << "};";
113 return buf.str();
114 }
115
116 static void trim(string& str) {
117 string::size_type pos = str.find_last_not_of(' ');
118 if(pos != string::npos) {
119 str.erase(pos + 1);
120 pos = str.find_first_not_of(' ');
121 if(pos != string::npos) str.erase(0, pos);
122 }
123 else str.erase(str.begin(), str.end());
124 }
125
126 static string::size_type skip( const char* chars, string::size_type pos, const std::string& str ) {
127 bool found = true;
128 while (pos<str.size() && found) {
129 found = false;
130 for (size_t i=0; chars[i]!=0 && !found; i++)
131 if (str.at(pos)==chars[i]) {
132 pos++;
133 found = true;
134 }
135 }
136 return pos;
137 }
138
139 template<class V>
140 size_t from_string_set( unordered_set<V>& set, string::size_type pos, const std::string& str ) {
141 while (pos < str.size() && pos != string::npos) {
142 pos = skip("} |\n\r", pos, str);
143 string::size_type nend1 = str.find('}',pos);
144 string::size_type nend2 = str.find('|',pos);
145 if (nend1==string::npos && nend2==string::npos) break;
146 if (nend1==string::npos) nend1=str.size();
147 if (nend2==string::npos) nend2=str.size();
148 string::size_type nend = nend2 < nend1 ? nend2:nend1;
149 std::string sub = str.substr(pos, min(nend2,nend1)-pos);
150 trim(sub);
151 V obj( sub );
152 set.insert(obj);
153 pos = nend+1;
154 if (nend1<nend2) break;
155 }
156 return pos-1;
157 }
158
159public:
160 enum layers {
161 Layer1 = 1, Layer2 = 2, Layer3 = 4, Layer4 = 8, Layer5 = 16,
[6829]162 Layer6 = 32, Layer7 = 64, Layer8 = 128, NoLoopback = 256,AllLayers = ~0,
[5284]163 Layer1_3 = Layer1|Layer2|Layer3,
164 Layer1_4 = Layer1|Layer2|Layer3|Layer4,
165 };
166
167 endpoint_set() {
168
169 }
170
171 endpoint_set( const endpoint_set& copy ) :
172 bluetooth(copy.bluetooth), ip(copy.ip), tcp(copy.tcp), rfcomm(copy.rfcomm) {
173 }
174
175 endpoint_set( const std::string& str ) {
176 assign(str);
177 }
178
179 endpoint_set( const uint8_t* bytes, size_t size ) {
180 assign(bytes, size);
181 }
182
183 /// adds an address or endpoint to this set
184 void add( const address_v* address, int layers = AllLayers ) {
185 scoped_lock lock(io_mutex);
186 if ( address->instanceof<tcpip_endpoint> () ) {
187 const tcpip_endpoint& addr = *address;
[6877]188 if (layers & Layer3 &&
189 !((layers & NoLoopback) && addr.address().is_loopback()) )
[6876]190 ip.insert( addr.address() );
[5284]191 if (layers & Layer4) tcp.insert( addr.port() );
192 } else
193 if ( address->instanceof<ip_address>() ) {
194 const ip_address& addr = *address;
[6877]195 if ((layers & Layer3) &&
196 !((layers & NoLoopback) && addr.is_loopback()))
[6829]197 ip.insert( addr );
[5284]198 } else
199 if (address->instanceof<rfcomm_endpoint>() ) {
200 const rfcomm_endpoint& endp = *address;
201 if (layers & Layer2) bluetooth.insert( endp.mac() );
202 if (layers & Layer4) rfcomm.insert( endp.channel() );
203 } else
204 if (address->instanceof<mac_address>() ) {
205 const mac_address& endp = *address;
206 if (layers & Layer2) bluetooth.insert( endp );
207 }
208 }
209
210 /// adds addresses from another endpoint set
211 void add( const endpoint_set& eps, int layers = AllLayers ) {
212 scoped_lock lock(io_mutex);
213
214 // merge layer 2 addresses
215 if (layers & Layer2) {
216 bluetooth.insert(eps.bluetooth.begin(), eps.bluetooth.end() );
217 }
218
219 // merge layer 3 addresses
220 if (layers & Layer3) {
221 ip.insert(eps.ip.begin(), eps.ip.end() );
222 }
223
224 // merge layer 4 addresses
225 if (layers & Layer4) {
226 tcp.insert(eps.tcp.begin(), eps.tcp.end() );
227 rfcomm.insert(eps.rfcomm.begin(), eps.rfcomm.end() );
228 }
229 }
230
231 /// removes an address or endpoint from this set
232 void remove( const address_vf address ) {
233 scoped_lock lock(io_mutex);
234 if ( address->instanceof<tcpip_endpoint> () ) {
235 const tcpip_endpoint& addr = *address;
236 ip.erase( addr.address() );
237 tcp.erase( addr.port() );
238 } else
239 if ( address->instanceof<ip_address>() ) {
240 const ip_address& addr = *address;
241 ip.erase( addr );
242 } else
243 if (address->instanceof<rfcomm_endpoint>() ) {
244 const rfcomm_endpoint& endp = *address;
245 bluetooth.erase( endp.mac() );
246 rfcomm.erase( endp.channel() );
247 }
248 }
249
250 /// checks whether two end-points are disjoint
251 /// (only check lower level addresses)
[5406]252 bool disjoint_to( const endpoint_set& set ) const {
[5284]253 scoped_lock lock(const_cast<boost::mutex&>(io_mutex));
254 BOOST_FOREACH( const mac_address& mac, bluetooth )
255 if (set.bluetooth.count(mac) !=0 ) return false;
256 BOOST_FOREACH( const ip_address& ip_, ip )
257 if (set.ip.count(ip_) !=0 ) return false;
258 return true;
259 }
260
[5406]261 bool intersects_with( const endpoint_set& set ) const {
262 return !disjoint_to(set);
263 }
264
265 bool is_subset_of( const endpoint_set& set ) const {
266 throw "Not implemented!";
267 return false;
268 }
269
[5284]270 /// returns true, if this address has a fixed size in bytes
271 bool is_bytes_size_static() const {
272 return false;
273 }
274
275 /// returns the number of bytes used for serialization of this address
276 size_t to_bytes_size() const {
277 scoped_lock lock(const_cast<boost::mutex&>(io_mutex));
278 size_t size = 0;
279
280 // bluetooth mac list (layer 2)
281 size += bluetooth.size() * mac_address().to_bytes_size();
282
283 // ip list (layer 3)
284 BOOST_FOREACH( const ip_address& ip_, ip )
285 size += (ip_.to_bytes_size() + 1 /* =length */);
286
287 // tcp ports (layer 4)
288 size += tcp.size() * tcp_port_address().to_bytes_size();
289
290 // bluetooth rfcomm channels (layer 4)
291 size += rfcomm.size() * rfcomm_channel_address().to_bytes_size();
292
293 // length/type encoding
294 size += 4 /* number of items*/ * 2 /* length of type and length */;
295
296 return size;
297 }
298
299 /// converts this address to a binary representation
300 void to_bytes(uint8_t* bytes) const {
301 scoped_lock lock(const_cast<boost::mutex&>(io_mutex));
302
303 /// bluetooth mac list (layer 2)
304 bytes += to_bytes_fixed<0x21, mac_address>( bluetooth, bytes );
305
306 // ip list (layer 3)
307 bytes += to_bytes_dynamic<0x31, ip_address>(ip, bytes);
308
309 // tcp ports (layer 4)
310 bytes += to_bytes_fixed<0x41, tcp_port_address>( tcp, bytes );
311
312 // rfcomm channels (layer 4)
313 bytes += to_bytes_fixed<0x42, rfcomm_channel_address>( rfcomm, bytes );
314 }
315
316 /// Assigns an address using a bunch of bytes
317 bool assign(const uint8_t* bytes, size_t size) {
318 scoped_lock lock(io_mutex);
319
320 size_t pos = 0;
321 while (pos < size) {
322 uint8_t type = bytes[0];
323 uint8_t length = bytes[1];
324 bytes+=2; pos+=2;
325
326 switch (type) {
327
328 // bluetooth mac
329 case 0x21: {
330 from_bytes_fixed<mac_address>( bluetooth, bytes, length );
331 break;
332 }
333
334 // ip
335 case 0x31: {
336 from_bytes_dynamic<ip_address>( ip, bytes, length );
337 break;
338 }
339 // tcp
340 case 0x41: {
341 from_bytes_fixed<tcp_port_address>( tcp, bytes, length );
342 break;
343 }
344 // rfcomm
345 case 0x42: {
346 from_bytes_fixed<rfcomm_channel_address>( rfcomm, bytes, length );
347 break;
348 }
349
350 default: {
351 pos = size;
352 break;
353 }
354 }
355 bytes += length; pos+=length;
356 }
357 return false;
358 }
359
360 /// generates a string out of this endpoint-set
361 std::string to_string() const {
362 scoped_lock lock(const_cast<boost::mutex&>(io_mutex));
363 std::string smac = to_string_set<mac_address>(bluetooth, "bluetooth");
364 std::string sip = to_string_set<ip_address>(ip, "ip");
365 std::string stcp = to_string_set<tcp_port_address>(tcp, "tcp");
366 std::string srfcomm = to_string_set<rfcomm_channel_address>(rfcomm, "rfcomm");
367 return smac+sip+stcp+srfcomm;
368 }
369
370 /// assigns an endpoint-set out of a string
371 void assign( const std::string& str ) {
372 scoped_lock lock(io_mutex);
373 string::size_type pos = 0;
374 while (pos < str.size() && pos!=string::npos) {
375 pos = skip("}; \n\r", pos, str );
376 string::size_type nend = str.find('{',pos);
377 if (nend == string::npos) break;
378 std::string type = str.substr(pos,nend-pos);
379 pos = nend+1;
380 trim(type);
381 if (type=="bluetooth")
382 pos = from_string_set<mac_address>(bluetooth, pos, str );
383 else if (type=="ip")
384 pos = from_string_set<ip_address>(ip, pos, str );
385 else if (type=="tcp")
386 pos = from_string_set<tcp_port_address>(tcp, pos, str );
387 else if (type=="rfcomm")
388 pos = from_string_set<rfcomm_channel_address>(rfcomm, pos, str );
389 else
390 pos = str.find('}',pos);
391 }
392 }
393
394 endpoint_set& operator=( const endpoint_set& rhs ) {
395 scoped_lock lock(io_mutex);
396 this->bluetooth = rhs.bluetooth;
397 this->ip = rhs.ip;
398 this->rfcomm = rhs.rfcomm;
399 this->tcp = rhs.tcp;
[6919]400 return *this;
[5284]401 }
[5406]402
403 /// checks wheter the two endpoint sets are identical
404 bool operator== ( const endpoint_set& rhs ) const {
405 return (rhs.rfcomm == rfcomm && rhs.ip == ip && rhs.tcp == tcp &&
406 rhs.bluetooth == bluetooth);
407 }
408
409 bool operator!= ( const endpoint_set& rhs ) const {
410 return !(*this==rhs);
411 }
[5284]412};
413
414}} // namespace ariba::addressing
415
416#endif /* ENDPOINT_SET_HPP_ */
Note: See TracBrowser for help on using the repository browser.