00001 #ifndef __LINK_DESCRIPTOR_H 00002 #define __LINK_DESCRIPTOR_H 00003 00004 #include <iostream> 00005 #include <sstream> 00006 #include <ctime> 00007 #include <deque> 00008 #include <boost/foreach.hpp> 00009 00010 #include "ariba/utility/messages.h" 00011 #include "ariba/utility/types.h" 00012 #include "ariba/CommunicationListener.h" 00013 00014 namespace ariba { 00015 class CommunicationListener; 00016 } 00017 00018 using std::deque; 00019 using ariba::utility::Message; 00020 using ariba::utility::NodeID; 00021 using ariba::utility::SpoVNetID; 00022 using ariba::utility::ServiceID; 00023 using ariba::utility::LinkID; 00024 using ariba::CommunicationListener; 00025 00026 namespace ariba { 00027 namespace overlay { 00028 00029 class LinkDescriptor; 00030 00031 std::ostream& operator<<(std::ostream& s, const LinkDescriptor* ld ); 00032 std::ostream& operator<<(std::ostream& s, const LinkDescriptor& ld ); 00033 00035 class LinkDescriptor { 00036 public: 00037 // ctor 00038 LinkDescriptor() { 00039 // default values 00040 this->up = false; 00041 this->fromRemote = false; 00042 this->remoteNode = NodeID::UNSPECIFIED; 00043 this->overlayId = LinkID::create(); 00044 this->communicationUp = false; 00045 this->communicationId = LinkID::UNSPECIFIED; 00046 this->keepAliveTime = time(NULL); 00047 this->keepAliveMissed = 0; 00048 this->relaying = false; 00049 this->timeRelaying = time(NULL); 00050 this->dropAfterRelaying = false; 00051 this->service = ServiceID::UNSPECIFIED; 00052 this->listener = &CommunicationListener::DEFAULT; 00053 this->relayed = false; 00054 this->remoteLink = LinkID::UNSPECIFIED; 00055 this->autolink = false; 00056 this->lastuse = time(NULL); 00057 } 00058 00059 // dtor 00060 ~LinkDescriptor() { 00061 flushQueue(); 00062 } 00063 00064 // general information about the link -------------------------------------- 00065 bool up; 00066 bool fromRemote; 00067 NodeID remoteNode; 00068 bool isVital() { 00069 return up && keepAliveMissed == 0; 00070 } 00071 bool isDirectVital() { 00072 return isVital() && communicationUp && !relayed; 00073 } 00074 00075 00076 // link identifiers -------------------------------------------------------- 00077 LinkID overlayId; 00078 LinkID communicationId; 00079 bool communicationUp; 00080 00081 // link alive information -------------------------------------------------- 00082 time_t keepAliveTime; 00083 int keepAliveMissed; 00084 void setAlive() { 00085 keepAliveMissed = 0; 00086 keepAliveTime = time(NULL); 00087 } 00088 00089 // relay information ------------------------------------------------------- 00090 bool relayed; 00091 LinkID remoteLink; 00092 vector<NodeID> routeRecord; 00093 00094 // relay state ------------------------------------------------------------- 00095 bool relaying; 00096 bool dropAfterRelaying; 00097 time_t timeRelaying; 00098 void setRelaying() { 00099 relaying = true; 00100 timeRelaying = time(NULL); 00101 } 00102 00103 // owner ------------------------------------------------------------------- 00104 ServiceID service; 00105 CommunicationListener* listener; 00106 00107 // auto links -------------------------------------------------------------- 00108 bool autolink; 00109 time_t lastuse; 00110 deque<Message*> messageQueue; 00111 void setAutoUsed() { 00112 if (autolink) lastuse = time(NULL); 00113 } 00115 void flushQueue() { 00116 BOOST_FOREACH( Message* msg, messageQueue ) delete msg; 00117 messageQueue.clear(); 00118 } 00119 00120 // string representation --------------------------------------------------- 00121 std::string to_string() const { 00122 std::ostringstream s; 00123 s << "up=" << up << " "; 00124 s << "init=" << !fromRemote << " "; 00125 s << "id=" << overlayId.toString().substr(0,4) << " "; 00126 s << "serv=" << service.toString() << " "; 00127 s << "node=" << remoteNode.toString().substr(0,4) << " "; 00128 s << "relaying=" << relaying << " "; 00129 s << "miss=" << keepAliveMissed << " "; 00130 s << "auto=" << autolink << " "; 00131 if ( relayed ) { 00132 s << "| Relayed: "; 00133 s << "remote link=" << remoteLink.toString().substr(0,4) << " "; 00134 if (routeRecord.size()>0) { 00135 s << "route record="; 00136 for (size_t i=0; i<routeRecord.size(); i++) 00137 s << routeRecord[i].toString().substr(0,4) << " "; 00138 } 00139 } else { 00140 s << "| Direct: "; 00141 s << "using id=" << communicationId.toString().substr(0,4) << " "; 00142 s << "(up=" << communicationUp << ") "; 00143 } 00144 return s.str(); 00145 } 00146 }; 00147 00148 }} // namespace ariba, overlay 00149 00150 #endif // __LINK_DESCRIPTOR_H 00151