|
1 /* |
|
2 * Copyright 2005-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 # include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #include <stdlib.h> |
|
22 |
|
23 #include "Connectivity.h" |
|
24 #include "ConnCommand.h" |
|
25 #include "Simulator.h" |
|
26 #include "Topology.h" |
|
27 |
|
28 namespace dtnsim { |
|
29 |
|
30 ConnCommand::ConnCommand() |
|
31 : TclCommand("conn") |
|
32 { |
|
33 bind_var(new oasys::StringOpt("type", &Connectivity::type_, |
|
34 "type", "Connectivity type.")); |
|
35 |
|
36 add_to_help("up", "Take connection up XXX"); |
|
37 add_to_help("down", "Take connection down XXX"); |
|
38 } |
|
39 |
|
40 int |
|
41 ConnCommand::exec(int argc, const char** argv, Tcl_Interp* tclinterp) |
|
42 { |
|
43 (void)tclinterp; |
|
44 |
|
45 if (argc < 2) { |
|
46 wrong_num_args(argc, argv, 1, 2, INT_MAX); |
|
47 return TCL_ERROR; |
|
48 } |
|
49 |
|
50 const char* cmd = argv[1]; |
|
51 |
|
52 Connectivity* conn = Connectivity::instance(); |
|
53 |
|
54 if (!strcmp(cmd, "up") || !strcmp(cmd, "down")) { |
|
55 // conn <up|down> <n1> <n2> <args> |
|
56 if (argc < 4) { |
|
57 wrong_num_args(argc, argv, 2, 4, INT_MAX); |
|
58 return TCL_ERROR; |
|
59 } |
|
60 |
|
61 const char* n1_name = argv[2]; |
|
62 const char* n2_name = argv[3]; |
|
63 |
|
64 if (strcmp(n1_name, "*") != 0 && |
|
65 Topology::find_node(n1_name) == NULL) |
|
66 { |
|
67 resultf("invalid node or wildcard '%s'", n1_name); |
|
68 return TCL_ERROR; |
|
69 } |
|
70 |
|
71 if (strcmp(n2_name, "*") != 0 && |
|
72 Topology::find_node(n2_name) == NULL) |
|
73 { |
|
74 resultf("invalid node or wildcard '%s'", n2_name); |
|
75 return TCL_ERROR; |
|
76 } |
|
77 |
|
78 ConnState s; |
|
79 const char* invalid; |
|
80 if (! s.parse_options(argc - 4, argv + 4, &invalid)) { |
|
81 resultf("invalid option '%s'", invalid); |
|
82 return TCL_ERROR; |
|
83 } |
|
84 |
|
85 s.open_ = !strcmp(cmd, "up"); |
|
86 |
|
87 conn->set_state(n1_name, n2_name, s); |
|
88 |
|
89 return TCL_OK; |
|
90 } |
|
91 |
|
92 resultf("conn: unsupported subcommand %s", cmd); |
|
93 return TCL_ERROR; |
|
94 } |
|
95 |
|
96 |
|
97 } // namespace dtnsim |