00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "Connectivity.h"
00019 #include "Node.h"
00020 #include "SimEvent.h"
00021 #include <oasys/util/OptParser.h>
00022 #include <oasys/util/StringBuffer.h>
00023
00024 #include "SimConvergenceLayer.h"
00025 #include "contacts/ContactManager.h"
00026 #include "contacts/Link.h"
00027 #include "naming/EndpointID.h"
00028
00029
00030
00031 namespace dtnsim {
00032
00033 Connectivity* Connectivity::instance_(NULL);
00034 std::string Connectivity::type_("");
00035
00036 Connectivity::Connectivity()
00037 : Logger("Connectivity", "/sim/conn")
00038 {
00039 }
00040
00041 Connectivity*
00042 Connectivity::create_conn()
00043 {
00044 ASSERT(type_ != "");
00045
00046 if (type_ == "static") {
00047
00048 return new Connectivity();
00049 } else {
00050 log_crit_p("/connectivity", "invalid connectivity module type %s",
00051 type_.c_str());
00052 return NULL;
00053 }
00054 }
00055
00059 bool
00060 ConnState::parse_bw(const char* bw_str, int* bw)
00061 {
00062 char* end;
00063 *bw = 0;
00064 *bw = strtoul(bw_str, &end, 10);
00065
00066 if (end == bw_str)
00067 return false;
00068
00069 if (*end == '\0') {
00070 return true;
00071
00072 } else if (!strcmp(end, "bps")) {
00073 return true;
00074
00075 } else if (!strcmp(end, "kbps")) {
00076 *bw = *bw * 1000;
00077 return true;
00078
00079 } else if (!strcmp(end, "Mbps")) {
00080 *bw = *bw * 1000000;
00081 return true;
00082
00083 } else {
00084 return false;
00085 }
00086 }
00087
00091 bool
00092 ConnState::parse_time(const char* time_str, int* time)
00093 {
00094 char* end;
00095 *time = 0;
00096 *time = strtoul(time_str, &end, 10);
00097
00098 if (end == time_str)
00099 return false;
00100
00101 if (*end == '\0') {
00102 return true;
00103
00104 } else if (!strcmp(end, "ms")) {
00105 return true;
00106
00107 } else if (!strcmp(end, "s")) {
00108 *time = *time * 1000;
00109 return true;
00110
00111 } else if (!strcmp(end, "min")) {
00112 *time = *time * 1000 * 60;
00113 return true;
00114
00115 } else if (!strcmp(end, "hr")) {
00116 *time = *time * 1000 * 3600;
00117 return true;
00118
00119 } else {
00120 return false;
00121 }
00122 }
00123
00128 bool
00129 ConnState::parse_options(int argc, const char** argv, const char** invalidp)
00130 {
00131 oasys::OptParser p;
00132 std::string bw_str;
00133 std::string latency_str;
00134
00135 p.addopt(new oasys::StringOpt("bw", &bw_str));
00136 p.addopt(new oasys::StringOpt("latency", &latency_str));
00137
00138 if (! p.parse(argc, argv, invalidp)) {
00139 return false;
00140 }
00141
00142 if (bw_str != "" && !parse_bw(bw_str.c_str(), &bw_)) {
00143 *invalidp = strdup(bw_str.c_str());
00144 return false;
00145 }
00146
00147 if (latency_str != "" && !parse_time(latency_str.c_str(), &latency_)) {
00148 *invalidp = strdup(latency_str.c_str());
00149 return false;
00150 }
00151
00152 return true;
00153 }
00154
00155
00159 void
00160 Connectivity::set_state(const char* n1, const char* n2, const ConnState& s)
00161 {
00162 oasys::StringBuffer key("%s,%s", n1, n2);
00163 StateTable::iterator iter = state_.find(key.c_str());
00164 if (iter != state_.end()) {
00165 iter->second = s;
00166 } else {
00167 state_[key.c_str()] = s;
00168 }
00169
00170 log_debug("set state %s,%s: %s bw=%d latency=%d",
00171 n1, n2, s.open_ ? "up" : "down", s.bw_, s.latency_);
00172 }
00173
00177 const ConnState*
00178 Connectivity::lookup(Node* n1, Node* n2)
00179 {
00180 oasys::StringBuffer buf("%s,%s", n1->name(), n2->name());
00181
00182 return NULL;
00183 }
00184
00188 void
00189 Connectivity::process(SimEvent *e)
00190 {
00191 if (e->type() != SIM_CONNECTIVITY) {
00192 PANIC("no Node handler for event %s", e->type_str());
00193 }
00194
00195 SimConnectivityEvent* ce = (SimConnectivityEvent*)e;
00196
00197 set_state(ce->n1_.c_str(), ce->n2_.c_str(), *ce->state_);
00198
00199 delete ce->state_;
00200 }
00201
00205 bool
00206 Connectivity::exec(int argc, const char** argv)
00207 {
00208 (void)argc;
00209 (void)argv;
00210 return false;
00211 }
00212
00213
00214 }