Connectivity.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2005-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
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         // just the base class
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') { // no specification means straight bps
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') { // no specification means ms
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()); // leak!
00144         return false;
00145     }
00146 
00147     if (latency_str != "" && !parse_time(latency_str.c_str(), &latency_)) {
00148         *invalidp = strdup(latency_str.c_str()); // leak!
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_; // XXX/demmer yuck
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 } // namespace dtnsim

Generated on Thu Jun 7 16:56:49 2007 for DTN Reference Implementation by  doxygen 1.5.1