SimCommand.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-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 "NodeCommand.h"
00021 #include "SimCommand.h"
00022 #include "Simulator.h"
00023 #include "Topology.h"
00024 
00025 #include "routing/BundleRouter.h"
00026 
00027 
00028 using namespace dtn;
00029 
00030 namespace dtnsim {
00031 
00032 SimCommand::SimCommand()
00033     : TclCommand("sim")
00034 {
00035     bind_var(new oasys::DoubleOpt("runtill", &Simulator::runtill_,
00036                                   "steps", "Run simulation for this many steps"));
00037     bind_var(new oasys::StringOpt("route_type", &BundleRouter::config_.type_,
00038                                   "type", "What type of router to use"));
00039 }
00040 
00041 int
00042 SimCommand::exec(int argc, const char** argv, Tcl_Interp* tclinterp)
00043 {
00044     (void)tclinterp;
00045     if (argc < 3) {
00046         wrong_num_args(argc, argv, 2, 3, 11);
00047         return TCL_ERROR;
00048     }
00049     
00050     // pull out the time and subcommand
00051     char* end;
00052     double time = strtod(argv[1], &end);
00053     if (*end != '\0') {
00054         resultf("time value '%s' invalid", argv[1]);
00055         return TCL_ERROR;
00056     }
00057     const char* cmd = argv[2];
00058     
00059     if (strcmp(cmd, "create_node") == 0) {
00060         // sim <time> create_node <name>
00061         if (argc < 4) {
00062             wrong_num_args(argc, argv, 2, 4, 4);
00063             return TCL_ERROR;
00064         }
00065 
00066         if (time != 0) {
00067             resultf("all nodes must be created at time 0");
00068             return TCL_ERROR;
00069         }
00070 
00071         const char* name = argv[3];
00072 
00073         // make sure no tcl command already exists with the given name
00074         oasys::TclCommandInterp* interp = oasys::TclCommandInterp::instance();
00075         if (interp->lookup(name)) {
00076             resultf("error creating node %s: tcl command already exists",
00077                     name);
00078             return TCL_ERROR;
00079         }
00080         
00081         Node* node = Topology::create_node(name);
00082 
00083         NodeCommand* cmd = new NodeCommand(node);
00084         interp->reg(cmd);
00085         
00086         return TCL_OK;
00087     }
00088 
00089 /* 
00090     // sim <time> create_contact <id> <src> <dst> <bw> <delay> <isup> <up> <down>
00091     if (strcmp(cmd, "create_contact") == 0) {
00092                 if (argc < 11) {
00093                wrong_num_args(argc, argv, 2, 11, 11);
00094                return TCL_ERROR;
00095                 }
00096 
00097                 int id = atoi(argv[3]) ;
00098                 int src = atoi(argv[4]) ;
00099                 int dst = atoi(argv[5]) ;
00100                 int  bw = atoi(argv[6]) ;
00101                 int delay = atoi(argv[7]) ;
00102                 int isup =  atoi(argv[8]) ;
00103                 int up = atoi(argv[9]) ;
00104                 int down = atoi(argv[10]) ;
00105         
00106                 Topology::create_contact(id,src,dst,bw,delay,isup,up,down);
00107                 log_info("new contact: (%d->%d), param:[%d,%d] \n",src,dst,bw,delay);
00108                    
00109                 return TCL_OK;
00110     }
00111 
00113     // sim <time> cup <contact_id> <>
00114         if (strcmp(cmd, "cup") == 0) {
00115                 int id = atoi(argv[3]) ;
00116                 bool forever = false;
00117                 if (argc == 5) {
00118                         if (atoi(argv[4]) != 0) forever = true;
00119                 }
00120                 
00121                 Event_contact_up* e = 
00122                         new Event_contact_up(time,Topology::contact(id));
00123                 
00124                 e->forever_ = forever;
00125                 Simulator::post(e);
00126                 
00127                 return TCL_OK;
00128     }
00129 /
00130     // sim <time> cdown <contact_id> <>
00131         if (strcmp(cmd, "cdown") == 0) {
00132                 int id = atoi(argv[3]) ;
00133                 bool forever = false;
00134                 if (argc == 5) {
00135                         if (atoi(argv[4]) != 0) forever = true;
00136                 }
00137                 Event_contact_down* e = 
00138                         new Event_contact_down(time,Topology::contact(id));
00139                 e->forever_ = forever;
00140                 Simulator::post(e);
00141                 return TCL_OK;
00142         }
00143         
00144         // sim <time> create_tr <src> <dst> <size> <batch> <reps> <gap>
00145         if (strcmp(cmd, "create_tr") == 0) {
00146                 if (argc < 9) {
00147                         wrong_num_args(argc, argv, 2, 9,9);
00148                         return TCL_ERROR;
00149                 }
00150                 int src = atoi(argv[3]) ;
00151                 int dst = atoi(argv[4]) ;
00152                 int size = atoi(argv[5]) ;
00153                 int batch = atoi(argv[6]) ;
00154                 int reps = atoi(argv[7]) ;
00155                 int gap = atoi(argv[8]) ;
00156                 TrAgent* tr = new TrAgent(time,src,dst,size,batch,reps,gap);
00157                 tr->start();
00158                 log_info("creating traffic btw (src,dst) (%d,%d)",src,dst);
00159                 //return TCL_OK;
00160         }       
00161     
00162      if (strcmp(cmd, "create_consumer") == 0) {
00163          if (argc < 4) {
00164              wrong_num_args(argc, argv, 2, 4, 4);
00165              return TCL_ERROR;
00166          }
00167          int id = atoi(argv[3]) ;
00168          Topology::create_consumer(id);       
00169          log_info("create_consumer %d \n",id);
00170      }
00171 
00172         if (strcmp(cmd, "print_stats") == 0) {
00173            Event_print_stats* e = 
00174                new Event_print_stats(time,Simulator::instance());
00175            log_info("COM: print_stats at:%3f event:%p",time,e);
00176            Simulator::post(e);
00177        }
00178 */
00179 
00180     resultf("sim: unsupported subcommand %s", cmd);
00181     return TCL_ERROR;
00182 }
00183 
00184 
00185 } // namespace dtnsim

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