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