00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <oasys/serialize/TclListSerialize.h>
00040 #include <oasys/thread/Notifier.h>
00041 #include <oasys/util/StringBuffer.h>
00042
00043 #include "RegistrationCommand.h"
00044 #include "CompletionNotifier.h"
00045 #include "bundling/BundleDaemon.h"
00046 #include "bundling/BundleEvent.h"
00047 #include "reg/LoggingRegistration.h"
00048 #include "reg/RegistrationTable.h"
00049 #include "reg/TclRegistration.h"
00050
00051 namespace dtn {
00052
00053 RegistrationCommand::RegistrationCommand()
00054 : TclCommand("registration")
00055 {
00056 add_to_help("add <logger | tcl> <endpoint> <args..>", "add a registration");
00057 add_to_help("tcl <reg id> <cmd <args ...>", "add a tcl registration");
00058 add_to_help("del <reg id>", "delete a registration");
00059 add_to_help("list", "list all of the registrations");
00060 add_to_help("dump_tcl <reg id>", "dump a tcl representation of the reg");
00061 }
00062
00063 int
00064 RegistrationCommand::exec(int argc, const char** argv, Tcl_Interp* interp)
00065 {
00066
00067 if (argc < 2) {
00068 wrong_num_args(argc, argv, 1, 2, INT_MAX);
00069 return TCL_ERROR;
00070 }
00071 const char* op = argv[1];
00072
00073 if (strcmp(op, "list") == 0 || strcmp(op, "dump") == 0) {
00074 oasys::StringBuffer buf;
00075 BundleDaemon::instance()->reg_table()->dump(&buf);
00076 set_result(buf.c_str());
00077 return TCL_OK;
00078
00079 } else if (strcmp(op, "dump_tcl") == 0) {
00080 if (argc < 3) {
00081 wrong_num_args(argc, argv, 2, 3, 3);
00082 return TCL_ERROR;
00083 }
00084
00085 const RegistrationTable* regtable =
00086 BundleDaemon::instance()->reg_table();
00087
00088 const char* regid_str = argv[2];
00089 int regid = atoi(regid_str);
00090
00091 Registration* reg = regtable->get(regid);
00092 if (!reg) {
00093 resultf("no registration exists with id %d", regid);
00094 return TCL_ERROR;
00095 }
00096
00097 Tcl_Obj* result = Tcl_NewListObj(0, 0);
00098 oasys::TclListSerialize s(interp, result,
00099 oasys::Serialize::CONTEXT_LOCAL, 0);
00100 reg->serialize(&s);
00101 set_objresult(result);
00102 return TCL_OK;
00103
00104 } else if (strcmp(op, "add") == 0) {
00105
00106 if (argc < 4) {
00107 wrong_num_args(argc, argv, 2, 4, INT_MAX);
00108 return TCL_ERROR;
00109 }
00110
00111 const char* type = argv[2];
00112 const char* eid_str = argv[3];
00113 EndpointIDPattern eid(eid_str);
00114
00115 if (!eid.valid()) {
00116 resultf("error in registration add %s %s: invalid endpoint id",
00117 type, eid_str);
00118 return TCL_ERROR;
00119 }
00120
00121 Registration* reg = NULL;
00122 if (strcmp(type, "logger") == 0) {
00123 reg = new LoggingRegistration(eid);
00124
00125 } else if (strcmp(type, "tcl") == 0) {
00126 reg = new TclRegistration(eid, interp);
00127
00128 } else {
00129 resultf("error in registration add %s %s: invalid type",
00130 type, eid_str);
00131 return TCL_ERROR;
00132 }
00133
00134 ASSERT(reg);
00135
00136 BundleDaemon::post_and_wait(
00137 new RegistrationAddedEvent(reg, EVENTSRC_ADMIN),
00138 CompletionNotifier::notifier());
00139
00140 resultf("%d", reg->regid());
00141 return TCL_OK;
00142
00143 } else if (strcmp(op, "del") == 0) {
00144 const RegistrationTable* regtable =
00145 BundleDaemon::instance()->reg_table();
00146
00147 const char* regid_str = argv[2];
00148 int regid = atoi(regid_str);
00149
00150 Registration* reg = regtable->get(regid);
00151 if (!reg) {
00152 resultf("no registration exists with id %d", regid);
00153 return TCL_ERROR;
00154 }
00155
00156 BundleDaemon::post_and_wait(new RegistrationRemovedEvent(reg),
00157 CompletionNotifier::notifier());
00158 return TCL_OK;
00159
00160 } else if (strcmp(op, "tcl") == 0) {
00161
00162 if (argc < 4) {
00163 wrong_num_args(argc, argv, 2, 5, INT_MAX);
00164 return TCL_ERROR;
00165 }
00166
00167 const char* regid_str = argv[2];
00168 int regid = atoi(regid_str);
00169
00170 const RegistrationTable* regtable =
00171 BundleDaemon::instance()->reg_table();
00172
00173 TclRegistration* reg = (TclRegistration*)regtable->get(regid);
00174
00175 if (!reg) {
00176 resultf("no matching registration for %d", regid);
00177 return TCL_ERROR;
00178 }
00179
00180 return reg->exec(argc - 3, &argv[3], interp);
00181 }
00182
00183 resultf("invalid registration subcommand '%s'", op);
00184 return TCL_ERROR;
00185 }
00186
00187 }