00001
00002
00003
00004
00005
00006
00007
00008 #include "ibrdtn/data/EID.h"
00009 #include <sstream>
00010 #include <iostream>
00011
00012 namespace dtn
00013 {
00014 namespace data
00015 {
00016 const std::string EID::DEFAULT_SCHEME = "dtn";
00017 const std::string EID::CBHE_SCHEME = "ipn";
00018
00019 EID::EID()
00020 : _scheme(DEFAULT_SCHEME), _ssp("none")
00021 {
00022 }
00023
00024 EID::EID(const EID &other)
00025 : _scheme(other._scheme), _ssp(other._ssp)
00026 {
00027 }
00028
00029 EID::EID(std::string scheme, std::string ssp)
00030 : _scheme(scheme), _ssp(ssp)
00031 {
00032
00033 }
00034
00035 EID::EID(std::string value)
00036 {
00037 try {
00038
00039 size_t delimiter = value.find_first_of(":");
00040
00041
00042 if (delimiter == std::string::npos)
00043 throw ibrcommon::Exception("wrong eid format");
00044
00045
00046 _scheme = value.substr(0, delimiter);
00047
00048
00049 size_t startofssp = delimiter + 1;
00050 _ssp = value.substr(startofssp, value.length() - startofssp);
00051
00052
00053 } catch (...) {
00054 _scheme = DEFAULT_SCHEME;
00055 _ssp = "none";
00056 }
00057 }
00058
00059 EID::EID(size_t node, size_t application)
00060 : _scheme(EID::CBHE_SCHEME), _ssp()
00061 {
00062 if (node == 0)
00063 {
00064 _scheme = DEFAULT_SCHEME;
00065 _ssp = "none";
00066 return;
00067 }
00068
00069 std::stringstream ss_ssp;
00070 ss_ssp << node << "." << application;
00071 _ssp = ss_ssp.str();
00072 }
00073
00074 EID::~EID()
00075 {
00076 }
00077
00078 EID& EID::operator=(const EID &other)
00079 {
00080 _ssp = other._ssp;
00081 _scheme = other._scheme;
00082 return *this;
00083 }
00084
00085 bool EID::operator==(EID const& other) const
00086 {
00087 return (_ssp == other._ssp) && (_scheme == other._scheme);
00088 }
00089
00090 bool EID::operator==(string const& other) const
00091 {
00092 return ((*this) == EID(other));
00093 }
00094
00095 bool EID::operator!=(EID const& other) const
00096 {
00097 return !((*this) == other);
00098 }
00099
00100 EID EID::operator+(string suffix)
00101 {
00102 return EID(getString() + suffix);
00103 }
00104
00105 bool EID::sameHost(string const& other) const
00106 {
00107 return ( other == getNodeEID() );
00108 }
00109
00110 bool EID::sameHost(EID const& other) const
00111 {
00112 return ( other.getNodeEID() == getNodeEID() );
00113 }
00114
00115 bool EID::operator<(EID const& other) const
00116 {
00117 return getString() < other.getString();
00118 }
00119
00120 bool EID::operator>(EID const& other) const
00121 {
00122 return other < (*this);
00123 }
00124
00125 std::string EID::getString() const
00126 {
00127 return _scheme + ":" + _ssp;
00128 }
00129
00130 std::string EID::getApplication() const throw (ibrcommon::Exception)
00131 {
00132 size_t first_char = 0;
00133 char delimiter = '.';
00134
00135 if (_scheme != EID::CBHE_SCHEME)
00136 {
00137
00138 delimiter = '/';
00139
00140
00141 first_char = _ssp.find_first_not_of(delimiter);
00142
00143
00144 if (first_char == std::string::npos)
00145 throw ibrcommon::Exception("wrong eid format");
00146 }
00147
00148
00149 size_t application_start = _ssp.find_first_of(delimiter, first_char);
00150
00151
00152 if (application_start == std::string::npos)
00153 return "";
00154
00155 if (_scheme == EID::CBHE_SCHEME)
00156 {
00157
00158 return _ssp.substr(application_start + 1, _ssp.length() - application_start - 1);
00159 }
00160 else
00161 {
00162
00163 return _ssp.substr(application_start, _ssp.length() - application_start);
00164 }
00165 }
00166
00167 std::string EID::getNode() const throw (ibrcommon::Exception)
00168 {
00169 size_t first_char = 0;
00170 char delimiter = '.';
00171
00172 if (_scheme != EID::CBHE_SCHEME)
00173 {
00174
00175 delimiter = '/';
00176
00177
00178 first_char = _ssp.find_first_not_of(delimiter);
00179
00180
00181 if (first_char == std::string::npos)
00182 throw ibrcommon::Exception("wrong eid format");
00183 }
00184
00185
00186 size_t application_start = _ssp.find_first_of(delimiter, first_char);
00187
00188
00189 if (application_start == std::string::npos)
00190 return _ssp;
00191
00192
00193 return _ssp.substr(0, application_start);
00194 }
00195
00196 std::string EID::getScheme() const
00197 {
00198 return _scheme;
00199 }
00200
00201 std::string EID::getNodeEID() const
00202 {
00203 return _scheme + ":" + getNode();
00204 }
00205
00206 bool EID::hasApplication() const
00207 {
00208
00209 if (_scheme == EID::CBHE_SCHEME)
00210 {
00211 return (_ssp.find_first_of(".") != std::string::npos);
00212 }
00213
00214
00215 size_t first_char = _ssp.find_first_not_of("/");
00216
00217
00218 if (first_char == std::string::npos)
00219 throw ibrcommon::Exception("wrong eid format");
00220
00221
00222 size_t application_start = _ssp.find_first_of("/", first_char);
00223
00224
00225 if (application_start == std::string::npos)
00226 return false;
00227
00228
00229 return true;
00230 }
00231
00232 bool EID::isCompressable() const
00233 {
00234 return ((_scheme == DEFAULT_SCHEME) && (_ssp == "none")) || (_scheme == EID::CBHE_SCHEME);
00235 }
00236
00237 std::pair<size_t, size_t> EID::getCompressed() const
00238 {
00239 size_t node = 0;
00240 size_t app = 0;
00241
00242 if (isCompressable())
00243 {
00244 std::stringstream ss_node(getNode());
00245 ss_node >> node;
00246
00247 if (hasApplication())
00248 {
00249 std::stringstream ss_app(getApplication());
00250 ss_app >> app;
00251 }
00252 }
00253
00254 return make_pair(node, app);
00255 }
00256 }
00257 }