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