sim/NodeCommand.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2005-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 "Node.h"
       
    24 #include "NodeCommand.h"
       
    25 #include "SimConvergenceLayer.h"
       
    26 #include "SimRegistration.h"
       
    27 #include "Simulator.h"
       
    28 #include "Topology.h"
       
    29 #include "TrAgent.h"
       
    30 #include "bundling/Bundle.h"
       
    31 #include "contacts/ContactManager.h"
       
    32 #include "contacts/Link.h"
       
    33 #include "naming/EndpointID.h"
       
    34 #include "routing/BundleRouter.h"
       
    35 #include "routing/RouteTable.h"
       
    36 #include "reg/RegistrationTable.h"
       
    37 
       
    38 using namespace dtn;
       
    39 
       
    40 namespace dtnsim {
       
    41 
       
    42 NodeCommand::NodeCommand(Node* node)
       
    43     : TclCommand(node->name()), node_(node),
       
    44       storage_cmd_(node->storage_config())
       
    45 {
       
    46 }
       
    47 
       
    48 int
       
    49 NodeCommand::exec(int objc, Tcl_Obj** objv, Tcl_Interp* interp)
       
    50 {
       
    51     int argc = objc;
       
    52     const char* argv[objc];
       
    53     for (int i = 0; i < objc; ++i) {
       
    54         argv[i] = Tcl_GetStringFromObj(objv[i], NULL);
       
    55     }
       
    56     
       
    57     if (argc < 2) {
       
    58         wrong_num_args(argc, argv, 1, 2, INT_MAX);
       
    59         add_to_help("bundle", "XXX");
       
    60         add_to_help("param", "XXX");
       
    61         add_to_help("link", "XXX");
       
    62         add_to_help("route", "XXX");
       
    63         add_to_help("tragent", "XXX");
       
    64         add_to_help("registration", "XXX");
       
    65         return TCL_ERROR;
       
    66     }
       
    67 
       
    68     // for all commands and their side-effects, install the node as
       
    69     // the "singleton" BundleDaemon instance, and store the command
       
    70     // time in the node so any posted events happen in the future
       
    71     node_->set_active();
       
    72 
       
    73     const char* cmd = argv[1];
       
    74     const char* subcmd = NULL;
       
    75     if (objc >= 3) {
       
    76         subcmd = argv[2];
       
    77     }
       
    78 
       
    79     // first we need to handle the set command since it's normally
       
    80     // handled by the TclCommand base class
       
    81     if (subcmd != NULL && strcmp(subcmd, "set") == 0) {
       
    82         TclCommand* cmdobj = NULL;
       
    83         if      (strcmp(cmd, "bundle")  == 0) cmdobj = &bundle_cmd_;
       
    84         else if (strcmp(cmd, "link")    == 0) cmdobj = &link_cmd_;
       
    85         else if (strcmp(cmd, "param")   == 0) cmdobj = &param_cmd_;
       
    86         else if (strcmp(cmd, "route")   == 0) cmdobj = &route_cmd_;
       
    87         else if (strcmp(cmd, "storage") == 0) cmdobj = &storage_cmd_;
       
    88         else 
       
    89         {
       
    90             resultf("node: unsupported subcommand %s", cmd);
       
    91             return TCL_ERROR;
       
    92         }
       
    93         
       
    94         return cmdobj->cmd_set(objc - 1, objv + 1, interp);
       
    95     }
       
    96         
       
    97     if (strcmp(cmd, "bundle") == 0)
       
    98     {
       
    99         return bundle_cmd_.exec(objc - 1, objv + 1, interp);
       
   100     }
       
   101     else if (strcmp(cmd, "link") == 0)
       
   102     {
       
   103         return link_cmd_.exec(argc - 1, argv + 1, interp);
       
   104     }
       
   105     else if (strcmp(cmd, "param") == 0)
       
   106     {
       
   107         return param_cmd_.exec(argc - 1, argv + 1, interp);
       
   108     }
       
   109     else if (strcmp(cmd, "route") == 0)
       
   110     {
       
   111         return route_cmd_.exec(argc - 1, argv + 1, interp);
       
   112     }
       
   113     else if (strcmp(cmd, "storage") == 0)
       
   114     {
       
   115         if (subcmd != NULL && !strcmp(subcmd, "set")) {
       
   116             return storage_cmd_.cmd_set(objc - 1, objv + 1, interp);
       
   117         }
       
   118         return storage_cmd_.exec(argc - 1, argv + 1, interp);
       
   119     }
       
   120     else if (strcmp(cmd, "registration") == 0)
       
   121     {
       
   122         if (strcmp(subcmd, "add") == 0) {
       
   123             // <node> registration add <eid>
       
   124             const char* eid_str = argv[3];
       
   125             EndpointIDPattern eid(eid_str);
       
   126 
       
   127             if (!eid.valid()) {
       
   128                 resultf("error in node registration add %s: "
       
   129                         "invalid demux eid", eid_str);
       
   130                 return TCL_ERROR;
       
   131             }
       
   132 
       
   133             Registration* r = new SimRegistration(node_, eid);
       
   134             RegistrationAddedEvent* e =
       
   135                 new RegistrationAddedEvent(r, EVENTSRC_ADMIN);
       
   136 
       
   137             node_->post_event(e);
       
   138             
       
   139             return TCL_OK;
       
   140         } else if (strcmp(subcmd, "list") == 0 || strcmp(subcmd, "dump") == 0) {
       
   141             oasys::StringBuffer buf;
       
   142             node_->reg_table()->dump(&buf);
       
   143             set_result(buf.c_str());
       
   144             return TCL_OK;
       
   145         
       
   146         } else if (strcmp(subcmd, "del") == 0) {
       
   147             const RegistrationTable* regtable = node_->reg_table();
       
   148 
       
   149             const char* regid_str = argv[2];
       
   150             int regid = atoi(regid_str);
       
   151 
       
   152             Registration* reg = regtable->get(regid);
       
   153             if (!reg) {
       
   154                 resultf("no registration exists with id %d", regid);
       
   155                 return TCL_ERROR;
       
   156             }
       
   157 
       
   158             node_->post_event(new RegistrationRemovedEvent(reg));
       
   159             return TCL_OK;
       
   160 
       
   161         }
       
   162         resultf("node registration: unsupported subcommand %s", subcmd);
       
   163         return TCL_ERROR;
       
   164     }
       
   165     else if (strcmp(cmd, "tragent") == 0)
       
   166     {
       
   167         // <node> tragent <src> <dst> <args>
       
   168         if (argc < 5) {
       
   169             wrong_num_args(argc, argv, 3, 5, INT_MAX);
       
   170             return TCL_ERROR;
       
   171         }
       
   172         
       
   173         const char* src = argv[2];
       
   174         const char* dst = argv[3];
       
   175 
       
   176         // see if src/dest are node names, in which case we use its
       
   177         // local eid as the source address
       
   178         EndpointID src_eid;
       
   179         Node* src_node = Topology::find_node(src);
       
   180         if (src_node) {
       
   181             src_eid.assign(src_node->local_eid());
       
   182         } else {
       
   183             src_eid.assign(src);
       
   184             if (!src_eid.valid()) {
       
   185                 resultf("node tragent: invalid src eid %s", src);
       
   186                 return TCL_ERROR;
       
   187             }
       
   188         }
       
   189         
       
   190         EndpointID dst_eid;
       
   191         Node* dst_node = Topology::find_node(dst);
       
   192         if (dst_node) {
       
   193             dst_eid.assign(dst_node->local_eid());
       
   194         } else {
       
   195             dst_eid.assign(dst);
       
   196             if (!dst_eid.valid()) {
       
   197                 resultf("node tragent: invalid dst eid %s", dst);
       
   198                 return TCL_ERROR;
       
   199             }
       
   200         }
       
   201         
       
   202         TrAgent* a = TrAgent::init(src_eid, dst_eid,
       
   203                                    argc - 4, argv + 4);
       
   204         if (!a) {
       
   205             resultf("error in tragent config");
       
   206             return TCL_ERROR;
       
   207         }
       
   208         
       
   209         return TCL_OK;
       
   210 
       
   211     } else {
       
   212         resultf("node: unsupported subcommand %s", cmd);
       
   213         return TCL_ERROR;
       
   214     }
       
   215 }
       
   216 
       
   217 } // namespace dtnsim