blob: 484a870a3da3d40a0db0cb8bccb9e3e0a5ef0d0a [file] [log] [blame]
Andrew Brown2f1fdbf2015-01-21 10:52:29 -08001/*
andrewsbrown7e6b9e82015-03-03 16:11:11 -08002 * jndn-management
3 * Copyright (c) 2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 3, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080013 */
14package com.intel.jndn.management;
15
Andrew Brownc46c1602015-02-18 10:45:56 -080016import com.intel.jndn.management.types.StatusDataset;
17import com.intel.jndn.management.types.ControlResponse;
18import com.intel.jndn.management.types.FaceStatus;
Andrew Brown63bed362015-02-18 11:28:50 -080019import com.intel.jndn.management.types.FibEntry;
andrewsbrowne8e8e852015-03-09 13:48:31 -070020import com.intel.jndn.management.types.LocalControlHeader;
Andrew Brownc46c1602015-02-18 10:45:56 -080021import com.intel.jndn.management.types.RibEntry;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080022import com.intel.jndn.utils.Client;
Andrew Brown07466442015-02-24 09:07:02 -080023import com.intel.jndn.utils.SegmentedClient;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080024import java.io.IOException;
25import java.util.List;
26import net.named_data.jndn.ControlParameters;
27import net.named_data.jndn.Data;
28import net.named_data.jndn.Face;
29import net.named_data.jndn.ForwardingFlags;
30import net.named_data.jndn.Interest;
31import net.named_data.jndn.Name;
32import net.named_data.jndn.encoding.EncodingException;
33import net.named_data.jndn.security.SecurityException;
Andrew Brownc46c1602015-02-18 10:45:56 -080034import java.util.logging.Logger;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080035
36/**
37 * Helper class for interacting with an NDN forwarder daemon; see
38 * http://redmine.named-data.net/projects/nfd/wiki/Management for explanations
39 * of the various protocols used.
40 *
41 * @author Andrew Brown <andrew.brown@intel.com>
42 */
43public class NFD {
44
Andrew Brown211d2b62015-02-18 11:12:02 -080045 public final static long DEFAULT_TIMEOUT = 2000;
46 static private final Logger logger = Logger.getLogger(NFD.class.getName());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080047
Andrew Brown211d2b62015-02-18 11:12:02 -080048 /**
49 * Ping a forwarder on an existing face to verify that the forwarder is
50 * working and responding to requests; this version sends a discovery packet
51 * to /localhost/nfd which should always respond if the requestor is on the
52 * same machine as the NDN forwarding daemon.
53 *
54 * @param face
55 * @return true if successful, false otherwise
56 */
57 public static boolean pingLocal(Face face) {
58 return ping(face, new Name("/localhost/nfd"));
59 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080060
Andrew Brown211d2b62015-02-18 11:12:02 -080061 /**
62 * Request a name on an existing face to verify the forwarder is working and
63 * responding to requests. Note that the name must be served or cached on the
64 * forwarder for this to return true.
65 *
66 * @param face
67 * @param name
68 * @return true if successful, false otherwise
69 */
70 public static boolean ping(Face face, Name name) {
71 // build interest
72 Interest interest = new Interest(name);
73 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
74 interest.setMustBeFresh(true);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080075
Andrew Brown211d2b62015-02-18 11:12:02 -080076 // send packet
77 Data data = Client.getDefault().getSync(face, interest);
78 return data != null;
79 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080080
Andrew Brown211d2b62015-02-18 11:12:02 -080081 /**
82 * Retrieve a list of faces and their status from the given forwarder; calls
83 * /localhost/nfd/faces/list which requires a local Face (all non-local
84 * packets are dropped)
85 *
86 * @param forwarder Only a localhost Face
87 * @return
88 * @throws Exception
89 */
90 public static List<FaceStatus> getFaceList(Face forwarder) throws Exception {
91 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
92 Interest interest = new Interest(new Name("/localhost/nfd/faces/list"));
93 interest.setMustBeFresh(true);
94 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
95 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080096
Andrew Brown211d2b62015-02-18 11:12:02 -080097 // send packet
Andrew Brown07466442015-02-24 09:07:02 -080098 Data data = SegmentedClient.getDefault().getSync(forwarder, interest);
Andrew Brown211d2b62015-02-18 11:12:02 -080099 if (data == null) {
100 throw new Exception("Failed to retrieve list of faces from the forwarder.");
101 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800102
andrewsbrown4c21ea22015-03-09 12:05:03 -0700103 // check for failed request
104 if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) {
105 ControlResponse response = new ControlResponse();
106 response.wireDecode(data.getContent().buf());
107 throw new Exception("Failed to retrieve list of faces, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText());
108 }
109
Andrew Brown211d2b62015-02-18 11:12:02 -0800110 // parse packet
111 return StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
112 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800113
Andrew Brown211d2b62015-02-18 11:12:02 -0800114 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800115 * Retrieve a list of FIB entries and their NextHopRecords from the given
116 * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all
117 * non-local packets are dropped).
118 *
119 * @param forwarder Only a localhost Face
120 * @return
121 * @throws Exception
122 */
123 public static List<FibEntry> getFibList(Face forwarder) throws Exception {
124 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
125 Interest interest = new Interest(new Name("/localhost/nfd/fib/list"));
126 interest.setMustBeFresh(true);
127 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
128 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
129
130 // TODO verify that all faces are being returned; right now they don't
131 // match up with the results from nfd-status-http-server but no
132 // additional segments are present;
133 // see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
134 // send packet
Andrew Brown07466442015-02-24 09:07:02 -0800135 Data data = SegmentedClient.getDefault().getSync(forwarder, interest);
Andrew Brown63bed362015-02-18 11:28:50 -0800136 if (data == null) {
andrewsbrown4c21ea22015-03-09 12:05:03 -0700137 throw new Exception("Failed to retrieve list of FIB entries from the forwarder.");
138 }
139
140 // check for failed request
141 if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) {
142 ControlResponse response = new ControlResponse();
143 response.wireDecode(data.getContent().buf());
144 throw new Exception("Failed to retrieve list of FIB entries, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText());
Andrew Brown63bed362015-02-18 11:28:50 -0800145 }
146
147 // parse packet
148 return StatusDataset.wireDecode(data.getContent(), FibEntry.class);
149 }
150
151 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800152 * Retrieve a list of routing entries from the RIB; calls
153 * /localhost/nfd/rib/list which requires a local Face (all non-local packets
154 * are dropped)
155 *
156 * @param forwarder Only a localhost Face
157 * @return
158 * @throws Exception
159 */
160 public static List<RibEntry> getRouteList(Face forwarder) throws Exception {
161 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
162 Interest interest = new Interest(new Name("/localhost/nfd/rib/list"));
163 interest.setMustBeFresh(true);
164 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
165 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
Andrew Brownc46c1602015-02-18 10:45:56 -0800166
Andrew Brown211d2b62015-02-18 11:12:02 -0800167 // send packet
Andrew Brown07466442015-02-24 09:07:02 -0800168 Data data = SegmentedClient.getDefault().getSync(forwarder, interest);
Andrew Brown211d2b62015-02-18 11:12:02 -0800169 if (data == null) {
andrewsbrown4c21ea22015-03-09 12:05:03 -0700170 throw new Exception("Failed to retrieve list of routes from the forwarder.");
171 }
172
173 // check for failed request
174 if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) {
175 ControlResponse response = new ControlResponse();
176 response.wireDecode(data.getContent().buf());
177 throw new Exception("Failed to retrieve list of routes, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText());
Andrew Brown211d2b62015-02-18 11:12:02 -0800178 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800179
Andrew Brown211d2b62015-02-18 11:12:02 -0800180 // parse packet
181 return StatusDataset.wireDecode(data.getContent(), RibEntry.class);
182 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800183
Andrew Brown211d2b62015-02-18 11:12:02 -0800184 /**
185 * Helper method to register a new face on the forwarder; as mentioned at
186 * http://named-data.net/doc/NFD/current/manpages/nfdc.html, this is more for
187 * debugging; use 'register' instead
188 *
189 * @param forwarder Only a localhost Face
190 * @param faceId
191 * @param prefix
192 * @return
193 * @throws Exception
194 */
195 public static boolean addNextHop(Face forwarder, int faceId, Name prefix) throws Exception {
196 // build command name
197 Name command = new Name("/localhost/nfd/fib/add-nexthop");
198 ControlParameters parameters = new ControlParameters();
199 parameters.setName(prefix);
200 parameters.setFaceId(faceId);
201 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800202
Andrew Brown211d2b62015-02-18 11:12:02 -0800203 // send the interest
204 return sendCommandAndErrorCheck(forwarder, new Interest(command));
205 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800206
Andrew Brown211d2b62015-02-18 11:12:02 -0800207 /**
208 * Create a new face on the given forwarder. Ensure the forwarding face is on
209 * the local machine (management requests are to /localhost/...) and that
210 * command signing has been set up (e.g. forwarder.setCommandSigningInfo()).
211 *
212 * @param forwarder Only a localhost Face
213 * @param uri
214 * @return
215 * @throws java.lang.Exception
216 */
217 public static int createFace(Face forwarder, String uri) throws Exception {
218 Name command = new Name("/localhost/nfd/faces/create");
219 ControlParameters parameters = new ControlParameters();
220 parameters.setUri(uri);
221 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800222
Andrew Brown211d2b62015-02-18 11:12:02 -0800223 // send the interest
224 ControlResponse response = sendCommand(forwarder, new Interest(command));
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800225
Andrew Brown211d2b62015-02-18 11:12:02 -0800226 // check for body and that status code is OK (TODO: 200 should be replaced with a CONSTANT like ControlResponse.STATUS_OK)
227 if (response.getBody().isEmpty() || response.getStatusCode() != 200) {
228 throw new Exception("Failed to create face: " + uri + " " + response.getStatusText());
229 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800230
Andrew Brown211d2b62015-02-18 11:12:02 -0800231 // return
232 return response.getBody().get(0).getFaceId();
233 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800234
Andrew Brown211d2b62015-02-18 11:12:02 -0800235 /**
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700236 * Create a new face on the given forwarder. Ensure the forwarding face is on
237 * the local machine (management requests are to /localhost/...) and that
238 * command signing has been set up (e.g. forwarder.setCommandSigningInfo()).
239 *
240 * @param forwarder Only a localhost Face
241 * @param faceId
242 * @throws java.lang.Exception
243 */
244 public static void destroyFace(Face forwarder, int faceId) throws Exception {
245 Name command = new Name("/localhost/nfd/faces/destroy");
246 ControlParameters parameters = new ControlParameters();
247 parameters.setFaceId(faceId);
248 command.append(parameters.wireEncode());
249
250 // send the interest
251 ControlResponse response = sendCommand(forwarder, new Interest(command));
252
253 // check for body and that status code is OK (TODO: 200 should be replaced with a CONSTANT like ControlResponse.STATUS_OK)
254 if (response.getBody().isEmpty() || response.getStatusCode() != 200) {
255 throw new Exception("Failed to destroy face: " + String.valueOf(faceId) + " " + response.getStatusText());
256 }
257
258 return;
259 }
260
261 /**
andrewsbrowne8e8e852015-03-09 13:48:31 -0700262 * Enable a local control feature on the given forwarder. See
263 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature
264 *
265 * @param forwarder
266 * @param header
267 * @return
268 * @throws Exception
269 */
270 public static boolean enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
271 // build command name
272 Name command = new Name("/localhost/nfd/faces/enable-local-control");
273 ControlParameters parameters = new ControlParameters();
274 parameters.setLocalControlFeature(header.getNumericValue());
275 command.append(parameters.wireEncode());
276
277 // send command and return
278 return sendCommandAndErrorCheck(forwarder, new Interest(command));
279 }
280
281 /**
282 * Disable a local control feature on the given forwarder. See
283 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature
284 *
285 * @param forwarder
286 * @param header
287 * @return
288 * @throws Exception
289 */
290 public static boolean disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
291 // build command name
292 Name command = new Name("/localhost/nfd/faces/disable-local-control");
293 ControlParameters parameters = new ControlParameters();
294 parameters.setLocalControlFeature(header.getNumericValue());
295 command.append(parameters.wireEncode());
296
297 // send command and return
298 return sendCommandAndErrorCheck(forwarder, new Interest(command));
299 }
300
301 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800302 * Register a route on the forwarder; see
303 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
304 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
305 * protocol documentation. Ensure the forwarding face is on the local machine
306 * (management requests are to /localhost/...) and that command signing has
307 * been set up (e.g. forwarder.setCommandSigningInfo()).
308 *
309 * @param forwarder Only a localhost Face
310 * @param controlParameters
311 * @return
312 * @throws Exception
313 */
314 public static boolean register(Face forwarder, ControlParameters controlParameters) throws Exception {
315 // build command name
316 Name command = new Name("/localhost/nfd/rib/register");
317 command.append(controlParameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800318
Andrew Brown211d2b62015-02-18 11:12:02 -0800319 // send the interest
320 return sendCommandAndErrorCheck(forwarder, new Interest(command));
321 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800322
Andrew Brown211d2b62015-02-18 11:12:02 -0800323 /**
324 * Register a route on a forwarder; this will create a new face on the
325 * forwarder to the given URI/route pair. See register(Face,
326 * ControlParameters) for more details documentation.
327 *
328 * @param forwarder Only a localhost Face
329 * @param uri
330 * @param cost
331 * @param route
332 * @return true for successful registration
333 * @throws java.lang.Exception
334 */
335 public static boolean register(Face forwarder, String uri, Name route, int cost) throws Exception {
336 // create the new face
337 int faceId = createFace(forwarder, uri);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800338
Andrew Brown211d2b62015-02-18 11:12:02 -0800339 // run base method
340 return register(forwarder, faceId, route, cost);
341 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800342
Andrew Brown211d2b62015-02-18 11:12:02 -0800343 /**
344 * Register a route on a forwarder; this will not create a new face since it
345 * is provided a faceId. See register(Face, ControlParameters) for full
346 * documentation
347 *
348 * @param forwarder Only a localhost Face
349 * @param faceId
350 * @param route
351 * @param cost
352 * @return true for successful registration
353 * @throws java.lang.Exception
354 */
355 public static boolean register(Face forwarder, int faceId, Name route, int cost) throws Exception {
356 // build command name
357 ControlParameters parameters = new ControlParameters();
358 parameters.setName(route);
359 parameters.setFaceId(faceId);
360 parameters.setCost(cost);
361 ForwardingFlags flags = new ForwardingFlags();
362 flags.setCapture(true);
363 flags.setChildInherit(true);
364 parameters.setForwardingFlags(flags);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800365
Andrew Brown211d2b62015-02-18 11:12:02 -0800366 // run base method
367 return register(forwarder, parameters);
368 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800369
Andrew Brown211d2b62015-02-18 11:12:02 -0800370 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800371 * Unregister a route on a forwarder; see
372 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
373 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
374 * protocol documentation. Ensure the forwarding face is on the local machine
375 * (management requests are to /localhost/...) and that command signing has
376 * been set up (e.g. forwarder.setCommandSigningInfo()
377 *
378 * @param forwarder
Andrew Brown07466442015-02-24 09:07:02 -0800379 * @param controlParameters
Andrew Brown63bed362015-02-18 11:28:50 -0800380 * @return
381 */
Andrew Brown07466442015-02-24 09:07:02 -0800382 public static boolean unregister(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown63bed362015-02-18 11:28:50 -0800383 // build command name
384 Name command = new Name("/localhost/nfd/rib/unregister");
385 command.append(controlParameters.wireEncode());
386
387 // send the interest
388 return sendCommandAndErrorCheck(forwarder, new Interest(command));
389 }
390
391 /**
Andrew Brown07466442015-02-24 09:07:02 -0800392 * Unregister a route on a forwarder; see
393 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
394 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
395 * protocol documentation. Ensure the forwarding face is on the local machine
396 * (management requests are to /localhost/...) and that command signing has
397 * been set up (e.g. forwarder.setCommandSigningInfo()
398 *
399 * @param forwarder
400 * @param route
401 * @return
402 */
403 public static boolean unregister(Face forwarder, Name route) throws Exception {
404 // build command name
405 ControlParameters controlParameters = new ControlParameters();
406 controlParameters.setName(route);
407
408 // send the interest
409 return unregister(forwarder, controlParameters);
410 }
411
412 /**
413 * Unregister a route on a forwarder; see
414 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
415 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
416 * protocol documentation. Ensure the forwarding face is on the local machine
417 * (management requests are to /localhost/...) and that command signing has
418 * been set up (e.g. forwarder.setCommandSigningInfo()
419 *
420 * @param forwarder
421 * @param route
422 * @param faceId
423 * @return
424 */
425 public static boolean unregister(Face forwarder, Name route, int faceId) throws Exception {
426 // build command name
427 ControlParameters controlParameters = new ControlParameters();
428 controlParameters.setName(route);
429 controlParameters.setFaceId(faceId);
430
431 // send the interest
432 return unregister(forwarder, controlParameters);
433 }
434
435 /**
436 * Unregister a route on a forwarder; see
437 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
438 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
439 * protocol documentation. Ensure the forwarding face is on the local machine
440 * (management requests are to /localhost/...) and that command signing has
441 * been set up (e.g. forwarder.setCommandSigningInfo()
442 *
443 * @param forwarder
444 * @param route
445 * @param uri
446 * @return
447 */
448 public static boolean unregister(Face forwarder, Name route, String uri) throws Exception {
449 int faceId = -1;
andrewsbrown4c21ea22015-03-09 12:05:03 -0700450 for (FaceStatus face : getFaceList(forwarder)) {
451 if (face.getUri().matches(uri)) {
Andrew Brown07466442015-02-24 09:07:02 -0800452 faceId = face.getFaceId();
453 break;
454 }
455 }
andrewsbrown4c21ea22015-03-09 12:05:03 -0700456
457 if (faceId == -1) {
Andrew Brown07466442015-02-24 09:07:02 -0800458 throw new Exception("Face not found: " + uri);
459 }
460
461 // send the interest
462 return unregister(forwarder, route, faceId);
463 }
464
465 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800466 * Set a strategy on the forwarder; see
467 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
468 * usage and http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice
469 * for protocol documentation. Ensure the forwarding face is on the local
470 * machine (management requests are to /localhost/...) and that command
471 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
472 *
473 * @param forwarder Only a localhost Face
474 * @param prefix
475 * @param strategy
476 * @return true for successful command
477 * @throws Exception
478 */
479 public static boolean setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception {
480 // build command name
481 Name command = new Name("/localhost/nfd/strategy-choice/set");
482 ControlParameters parameters = new ControlParameters();
483 parameters.setName(prefix);
484 parameters.setStrategy(strategy);
485 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800486
Andrew Brown211d2b62015-02-18 11:12:02 -0800487 // send the interest
488 return sendCommandAndErrorCheck(forwarder, new Interest(command));
489 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800490
Andrew Brown211d2b62015-02-18 11:12:02 -0800491 /**
492 * Send an interest as a command to the forwarder; this method will convert
493 * the interest to a command interest and block until a response is received
494 * from the forwarder. Ensure the forwarding face is on the local machine
495 * (management requests are to /localhost/...) and that command signing has
496 * been set up (e.g. forwarder.setCommandSigningInfo()).
497 *
498 * @param forwarder Only a localhost Face
499 * @param interest As described at
500 * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the
501 * requested interest must have encoded ControlParameters appended to the
502 * interest name
503 * @return
504 * @throws net.named_data.jndn.security.SecurityException
505 * @throws java.io.IOException
506 * @throws net.named_data.jndn.encoding.EncodingException
507 */
508 public static ControlResponse sendCommand(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException {
509 forwarder.makeCommandInterest(interest);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800510
Andrew Brown211d2b62015-02-18 11:12:02 -0800511 // send command packet
512 Data data = Client.getDefault().getSync(forwarder, interest);
513 if (data == null) {
514 throw new IOException("Failed to receive command response.");
515 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800516
Andrew Brown211d2b62015-02-18 11:12:02 -0800517 // return response
518 ControlResponse response = new ControlResponse();
519 response.wireDecode(data.getContent().buf());
520 return response;
521 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800522
Andrew Brown211d2b62015-02-18 11:12:02 -0800523 /**
524 * Send an interest as a command to the forwarder; this method will convert
525 * the interest to a command interest and block until a response is received
526 * from the forwarder.
527 *
528 * @param forwarder Only a localhost Face
529 * @param interest As described at
530 * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the
531 * requested interest must have encoded ControlParameters appended to the
532 * interest name
533 * @return
534 * @throws net.named_data.jndn.security.SecurityException
535 * @throws java.io.IOException
536 * @throws net.named_data.jndn.encoding.EncodingException
537 */
538 public static boolean sendCommandAndErrorCheck(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException {
539 ControlResponse response = sendCommand(forwarder, interest);
540 if (response.getStatusCode() < 400) {
541 return true;
542 } else {
543 logger.warning("Command sent but failed: " + response.getStatusCode() + " " + response.getStatusText());
544 return false;
545 }
546 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800547}