blob: 2a39a859f59f0f63c2e5744aa9d7aa386e2e247e [file] [log] [blame]
hilata198cadb2014-02-15 23:46:19 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6#include "nfdc.hpp"
7#include <boost/lexical_cast.hpp>
8#include <boost/algorithm/string.hpp>
9#include <boost/algorithm/string/regex_find_format.hpp>
10#include <boost/regex.hpp>
11
12void
13usage(const char* programName)
14{
15 std::cout << "Usage:\n" << programName << " [-h] COMMAND\n"
16 " -h print usage and exit\n"
17 "\n"
18 " COMMAND can be one of following:\n"
19 " insert <name> \n"
20 " Insert a FIB entry \n"
21 " delete <name> \n"
22 " Delete a FIB entry\n"
23 " add-nexthop <name> <faceId> [<cost>]\n"
24 " Add a nexthop to an existing FIB entry\n"
25 " remove-nexthop <name> <faceId> \n"
26 " Remove a nexthop from a FIB entry\n"
27 " set-strategy <name> <stratgy>\n"
28 " Set a forwarding strategy for a namespace\n"
29 " create <uri> \n"
30 " Create a face in one of the following formats:\n"
31 " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n"
32 " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n"
33 " destroy <faceId> \n"
34 " Destroy a face\n"
35 << std::endl;
36}
37
38namespace nfdc {
39
40Controller::Controller(ndn::Face& face)
41 : ndn::nfd::Controller(face)
42{
43}
44
45Controller::~Controller()
46{
47}
48bool
49Controller::dispatch(const std::string& command, const char* commandOptions[], int nOptions)
50{
51 if (command == "insert") {
52 if (nOptions != 1)
53 return false;
54 fibInsert(commandOptions);
55 }
56 else if (command == "delete") {
57 if (nOptions != 1)
58 return false;
59 fibDelete(commandOptions);
60 }
61 else if (command == "add-nexthop") {
62 if (nOptions == 2)
63 fibAddNextHop(commandOptions, false);
64 else if (nOptions == 3)
65 fibAddNextHop(commandOptions, true);
66 else
67 return false;
68 }
69 else if (command == "remove-nexthop") {
70 if (nOptions != 2)
71 return false;
72 fibRemoveNextHop(commandOptions);
73 }
74 else if (command == "set-strategy") {
75 if (nOptions != 2)
76 return false;
77 fibSetStrategy(commandOptions);
78 }
79 else if (command == "create") {
80 if (nOptions != 1)
81 return false;
82 faceCreate(commandOptions);
83 }
84 else if (command == "destroy") {
85 if (nOptions != 1)
86 return false;
87 faceDestroy(commandOptions);
88 }
89 else
90 usage(m_programName);
91
92 return true;
93}
94
95void
96Controller::fibInsert(const char* commandOptions[])
97{
98 const std::string& name = commandOptions[0];
99
100 ndn::nfd::FibManagementOptions fibOptions;
101 fibOptions.setName(name);
102 startFibCommand("insert",
103 fibOptions,
104 bind(&Controller::onFibSuccess, this, _1, "Fib insertion succeeded"),
105 bind(&Controller::onError, this, _1, "Fib insertion failed"));
106}
107
108void
109Controller::fibDelete(const char* commandOptions[])
110{
111 const std::string& name = commandOptions[0];
112 ndn::nfd::FibManagementOptions fibOptions;
113 fibOptions.setName(name);
114 startFibCommand("delete",
115 fibOptions,
116 bind(&Controller::onFibSuccess, this, _1, "Fib deletion succeeded"),
117 bind(&Controller::onError, this, _1, "Fib deletion failed" ));
118}
119
120
121void
122Controller::fibAddNextHop(const char* commandOptions[], bool hasCost)
123{
124 ndn::nfd::FibManagementOptions fibOptions;
125
126 const std::string& name = commandOptions[0];
127 const int faceId = boost::lexical_cast<int>(commandOptions[1]);
128
129 fibOptions.setName(name);
130 fibOptions.setFaceId(faceId);
131
132 if (hasCost)
133 {
134 const int cost = boost::lexical_cast<int>(commandOptions[2]);
135 fibOptions.setCost(cost);
136 }
137 startFibCommand("add-nexthop",
138 fibOptions,
139 bind(&Controller::onFibSuccess, this, _1, "Nexthop insertion succeeded"),
140 bind(&Controller::onError, this, _1, "Nexthop insertion failed"));
141}
142
143void
144Controller::fibRemoveNextHop(const char* commandOptions[])
145{
146 const std::string& name = commandOptions[0];
147 const int faceId = boost::lexical_cast<int>(commandOptions[1]);
148 ndn::nfd::FibManagementOptions fibOptions;
149
150 fibOptions.setName(name);
151 fibOptions.setFaceId(faceId);
152 startFibCommand("remove-nexthop",
153 fibOptions,
154 bind(&Controller::onFibSuccess, this, _1, "Nexthop Removal succeeded"),
155 bind(&Controller::onError, this, _1, "Nexthop Removal failed"));
156}
hilata9b27e692014-02-25 15:43:19 -0600157
hilata198cadb2014-02-15 23:46:19 -0600158void
159Controller::fibSetStrategy(const char* commandOptions[])
160{
hilata9b27e692014-02-25 15:43:19 -0600161 const std::string& name = commandOptions[0];
162 const std::string& strategy = commandOptions[1];
163 ndn::nfd::FibManagementOptions fibOptions;
164
165 fibOptions.setName(name);
166 fibOptions.setStrategy(strategy);
167
168 startFibCommand("set-strategy",
169 fibOptions,
170 bind(&Controller::onFibSuccess,this, _1, "Successfully set forwarding strategy"),
171 bind(&Controller::onError,this, _1, "Failed to set forwarding strategy"));
172
hilata198cadb2014-02-15 23:46:19 -0600173}
hilata9b27e692014-02-25 15:43:19 -0600174
hilata198cadb2014-02-15 23:46:19 -0600175namespace {
176bool
177isValidUri(const std::string& input)
178{
179 // an extended regex to support the validation of uri structure
180 // boost::regex e("^[a-z0-9]+-?+[a-z0-9]+\\:\\/\\/.*");
181 boost::regex e("^[a-z0-9]+\\:.*");
182 return boost::regex_match(input, e);
183}
184} // anonymous namespace
185
186void
187Controller::faceCreate(const char* commandOptions[])
188{
189 ndn::nfd::FaceManagementOptions faceOptions;
190 const std::string& uri = commandOptions[0];
191 faceOptions.setUri(uri);
192
193 if (isValidUri(uri))
194 {
195 startFaceCommand("create",
196 faceOptions,
197 bind(&Controller::onFaceSuccess, this, _1, "Face creation succeeded"),
198 bind(&Controller::onError, this, _1, "Face creation failed"));
199 }
200 else
201 throw Error("invalid uri format");
202}
203
204void
205Controller::faceDestroy(const char* commandOptions[])
206{
207 ndn::nfd::FaceManagementOptions faceOptions;
208 const int faceId = boost::lexical_cast<int>(commandOptions[0]);
209 faceOptions.setFaceId(faceId);
210
211 startFaceCommand("destroy",
212 faceOptions,
213 bind(&Controller::onFaceSuccess, this, _1, "Face destroy succeeded"),
214 bind(&Controller::onError, this, _1, "Face destroy failed"));
215}
216
217void
218Controller::onFibSuccess(const ndn::nfd::FibManagementOptions& resp, const std::string& message)
219{
220 std::cout << resp << std::endl;
221}
222
223void
224Controller::onFaceSuccess(const ndn::nfd::FaceManagementOptions& resp, const std::string& message)
225{
226 std::cout << resp << std::endl;
227}
228
229void
230Controller::onError(const std::string& error, const std::string& message)
231{
232 throw Error(message + ": " + error);
233}
234}// namespace nfdc
235
236int
237main(int argc, char** argv)
238{
239 ndn::Face face;
240 nfdc::Controller p(face);
Alexander Afanasyev9f935b82014-02-24 15:14:22 -0800241
hilata198cadb2014-02-15 23:46:19 -0600242 p.m_programName = argv[0];
243 int opt;
244 while ((opt = getopt(argc, argv, "h")) != -1) {
245 switch (opt) {
246 case 'h':
247 usage(p.m_programName);
248 return 0;
249
250 default:
251 usage(p.m_programName);
252 return 1;
253 }
254 }
Alexander Afanasyev9f935b82014-02-24 15:14:22 -0800255
256 if (argc == optind) {
257 usage(p.m_programName);
258 return 1;
259 }
260
hilata198cadb2014-02-15 23:46:19 -0600261 try {
262 bool hasSucceeded = p.dispatch(argv[optind],
263 const_cast<const char**>(argv + optind + 1),
264 argc - optind - 1);
265 if (hasSucceeded == false) {
266 usage(p.m_programName);
267 return 1;
268 }
269
270 face.processEvents();
271 }
272 catch (const std::exception& e) {
273 std::cerr << "ERROR: " << e.what() << std::endl;
274 return 2;
275 }
276 return 0;
277}
278