| 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 INSTITUTE OF TELEMATICS 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 NAME_H_
|
---|
| 40 | #define NAME_H_
|
---|
| 41 |
|
---|
| 42 | #include <iostream>
|
---|
| 43 | #include <memory.h>
|
---|
| 44 |
|
---|
| 45 | // forward declaration
|
---|
| 46 | namespace ariba { class Name; }
|
---|
| 47 |
|
---|
| 48 | std::ostream& operator<<( std::ostream&, const ariba::Name& );
|
---|
| 49 |
|
---|
| 50 | #include "Identifiers.h"
|
---|
| 51 |
|
---|
| 52 | namespace ariba {
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * This class is a wrapper for canonical names.
|
---|
| 56 | * Currently only human readable names are supported.
|
---|
| 57 | *
|
---|
| 58 | * @author Sebastian Mies <mies@tm.uka.de>
|
---|
| 59 | */
|
---|
| 60 | class Name {
|
---|
| 61 | friend std::ostream& operator<<( std::ostream&, const ::ariba::Name& );
|
---|
| 62 |
|
---|
| 63 | private:
|
---|
| 64 | bool _hreadable;
|
---|
| 65 | bool _copy;
|
---|
| 66 | int _length;
|
---|
| 67 | uint8_t* _bytes;
|
---|
| 68 |
|
---|
| 69 | inline void init(const char* name, int len, bool copy, bool hreadable) {
|
---|
| 70 | if (len == -1)
|
---|
| 71 | len = strlen(name);
|
---|
| 72 |
|
---|
| 73 | if (copy) {
|
---|
| 74 | _bytes = new uint8_t[len];
|
---|
| 75 | memcpy(_bytes, name, len);
|
---|
| 76 | } else {
|
---|
| 77 | _bytes = (uint8_t*)name;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | _copy = copy;
|
---|
| 81 | _length = len;
|
---|
| 82 | _hreadable = hreadable;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public:
|
---|
| 86 | static const Name UNSPECIFIED;
|
---|
| 87 |
|
---|
| 88 | public:
|
---|
| 89 | /**
|
---|
| 90 | * Constructs a new, yet unspecified name.
|
---|
| 91 | */
|
---|
| 92 | inline Name() {
|
---|
| 93 | _bytes = NULL;
|
---|
| 94 | _length = 0;
|
---|
| 95 | _copy = false;
|
---|
| 96 | _hreadable = false;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Constructs a new name. If no length is specified, a human-readable
|
---|
| 101 | * name is assumed.
|
---|
| 102 | *
|
---|
| 103 | * @param name The name
|
---|
| 104 | * @param len The optional name length, if binary data is used as name
|
---|
| 105 | * @param copy A flag, whether the name's memory needs to be copied
|
---|
| 106 | */
|
---|
| 107 | inline Name(const char* name, int len = -1, bool copy = false) {
|
---|
| 108 | init(name, len, copy, len == -1);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Constructs a new name out of a human readable string.
|
---|
| 113 | *
|
---|
| 114 | * @param name A human readable name
|
---|
| 115 | */
|
---|
| 116 | inline Name(std::string name) {
|
---|
| 117 | init(name.c_str(), -1, true, true);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * The copy constructor.
|
---|
| 122 | */
|
---|
| 123 | inline Name(const Name& name) {
|
---|
| 124 | init((const char*)name.bytes(), name.length(), true, name._hreadable);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Destroys the name and releases underlying memory if this name is a copy.
|
---|
| 129 | */
|
---|
| 130 | inline ~Name() {
|
---|
| 131 | if (_copy) delete _bytes;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Returns the binary bytes of the name
|
---|
| 136 | *
|
---|
| 137 | * @return The binary data
|
---|
| 138 | */
|
---|
| 139 | inline const uint8_t* bytes() const {
|
---|
| 140 | return _bytes;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * Returns the length of the name in bytes.
|
---|
| 145 | *
|
---|
| 146 | * @return The length of the name
|
---|
| 147 | */
|
---|
| 148 | inline const size_t length() const {
|
---|
| 149 | return _length;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * The common implementation of the "equal" operator.
|
---|
| 154 | */
|
---|
| 155 | inline bool operator==(const Name& name) const {
|
---|
| 156 | if (_bytes == NULL || name._bytes == NULL) return false;
|
---|
| 157 | if (name.length() != length()) return false;
|
---|
| 158 | return (memcmp(name.bytes(), bytes(), length()) == 0);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * The common implementation of the "unequal" operator.
|
---|
| 163 | */
|
---|
| 164 | inline bool operator!=(const Name& name) const {
|
---|
| 165 | return !(*this == name);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * Returns true, if the name is yet unspecified
|
---|
| 170 | */
|
---|
| 171 | inline bool isUnspecified() {
|
---|
| 172 | return *this == UNSPECIFIED;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * Returns a random name.
|
---|
| 177 | */
|
---|
| 178 | inline static Name random() {
|
---|
| 179 | char name[17];
|
---|
| 180 | for (int i=0;i<16; i++)
|
---|
| 181 | name[i] = ("abcdefghijklmnopqrstuvwxyz")[((unsigned)rand())%26];
|
---|
| 182 | name[16] = 0;
|
---|
| 183 | return Name(name);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * Returns a human-readable representation of this name
|
---|
| 188 | */
|
---|
| 189 | inline std::string toString() const {
|
---|
| 190 | if (_hreadable) {
|
---|
| 191 | char str[256];
|
---|
| 192 | for (int i=0; i<length(); i++) str[i] = bytes()[i];
|
---|
| 193 | str[length()] = 0;
|
---|
| 194 | return std::string(str);
|
---|
| 195 | } else {
|
---|
| 196 | return std::string("<not readable>");
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | // hack: to be changed!
|
---|
| 201 | inline NodeID toNodeId() const {
|
---|
| 202 | return NodeID::sha1( bytes(), length() );
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | // hack: to be changed!
|
---|
| 206 | inline SpoVNetID toSpoVNetId() const {
|
---|
| 207 | return SpoVNetID::sha1(bytes(), length() );
|
---|
| 208 | }
|
---|
| 209 | };
|
---|
| 210 |
|
---|
| 211 | } // namespace ariba
|
---|
| 212 |
|
---|
| 213 | #endif /* NAME_H_ */
|
---|