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