IBR-DTNSuite 0.6

daemon/src/core/Node.cpp

Go to the documentation of this file.
00001 #include "core/Node.h"
00002 #include "net/ConvergenceLayer.h"
00003 #include <ibrdtn/utils/Clock.h>
00004 #include <ibrdtn/utils/Utils.h>
00005 #include <ibrcommon/Logger.h>
00006 
00007 #include <iostream>
00008 #include <sstream>
00009 
00010 using namespace std;
00011 
00012 namespace dtn
00013 {
00014         namespace core
00015         {
00016                 Node::URI::URI(const Node::Type t, const Node::Protocol p, const std::string &uri, const size_t to)
00017                  : type(t), protocol(p), value(uri), expire((to == 0) ? 0 : dtn::utils::Clock::getTime() + to)
00018                 {
00019                 }
00020 
00021                 Node::URI::~URI()
00022                 {
00023                 }
00024 
00025                 void Node::URI::decode(std::string &address, unsigned int &port) const
00026                 {
00027                         // parse parameters
00028                         std::vector<string> parameters = dtn::utils::Utils::tokenize(";", value);
00029                         std::vector<string>::const_iterator param_iter = parameters.begin();
00030 
00031                         while (param_iter != parameters.end())
00032                         {
00033                                 std::vector<string> p = dtn::utils::Utils::tokenize("=", (*param_iter));
00034 
00035                                 if (p[0].compare("ip") == 0)
00036                                 {
00037                                         address = p[1];
00038                                 }
00039 
00040                                 if (p[0].compare("port") == 0)
00041                                 {
00042                                         std::stringstream port_stream;
00043                                         port_stream << p[1];
00044                                         port_stream >> port;
00045                                 }
00046 
00047                                 param_iter++;
00048                         }
00049                 }
00050 
00051                 bool Node::URI::operator<(const URI &other) const
00052                 {
00053                         if (protocol < other.protocol) return true;
00054                         if (protocol != other.protocol) return false;
00055 
00056                         if (type < other.type) return true;
00057                         if (type != other.type) return false;
00058 
00059                         return (value < other.value);
00060                 }
00061 
00062                 bool Node::URI::operator==(const URI &other) const
00063                 {
00064                         return ((type == other.type) && (protocol == other.protocol) && (value == other.value));
00065                 }
00066 
00067                 bool Node::URI::operator==(const Node::Protocol &p) const
00068                 {
00069                         return (protocol == p);
00070                 }
00071 
00072                 bool Node::URI::operator==(const Node::Type &t) const
00073                 {
00074                         return (type == t);
00075                 }
00076 
00077                 std::ostream& operator<<(std::ostream &stream, const Node::URI &u)
00078                 {
00079                         stream << Node::toString(u.type) << "#" << Node::toString(u.protocol) << "#" << u.value;
00080                         return stream;
00081                 }
00082 
00083                 Node::Attribute::Attribute(const Type t, const std::string &n, const std::string &v, const size_t to)
00084                  : type(t), name(n), value(v), expire((to == 0) ? 0 : dtn::utils::Clock::getTime() + to)
00085                 {
00086                 }
00087 
00088                 Node::Attribute::~Attribute()
00089                 {
00090                 }
00091 
00092                 bool Node::Attribute::operator<(const Attribute &other) const
00093                 {
00094                         if (name < other.name) return true;
00095                         if (name != other.name) return false;
00096 
00097                         return (type < other.type);
00098                 }
00099 
00100                 bool Node::Attribute::operator==(const Attribute &other) const
00101                 {
00102                         return ((type == other.type) && (name == other.name));
00103                 }
00104 
00105                 bool Node::Attribute::operator==(const std::string &n) const
00106                 {
00107                         return (name == n);
00108                 }
00109 
00110                 std::ostream& operator<<(std::ostream &stream, const Node::Attribute &a)
00111                 {
00112                         stream << Node::toString(a.type) << "#" << a.name << "#" << a.value;
00113                         return stream;
00114                 }
00115 
00116                 Node::Node(const dtn::data::EID &id)
00117                 : _connect_immediately(false), _id(id)
00118                 {
00119                 }
00120 
00121                 Node::~Node()
00122                 {
00123                 }
00124 
00125                 std::string Node::toString(Node::Type type)
00126                 {
00127                         switch (type)
00128                         {
00129                         case Node::NODE_UNAVAILABLE:
00130                                 return "unavailable";
00131 
00132                         case Node::NODE_DISCOVERED:
00133                                 return "discovered";
00134 
00135                         case Node::NODE_STATIC:
00136                                 return "static";
00137 
00138                         case Node::NODE_CONNECTED:
00139                                 return "connected";
00140                         }
00141 
00142                         return "unknown";
00143                 }
00144 
00145                 std::string Node::toString(Node::Protocol proto)
00146                 {
00147                         switch (proto)
00148                         {
00149                         case Node::CONN_UNSUPPORTED:
00150                                 return "unsupported";
00151 
00152                         case Node::CONN_UNDEFINED:
00153                                 return "undefined";
00154 
00155                         case Node::CONN_UDPIP:
00156                                 return "UDP";
00157 
00158                         case Node::CONN_TCPIP:
00159                                 return "TCP";
00160 
00161                         case Node::CONN_LOWPAN:
00162                                 return "LoWPAN";
00163 
00164                         case Node::CONN_BLUETOOTH:
00165                                 return "Bluetooth";
00166 
00167                         case Node::CONN_HTTP:
00168                                 return "HTTP";
00169                         }
00170 
00171                         return "unknown";
00172                 }
00173 
00174                 bool Node::has(Node::Protocol proto) const
00175                 {
00176                         for (std::set<URI>::const_iterator iter = _uri_list.begin(); iter != _uri_list.end(); iter++)
00177                         {
00178                                 if ((*iter) == proto) return true;
00179                         }
00180                         return false;
00181                 }
00182 
00183                 bool Node::has(const std::string &name) const
00184                 {
00185                         for (std::set<Attribute>::const_iterator iter = _attr_list.begin(); iter != _attr_list.end(); iter++)
00186                         {
00187                                 if ((*iter) == name) return true;
00188                         }
00189                         return false;
00190                 }
00191 
00192                 void Node::add(const URI &u)
00193                 {
00194                         _uri_list.erase(u);
00195                         _uri_list.insert(u);
00196                 }
00197 
00198                 void Node::add(const Attribute &attr)
00199                 {
00200                         _attr_list.erase(attr);
00201                         _attr_list.insert(attr);
00202                 }
00203 
00204                 void Node::remove(const URI &u)
00205                 {
00206                         _uri_list.erase(u);
00207                 }
00208 
00209                 void Node::remove(const Attribute &attr)
00210                 {
00211                         _attr_list.erase(attr);
00212                 }
00213 
00214                 void Node::clear()
00215                 {
00216                         _uri_list.clear();
00217                         _attr_list.clear();
00218                 }
00219 
00220                 std::list<Node::URI> Node::get(Node::Protocol proto) const
00221                 {
00222                         std::list<URI> ret;
00223                         for (std::set<URI>::const_iterator iter = _uri_list.begin(); iter != _uri_list.end(); iter++)
00224                         {
00225                                 if ((*iter) == proto) ret.push_back(*iter);
00226                         }
00227                         return ret;
00228                 }
00229 
00230                 std::list<Node::URI> Node::get(Node::Type type, Node::Protocol proto) const
00231                 {
00232                         std::list<URI> ret;
00233                         for (std::set<URI>::const_iterator iter = _uri_list.begin(); iter != _uri_list.end(); iter++)
00234                         {
00235                                 if (((*iter) == proto) && ((*iter) == type)) ret.push_back(*iter);
00236                         }
00237                         return ret;
00238                 }
00239 
00240                 std::list<Node::Attribute> Node::get(const std::string &name) const
00241                 {
00242                         std::list<Attribute> ret;
00243                         for (std::set<Attribute>::const_iterator iter = _attr_list.begin(); iter != _attr_list.end(); iter++)
00244                         {
00245                                 if ((*iter) == name) ret.push_back(*iter);
00246                         }
00247                         return ret;
00248                 }
00249 
00250                 const dtn::data::EID& Node::getEID() const
00251                 {
00252                         return _id;
00253                 }
00254 
00259                 bool Node::expire()
00260                 {
00261                         // get the current timestamp
00262                         size_t ct = dtn::utils::Clock::getTime();
00263 
00264                         // walk though all Attribute elements and remove the expired ones
00265                         {
00266                                 std::set<Attribute>::const_iterator iter = _attr_list.begin();
00267                                 while ( iter != _attr_list.end() )
00268                                 {
00269                                         const Attribute &attr = (*iter);
00270                                         if ((attr.expire > 0) && (attr.expire < ct))
00271                                         {
00272                                                 // remove this attribute
00273                                                 _attr_list.erase(iter++);
00274                                         }
00275                                         else
00276                                         {
00277                                                 iter++;
00278                                         }
00279                                 }
00280                         }
00281 
00282                         // walk though all URI elements and remove the expired ones
00283                         {
00284                                 std::set<URI>::const_iterator iter = _uri_list.begin();
00285                                 while ( iter != _uri_list.end() )
00286                                 {
00287                                         const URI &u = (*iter);
00288                                         if ((u.expire > 0) && (u.expire < ct))
00289                                         {
00290                                                 // remove this attribute
00291                                                 _uri_list.erase(iter++);
00292                                         }
00293                                         else
00294                                         {
00295                                                 iter++;
00296                                         }
00297                                 }
00298                         }
00299 
00300                         return (_attr_list.empty() && _uri_list.empty());
00301                 }
00302 
00303                 const Node& Node::operator+=(const Node &other)
00304                 {
00305                         for (std::set<Attribute>::const_iterator iter = other._attr_list.begin(); iter != other._attr_list.end(); iter++)
00306                         {
00307                                 const Attribute &attr = (*iter);
00308                                 add(attr);
00309                         }
00310 
00311                         for (std::set<URI>::const_iterator iter = other._uri_list.begin(); iter != other._uri_list.end(); iter++)
00312                         {
00313                                 const URI &u = (*iter);
00314                                 add(u);
00315                         }
00316 
00317                         return (*this);
00318                 }
00319 
00320                 const Node& Node::operator-=(const Node &other)
00321                 {
00322                         for (std::set<Attribute>::const_iterator iter = other._attr_list.begin(); iter != other._attr_list.end(); iter++)
00323                         {
00324                                 const Attribute &attr = (*iter);
00325                                 remove(attr);
00326                         }
00327 
00328                         for (std::set<URI>::const_iterator iter = other._uri_list.begin(); iter != other._uri_list.end(); iter++)
00329                         {
00330                                 const URI &u = (*iter);
00331                                 remove(u);
00332                         }
00333 
00334                         return (*this);
00335                 }
00336 
00337                 bool Node::operator==(const dtn::data::EID &other) const
00338                 {
00339                         return (other == _id);
00340                 }
00341 
00342                 bool Node::operator==(const Node &other) const
00343                 {
00344                         return (other._id == _id);
00345                 }
00346 
00347                 bool Node::operator<(const Node &other) const
00348                 {
00349                         if (_id < other._id ) return true;
00350 
00351                         return false;
00352                 }
00353 
00354                 std::string Node::toString() const
00355                 {
00356                         std::stringstream ss; ss << getEID().getString();
00357                         return ss.str();
00358                 }
00359 
00360                 bool Node::doConnectImmediately() const
00361                 {
00362                         return _connect_immediately;
00363                 }
00364 
00365                 void Node::setConnectImmediately(bool val)
00366                 {
00367                         _connect_immediately = val;
00368                 }
00369 
00370                 bool Node::isAvailable() const
00371                 {
00372                         return !_uri_list.empty();
00373                 }
00374 
00375                 std::ostream& operator<<(std::ostream &stream, const Node &node)
00376                 {
00377                         stream << "Node: " << node._id.getString() << " [ ";
00378                         for (std::set<Node::Attribute>::const_iterator iter = node._attr_list.begin(); iter != node._attr_list.end(); iter++)
00379                         {
00380                                 const Node::Attribute &attr = (*iter);
00381                                 stream << attr << "#expire=" << attr.expire << "; ";
00382                         }
00383 
00384                         for (std::set<Node::URI>::const_iterator iter = node._uri_list.begin(); iter != node._uri_list.end(); iter++)
00385                         {
00386                                 const Node::URI &u = (*iter);
00387                                 stream << u << "#expire=" << u.expire << "; ";
00388                         }
00389                         stream << " ]";
00390 
00391                         return stream;
00392                 }
00393         }
00394 }