RouteCommand.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/util/StringBuffer.h>
00040 
00041 #include "RouteCommand.h"
00042 #include "CompletionNotifier.h"
00043 
00044 #include "contacts/Link.h"
00045 #include "contacts/ContactManager.h"
00046 
00047 #include "bundling/BundleEvent.h"
00048 #include "bundling/BundleDaemon.h"
00049 
00050 #include "routing/BundleRouter.h"
00051 #include "routing/RouteEntry.h"
00052 
00053 namespace dtn {
00054 
00055 RouteCommand::RouteCommand()
00056     : TclCommand("route")
00057 {
00058     bind_s("type", &BundleRouter::Config.type_, "static",
00059            "Which routing algorithm to use.");
00060 
00061     bind_b("add_nexthop_routes",
00062            &BundleRouter::Config.add_nexthop_routes_, true,
00063            "Whether or not to automatically add routes for next hop links");
00064     
00065     bind_i("default_priority",
00066            &BundleRouter::Config.default_priority_, 0,
00067            "Default priority for new routes (initially zero)");
00068     
00069     add_to_help("add <dest> <link/endpoint> [opts]", "add a route");
00070     add_to_help("del <dest> <link/endpoint>", "delete a route");
00071     add_to_help("dump", "dump all of the static routes");
00072 }
00073 
00074 int
00075 RouteCommand::exec(int argc, const char** argv, Tcl_Interp* interp)
00076 {
00077     (void)interp;
00078     
00079     if (argc < 2) {
00080         resultf("need a route subcommand");
00081         return TCL_ERROR;
00082     }
00083 
00084     const char* cmd = argv[1];
00085     
00086     if (strcmp(cmd, "add") == 0) {
00087         // route add <dest> <link/endpoint> <args>
00088         if (argc < 4) {
00089             wrong_num_args(argc, argv, 2, 4, INT_MAX);
00090             return TCL_ERROR;
00091         }
00092 
00093         const char* dest_str = argv[2];
00094 
00095         EndpointIDPattern dest(dest_str);
00096         if (!dest.valid()) {
00097             resultf("invalid destination eid %s", dest_str);
00098             return TCL_ERROR;
00099         }
00100         const char* name = argv[3];
00101         Link* link = NULL;
00102         
00103         link = BundleDaemon::instance()->contactmgr()->find_link(name);
00104 
00105         if (link == NULL) {
00106             resultf("no such link %s", name);
00107             return TCL_ERROR;
00108         }
00109 
00110         RouteEntry* entry = new RouteEntry(dest, link);
00111         
00112         // skip over the consumed arguments and parse optional ones.
00113         // any invalid options are shifted into argv[0]
00114         argc -= 4;
00115         argv += 4;
00116         if (argc != 0 && (entry->parse_options(argc, argv) != argc))
00117         {
00118             resultf("invalid argument '%s'", argv[0]);
00119             return TCL_ERROR;
00120         }
00121         
00122         // post the event -- if the daemon has been started, we wait
00123         // for the event to be consumed, otherwise we just return
00124         // immediately. this allows the command to have the
00125         // appropriate semantics both in the static config file and in
00126         // an interactive mode
00127         
00128         if (BundleDaemon::instance()->started()) {
00129             BundleDaemon::post_and_wait(new RouteAddEvent(entry),
00130                                         CompletionNotifier::notifier());
00131         } else {
00132             BundleDaemon::post(new RouteAddEvent(entry));
00133         }
00134 
00135         return TCL_OK;
00136     }
00137 
00138     else if (strcmp(cmd, "del") == 0) {
00139         // route del <dest>
00140         if (argc != 3) {
00141             wrong_num_args(argc, argv, 2, 3, 3);
00142             return TCL_ERROR;
00143         }
00144 
00145         EndpointIDPattern pat(argv[2]);
00146         if (!pat.valid()) {
00147             resultf("invalid endpoint id pattern '%s'", argv[2]);
00148             return TCL_ERROR;
00149         }
00150 
00151         if (BundleDaemon::instance()->started()) {
00152             BundleDaemon::post_and_wait(new RouteDelEvent(pat),
00153                                         CompletionNotifier::notifier());
00154         } else {
00155             BundleDaemon::post(new RouteDelEvent(pat));
00156         }
00157         
00158         return TCL_OK;
00159     }
00160 
00161     else if (strcmp(cmd, "dump") == 0) {
00162         oasys::StringBuffer buf;
00163         BundleDaemon::instance()->get_routing_state(&buf);
00164         set_result(buf.c_str());
00165         return TCL_OK;
00166     }
00167 
00168     else if (strcmp(cmd, "local_eid") == 0) {
00169         if (argc == 2) {
00170             // route local_eid
00171             set_result(BundleDaemon::instance()->local_eid().c_str());
00172             return TCL_OK;
00173             
00174         } else if (argc == 3) {
00175             // route local_eid <eid?>
00176             BundleDaemon::instance()->set_local_eid(argv[2]);
00177             if (! BundleDaemon::instance()->local_eid().valid()) {
00178                 resultf("invalid eid '%s'", argv[2]);
00179                 return TCL_ERROR;
00180             }
00181             if (! BundleDaemon::instance()->local_eid().known_scheme()) {
00182                 resultf("local eid '%s' has unknown scheme", argv[2]);
00183                 return TCL_ERROR;
00184             }
00185         } else {
00186             wrong_num_args(argc, argv, 2, 2, 3);
00187             return TCL_ERROR;
00188         }
00189     }
00190 
00191     else {
00192         resultf("unimplemented route subcommand %s", cmd);
00193         return TCL_ERROR;
00194     }
00195     
00196     return TCL_OK;
00197 }
00198 
00199 } // namespace dtn

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