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