00001 #include "core/Node.h"
00002 #include "net/ConvergenceLayer.h"
00003 #include <ibrcommon/Logger.h>
00004
00005 #include <iostream>
00006
00007 using namespace std;
00008
00009 namespace dtn
00010 {
00011 namespace core
00012 {
00013 Node::Node(Node::Type type, unsigned int rtt)
00014 : _address(), _description(), _id("dtn:none"), _timeout(5), _rtt(rtt), _type(type), _port(4556), _protocol(CONN_UNDEFINED)
00015 {
00016 }
00017
00018 Node::Node(dtn::data::EID id, Node::Protocol proto, Node::Type type, unsigned int rtt)
00019 : _address(), _description(), _id(id), _timeout(5), _rtt(rtt), _type(type), _port(4556), _protocol(proto)
00020 {
00021
00022 }
00023
00024 Node::~Node()
00025 {
00026 }
00027
00028 std::string Node::getTypeName(Node::Type type)
00029 {
00030 switch (type)
00031 {
00032 case Node::NODE_FLOATING:
00033 return "floating";
00034
00035 case Node::NODE_PERMANENT:
00036 return "permanent";
00037 }
00038
00039 return "unknown";
00040 }
00041
00042 std::string Node::getProtocolName(Node::Protocol proto)
00043 {
00044 switch (proto)
00045 {
00046 case Node::CONN_UNSUPPORTED:
00047 return "unsupported";
00048
00049 case Node::CONN_UNDEFINED:
00050 return "undefined";
00051
00052 case Node::CONN_UDPIP:
00053 return "UDP";
00054
00055 case Node::CONN_TCPIP:
00056 return "TCP";
00057
00058 case Node::CONN_ZIGBEE:
00059 return "ZigBee";
00060
00061 case Node::CONN_BLUETOOTH:
00062 return "Bluetooth";
00063
00064 case Node::CONN_HTTP:
00065 return "HTTP";
00066 }
00067
00068 return "unknown";
00069 }
00070
00071 Node::Type Node::getType() const
00072 {
00073 return _type;
00074 }
00075
00076 void Node::setProtocol(Node::Protocol protocol)
00077 {
00078 _protocol = protocol;
00079 }
00080
00081 Node::Protocol Node::getProtocol() const
00082 {
00083 return _protocol;
00084 }
00085
00086 void Node::setAddress(string address)
00087 {
00088 _address = address;
00089 }
00090
00091 string Node::getAddress() const
00092 {
00093 return _address;
00094 }
00095
00096 void Node::setPort(unsigned int port)
00097 {
00098 _port = port;
00099 }
00100
00101 unsigned int Node::getPort() const
00102 {
00103 return _port;
00104 }
00105
00106 void Node::setDescription(string description)
00107 {
00108 _description = description;
00109 }
00110
00111 string Node::getDescription() const
00112 {
00113 return _description;
00114 }
00115
00116
00117 void Node::setURI(string uri)
00118 {
00119 _id = dtn::data::EID(uri);
00120 }
00121
00122 string Node::getURI() const
00123 {
00124 return _id.getString();
00125 }
00126
00127
00128 void Node::setTimeout(int timeout)
00129 {
00130 _timeout = timeout;
00131 }
00132
00133 int Node::getTimeout() const
00134 {
00135 return _timeout;
00136 }
00137
00138 unsigned int Node::getRoundTripTime() const
00139 {
00140 return _rtt;
00141 }
00142
00143 bool Node::decrementTimeout(int step)
00144 {
00145 if (_type == NODE_PERMANENT) return true;
00146
00147 if (_timeout <= 0) return false;
00148 _timeout -= step;
00149 return true;
00150 }
00151
00152 bool Node::operator==(const Node &other) const
00153 {
00154 return (other._id == _id) && (other._protocol == _protocol);
00155 }
00156
00157 bool Node::operator<(const Node &other) const
00158 {
00159
00160
00161
00162
00163
00164 if (_protocol < other._protocol) return true;
00165 if (_protocol != other._protocol) return false;
00166 if (_id < other._id ) return true;
00167
00168 return false;
00169 }
00170
00171 std::string Node::toString() const
00172 {
00173 std::stringstream ss; ss << getURI() << " (" << Node::getTypeName(getType()) << ", " << Node::getProtocolName(getProtocol()) << ", " << getAddress() << ", " << getPort() << ")";
00174 return ss.str();
00175 }
00176 }
00177 }