RegistrationCommand.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     // need a subcommand
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         // registration add <logger|tcl> <demux> <args...>
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         // registration tcl <regid> <cmd> <args...>
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 } // namespace dtn

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1