An Overlay-based
Virtual Network Substrate
SpoVNet

source: source/ariba/Name.cpp @ 7468

Last change on this file since 7468 was 6919, checked in by mies, 14 years ago

Fixed tons of warnings when using CXXFLAGS="-Wall"!

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