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