sim/SimCommand.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2004-2006 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #  include <dtn-config.h>
       
    19 #endif
       
    20 
       
    21 #include <stdlib.h>
       
    22 
       
    23 #include "routing/BundleRouter.h"
       
    24 
       
    25 #include "Node.h"
       
    26 #include "NodeCommand.h"
       
    27 #include "SimCommand.h"
       
    28 #include "Simulator.h"
       
    29 #include "Topology.h"
       
    30 
       
    31 using namespace dtn;
       
    32 
       
    33 namespace dtnsim {
       
    34 
       
    35 SimCommand::SimCommand()
       
    36     : TclCommand("sim")
       
    37 {
       
    38     bind_var(new oasys::DoubleOpt("runtill", &Simulator::runtill_,
       
    39                                   "steps", "Run simulation for this many steps"));
       
    40     bind_var(new oasys::StringOpt("route_type", &BundleRouter::config_.type_,
       
    41                                   "type", "What type of router to use"));
       
    42 }
       
    43 
       
    44 int
       
    45 SimCommand::exec(int argc, const char** argv, Tcl_Interp* tclinterp)
       
    46 {
       
    47     (void)tclinterp;
       
    48     if (argc < 2) {
       
    49         wrong_num_args(argc, argv, 2, 2, INT_MAX);
       
    50         return TCL_ERROR;
       
    51     }
       
    52 
       
    53     const char* cmd = argv[1];
       
    54     if (strcmp(cmd, "create_node") == 0) {
       
    55         // sim create_node <name>
       
    56         if (argc != 3) {
       
    57             wrong_num_args(argc, argv, 2, 3, 3);
       
    58             return TCL_ERROR;
       
    59         }
       
    60 
       
    61         const char* name = argv[2];
       
    62 
       
    63         // make sure no tcl command already exists with the given name
       
    64         oasys::TclCommandInterp* interp = oasys::TclCommandInterp::instance();
       
    65         if (interp->lookup(name)) {
       
    66             resultf("error creating node %s: tcl command already exists",
       
    67                     name);
       
    68             return TCL_ERROR;
       
    69         }
       
    70         
       
    71         Node* node = Topology::create_node(name);
       
    72 
       
    73         NodeCommand* cmd = new NodeCommand(node);
       
    74         interp->reg(cmd);
       
    75         
       
    76         return TCL_OK;
       
    77 
       
    78     } else if (strcmp(cmd, "at") == 0) {
       
    79         // sim at [<time>|exit] <cmd...>
       
    80         if (argc < 4) {
       
    81             wrong_num_args(argc, argv, 2, 4, INT_MAX);
       
    82             return TCL_ERROR;
       
    83         }
       
    84 
       
    85         char* end;
       
    86         double time;
       
    87         if (!strcmp(argv[2], "exit")) {
       
    88             time = -1;
       
    89         } else {
       
    90             time = strtod(argv[2], &end);
       
    91             if (*end != '\0') {
       
    92                 resultf("time value '%s' invalid", argv[1]);
       
    93                 return TCL_ERROR;
       
    94             }
       
    95         }
       
    96         
       
    97         SimAtEvent* e = new SimAtEvent(time, Simulator::instance());
       
    98         e->objc_ = argc - 3;
       
    99         for (int i = 0; i < e->objc_; ++i) {
       
   100             e->objv_[i] = Tcl_NewStringObj(argv[i+3], -1);
       
   101 	    Tcl_IncrRefCount(e->objv_[i]);
       
   102         }
       
   103 
       
   104         if (time == -1) {
       
   105             Simulator::instance()->set_exit_event(e);
       
   106         } else {
       
   107             Simulator::post(e);
       
   108         }
       
   109         return TCL_OK;
       
   110         
       
   111     } else if (strcmp(cmd, "run") == 0) {
       
   112         Simulator::instance()->run();
       
   113         return TCL_OK;
       
   114 
       
   115     } else if (strcmp(cmd, "run_events") == 0) {
       
   116         Simulator::instance()->run_node_events();
       
   117         return TCL_OK;
       
   118 
       
   119     } else if (strcmp(cmd, "pause") == 0) {
       
   120         Simulator::instance()->pause();
       
   121         return TCL_OK;
       
   122     } else if (strcmp(cmd, "nodes") == 0) {
       
   123         oasys::StringBuffer buf;
       
   124         Topology::NodeTable* nodes = Topology::node_table();
       
   125         for (Topology::NodeTable::iterator i = nodes->begin();
       
   126              i != nodes->end(); ++i)
       
   127         {
       
   128             buf.appendf("%s ", i->second->name());
       
   129         }
       
   130         set_result(buf.c_str());
       
   131         return TCL_OK;
       
   132     }        
       
   133 
       
   134     resultf("sim: unsupported subcommand %s", cmd);
       
   135     return TCL_ERROR;
       
   136 }
       
   137 
       
   138 
       
   139 } // namespace dtnsim