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 "Node.h"
00024 #include "NodeCommand.h"
00025 #include "SimConvergenceLayer.h"
00026 #include "SimRegistration.h"
00027 #include "Simulator.h"
00028 #include "Topology.h"
00029 #include "TrAgent.h"
00030 #include "bundling/Bundle.h"
00031 #include "contacts/ContactManager.h"
00032 #include "contacts/Link.h"
00033 #include "naming/EndpointID.h"
00034 #include "routing/BundleRouter.h"
00035 #include "routing/RouteTable.h"
00036 #include "reg/RegistrationTable.h"
00037
00038 using namespace dtn;
00039
00040 namespace dtnsim {
00041
00042 NodeCommand::NodeCommand(Node* node)
00043 : TclCommand(node->name()), node_(node),
00044 storage_cmd_(node->storage_config())
00045 {
00046 }
00047
00048 int
00049 NodeCommand::exec(int objc, Tcl_Obj** objv, Tcl_Interp* interp)
00050 {
00051 int argc = objc;
00052 const char* argv[objc];
00053 for (int i = 0; i < objc; ++i) {
00054 argv[i] = Tcl_GetStringFromObj(objv[i], NULL);
00055 }
00056
00057 if (argc < 2) {
00058 wrong_num_args(argc, argv, 1, 2, INT_MAX);
00059 add_to_help("bundle", "XXX");
00060 add_to_help("param", "XXX");
00061 add_to_help("link", "XXX");
00062 add_to_help("route", "XXX");
00063 add_to_help("tragent", "XXX");
00064 add_to_help("registration", "XXX");
00065 return TCL_ERROR;
00066 }
00067
00068
00069
00070
00071 node_->set_active();
00072
00073 const char* cmd = argv[1];
00074 const char* subcmd = NULL;
00075 if (objc >= 3) {
00076 subcmd = argv[2];
00077 }
00078
00079
00080
00081 if (subcmd != NULL && strcmp(subcmd, "set") == 0) {
00082 TclCommand* cmdobj = NULL;
00083 if (strcmp(cmd, "bundle") == 0) cmdobj = &bundle_cmd_;
00084 else if (strcmp(cmd, "link") == 0) cmdobj = &link_cmd_;
00085 else if (strcmp(cmd, "param") == 0) cmdobj = ¶m_cmd_;
00086 else if (strcmp(cmd, "route") == 0) cmdobj = &route_cmd_;
00087 else if (strcmp(cmd, "storage") == 0) cmdobj = &storage_cmd_;
00088 else
00089 {
00090 resultf("node: unsupported subcommand %s", cmd);
00091 return TCL_ERROR;
00092 }
00093
00094 return cmdobj->cmd_set(objc - 1, objv + 1, interp);
00095 }
00096
00097 if (strcmp(cmd, "bundle") == 0)
00098 {
00099 return bundle_cmd_.exec(objc - 1, objv + 1, interp);
00100 }
00101 else if (strcmp(cmd, "link") == 0)
00102 {
00103 return link_cmd_.exec(argc - 1, argv + 1, interp);
00104 }
00105 else if (strcmp(cmd, "param") == 0)
00106 {
00107 return param_cmd_.exec(argc - 1, argv + 1, interp);
00108 }
00109 else if (strcmp(cmd, "route") == 0)
00110 {
00111 return route_cmd_.exec(argc - 1, argv + 1, interp);
00112 }
00113 else if (strcmp(cmd, "storage") == 0)
00114 {
00115 if (subcmd != NULL && !strcmp(subcmd, "set")) {
00116 return storage_cmd_.cmd_set(objc - 1, objv + 1, interp);
00117 }
00118 return storage_cmd_.exec(argc - 1, argv + 1, interp);
00119 }
00120 else if (strcmp(cmd, "registration") == 0)
00121 {
00122 if (strcmp(subcmd, "add") == 0) {
00123
00124 const char* eid_str = argv[3];
00125 EndpointIDPattern eid(eid_str);
00126
00127 if (!eid.valid()) {
00128 resultf("error in node registration add %s: "
00129 "invalid demux eid", eid_str);
00130 return TCL_ERROR;
00131 }
00132
00133 Registration* r = new SimRegistration(node_, eid);
00134 RegistrationAddedEvent* e =
00135 new RegistrationAddedEvent(r, EVENTSRC_ADMIN);
00136
00137 node_->post_event(e);
00138
00139 return TCL_OK;
00140 } else if (strcmp(subcmd, "list") == 0 || strcmp(subcmd, "dump") == 0) {
00141 oasys::StringBuffer buf;
00142 node_->reg_table()->dump(&buf);
00143 set_result(buf.c_str());
00144 return TCL_OK;
00145
00146 } else if (strcmp(subcmd, "del") == 0) {
00147 const RegistrationTable* regtable = node_->reg_table();
00148
00149 const char* regid_str = argv[2];
00150 int regid = atoi(regid_str);
00151
00152 Registration* reg = regtable->get(regid);
00153 if (!reg) {
00154 resultf("no registration exists with id %d", regid);
00155 return TCL_ERROR;
00156 }
00157
00158 node_->post_event(new RegistrationRemovedEvent(reg));
00159 return TCL_OK;
00160
00161 }
00162 resultf("node registration: unsupported subcommand %s", subcmd);
00163 return TCL_ERROR;
00164 }
00165 else if (strcmp(cmd, "tragent") == 0)
00166 {
00167
00168 if (argc < 5) {
00169 wrong_num_args(argc, argv, 3, 5, INT_MAX);
00170 return TCL_ERROR;
00171 }
00172
00173 const char* src = argv[2];
00174 const char* dst = argv[3];
00175
00176
00177
00178 EndpointID src_eid;
00179 Node* src_node = Topology::find_node(src);
00180 if (src_node) {
00181 src_eid.assign(src_node->local_eid());
00182 } else {
00183 src_eid.assign(src);
00184 if (!src_eid.valid()) {
00185 resultf("node tragent: invalid src eid %s", src);
00186 return TCL_ERROR;
00187 }
00188 }
00189
00190 EndpointID dst_eid;
00191 Node* dst_node = Topology::find_node(dst);
00192 if (dst_node) {
00193 dst_eid.assign(dst_node->local_eid());
00194 } else {
00195 dst_eid.assign(dst);
00196 if (!dst_eid.valid()) {
00197 resultf("node tragent: invalid dst eid %s", dst);
00198 return TCL_ERROR;
00199 }
00200 }
00201
00202 TrAgent* a = TrAgent::init(src_eid, dst_eid,
00203 argc - 4, argv + 4);
00204 if (!a) {
00205 resultf("error in tragent config");
00206 return TCL_ERROR;
00207 }
00208
00209 return TCL_OK;
00210
00211 } else {
00212 resultf("node: unsupported subcommand %s", cmd);
00213 return TCL_ERROR;
00214 }
00215 }
00216
00217 }