source: source/ariba/utility/types/Identifier.h@ 12745

Last change on this file since 12745 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: 15.4 KB
Line 
1// [License]
2// The Ariba-Underlay Copyright
3//
4// Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
5//
6// Institute of Telematics
7// UniversitÀt Karlsruhe (TH)
8// Zirkel 2, 76128 Karlsruhe
9// Germany
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
22// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
25// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32//
33// The views and conclusions contained in the software and documentation
34// are those of the authors and should not be interpreted as representing
35// official policies, either expressed or implied, of the Institute of
36// Telematics.
37// [License]
38
39#ifndef IDENTIFIER_H_
40#define IDENTIFIER_H_
41
42#include <memory>
43#include <string>
44#include <cassert>
45#include <iostream>
46#include <gmp.h>
47#include <boost/cstdint.hpp>
48#include "ariba/utility/logging/Logging.h"
49#include "ariba/utility/types/Address.h"
50#include "ariba/utility/serialization.h"
51
52// XXX EXPERIMENTAL
53#include "ariba/utility/transport/messages/shared_buffer.hpp"
54
55/**< maximum length of the key */
56#define MAX_KEYLENGTH 192
57
58namespace ariba {
59namespace utility {
60
61using_serialization;
62
63class IdentifierBit;
64
65/**
66 * An abstract address class for identifier.<br/>
67 *
68 * This class is the base for all identifier classes.
69 */
70class Identifier: public Address {
71 VSERIALIZEABLE;
72 use_logging_h( Identifier );
73public:
74
75 // XXX EXPERIMENTAL
76 reboost::shared_buffer_t serialize() const
77 {
78 Data data = data_serialize(this, DEFAULT_V);
79 reboost::shared_buffer_t buf(data.getBuffer(), data.getLength() / 8);
80
81 return buf;
82 }
83
84 reboost::shared_buffer_t deserialize(reboost::shared_buffer_t buff)
85 {
86 Data dat(const_cast<uint8_t*>(buff.data()), buff.size() * 8);
87
88 size_t len = this->SERIALIZATION_METHOD_NAME(DESERIALIZE, dat) / 8;
89
90 // return remaining sub-buffer
91 return buff(len);
92 }
93
94
95 //-------------------------------------------------------------------------
96 // virtual function from Address
97 //-------------------------------------------------------------------------
98
99 virtual void clearAddress();
100 virtual bool setAddress(string address);
101 virtual string getAddress() const;
102
103 //-------------------------------------------------------------------------
104 // constants
105 //-------------------------------------------------------------------------
106
107 static const Identifier UNSPECIFIED_KEY; /**< Identifier without defined key */
108 static const Identifier ZERO; /**< Identifier with key initialized as 0 */
109 static const Identifier ONE; /**< Identifier with key initialized as 1 */
110
111 //-------------------------------------------------------------------------
112 // construction and destruction
113 //-------------------------------------------------------------------------
114
115 /**
116 * Default constructor
117 *
118 * Contructs an unspecified overlay key
119 */
120 Identifier();
121
122 /**
123 * Constructs an overlay key initialized with a common integer
124 *
125 * @param num The integer to initialize this key with
126 */
127 Identifier(uint32_t num);
128
129 /**
130 * Constructs a key out of a buffer
131 * @param buffer Source buffer
132 * @param size Buffer size (in bytes)
133 */
134 Identifier(const unsigned char* buffer, uint size);
135
136 /**
137 * Constructs a key out of a string number.
138 */
139 Identifier(const std::string& str, uint base = 16);
140
141 /**
142 * Copy constructor.
143 *
144 * @param rhs The key to copy.
145 */
146 Identifier(const Identifier& rhs);
147
148 /**
149 * Default destructor.
150 *
151 * Does nothing ATM.
152 */
153 virtual ~Identifier();
154
155 //-------------------------------------------------------------------------
156 // string representations & node key attributes
157 //-------------------------------------------------------------------------
158
159 /**
160 * Returns a string representation of this key
161 *
162 * @return String representation of this key
163 */
164 virtual std::string toString(uint base = 16) const;
165
166 /**
167 * Common stdc++ console output method
168 */
169 friend std::ostream& operator<<(std::ostream& os, const Identifier& c);
170
171 /**
172 * Returns true, if the key is unspecified
173 *
174 * @return Returns true, if key is unspecified
175 */
176 bool isUnspecified() const;
177
178 //-------------------------------------------------------------------------
179 // operators
180 //-------------------------------------------------------------------------
181
182 /**
183 * compares this to a given Identifier
184 *
185 * @param compKey the the Identifier to compare this to
186 * @return true if compKey->key is smaller than this->key, else false
187 */
188 bool operator<(const Identifier& compKey) const;
189
190 /**
191 * compares this to a given Identifier
192 *
193 * @param compKey the the Identifier to compare this to
194 * @return true if compKey->key is greater than this->key, else false
195 */
196 bool operator>(const Identifier& compKey) const;
197
198 /**
199 * compares this to a given Identifier
200 *
201 * @param compKey the the Identifier to compare this to
202 * @return true if compKey->key is smaller than or equal to this->key, else false
203 */
204 bool operator<=(const Identifier& compKey) const;
205
206 /**
207 * compares this to a given Identifier
208 *
209 * @param compKey the the Identifier to compare this to
210 * @return true if compKey->key is greater than or equal to this->key, else false
211 */
212 bool operator>=(const Identifier& compKey) const;
213
214 /**
215 * compares this to a given Identifier
216 *
217 * @param compKey the the Identifier to compare this to
218 * @return true if compKey->key is equal to this->key, else false
219 */
220 bool operator==(const Identifier& compKey) const;
221
222 /**
223 * compares this to a given Identifier
224 *
225 * @param compKey the the Identifier to compare this to
226 * @return true if compKey->key is not equal to this->key, else false
227 */
228 bool operator!=(const Identifier& compKey) const;
229
230 /**
231 * Unifies all compare operations in one method
232 *
233 * @param compKey key to compare with
234 * @return int -1 if smaller, 0 if equal, 1 if greater
235 */
236 int compareTo(const Identifier& compKey) const;
237
238 /**
239 * assigns Identifier of rhs to this->key
240 *
241 * @param rhs the Identifier with the defined key
242 * @return this Identifier object
243 */
244 Identifier& operator=(const Identifier& rhs);
245
246 /**
247 * substracts 1 from this->key
248 *
249 * @return this Identifier object
250 */
251 Identifier& operator--();
252
253 /**
254 * adds 1 to this->key
255 *
256 * @return this Identifier object
257 */
258 Identifier& operator++();
259
260 /**
261 * adds rhs->key to this->key
262 *
263 * @param rhs the Identifier with the defined key
264 * @return this Identifier object
265 */
266 Identifier& operator+=(const Identifier& rhs);
267
268 /**
269 * substracts rhs->key from this->key
270 *
271 * @param rhs the Identifier with the defined key
272 * @return this Identifier object
273 */
274 Identifier& operator-=(const Identifier& rhs);
275
276 /**
277 * adds rhs->key to this->key
278 *
279 * @param rhs the Identifier with the defined key
280 * @return this Identifier object
281 */
282 Identifier operator+(const Identifier& rhs) const;
283
284 /**
285 * substracts rhs->key from this->key
286 *
287 * @param rhs the Identifier with the defined key
288 * @return this Identifier object
289 */
290 Identifier operator-(const Identifier& rhs) const;
291
292 /**
293 * substracts 1 from this->key
294 *
295 * @return this Identifier object
296 */
297 Identifier operator--(int);
298
299 /**
300 * adds 1 to this->key
301 *
302 * @return this Identifier object
303 */
304 Identifier operator++(int);
305
306 /**
307 * bitwise shift right
308 *
309 * @param num number of bits to shift
310 * @return this Identifier object
311 */
312 Identifier operator>>(uint num) const;
313
314 /**
315 * bitwise shift left
316 *
317 * @param num number of bits to shift
318 * @return this Identifier object
319 */
320 Identifier operator<<(uint num) const;
321
322 /**
323 * bitwise AND of rhs->key and this->key
324 *
325 * @param rhs the Identifier AND is calculated with
326 * @return this Identifier object
327 */
328 Identifier operator&(const Identifier& rhs) const;
329
330 /**
331 * bitwise OR of rhs->key and this->key
332 *
333 * @param rhs the Identifier OR is calculated with
334 * @return this Identifier object
335 */
336 Identifier operator|(const Identifier& rhs) const;
337
338 /**
339 * bitwise XOR of rhs->key and this->key
340 *
341 * @param rhs the Identifier XOR is calculated with
342 * @return this Identifier object
343 */
344 Identifier operator^(const Identifier& rhs) const;
345
346 /**
347 * bitwise NOT of this->key
348 *
349 * @return this Identifier object
350 */
351 Identifier operator~() const;
352
353 /**
354 * returns the n-th bit of this->key
355 *
356 * @param n the position of the returned bit
357 * @return the bit on position n in this->key
358 */
359 IdentifierBit operator[](uint n);
360
361 /**
362 * sets a bit of this->key
363 *
364 * @param pos the position of the bit to set
365 * @param value new value for bit at position pos
366 * @return *this
367 */
368 Identifier& setBitAt(uint pos, bool value);
369
370 //-------------------------------------------------------------------------
371 // additional math
372 //-------------------------------------------------------------------------
373
374 /**
375 * Returns a sub integer at position p with n-bits. p is counted starting
376 * from the least significant bit of the key as bit 0. Bit p of the key
377 * becomes bit 0 of the returned integer.
378 *
379 * @param p the position of the sub-integer
380 * @param n the number of bits to be returned (max.32)
381 * @return The sub-integer.
382 */
383 uint32_t get(uint p, uint n) const;
384
385 /**
386 * Returns a hash value for the key
387 *
388 * @return size_t The hash value
389 */
390 size_t hash() const;
391
392 /**
393 * Returns the position of the msb in this key, which represents
394 * just the logarithm to base 2.
395 *
396 * @return The logarithm to base 2 of this key.
397 */
398 int log_2() const;
399
400 /**
401 * Fills the suffix starting at pos with random bits to lsb.
402 *
403 * @param pos
404 * @return Identifier
405 */
406 Identifier randomSuffix(uint pos) const;
407
408 /**
409 * Fills the prefix starting at pos with random bits to msb.
410 *
411 * @param pos
412 * @return Identifier
413 */
414 Identifier randomPrefix(uint pos) const;
415
416 /**
417 * Calculates the number of equal bits from the left with another
418 * Key (shared prefix length)
419 *
420 * @param compKey the Key to compare with
421 * @return length of shared prefix
422 */
423 uint sharedPrefixLength(const Identifier& compKey) const;
424
425 /**
426 * Returns true, if this key is element of the interval (keyA, keyB)
427 * on the ring.
428 *
429 * @param keyA The left border of the interval
430 * @param keyB The right border of the interval
431 * @return True, if the key is element of the interval (keyA, keyB)
432 */
433 bool isBetween(const Identifier& keyA, const Identifier& keyB) const;
434
435 /**
436 * Returns true, if this key is element of the interval (keyA, keyB]
437 * on the ring.
438 *
439 * @param keyA The left border of the interval
440 * @param keyB The right border of the interval
441 * @return True, if the key is element of the interval (keyA, keyB]
442 */
443 bool isBetweenR(const Identifier& keyA, const Identifier& keyB) const;
444
445 /**
446 * Returns true, if this key is element of the interval [keyA, keyB)
447 * on the ring.
448 *
449 * @param keyA The left border of the interval
450 * @param keyB The right border of the interval
451 * @return True, if the key is element of the interval [keyA, keyB)
452 */
453 bool isBetweenL(const Identifier& keyA, const Identifier& keyB) const;
454
455 /**
456 * Returns true, if this key is element of the interval [keyA, keyB]
457 * on the ring.
458 *
459 * @param keyA The left border of the interval
460 * @param keyB The right border of the interval
461 * @return True, if the key is element of the interval [keyA, keyB]
462 */
463 bool isBetweenLR(const Identifier& keyA, const Identifier& keyB) const;
464
465 //-------------------------------------------------------------------------
466 // static methods
467 //-------------------------------------------------------------------------
468
469 /**
470 * Set the length of an Identifier
471 *
472 * @param length keylength in bits
473 */
474 static void setKeyLength(uint length);
475
476 /**
477 * Returns the length in number of bits.
478 *
479 * @return The length in number of bits.
480 */
481 static uint getLength();
482
483 /**
484 * Returns a random key.
485 *
486 * @return A random key.
487 */
488 static Identifier random();
489
490 /**
491 * Returns the maximum key, i.e. a key filled with bit 1
492 *
493 * @return The maximum key, i.e. a key filled with bit 1
494 */
495 static Identifier max();
496
497
498 // README: due to some problems with serialization the keylength
499 // was changed to 192 bit! As none of the hashing functions
500 // can provide 192 bit output, and we currently don't use any
501 // hashing for identifiers in the demo at all ... this is all commented out!!
502 /**
503 * Returns a key with the SHA1 cryptographic hash of a
504 * string
505 *
506 * @param value A string value.
507 * @return SHA1 of value
508 */
509 static Identifier sha1(const string& value);
510
511 static Identifier sha1(const uint8_t* value, size_t length );
512
513 /**
514 * Returns a key 2^exponent.
515 *
516 * @param exponent The exponent.
517 * @return Key=2^exponent.
518 */
519 static Identifier pow2(uint exponent);
520
521private:
522 // private constants
523
524 static uint keyLength; /**< actual length of the key */
525 static uint aSize; /**< number of needed machine words to hold the key*/
526 static mp_limb_t GMP_MSB_MASK; /**< bits to fill up if key does not
527 exactly fit in one or more machine words */
528
529 // private fields
530 bool isUnspec; /**< is this->key unspecified? */
531
532 void seed();
533
534 static const size_t array_size = MAX_KEYLENGTH / (8 * sizeof(mp_limb_t))
535 + (MAX_KEYLENGTH % (8 * sizeof(mp_limb_t)) != 0 ? 1 : 0);
536
537 /** the overlay key this object represents */
538 mp_limb_t key[array_size + 1];
539
540 // private "helper" methods
541 /**
542 * trims key after key operations
543 */
544 void trim();
545
546 /**
547 * set this->key to 0 and isUnspec to false
548 */
549 void clear();
550};
551
552/**
553 * An auxiliary class for single bits in OverlayKey
554 *
555 * Allows statements like "key[n] = true"
556 */
557class IdentifierBit {
558public:
559
560 IdentifierBit(bool value, uint pos, Identifier* key) :
561 bit(value), pos(pos), key(key) {
562 }
563
564 /** Converts to a boolean value */
565 inline operator bool() {
566 return bit;
567 }
568
569 /** Sets the corresponding bit to a boolean value
570 * @param value value to set to
571 */
572 inline IdentifierBit& operator=(bool value) {
573 key->setBitAt(pos, value);
574 return *this;
575 }
576
577 inline IdentifierBit& operator^=(bool value) {
578 key->setBitAt(pos, (*key)[pos] ^ value);
579 return *this;
580 }
581
582private:
583
584 bool bit;
585 uint pos;
586 Identifier* key;
587};
588
589}
590} // namespace ariba, common
591
592/* serializers */
593sznBeginDefault( ariba::utility::Identifier, X ) {
594
595 // calculate length of key
596 uint16_t len = array_size*sizeof(mp_limb_t);
597 uint8_t unspec = isUnspec;
598
599 // only serialize the lower 16 bits of keyLength
600 X && unspec && len;
601
602 // serialize the identifier
603 for (int i=array_size-1; i>=0; i--) X && key[i];
604
605 // when deserializing set unspec flag
606 if (X.isDeserializer()){
607 isUnspec = unspec;
608 }
609
610} sznEnd();
611
612#endif /* IDENTIFIER_H_ */
Note: See TracBrowser for help on using the repository browser.