00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include <stdlib.h>
00022
00023 #include "routing/BundleRouter.h"
00024
00025 #include "Node.h"
00026 #include "NodeCommand.h"
00027 #include "SimCommand.h"
00028 #include "Simulator.h"
00029 #include "Topology.h"
00030
00031 using namespace dtn;
00032
00033 namespace dtnsim {
00034
00035 SimCommand::SimCommand()
00036 : TclCommand("sim")
00037 {
00038 bind_var(new oasys::DoubleOpt("runtill", &Simulator::runtill_,
00039 "steps", "Run simulation for this many steps"));
00040 bind_var(new oasys::StringOpt("route_type", &BundleRouter::config_.type_,
00041 "type", "What type of router to use"));
00042 }
00043
00044 int
00045 SimCommand::exec(int argc, const char** argv, Tcl_Interp* tclinterp)
00046 {
00047 (void)tclinterp;
00048 if (argc < 2) {
00049 wrong_num_args(argc, argv, 2, 2, INT_MAX);
00050 return TCL_ERROR;
00051 }
00052
00053 const char* cmd = argv[1];
00054 if (strcmp(cmd, "create_node") == 0) {
00055
00056 if (argc != 3) {
00057 wrong_num_args(argc, argv, 2, 3, 3);
00058 return TCL_ERROR;
00059 }
00060
00061 const char* name = argv[2];
00062
00063
00064 oasys::TclCommandInterp* interp = oasys::TclCommandInterp::instance();
00065 if (interp->lookup(name)) {
00066 resultf("error creating node %s: tcl command already exists",
00067 name);
00068 return TCL_ERROR;
00069 }
00070
00071 Node* node = Topology::create_node(name);
00072
00073 NodeCommand* cmd = new NodeCommand(node);
00074 interp->reg(cmd);
00075
00076 return TCL_OK;
00077
00078 } else if (strcmp(cmd, "at") == 0) {
00079
00080 if (argc < 4) {
00081 wrong_num_args(argc, argv, 2, 4, INT_MAX);
00082 return TCL_ERROR;
00083 }
00084
00085 char* end;
00086 double time;
00087 if (!strcmp(argv[2], "exit")) {
00088 time = -1;
00089 } else {
00090 time = strtod(argv[2], &end);
00091 if (*end != '\0') {
00092 resultf("time value '%s' invalid", argv[1]);
00093 return TCL_ERROR;
00094 }
00095 }
00096
00097 SimAtEvent* e = new SimAtEvent(time, Simulator::instance());
00098 e->objc_ = argc - 3;
00099 for (int i = 0; i < e->objc_; ++i) {
00100 e->objv_[i] = Tcl_NewStringObj(argv[i+3], -1);
00101 Tcl_IncrRefCount(e->objv_[i]);
00102 }
00103
00104 if (time == -1) {
00105 Simulator::instance()->set_exit_event(e);
00106 } else {
00107 Simulator::post(e);
00108 }
00109 return TCL_OK;
00110
00111 } else if (strcmp(cmd, "run") == 0) {
00112 Simulator::instance()->run();
00113 return TCL_OK;
00114
00115 } else if (strcmp(cmd, "run_events") == 0) {
00116 Simulator::instance()->run_node_events();
00117 return TCL_OK;
00118
00119 } else if (strcmp(cmd, "pause") == 0) {
00120 Simulator::instance()->pause();
00121 return TCL_OK;
00122 } else if (strcmp(cmd, "nodes") == 0) {
00123 oasys::StringBuffer buf;
00124 Topology::NodeTable* nodes = Topology::node_table();
00125 for (Topology::NodeTable::iterator i = nodes->begin();
00126 i != nodes->end(); ++i)
00127 {
00128 buf.appendf("%s ", i->second->name());
00129 }
00130 set_result(buf.c_str());
00131 return TCL_OK;
00132 }
00133
00134 resultf("sim: unsupported subcommand %s", cmd);
00135 return TCL_ERROR;
00136 }
00137
00138
00139 }