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/Utils.h>
00004 #include <ibrcommon/Logger.h>
00005 
00006 #include <iostream>
00007 #include <sstream>
00008 
00009 using namespace std;
00010 
00011 namespace dtn
00012 {
00013         namespace core
00014         {
00015                 Node::URI::URI(const std::string &uri, const Protocol p)
00016                  : protocol(p), value(uri)
00017                 {
00018                 }
00019 
00020                 Node::URI::~URI()
00021                 {
00022                 }
00023 
00024                 void Node::URI::decode(std::string &address, unsigned int &port) const
00025                 {
00026                         // parse parameters
00027                         std::vector<string> parameters = dtn::utils::Utils::tokenize(";", value);
00028                         std::vector<string>::const_iterator param_iter = parameters.begin();
00029 
00030                         while (param_iter != parameters.end())
00031                         {
00032                                 std::vector<string> p = dtn::utils::Utils::tokenize("=", (*param_iter));
00033 
00034                                 if (p[0].compare("ip") == 0)
00035                                 {
00036                                         address = p[1];
00037                                 }
00038 
00039                                 if (p[0].compare("port") == 0)
00040                                 {
00041                                         std::stringstream port_stream;
00042                                         port_stream << p[1];
00043                                         port_stream >> port;
00044                                 }
00045 
00046                                 param_iter++;
00047                         }
00048                 }
00049 
00050                 bool Node::URI::operator<(const URI &other) const
00051                 {
00052                         if (protocol < other.protocol) return true;
00053                         if (protocol != other.protocol) return false;
00054 
00055                         return (value < other.value);
00056                 }
00057 
00058                 bool Node::URI::operator==(const URI &other) const
00059                 {
00060                         return ((protocol == other.protocol) && (value == other.value));
00061                 }
00062 
00063                 bool Node::URI::operator==(const Node::Protocol &p) const
00064                 {
00065                         return (protocol == p);
00066                 }
00067 
00068                 Node::Attribute::Attribute(const std::string &n, const std::string &v)
00069                  : name(n), value(v)
00070                 {
00071                 }
00072 
00073                 Node::Attribute::~Attribute()
00074                 {
00075                 }
00076 
00077                 bool Node::Attribute::operator<(const Attribute &other) const
00078                 {
00079                         if (name < other.name) return true;
00080                         if (name != other.name) return false;
00081 
00082                         return (value < other.value);
00083                 }
00084 
00085                 bool Node::Attribute::operator==(const Attribute &other) const
00086                 {
00087                         return ((name == other.name) && (value == other.value));
00088                 }
00089 
00090                 bool Node::Attribute::operator==(const std::string &n) const
00091                 {
00092                         return (name == n);
00093                 }
00094 
00095                 Node::Node(Node::Type type, unsigned int rtt)
00096                 : _connect_immediately(false), _description(), _id("dtn:none"), _timeout(5), _rtt(rtt), _type(type)
00097                 {
00098                 }
00099 
00100                 Node::Node(const dtn::data::EID &id, Node::Type type, unsigned int rtt)
00101                 : _connect_immediately(false), _description(), _id(id), _timeout(5), _rtt(rtt), _type(type)
00102                 {
00103 
00104                 }
00105 
00106                 Node::~Node()
00107                 {
00108                 }
00109 
00110                 std::string Node::getTypeName(Node::Type type)
00111                 {
00112                         switch (type)
00113                         {
00114                         case Node::NODE_FLOATING:
00115                                 return "floating";
00116 
00117                         case Node::NODE_PERMANENT:
00118                                 return "permanent";
00119 
00120                         case Node::NODE_CONNECTED:
00121                                 return "connected";
00122                         }
00123 
00124                         return "unknown";
00125                 }
00126 
00127                 std::string Node::getProtocolName(Node::Protocol proto)
00128                 {
00129                         switch (proto)
00130                         {
00131                         case Node::CONN_UNSUPPORTED:
00132                                 return "unsupported";
00133 
00134                         case Node::CONN_UNDEFINED:
00135                                 return "undefined";
00136 
00137                         case Node::CONN_UDPIP:
00138                                 return "UDP";
00139 
00140                         case Node::CONN_TCPIP:
00141                                 return "TCP";
00142 
00143                         case Node::CONN_ZIGBEE:
00144                                 return "ZigBee";
00145 
00146                         case Node::CONN_BLUETOOTH:
00147                                 return "Bluetooth";
00148 
00149                         case Node::CONN_HTTP:
00150                                 return "HTTP";
00151                         }
00152 
00153                         return "unknown";
00154                 }
00155 
00156                 Node::Type Node::getType() const
00157                 {
00158                         return _type;
00159                 }
00160 
00161                 void Node::setType(Node::Type type)
00162                 {
00163                         _type = type;
00164                 }
00165 
00166                 bool Node::has(Node::Protocol proto) const
00167                 {
00168                         for (std::set<URI>::const_iterator iter = _uri_list.begin(); iter != _uri_list.end(); iter++)
00169                         {
00170                                 if ((*iter) == proto) return true;
00171                         }
00172                         return false;
00173                 }
00174 
00175                 bool Node::has(const std::string &name) const
00176                 {
00177                         for (std::set<Attribute>::const_iterator iter = _attr_list.begin(); iter != _attr_list.end(); iter++)
00178                         {
00179                                 if ((*iter) == name) return true;
00180                         }
00181                         return false;
00182                 }
00183 
00184                 void Node::add(const URI &u)
00185                 {
00186                         _uri_list.insert(u);
00187                 }
00188 
00189                 void Node::add(const Attribute &attr)
00190                 {
00191                         _attr_list.insert(attr);
00192                 }
00193 
00194                 void Node::remove(const URI &u)
00195                 {
00196                         _uri_list.erase(u);
00197                 }
00198 
00199                 void Node::remove(const Attribute &attr)
00200                 {
00201                         _attr_list.erase(attr);
00202                 }
00203 
00204                 void Node::clear()
00205                 {
00206                         _uri_list.clear();
00207                         _attr_list.clear();
00208                 }
00209 
00210                 std::list<Node::URI> Node::get(Node::Protocol proto) const
00211                 {
00212                         std::list<URI> ret;
00213                         for (std::set<URI>::const_iterator iter = _uri_list.begin(); iter != _uri_list.end(); iter++)
00214                         {
00215                                 if ((*iter) == proto) ret.push_back(*iter);
00216                         }
00217                         return ret;
00218                 }
00219 
00220                 std::list<Node::Attribute> Node::get(const std::string &name) const
00221                 {
00222                         std::list<Attribute> ret;
00223                         for (std::set<Attribute>::const_iterator iter = _attr_list.begin(); iter != _attr_list.end(); iter++)
00224                         {
00225                                 if ((*iter) == name) ret.push_back(*iter);
00226                         }
00227                         return ret;
00228                 }
00229 
00230                 void Node::setDescription(string description)
00231                 {
00232                         _description = description;
00233                 }
00234 
00235                 string Node::getDescription() const
00236                 {
00237                         return _description;
00238                 }
00239 
00240                 void Node::setEID(const dtn::data::EID &id)
00241                 {
00242                         _id = id;
00243                 }
00244 
00245                 const dtn::data::EID& Node::getEID() const
00246                 {
00247                         return _id;
00248                 }
00249 
00250                 void Node::setTimeout(int timeout)
00251                 {
00252                         _timeout = timeout;
00253                 }
00254 
00255                 int Node::getTimeout() const
00256                 {
00257                         return _timeout;
00258                 }
00259 
00260                 unsigned int Node::getRoundTripTime() const
00261                 {
00262                         return _rtt;
00263                 }
00264 
00265                 bool Node::decrementTimeout(int step)
00266                 {
00267                         if (_type == NODE_PERMANENT) return true;
00268 
00269                         if (_timeout <= 0) return false;
00270                         _timeout -= step;
00271                         return true;
00272                 }
00273 
00274                 bool Node::operator==(const Node &other) const
00275                 {
00276                         return (other._id == _id);
00277                 }
00278 
00279                 bool Node::operator<(const Node &other) const
00280                 {
00281                         if (_id < other._id ) return true;
00282 
00283                         return false;
00284                 }
00285 
00286                 std::string Node::toString() const
00287                 {
00288                         std::stringstream ss; ss << getEID().getString() << " (" << Node::getTypeName(getType()) << ")";
00289                         return ss.str();
00290                 }
00291 
00292                 bool Node::doConnectImmediately() const
00293                 {
00294                         return _connect_immediately;
00295                 }
00296 
00297                 void Node::setConnectImmediately(bool val)
00298                 {
00299                         _connect_immediately = val;
00300                 }
00301         }
00302 }