ConnCommand.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) 2005 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 <stdlib.h>
00040 
00041 #include "Connectivity.h"
00042 #include "ConnCommand.h"
00043 #include "Simulator.h"
00044 #include "Topology.h"
00045 
00046 namespace dtnsim {
00047 
00048 ConnCommand::ConnCommand()
00049     : TclCommand("conn")
00050 {
00051     bind_s("type", &Connectivity::type_, "Connectivity type.");
00052     
00053     add_to_help("up", "Take connection up XXX");
00054     add_to_help("down", "Take connection down XXX");
00055 }
00056 
00057 int
00058 ConnCommand::exec(int argc, const char** argv, Tcl_Interp* tclinterp)
00059 {
00060     (void)tclinterp;
00061     
00062     if (argc < 3) {
00063         wrong_num_args(argc, argv, 2, 4, INT_MAX);
00064         return TCL_ERROR;
00065     }
00066     
00067     // pull out the time
00068     char* end;
00069     double time = strtod(argv[1], &end);
00070     if (*end != '\0') {
00071         resultf("time value '%s' invalid", argv[1]);
00072         return TCL_ERROR;
00073     }
00074 
00075     const char* cmd = argv[2];
00076 
00077     Connectivity* conn = Connectivity::instance();
00078 
00079     if (!strcmp(cmd, "up") || !strcmp(cmd, "down")) {
00080         // conn <time> <up|down> <n1> <n2> <args>
00081         if (argc < 5) {
00082             wrong_num_args(argc, argv, 2, 5, INT_MAX);
00083             return TCL_ERROR;
00084         }
00085 
00086         const char* n1_name = argv[3];
00087         const char* n2_name = argv[4];
00088 
00089         if (strcmp(n1_name, "*") != 0 &&
00090             Topology::find_node(n1_name) == NULL)
00091         {
00092             resultf("invalid node or wildcard '%s'", n1_name);
00093             return TCL_ERROR;
00094         }
00095         
00096         if (strcmp(n2_name, "*") != 0 &&
00097             Topology::find_node(n2_name) == NULL)
00098         {
00099             resultf("invalid node or wildcard '%s'", n2_name);
00100             return TCL_ERROR;
00101         }
00102         
00103         ConnState* s = new ConnState();
00104         const char* invalid;
00105         if (! s->parse_options(argc - 5, argv + 5, &invalid)) {
00106             resultf("invalid option '%s'", invalid);
00107             delete s;
00108             return TCL_ERROR;
00109         }
00110         
00111         s->open_ = !strcmp(cmd, "up");
00112 
00113         Simulator::post(
00114             new SimConnectivityEvent(time, conn, n1_name, n2_name, s));
00115 
00116         return TCL_OK;
00117 
00118     } else {
00119         // dispatch to the connectivity module itself
00120         if (! conn->exec(argc - 3, argv + 3)) {
00121             resultf("conn: error handling command");
00122             return TCL_ERROR;
00123         }
00124 
00125 
00126         return TCL_OK;
00127     }
00128     
00129     resultf("conn: unsupported subcommand %s", cmd);
00130     return TCL_ERROR;
00131 }
00132 
00133 
00134 } // namespace dtnsim

Generated on Fri Dec 22 14:47:58 2006 for DTN Reference Implementation by  doxygen 1.5.1