| 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 LINKID_H_
|
|---|
| 40 | #define LINKID_H_
|
|---|
| 41 |
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 | #include <stdint.h>
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 |
|
|---|
| 46 | #include <vector>
|
|---|
| 47 | #include <string>
|
|---|
| 48 |
|
|---|
| 49 | #include "ariba/utility/serialization.h"
|
|---|
| 50 |
|
|---|
| 51 | using std::vector;
|
|---|
| 52 |
|
|---|
| 53 | namespace ariba {
|
|---|
| 54 | namespace utility {
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * The link id identifies a link between two devices/nodes.
|
|---|
| 58 | *
|
|---|
| 59 | * Its a 32-bit value, that is composed out of two 16-bit values
|
|---|
| 60 | * identifing a initiator and acceptor id.
|
|---|
| 61 | */
|
|---|
| 62 | class LinkID {
|
|---|
| 63 | private:
|
|---|
| 64 | uint16_t local_id;
|
|---|
| 65 | uint16_t initiator_id;
|
|---|
| 66 | uint16_t acceptor_id;
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | LinkID(uint16_t local_id) {
|
|---|
| 70 | this->local_id = local_id;
|
|---|
| 71 | this->initiator_id = 0;
|
|---|
| 72 | this->acceptor_id = 0;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /// returns the full id
|
|---|
| 76 | inline uint32_t getFullId() const {
|
|---|
| 77 | return (initiator_id << 16) + acceptor_id;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | inline int compareTo( const LinkID& rhs ) const {
|
|---|
| 81 | // compare local id
|
|---|
| 82 | if (rhs.isLocal() && this->isLocal())
|
|---|
| 83 | return local_id - rhs.local_id;
|
|---|
| 84 |
|
|---|
| 85 | // compare initiator id
|
|---|
| 86 | else if ((initiator_id == 0 || rhs.initiator_id == 0)
|
|---|
| 87 | && (acceptor_id == rhs.acceptor_id) ) return 0;
|
|---|
| 88 |
|
|---|
| 89 | // compare acceptor id
|
|---|
| 90 | else if ((acceptor_id == 0 || rhs.acceptor_id == 0)
|
|---|
| 91 | && (initiator_id == rhs.initiator_id) ) return 0;
|
|---|
| 92 |
|
|---|
| 93 | // compare full id
|
|---|
| 94 | else if (getFullId() == rhs.getFullId()) return 0;
|
|---|
| 95 | else if (getFullId() < rhs.getFullId()) return -1;
|
|---|
| 96 | else if (getFullId() > rhs.getFullId()) return 1;
|
|---|
| 97 | return -1;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static const char* getInfo( const LinkID& id );
|
|---|
| 101 |
|
|---|
| 102 | static bool isValid( const LinkID& id );
|
|---|
| 103 |
|
|---|
| 104 | public:
|
|---|
| 105 | /// the unspecified link id
|
|---|
| 106 | static const LinkID UNSPECIFIED;
|
|---|
| 107 |
|
|---|
| 108 | /// create a new locally unique link id
|
|---|
| 109 | static LinkID create( const char* info = NULL );
|
|---|
| 110 |
|
|---|
| 111 | /// free a locally unique link id
|
|---|
| 112 | static void destroy( const LinkID& id );
|
|---|
| 113 |
|
|---|
| 114 | /// construct a unspecified id
|
|---|
| 115 | LinkID() {
|
|---|
| 116 | local_id = 0;
|
|---|
| 117 | initiator_id = 0;
|
|---|
| 118 | acceptor_id = 0;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /// copy constructor
|
|---|
| 122 | LinkID( const LinkID& rh );
|
|---|
| 123 |
|
|---|
| 124 | /// assigns another link id
|
|---|
| 125 | LinkID& operator=(const LinkID& rh) {
|
|---|
| 126 | local_id = rh.local_id;
|
|---|
| 127 | initiator_id = rh.initiator_id;
|
|---|
| 128 | acceptor_id = rh.acceptor_id;
|
|---|
| 129 | return *this;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /// returns true, if the local link id is known and registered
|
|---|
| 133 | bool isValid() const {
|
|---|
| 134 | return isValid(*this);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | bool isUnspecified() const {
|
|---|
| 138 | return local_id == 0 && initiator_id == 0 && acceptor_id == 0;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /// returns true, if this is a local id only
|
|---|
| 142 | bool isLocal() const {
|
|---|
| 143 | return acceptor_id == 0 && initiator_id == 0;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /// returns true, if this is a remote id only
|
|---|
| 147 | bool isRemote() const {
|
|---|
| 148 | return local_id == 0;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /// returns true, if this is a full link id (with acceptor/initiator id)
|
|---|
| 152 | bool isLink() const {
|
|---|
| 153 | return acceptor_id != 0 && initiator_id != 0;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /// returns the local id
|
|---|
| 157 | uint16_t getLocalId() const {
|
|---|
| 158 | return local_id;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /// returns the remote id
|
|---|
| 162 | uint16_t getRemoteId() const {
|
|---|
| 163 | return local_id == initiator_id ? acceptor_id : initiator_id;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /// returns the initiators local link id
|
|---|
| 167 | uint16_t getInitiatorId() const {
|
|---|
| 168 | return initiator_id;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /// returns the acceptors local link id
|
|---|
| 172 | uint16_t getAcceptorId() const {
|
|---|
| 173 | return acceptor_id;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /// sets the local initiator id of the link
|
|---|
| 177 | /// if id is unspecified a new local id is used as initiator id
|
|---|
| 178 | void setInitiatorId( const LinkID& id = UNSPECIFIED ) {
|
|---|
| 179 | assert(initiator_id == 0);
|
|---|
| 180 | if ( id == UNSPECIFIED ) {
|
|---|
| 181 | assert(local_id == 0);
|
|---|
| 182 | local_id = LinkID::create().local_id;
|
|---|
| 183 | initiator_id = local_id;
|
|---|
| 184 | } else {
|
|---|
| 185 | assert(local_id == acceptor_id && id.local_id == 0 && id.initiator_id != 0);
|
|---|
| 186 | initiator_id = id.initiator_id;
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /// sets the local acceptor id of the link
|
|---|
| 191 | /// if id is unspecified a new local id is used as acceptor id
|
|---|
| 192 | void setAcceptorId( const LinkID& id = UNSPECIFIED ) {
|
|---|
| 193 | assert(acceptor_id == 0);
|
|---|
| 194 | if ( id == UNSPECIFIED ) {
|
|---|
| 195 | assert(local_id == 0);
|
|---|
| 196 | local_id = LinkID::create().local_id;
|
|---|
| 197 | acceptor_id = local_id;
|
|---|
| 198 | } else {
|
|---|
| 199 | assert(local_id == initiator_id && id.local_id == 0 && id.acceptor_id != 0);
|
|---|
| 200 | acceptor_id = id.acceptor_id;
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | void combine( const LinkID& id ) {
|
|---|
| 205 |
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /// returns a string representation of the link id
|
|---|
| 209 | std::string toString() const {
|
|---|
| 210 | char str[20];
|
|---|
| 211 | if (isLocal())
|
|---|
| 212 | sprintf(str, "l%04x", local_id);
|
|---|
| 213 | else
|
|---|
| 214 | sprintf(str, "i%04x.a%04x", initiator_id, acceptor_id );
|
|---|
| 215 | return std::string(str);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /// returns the info of the link id
|
|---|
| 219 | const char* getInfo() const {
|
|---|
| 220 | return getInfo(*this);
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /// convenience operators
|
|---|
| 224 | bool operator==( const LinkID& rhs ) const { return compareTo(rhs) == 0; }
|
|---|
| 225 | bool operator!=( const LinkID& rhs ) const { return compareTo(rhs) != 0; }
|
|---|
| 226 | bool operator< ( const LinkID& rhs ) const { return compareTo(rhs) < 0; }
|
|---|
| 227 | bool operator<=( const LinkID& rhs ) const { return compareTo(rhs) <= 0; }
|
|---|
| 228 | bool operator> ( const LinkID& rhs ) const { return compareTo(rhs) > 0; }
|
|---|
| 229 | bool operator>=( const LinkID& rhs ) const { return compareTo(rhs) >= 0; }
|
|---|
| 230 | };
|
|---|
| 231 |
|
|---|
| 232 | std::ostream& operator<<(std::ostream& s, const LinkID& id );
|
|---|
| 233 |
|
|---|
| 234 | typedef vector<LinkID> LinkIDs;
|
|---|
| 235 |
|
|---|
| 236 | }} // namespace ariba, utility
|
|---|
| 237 |
|
|---|
| 238 | sznBeginDefault( ariba::utility::LinkID, X ) {
|
|---|
| 239 | if (X.isDeserializer()) local_id = 0;
|
|---|
| 240 | X && initiator_id && acceptor_id;
|
|---|
| 241 | } sznEnd();
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 | #endif /* LINKID_H_ */
|
|---|