blob: 026faca80e518eec11fff028c81736cbe449f3e1 [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 /**
andrewsbrowne8e8e852015-03-09 13:48:31 -0700236 * Enable a local control feature on the given forwarder. See
237 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature
238 *
239 * @param forwarder
240 * @param header
241 * @return
242 * @throws Exception
243 */
244 public static boolean enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
245 // build command name
246 Name command = new Name("/localhost/nfd/faces/enable-local-control");
247 ControlParameters parameters = new ControlParameters();
248 parameters.setLocalControlFeature(header.getNumericValue());
249 command.append(parameters.wireEncode());
250
251 // send command and return
252 return sendCommandAndErrorCheck(forwarder, new Interest(command));
253 }
254
255 /**
256 * Disable a local control feature on the given forwarder. See
257 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature
258 *
259 * @param forwarder
260 * @param header
261 * @return
262 * @throws Exception
263 */
264 public static boolean disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
265 // build command name
266 Name command = new Name("/localhost/nfd/faces/disable-local-control");
267 ControlParameters parameters = new ControlParameters();
268 parameters.setLocalControlFeature(header.getNumericValue());
269 command.append(parameters.wireEncode());
270
271 // send command and return
272 return sendCommandAndErrorCheck(forwarder, new Interest(command));
273 }
274
275 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800276 * Register a route on the forwarder; see
277 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
278 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
279 * protocol documentation. Ensure the forwarding face is on the local machine
280 * (management requests are to /localhost/...) and that command signing has
281 * been set up (e.g. forwarder.setCommandSigningInfo()).
282 *
283 * @param forwarder Only a localhost Face
284 * @param controlParameters
285 * @return
286 * @throws Exception
287 */
288 public static boolean register(Face forwarder, ControlParameters controlParameters) throws Exception {
289 // build command name
290 Name command = new Name("/localhost/nfd/rib/register");
291 command.append(controlParameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800292
Andrew Brown211d2b62015-02-18 11:12:02 -0800293 // send the interest
294 return sendCommandAndErrorCheck(forwarder, new Interest(command));
295 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800296
Andrew Brown211d2b62015-02-18 11:12:02 -0800297 /**
298 * Register a route on a forwarder; this will create a new face on the
299 * forwarder to the given URI/route pair. See register(Face,
300 * ControlParameters) for more details documentation.
301 *
302 * @param forwarder Only a localhost Face
303 * @param uri
304 * @param cost
305 * @param route
306 * @return true for successful registration
307 * @throws java.lang.Exception
308 */
309 public static boolean register(Face forwarder, String uri, Name route, int cost) throws Exception {
310 // create the new face
311 int faceId = createFace(forwarder, uri);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800312
Andrew Brown211d2b62015-02-18 11:12:02 -0800313 // run base method
314 return register(forwarder, faceId, route, cost);
315 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800316
Andrew Brown211d2b62015-02-18 11:12:02 -0800317 /**
318 * Register a route on a forwarder; this will not create a new face since it
319 * is provided a faceId. See register(Face, ControlParameters) for full
320 * documentation
321 *
322 * @param forwarder Only a localhost Face
323 * @param faceId
324 * @param route
325 * @param cost
326 * @return true for successful registration
327 * @throws java.lang.Exception
328 */
329 public static boolean register(Face forwarder, int faceId, Name route, int cost) throws Exception {
330 // build command name
331 ControlParameters parameters = new ControlParameters();
332 parameters.setName(route);
333 parameters.setFaceId(faceId);
334 parameters.setCost(cost);
335 ForwardingFlags flags = new ForwardingFlags();
336 flags.setCapture(true);
337 flags.setChildInherit(true);
338 parameters.setForwardingFlags(flags);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800339
Andrew Brown211d2b62015-02-18 11:12:02 -0800340 // run base method
341 return register(forwarder, parameters);
342 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800343
Andrew Brown211d2b62015-02-18 11:12:02 -0800344 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800345 * Unregister a route on a forwarder; see
346 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
347 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
348 * protocol documentation. Ensure the forwarding face is on the local machine
349 * (management requests are to /localhost/...) and that command signing has
350 * been set up (e.g. forwarder.setCommandSigningInfo()
351 *
352 * @param forwarder
Andrew Brown07466442015-02-24 09:07:02 -0800353 * @param controlParameters
Andrew Brown63bed362015-02-18 11:28:50 -0800354 * @return
355 */
Andrew Brown07466442015-02-24 09:07:02 -0800356 public static boolean unregister(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown63bed362015-02-18 11:28:50 -0800357 // build command name
358 Name command = new Name("/localhost/nfd/rib/unregister");
359 command.append(controlParameters.wireEncode());
360
361 // send the interest
362 return sendCommandAndErrorCheck(forwarder, new Interest(command));
363 }
364
365 /**
Andrew Brown07466442015-02-24 09:07:02 -0800366 * Unregister a route on a forwarder; see
367 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
368 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
369 * protocol documentation. Ensure the forwarding face is on the local machine
370 * (management requests are to /localhost/...) and that command signing has
371 * been set up (e.g. forwarder.setCommandSigningInfo()
372 *
373 * @param forwarder
374 * @param route
375 * @return
376 */
377 public static boolean unregister(Face forwarder, Name route) throws Exception {
378 // build command name
379 ControlParameters controlParameters = new ControlParameters();
380 controlParameters.setName(route);
381
382 // send the interest
383 return unregister(forwarder, controlParameters);
384 }
385
386 /**
387 * Unregister a route on a forwarder; see
388 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
389 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
390 * protocol documentation. Ensure the forwarding face is on the local machine
391 * (management requests are to /localhost/...) and that command signing has
392 * been set up (e.g. forwarder.setCommandSigningInfo()
393 *
394 * @param forwarder
395 * @param route
396 * @param faceId
397 * @return
398 */
399 public static boolean unregister(Face forwarder, Name route, int faceId) throws Exception {
400 // build command name
401 ControlParameters controlParameters = new ControlParameters();
402 controlParameters.setName(route);
403 controlParameters.setFaceId(faceId);
404
405 // send the interest
406 return unregister(forwarder, controlParameters);
407 }
408
409 /**
410 * Unregister a route on a forwarder; see
411 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
412 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
413 * protocol documentation. Ensure the forwarding face is on the local machine
414 * (management requests are to /localhost/...) and that command signing has
415 * been set up (e.g. forwarder.setCommandSigningInfo()
416 *
417 * @param forwarder
418 * @param route
419 * @param uri
420 * @return
421 */
422 public static boolean unregister(Face forwarder, Name route, String uri) throws Exception {
423 int faceId = -1;
andrewsbrown4c21ea22015-03-09 12:05:03 -0700424 for (FaceStatus face : getFaceList(forwarder)) {
425 if (face.getUri().matches(uri)) {
Andrew Brown07466442015-02-24 09:07:02 -0800426 faceId = face.getFaceId();
427 break;
428 }
429 }
andrewsbrown4c21ea22015-03-09 12:05:03 -0700430
431 if (faceId == -1) {
Andrew Brown07466442015-02-24 09:07:02 -0800432 throw new Exception("Face not found: " + uri);
433 }
434
435 // send the interest
436 return unregister(forwarder, route, faceId);
437 }
438
439 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800440 * Set a strategy on the forwarder; see
441 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
442 * usage and http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice
443 * for protocol documentation. Ensure the forwarding face is on the local
444 * machine (management requests are to /localhost/...) and that command
445 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
446 *
447 * @param forwarder Only a localhost Face
448 * @param prefix
449 * @param strategy
450 * @return true for successful command
451 * @throws Exception
452 */
453 public static boolean setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception {
454 // build command name
455 Name command = new Name("/localhost/nfd/strategy-choice/set");
456 ControlParameters parameters = new ControlParameters();
457 parameters.setName(prefix);
458 parameters.setStrategy(strategy);
459 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800460
Andrew Brown211d2b62015-02-18 11:12:02 -0800461 // send the interest
462 return sendCommandAndErrorCheck(forwarder, new Interest(command));
463 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800464
Andrew Brown211d2b62015-02-18 11:12:02 -0800465 /**
466 * Send an interest as a command to the forwarder; this method will convert
467 * the interest to a command interest and block until a response is received
468 * from the forwarder. Ensure the forwarding face is on the local machine
469 * (management requests are to /localhost/...) and that command signing has
470 * been set up (e.g. forwarder.setCommandSigningInfo()).
471 *
472 * @param forwarder Only a localhost Face
473 * @param interest As described at
474 * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the
475 * requested interest must have encoded ControlParameters appended to the
476 * interest name
477 * @return
478 * @throws net.named_data.jndn.security.SecurityException
479 * @throws java.io.IOException
480 * @throws net.named_data.jndn.encoding.EncodingException
481 */
482 public static ControlResponse sendCommand(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException {
483 forwarder.makeCommandInterest(interest);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800484
Andrew Brown211d2b62015-02-18 11:12:02 -0800485 // send command packet
486 Data data = Client.getDefault().getSync(forwarder, interest);
487 if (data == null) {
488 throw new IOException("Failed to receive command response.");
489 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800490
Andrew Brown211d2b62015-02-18 11:12:02 -0800491 // return response
492 ControlResponse response = new ControlResponse();
493 response.wireDecode(data.getContent().buf());
494 return response;
495 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800496
Andrew Brown211d2b62015-02-18 11:12:02 -0800497 /**
498 * Send an interest as a command to the forwarder; this method will convert
499 * the interest to a command interest and block until a response is received
500 * from the forwarder.
501 *
502 * @param forwarder Only a localhost Face
503 * @param interest As described at
504 * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the
505 * requested interest must have encoded ControlParameters appended to the
506 * interest name
507 * @return
508 * @throws net.named_data.jndn.security.SecurityException
509 * @throws java.io.IOException
510 * @throws net.named_data.jndn.encoding.EncodingException
511 */
512 public static boolean sendCommandAndErrorCheck(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException {
513 ControlResponse response = sendCommand(forwarder, interest);
514 if (response.getStatusCode() < 400) {
515 return true;
516 } else {
517 logger.warning("Command sent but failed: " + response.getStatusCode() + " " + response.getStatusText());
518 return false;
519 }
520 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800521}