• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

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 <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(std::string scheme, std::string ssp)
00025                  : _scheme(scheme), _ssp(ssp)
00026                 {
00027                         // TODO: checks for illegal characters
00028                 }
00029 
00030                 EID::EID(std::string value)
00031                 {
00032                         try {
00033                                 // search for the delimiter
00034                                 size_t delimiter = value.find_first_of(":");
00035 
00036                                 // jump to default eid if the format is wrong
00037                                 if (delimiter == std::string::npos)
00038                                         throw ibrcommon::Exception("wrong eid format");
00039 
00040                                 // the scheme is everything before the delimiter
00041                                 _scheme = value.substr(0, delimiter);
00042 
00043                                 // the ssp is everything else
00044                                 size_t startofssp = delimiter + 1;
00045                                 _ssp = value.substr(startofssp, value.length() - startofssp);
00046 
00047                                 // TODO: do syntax check
00048                         } catch (...) {
00049                                 _scheme = DEFAULT_SCHEME;
00050                                 _ssp = "none";
00051                         }
00052                 }
00053 
00054                 EID::EID(size_t node, size_t application)
00055                  : _scheme(EID::CBHE_SCHEME), _ssp()
00056                 {
00057                         if (node == 0)
00058                         {
00059                                 _scheme = DEFAULT_SCHEME;
00060                                 _ssp = "none";
00061                                 return;
00062                         }
00063 
00064                         std::stringstream ss_ssp;
00065                         ss_ssp << node << "." << application;
00066                         _ssp = ss_ssp.str();
00067                 }
00068 
00069                 EID::~EID()
00070                 {
00071                 }
00072 
00073                 EID& EID::operator=(const EID &other)
00074                 {
00075                         _ssp = other._ssp;
00076                         _scheme = other._scheme;
00077                         return *this;
00078                 }
00079 
00080                 bool EID::operator==(EID const& other) const
00081                 {
00082                         return (_ssp == other._ssp) && (_scheme == other._scheme);
00083                 }
00084 
00085                 bool EID::operator==(string const& other) const
00086                 {
00087                         return ((*this) == EID(other));
00088                 }
00089 
00090                 bool EID::operator!=(EID const& other) const
00091                 {
00092                         return !((*this) == other);
00093                 }
00094 
00095                 EID EID::operator+(string suffix) const
00096                 {
00097                         return EID(getString() + suffix);
00098                 }
00099 
00100                 bool EID::sameHost(string const& other) const
00101                 {
00102                         return ( other == getNodeEID() );
00103                 }
00104 
00105                 bool EID::sameHost(EID const& other) const
00106                 {
00107                         return ( other.getNodeEID() == getNodeEID() );
00108                 }
00109 
00110                 bool EID::operator<(EID const& other) const
00111                 {
00112                         return getString() < other.getString();
00113                 }
00114 
00115                 bool EID::operator>(EID const& other) const
00116                 {
00117                         return other < (*this);
00118                 }
00119 
00120                 std::string EID::getString() const
00121                 {
00122                         return _scheme + ":" + _ssp;
00123                 }
00124 
00125                 std::string EID::getApplication() const throw (ibrcommon::Exception)
00126                 {
00127                         size_t first_char = 0;
00128                         char delimiter = '.';
00129 
00130                         if (_scheme != EID::CBHE_SCHEME)
00131                         {
00132                                 // with a uncompressed bundle header we have another delimiter
00133                                 delimiter = '/';
00134 
00135                                 // first char not "/", e.g. "//node1" -> 2
00136                                 first_char = _ssp.find_first_not_of(delimiter);
00137 
00138                                 // only "/" ? thats bad!
00139                                 if (first_char == std::string::npos)
00140                                         throw ibrcommon::Exception("wrong eid format");
00141                         }
00142 
00143                         // start of application part
00144                         size_t application_start = _ssp.find_first_of(delimiter, first_char);
00145 
00146                         // no application part available
00147                         if (application_start == std::string::npos)
00148                                 return "";
00149 
00150                         if (_scheme == EID::CBHE_SCHEME)
00151                         {
00152                                 // return the application part
00153                                 return _ssp.substr(application_start + 1, _ssp.length() - application_start - 1);
00154                         }
00155                         else
00156                         {
00157                                 // return the application part
00158                                 return _ssp.substr(application_start, _ssp.length() - application_start);
00159                         }
00160                 }
00161 
00162                 std::string EID::getNode() const throw (ibrcommon::Exception)
00163                 {
00164                         size_t first_char = 0;
00165                         char delimiter = '.';
00166 
00167                         if (_scheme != EID::CBHE_SCHEME)
00168                         {
00169                                 // with a uncompressed bundle header we have another delimiter
00170                                 delimiter = '/';
00171 
00172                                 // first char not "/", e.g. "//node1" -> 2
00173                                 first_char = _ssp.find_first_not_of(delimiter);
00174 
00175                                 // only "/" ? thats bad!
00176                                 if (first_char == std::string::npos)
00177                                         throw ibrcommon::Exception("wrong eid format");
00178                         }
00179 
00180                         // start of application part
00181                         size_t application_start = _ssp.find_first_of(delimiter, first_char);
00182 
00183                         // no application part available
00184                         if (application_start == std::string::npos)
00185                                 return _ssp;
00186 
00187                         // return the node part
00188                         return _ssp.substr(0, application_start);
00189                 }
00190 
00191                 std::string EID::getScheme() const
00192                 {
00193                         return _scheme;
00194                 }
00195 
00196                 std::string EID::getNodeEID() const
00197                 {
00198                         return _scheme + ":" + getNode();
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                 std::pair<size_t, size_t> EID::getCompressed() const
00233                 {
00234                         size_t node = 0;
00235                         size_t app = 0;
00236 
00237                         if (isCompressable())
00238                         {
00239                                 std::stringstream ss_node(getNode());
00240                                 ss_node >> node;
00241 
00242                                 if (hasApplication())
00243                                 {
00244                                         std::stringstream ss_app(getApplication());
00245                                         ss_app >> app;
00246                                 }
00247                         }
00248 
00249                         return make_pair(node, app);
00250                 }
00251         }
00252 }

Generated on Wed Mar 30 2011 11:11:49 for IBR-DTNSuite by  doxygen 1.7.1