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 | #include "Name.h"
|
---|
40 |
|
---|
41 | #include "ariba/utility/types/Identifier.h"
|
---|
42 |
|
---|
43 | using ariba::utility::Identifier;
|
---|
44 |
|
---|
45 | std::ostream& operator<<( std::ostream& s, const ariba::Name& n ) {
|
---|
46 | return s << n.toString();
|
---|
47 | }
|
---|
48 |
|
---|
49 | namespace ariba {
|
---|
50 |
|
---|
51 | const Name Name::UNSPECIFIED;
|
---|
52 |
|
---|
53 | void Name::init(const char* name, int len, bool copy, bool hreadable) {
|
---|
54 |
|
---|
55 | // delete the old buffer
|
---|
56 | if( _bytes != NULL ) {
|
---|
57 | delete[] _bytes;
|
---|
58 | _bytes = NULL;
|
---|
59 | _length = 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | // alloc new stuff
|
---|
63 | if (len == -1)
|
---|
64 | len = strlen(name);
|
---|
65 |
|
---|
66 | if (copy) {
|
---|
67 |
|
---|
68 | if ( (name!=NULL) && (len>0) ){
|
---|
69 | _bytes = new uint8_t[len];
|
---|
70 | memcpy( _bytes, name, len );
|
---|
71 | } else {
|
---|
72 | len = 0;
|
---|
73 | _bytes = NULL;
|
---|
74 | }
|
---|
75 |
|
---|
76 | } else {
|
---|
77 | _bytes = (uint8_t*)name;
|
---|
78 | }
|
---|
79 |
|
---|
80 | _copy = copy;
|
---|
81 | _length = len;
|
---|
82 | _hreadable = hreadable;
|
---|
83 | }
|
---|
84 |
|
---|
85 | Name::Name()
|
---|
86 | : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
|
---|
87 | }
|
---|
88 |
|
---|
89 | Name::Name(const char* name, int len, bool copy)
|
---|
90 | : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
|
---|
91 | init(name, len, copy, len == -1);
|
---|
92 | }
|
---|
93 |
|
---|
94 | Name::Name(string name)
|
---|
95 | : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
|
---|
96 | init(name.c_str(), name.length(), true, true);
|
---|
97 | }
|
---|
98 |
|
---|
99 | Name::Name(const Name& name)
|
---|
100 | : _bytes( NULL ), _length( 0 ), _copy( false ), _hreadable( false) {
|
---|
101 | init((const char*)name.bytes(), name.length(), true, name._hreadable);
|
---|
102 | }
|
---|
103 |
|
---|
104 | Name::~Name() {
|
---|
105 | if (_copy && (_bytes!=NULL)){
|
---|
106 | delete[] _bytes;
|
---|
107 | _bytes = NULL;
|
---|
108 | _length = 0;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | Name& Name::operator=( const Name& name ) {
|
---|
113 | init((const char*)name.bytes(), name.length(), true, name._hreadable);
|
---|
114 | return *this;
|
---|
115 | }
|
---|
116 |
|
---|
117 | const uint8_t* Name::bytes() const {
|
---|
118 | return _bytes;
|
---|
119 | }
|
---|
120 |
|
---|
121 | const size_t Name::length() const {
|
---|
122 | return _length;
|
---|
123 | }
|
---|
124 |
|
---|
125 | bool Name::operator==(const Name& name) const {
|
---|
126 |
|
---|
127 | // unspecified Name objects
|
---|
128 | if (_bytes == NULL && name._bytes == NULL &&
|
---|
129 | length() == name.length()) return true;
|
---|
130 |
|
---|
131 | // specified name objects
|
---|
132 | if (_bytes == NULL || name._bytes == NULL) return false;
|
---|
133 | if (name.length() != length()) return false;
|
---|
134 | return (memcmp(name.bytes(), bytes(), length()) == 0);
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool Name::operator!=(const Name& name) const {
|
---|
138 | return !(*this == name);
|
---|
139 | }
|
---|
140 |
|
---|
141 | bool Name::isUnspecified() const {
|
---|
142 | return *this == UNSPECIFIED;
|
---|
143 | }
|
---|
144 |
|
---|
145 | Name Name::random() {
|
---|
146 | char name[17];
|
---|
147 | srand( time(NULL) );
|
---|
148 |
|
---|
149 | for (int i=0;i<16; i++)
|
---|
150 | name[i] = ("abcdefghijklmnopqrstuvwxyz")[((unsigned)rand())%26];
|
---|
151 | name[16] = 0;
|
---|
152 |
|
---|
153 | // force use of the std::string ctor with this
|
---|
154 | return Name( string(name) );
|
---|
155 | }
|
---|
156 |
|
---|
157 | string Name::toString() const {
|
---|
158 | if (_hreadable) {
|
---|
159 | char str[256];
|
---|
160 | for (size_t i=0; i<length(); i++) str[i] = bytes()[i];
|
---|
161 | str[length()] = 0;
|
---|
162 | return string(str);
|
---|
163 | } else {
|
---|
164 | return string("<not readable>");
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | NodeID Name::toNodeId() const {
|
---|
169 | if( bytes()==NULL || length()==0 ) return NodeID::UNSPECIFIED;
|
---|
170 | return NodeID( Identifier::sha1(bytes(),length()) );
|
---|
171 | }
|
---|
172 |
|
---|
173 | SpoVNetID Name::toSpoVNetId() const {
|
---|
174 | if( bytes()==NULL || length()==0 ) return SpoVNetID::UNSPECIFIED;
|
---|
175 | return SpoVNetID( Identifier::sha1(bytes(),length()) );
|
---|
176 | }
|
---|
177 |
|
---|
178 | } // namespace ariba
|
---|