Connectivity.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2005 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         // just the base class
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') { // no specification means straight bps
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') { // no specification means ms
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()); // leak!
00158         return false;
00159     }
00160 
00161     if (latency_str != "" && !parse_time(latency_str.c_str(), &latency_)) {
00162         *invalidp = strdup(latency_str.c_str()); // leak!
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_; // XXX/demmer yuck
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 } // namespace dtnsim

Generated on Fri Dec 22 14:47:58 2006 for DTN Reference Implementation by  doxygen 1.5.1