ConnCommand.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 <stdlib.h>
00019 
00020 #include "Connectivity.h"
00021 #include "ConnCommand.h"
00022 #include "Simulator.h"
00023 #include "Topology.h"
00024 
00025 namespace dtnsim {
00026 
00027 ConnCommand::ConnCommand()
00028     : TclCommand("conn")
00029 {
00030     bind_var(new oasys::StringOpt("type", &Connectivity::type_,
00031                                   "type", "Connectivity type."));
00032     
00033     add_to_help("up", "Take connection up XXX");
00034     add_to_help("down", "Take connection down XXX");
00035 }
00036 
00037 int
00038 ConnCommand::exec(int argc, const char** argv, Tcl_Interp* tclinterp)
00039 {
00040     (void)tclinterp;
00041     
00042     if (argc < 3) {
00043         wrong_num_args(argc, argv, 2, 4, INT_MAX);
00044         return TCL_ERROR;
00045     }
00046     
00047     // pull out the time
00048     char* end;
00049     double time = strtod(argv[1], &end);
00050     if (*end != '\0') {
00051         resultf("time value '%s' invalid", argv[1]);
00052         return TCL_ERROR;
00053     }
00054 
00055     const char* cmd = argv[2];
00056 
00057     Connectivity* conn = Connectivity::instance();
00058 
00059     if (!strcmp(cmd, "up") || !strcmp(cmd, "down")) {
00060         // conn <time> <up|down> <n1> <n2> <args>
00061         if (argc < 5) {
00062             wrong_num_args(argc, argv, 2, 5, INT_MAX);
00063             return TCL_ERROR;
00064         }
00065 
00066         const char* n1_name = argv[3];
00067         const char* n2_name = argv[4];
00068 
00069         if (strcmp(n1_name, "*") != 0 &&
00070             Topology::find_node(n1_name) == NULL)
00071         {
00072             resultf("invalid node or wildcard '%s'", n1_name);
00073             return TCL_ERROR;
00074         }
00075         
00076         if (strcmp(n2_name, "*") != 0 &&
00077             Topology::find_node(n2_name) == NULL)
00078         {
00079             resultf("invalid node or wildcard '%s'", n2_name);
00080             return TCL_ERROR;
00081         }
00082         
00083         ConnState* s = new ConnState();
00084         const char* invalid;
00085         if (! s->parse_options(argc - 5, argv + 5, &invalid)) {
00086             resultf("invalid option '%s'", invalid);
00087             delete s;
00088             return TCL_ERROR;
00089         }
00090         
00091         s->open_ = !strcmp(cmd, "up");
00092 
00093         Simulator::post(
00094             new SimConnectivityEvent(time, conn, n1_name, n2_name, s));
00095 
00096         return TCL_OK;
00097 
00098     } else {
00099         // dispatch to the connectivity module itself
00100         if (! conn->exec(argc - 3, argv + 3)) {
00101             resultf("conn: error handling command");
00102             return TCL_ERROR;
00103         }
00104 
00105 
00106         return TCL_OK;
00107     }
00108     
00109     resultf("conn: unsupported subcommand %s", cmd);
00110     return TCL_ERROR;
00111 }
00112 
00113 
00114 } // namespace dtnsim

Generated on Sat Sep 8 08:43:26 2007 for DTN Reference Implementation by  doxygen 1.5.3