blob: 8dc94f23d2fc7a01dbb3ba9913ac6a60f057b58a [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}
157
158void
159Controller::fibSetStrategy(const char* commandOptions[])
160{
161
162 // std::string name = commandOptions[0];
163 // std::string strategy = commandOptions[1];
164 // startFibCommand("set-strategy",
165 // ndn::nfd::FibManagementOptions()
166 // .setName(name)
167 // .setStrategy(strategy),
168 // bind(&Controller::onFibSuccess,this, _1),
169 // bind(&Controller::onError,this, _1));
170}
171
172namespace {
173bool
174isValidUri(const std::string& input)
175{
176 // an extended regex to support the validation of uri structure
177 // boost::regex e("^[a-z0-9]+-?+[a-z0-9]+\\:\\/\\/.*");
178 boost::regex e("^[a-z0-9]+\\:.*");
179 return boost::regex_match(input, e);
180}
181} // anonymous namespace
182
183void
184Controller::faceCreate(const char* commandOptions[])
185{
186 ndn::nfd::FaceManagementOptions faceOptions;
187 const std::string& uri = commandOptions[0];
188 faceOptions.setUri(uri);
189
190 if (isValidUri(uri))
191 {
192 startFaceCommand("create",
193 faceOptions,
194 bind(&Controller::onFaceSuccess, this, _1, "Face creation succeeded"),
195 bind(&Controller::onError, this, _1, "Face creation failed"));
196 }
197 else
198 throw Error("invalid uri format");
199}
200
201void
202Controller::faceDestroy(const char* commandOptions[])
203{
204 ndn::nfd::FaceManagementOptions faceOptions;
205 const int faceId = boost::lexical_cast<int>(commandOptions[0]);
206 faceOptions.setFaceId(faceId);
207
208 startFaceCommand("destroy",
209 faceOptions,
210 bind(&Controller::onFaceSuccess, this, _1, "Face destroy succeeded"),
211 bind(&Controller::onError, this, _1, "Face destroy failed"));
212}
213
214void
215Controller::onFibSuccess(const ndn::nfd::FibManagementOptions& resp, const std::string& message)
216{
217 std::cout << resp << std::endl;
218}
219
220void
221Controller::onFaceSuccess(const ndn::nfd::FaceManagementOptions& resp, const std::string& message)
222{
223 std::cout << resp << std::endl;
224}
225
226void
227Controller::onError(const std::string& error, const std::string& message)
228{
229 throw Error(message + ": " + error);
230}
231}// namespace nfdc
232
233int
234main(int argc, char** argv)
235{
236 ndn::Face face;
237 nfdc::Controller p(face);
Alexander Afanasyev9f935b82014-02-24 15:14:22 -0800238
hilata198cadb2014-02-15 23:46:19 -0600239 p.m_programName = argv[0];
240 int opt;
241 while ((opt = getopt(argc, argv, "h")) != -1) {
242 switch (opt) {
243 case 'h':
244 usage(p.m_programName);
245 return 0;
246
247 default:
248 usage(p.m_programName);
249 return 1;
250 }
251 }
Alexander Afanasyev9f935b82014-02-24 15:14:22 -0800252
253 if (argc == optind) {
254 usage(p.m_programName);
255 return 1;
256 }
257
hilata198cadb2014-02-15 23:46:19 -0600258 try {
259 bool hasSucceeded = p.dispatch(argv[optind],
260 const_cast<const char**>(argv + optind + 1),
261 argc - optind - 1);
262 if (hasSucceeded == false) {
263 usage(p.m_programName);
264 return 1;
265 }
266
267 face.processEvents();
268 }
269 catch (const std::exception& e) {
270 std::cerr << "ERROR: " << e.what() << std::endl;
271 return 2;
272 }
273 return 0;
274}
275