Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 7e6b9e8 | 2015-03-03 16:11:11 -0800 | [diff] [blame] | 2 | * 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 16 | import com.intel.jndn.management.types.StatusDataset; |
| 17 | import com.intel.jndn.management.types.ControlResponse; |
| 18 | import com.intel.jndn.management.types.FaceStatus; |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 19 | import com.intel.jndn.management.types.FibEntry; |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 20 | import com.intel.jndn.management.types.ForwarderStatus; |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 21 | import com.intel.jndn.management.types.LocalControlHeader; |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 22 | import com.intel.jndn.management.types.RibEntry; |
andrewsbrown | 39b0fc9 | 2015-05-11 13:44:38 -0700 | [diff] [blame] | 23 | import com.intel.jndn.management.types.StrategyChoice; |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 24 | import com.intel.jndn.utils.SimpleClient; |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 25 | import com.intel.jndn.utils.SegmentedClient; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.util.List; |
| 28 | import net.named_data.jndn.ControlParameters; |
| 29 | import net.named_data.jndn.Data; |
| 30 | import net.named_data.jndn.Face; |
| 31 | import net.named_data.jndn.ForwardingFlags; |
| 32 | import net.named_data.jndn.Interest; |
| 33 | import net.named_data.jndn.Name; |
| 34 | import net.named_data.jndn.encoding.EncodingException; |
| 35 | import net.named_data.jndn.security.SecurityException; |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 36 | import java.util.logging.Logger; |
Andrew Brown | c5dda70 | 2015-04-13 09:36:04 -0700 | [diff] [blame] | 37 | import net.named_data.jndn.KeyLocator; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Helper class for interacting with an NDN forwarder daemon; see |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 41 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/Management">http://redmine.named-data.net/projects/nfd/wiki/Management</a> |
| 42 | * for explanations of the various protocols used. |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 43 | * |
| 44 | * @author Andrew Brown <andrew.brown@intel.com> |
| 45 | */ |
| 46 | public class NFD { |
| 47 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 48 | public final static long DEFAULT_TIMEOUT = 2000; |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 49 | public final static int OK_STATUS = 200; |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 50 | static private final Logger logger = Logger.getLogger(NFD.class.getName()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 51 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Ping a forwarder on an existing face to verify that the forwarder is |
| 54 | * working and responding to requests; this version sends a discovery packet |
| 55 | * to /localhost/nfd which should always respond if the requestor is on the |
| 56 | * same machine as the NDN forwarding daemon. |
| 57 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 58 | * @param face only a localhost Face |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 59 | * @return true if successful, false otherwise |
| 60 | */ |
| 61 | public static boolean pingLocal(Face face) { |
| 62 | return ping(face, new Name("/localhost/nfd")); |
| 63 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 64 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Request a name on an existing face to verify the forwarder is working and |
| 67 | * responding to requests. Note that the name must be served or cached on the |
| 68 | * forwarder for this to return true. |
| 69 | * |
| 70 | * @param face |
| 71 | * @param name |
| 72 | * @return true if successful, false otherwise |
| 73 | */ |
| 74 | public static boolean ping(Face face, Name name) { |
| 75 | // build interest |
| 76 | Interest interest = new Interest(name); |
| 77 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
| 78 | interest.setMustBeFresh(true); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 79 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 80 | // send packet |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 81 | try { |
| 82 | Data data = SimpleClient.getDefault().getSync(face, interest); |
| 83 | return data != null; |
| 84 | } catch (IOException e) { |
| 85 | return false; |
| 86 | } |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 87 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 88 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 89 | /** |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 90 | * Retrieve the status of the given forwarder; calls /localhost/nfd/status |
| 91 | * which requires a local Face (all non-local packets are dropped) |
| 92 | * |
| 93 | * @param forwarder only a localhost Face |
| 94 | * @return the forwarder status object, see |
Andrew Brown | dfa8fc1 | 2015-03-25 10:46:47 -0700 | [diff] [blame] | 95 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus"> |
| 96 | * http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus</a>. |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 97 | * @throws java.lang.Exception |
| 98 | */ |
| 99 | public static ForwarderStatus getForwarderStatus(Face forwarder) throws Exception { |
Andrew Brown | c5dda70 | 2015-04-13 09:36:04 -0700 | [diff] [blame] | 100 | Data data = retrieveStatus(forwarder); |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 101 | ForwarderStatus status = new ForwarderStatus(); |
| 102 | status.wireDecode(data.getContent().buf()); |
| 103 | return status; |
| 104 | } |
| 105 | |
| 106 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 107 | * Retrieve a list of faces and their status from the given forwarder; calls |
| 108 | * /localhost/nfd/faces/list which requires a local Face (all non-local |
| 109 | * packets are dropped) |
| 110 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 111 | * @param forwarder only a localhost Face |
| 112 | * @return a list of face status objects, see |
| 113 | * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt. |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 114 | * @throws java.lang.Exception |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 115 | */ |
| 116 | public static List<FaceStatus> getFaceList(Face forwarder) throws Exception { |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 117 | Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/faces/list")); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 118 | return StatusDataset.wireDecode(data.getContent(), FaceStatus.class); |
| 119 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 120 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 121 | /** |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 122 | * Retrieve a list of FIB entries and their NextHopRecords from the given |
| 123 | * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all |
| 124 | * non-local packets are dropped). |
| 125 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 126 | * @param forwarder only a localhost Face |
| 127 | * @return a list of FIB entries, see |
| 128 | * http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset. |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 129 | * @throws java.lang.Exception |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 130 | */ |
| 131 | public static List<FibEntry> getFibList(Face forwarder) throws Exception { |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 132 | Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/fib/list")); |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 133 | return StatusDataset.wireDecode(data.getContent(), FibEntry.class); |
| 134 | } |
| 135 | |
| 136 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 137 | * Retrieve a list of routing entries from the RIB; calls |
| 138 | * /localhost/nfd/rib/list which requires a local Face (all non-local packets |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 139 | * are dropped). |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 140 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 141 | * @param forwarder only a localhost Face |
| 142 | * @return a list of RIB entries, i.e. routes, see |
| 143 | * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset. |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 144 | * @throws java.lang.Exception |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 145 | */ |
| 146 | public static List<RibEntry> getRouteList(Face forwarder) throws Exception { |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 147 | Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/rib/list")); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 148 | return StatusDataset.wireDecode(data.getContent(), RibEntry.class); |
| 149 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 150 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 151 | /** |
andrewsbrown | 39b0fc9 | 2015-05-11 13:44:38 -0700 | [diff] [blame] | 152 | * Retrieve the list of strategy choice entries from the NFD; 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 a list of strategy choice entries, i.e. routes, see |
| 158 | * http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice. |
| 159 | * @throws java.lang.Exception |
| 160 | */ |
| 161 | public static List<StrategyChoice> getStrategyList(Face forwarder) throws Exception { |
| 162 | Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/strategy-choice/list")); |
| 163 | return StatusDataset.wireDecode(data.getContent(), StrategyChoice.class); |
| 164 | } |
| 165 | |
| 166 | /** |
Andrew Brown | c5dda70 | 2015-04-13 09:36:04 -0700 | [diff] [blame] | 167 | * Retrieve the {@link KeyLocator} for an NFD. |
| 168 | * |
| 169 | * @param forwarder only a localhost {@link Face} |
| 170 | * @return the {@link KeyLocator} of the NFD's key |
| 171 | * @throws ManagementException if the key is not available |
| 172 | * @throws IOException if the request fails |
| 173 | */ |
| 174 | public static KeyLocator getKeyLocator(Face forwarder) throws ManagementException, IOException { |
| 175 | Data data = retrieveStatus(forwarder); |
| 176 | if (!KeyLocator.canGetFromSignature(data.getSignature())) { |
| 177 | throw new ManagementException("No key locator available."); |
| 178 | } |
| 179 | return KeyLocator.getFromSignature(data.getSignature()); |
| 180 | } |
| 181 | |
| 182 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 183 | * Helper method to register a new face on the forwarder; as mentioned at |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 184 | * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>, |
| 185 | * this is more for debugging; use 'register' instead |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 186 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 187 | * @param forwarder only a localhost Face |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 188 | * @param faceId |
| 189 | * @param prefix |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 190 | * @throws java.lang.Exception |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 191 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 192 | public static void addNextHop(Face forwarder, int faceId, Name prefix) throws Exception { |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 193 | // build command name |
| 194 | Name command = new Name("/localhost/nfd/fib/add-nexthop"); |
| 195 | ControlParameters parameters = new ControlParameters(); |
| 196 | parameters.setName(prefix); |
| 197 | parameters.setFaceId(faceId); |
| 198 | command.append(parameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 199 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 200 | // send the interest |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 201 | sendCommand(forwarder, new Interest(command)); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 202 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 203 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Create a new face on the given forwarder. Ensure the forwarding face is on |
| 206 | * the local machine (management requests are to /localhost/...) and that |
| 207 | * command signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 208 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 209 | * @param forwarder only a localhost Face |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 210 | * @param uri |
| 211 | * @return |
| 212 | * @throws java.lang.Exception |
| 213 | */ |
| 214 | public static int createFace(Face forwarder, String uri) throws Exception { |
| 215 | Name command = new Name("/localhost/nfd/faces/create"); |
| 216 | ControlParameters parameters = new ControlParameters(); |
| 217 | parameters.setUri(uri); |
| 218 | command.append(parameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 219 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 220 | // send the interest |
| 221 | ControlResponse response = sendCommand(forwarder, new Interest(command)); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 222 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 223 | // return |
| 224 | return response.getBody().get(0).getFaceId(); |
| 225 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 226 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 227 | /** |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 228 | * Destroy a face on given forwarder. Ensure the forwarding face is on the |
| 229 | * local machine (management requests are to /localhost/...) and that command |
| 230 | * signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
Alexander Afanasyev | 75d8dfc | 2015-03-13 16:41:01 -0700 | [diff] [blame] | 231 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 232 | * @param forwarder only a localhost Face |
Alexander Afanasyev | 75d8dfc | 2015-03-13 16:41:01 -0700 | [diff] [blame] | 233 | * @param faceId |
| 234 | * @throws java.lang.Exception |
| 235 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 236 | public static void destroyFace(Face forwarder, int faceId) throws Exception { |
Alexander Afanasyev | 75d8dfc | 2015-03-13 16:41:01 -0700 | [diff] [blame] | 237 | Name command = new Name("/localhost/nfd/faces/destroy"); |
| 238 | ControlParameters parameters = new ControlParameters(); |
| 239 | parameters.setFaceId(faceId); |
| 240 | command.append(parameters.wireEncode()); |
| 241 | |
| 242 | // send the interest |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 243 | sendCommand(forwarder, new Interest(command)); |
Alexander Afanasyev | 75d8dfc | 2015-03-13 16:41:01 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 247 | * Enable a local control feature on the given forwarder. See |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 248 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature">http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature</a> |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 249 | * |
| 250 | * @param forwarder |
| 251 | * @param header |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 252 | * @throws Exception |
| 253 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 254 | public static void enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception { |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 255 | // build command name |
| 256 | Name command = new Name("/localhost/nfd/faces/enable-local-control"); |
| 257 | ControlParameters parameters = new ControlParameters(); |
| 258 | parameters.setLocalControlFeature(header.getNumericValue()); |
| 259 | command.append(parameters.wireEncode()); |
| 260 | |
| 261 | // send command and return |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 262 | sendCommand(forwarder, new Interest(command)); |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Disable a local control feature on the given forwarder. See |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 267 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature">http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature</a> |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 268 | * |
| 269 | * @param forwarder |
| 270 | * @param header |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 271 | * @throws Exception |
| 272 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 273 | public static void disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception { |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 274 | // build command name |
| 275 | Name command = new Name("/localhost/nfd/faces/disable-local-control"); |
| 276 | ControlParameters parameters = new ControlParameters(); |
| 277 | parameters.setLocalControlFeature(header.getNumericValue()); |
| 278 | command.append(parameters.wireEncode()); |
| 279 | |
| 280 | // send command and return |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 281 | sendCommand(forwarder, new Interest(command)); |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 285 | * Register a route on the forwarder; see |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 286 | * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a> |
| 287 | * for command-line usage and |
| 288 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a> |
| 289 | * for protocol documentation. Ensure the forwarding face is on the local |
| 290 | * machine (management requests are to /localhost/...) and that command |
| 291 | * signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 292 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 293 | * @param forwarder only a localhost Face |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 294 | * @param controlParameters |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 295 | * @throws Exception |
| 296 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 297 | public static void register(Face forwarder, ControlParameters controlParameters) throws Exception { |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 298 | // build command name |
| 299 | Name command = new Name("/localhost/nfd/rib/register"); |
| 300 | command.append(controlParameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 301 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 302 | // send the interest |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 303 | sendCommand(forwarder, new Interest(command)); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 304 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 305 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 306 | /** |
| 307 | * Register a route on a forwarder; this will create a new face on the |
| 308 | * forwarder to the given URI/route pair. See register(Face, |
| 309 | * ControlParameters) for more details documentation. |
| 310 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 311 | * @param forwarder only a localhost Face |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 312 | * @param uri |
| 313 | * @param cost |
| 314 | * @param route |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 315 | * @throws java.lang.Exception |
| 316 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 317 | public static void register(Face forwarder, String uri, Name route, int cost) throws Exception { |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 318 | // create the new face |
| 319 | int faceId = createFace(forwarder, uri); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 320 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 321 | // run base method |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 322 | register(forwarder, faceId, route, cost); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 323 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 324 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Register a route on a forwarder; this will not create a new face since it |
| 327 | * is provided a faceId. See register(Face, ControlParameters) for full |
| 328 | * documentation |
| 329 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 330 | * @param forwarder only a localhost Face |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 331 | * @param faceId |
| 332 | * @param route |
| 333 | * @param cost |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 334 | * @throws java.lang.Exception |
| 335 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 336 | public static void register(Face forwarder, int faceId, Name route, int cost) throws Exception { |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 337 | // build command name |
| 338 | ControlParameters parameters = new ControlParameters(); |
| 339 | parameters.setName(route); |
| 340 | parameters.setFaceId(faceId); |
| 341 | parameters.setCost(cost); |
| 342 | ForwardingFlags flags = new ForwardingFlags(); |
| 343 | flags.setCapture(true); |
| 344 | flags.setChildInherit(true); |
| 345 | parameters.setForwardingFlags(flags); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 346 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 347 | // run base method |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 348 | register(forwarder, parameters); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 349 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 350 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 351 | /** |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 352 | * Unregister a route on a forwarder; see |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 353 | * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a> |
| 354 | * for command-line usage and |
| 355 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a> |
| 356 | * for protocol documentation. Ensure the forwarding face is on the local |
| 357 | * machine (management requests are to /localhost/...) and that command |
| 358 | * signing has been set up (e.g. forwarder.setCommandSigningInfo() |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 359 | * |
| 360 | * @param forwarder |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 361 | * @param controlParameters |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 362 | * @throws java.lang.Exception |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 363 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 364 | public static void unregister(Face forwarder, ControlParameters controlParameters) throws Exception { |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 365 | // build command name |
| 366 | Name command = new Name("/localhost/nfd/rib/unregister"); |
| 367 | command.append(controlParameters.wireEncode()); |
| 368 | |
| 369 | // send the interest |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 370 | sendCommand(forwarder, new Interest(command)); |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /** |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 374 | * Unregister a route on a forwarder; see |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 375 | * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a> |
| 376 | * for command-line usage and |
| 377 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a> |
| 378 | * for protocol documentation. Ensure the forwarding face is on the local |
| 379 | * machine (management requests are to /localhost/...) and that command |
| 380 | * signing has been set up (e.g. forwarder.setCommandSigningInfo() |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 381 | * |
| 382 | * @param forwarder |
| 383 | * @param route |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 384 | * @throws java.lang.Exception |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 385 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 386 | public static void unregister(Face forwarder, Name route) throws Exception { |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 387 | // build command name |
| 388 | ControlParameters controlParameters = new ControlParameters(); |
| 389 | controlParameters.setName(route); |
| 390 | |
| 391 | // send the interest |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 392 | unregister(forwarder, controlParameters); |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Unregister a route on a forwarder; see |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 397 | * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a> |
| 398 | * for command-line usage and |
| 399 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a> |
| 400 | * for protocol documentation. Ensure the forwarding face is on the local |
| 401 | * machine (management requests are to /localhost/...) and that command |
| 402 | * signing has been set up (e.g. forwarder.setCommandSigningInfo() |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 403 | * |
| 404 | * @param forwarder |
| 405 | * @param route |
| 406 | * @param faceId |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 407 | * @throws java.lang.Exception |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 408 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 409 | public static void unregister(Face forwarder, Name route, int faceId) throws Exception { |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 410 | // build command name |
| 411 | ControlParameters controlParameters = new ControlParameters(); |
| 412 | controlParameters.setName(route); |
| 413 | controlParameters.setFaceId(faceId); |
| 414 | |
| 415 | // send the interest |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 416 | unregister(forwarder, controlParameters); |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Unregister a route on a forwarder; see |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 421 | * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a> |
| 422 | * for command-line usage and |
| 423 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a> |
| 424 | * for protocol documentation. Ensure the forwarding face is on the local |
| 425 | * machine (management requests are to /localhost/...) and that command |
| 426 | * signing has been set up (e.g. forwarder.setCommandSigningInfo() |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 427 | * |
| 428 | * @param forwarder |
| 429 | * @param route |
| 430 | * @param uri |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 431 | * @throws java.lang.Exception |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 432 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 433 | public static void unregister(Face forwarder, Name route, String uri) throws Exception { |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 434 | int faceId = -1; |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 435 | for (FaceStatus face : getFaceList(forwarder)) { |
| 436 | if (face.getUri().matches(uri)) { |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 437 | faceId = face.getFaceId(); |
| 438 | break; |
| 439 | } |
| 440 | } |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 441 | |
| 442 | if (faceId == -1) { |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 443 | throw new ManagementException("Face not found: " + uri); |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // send the interest |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 447 | unregister(forwarder, route, faceId); |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 451 | * Set a strategy on the forwarder; see |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 452 | * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a> |
| 453 | * for command-line usage and |
| 454 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice">http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice</a> |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 455 | * for protocol documentation. Ensure the forwarding face is on the local |
| 456 | * machine (management requests are to /localhost/...) and that command |
| 457 | * signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 458 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 459 | * @param forwarder only a localhost Face |
andrewsbrown | cad04eb | 2015-05-11 13:16:24 -0700 | [diff] [blame] | 460 | * @param prefix the {@link Name} prefix |
andrewsbrown | 39b0fc9 | 2015-05-11 13:44:38 -0700 | [diff] [blame] | 461 | * @param strategy the {@link Name} of the strategy to set, e.g. |
| 462 | * /localhost/nfd/strategy/broadcast |
andrewsbrown | cad04eb | 2015-05-11 13:16:24 -0700 | [diff] [blame] | 463 | * @throws Exception if the command fails |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 464 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 465 | public static void setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception { |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 466 | // build command name |
| 467 | Name command = new Name("/localhost/nfd/strategy-choice/set"); |
| 468 | ControlParameters parameters = new ControlParameters(); |
| 469 | parameters.setName(prefix); |
| 470 | parameters.setStrategy(strategy); |
| 471 | command.append(parameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 472 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 473 | // send the interest |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 474 | sendCommand(forwarder, new Interest(command)); |
| 475 | } |
| 476 | |
| 477 | /** |
andrewsbrown | cad04eb | 2015-05-11 13:16:24 -0700 | [diff] [blame] | 478 | * Set a strategy on the forwarder; see |
| 479 | * {@link #setStrategy(net.named_data.jndn.Face, net.named_data.jndn.Name, net.named_data.jndn.Name)} |
| 480 | * for more information. Ensure the forwarding face is on the local machine |
| 481 | * (management requests are to /localhost/...) and that command signing has |
| 482 | * been set up (e.g. forwarder.setCommandSigningInfo()). |
| 483 | * |
| 484 | * @param forwarder only a localhost {@link Face} |
| 485 | * @param prefix the {@link Name} prefix |
| 486 | * @throws Exception if the command fails |
| 487 | */ |
| 488 | public static void unsetStrategy(Face forwarder, Name prefix) throws Exception { |
| 489 | // build command name |
Andrew Brown | 37cec24 | 2015-05-11 14:24:13 -0700 | [diff] [blame^] | 490 | Name command = new Name("/localhost/nfd/strategy-choice/unset"); |
andrewsbrown | cad04eb | 2015-05-11 13:16:24 -0700 | [diff] [blame] | 491 | ControlParameters parameters = new ControlParameters(); |
| 492 | parameters.setName(prefix); |
| 493 | command.append(parameters.wireEncode()); |
| 494 | |
| 495 | // send the interest |
| 496 | sendCommand(forwarder, new Interest(command)); |
| 497 | } |
| 498 | |
| 499 | /** |
Andrew Brown | c5dda70 | 2015-04-13 09:36:04 -0700 | [diff] [blame] | 500 | * Build an interest to retrieve the NFD status. |
| 501 | * |
| 502 | * @param forwarder only a localhost {@link Face} |
| 503 | * @return the status {@link Data} packet |
| 504 | * @throws IOException if the retrieval fails |
| 505 | */ |
| 506 | private static Data retrieveStatus(Face forwarder) throws IOException { |
| 507 | Interest interest = new Interest(new Name("/localhost/nfd/status")); |
| 508 | interest.setMustBeFresh(true); |
| 509 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 510 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
| 511 | Data data = SimpleClient.getDefault().getSync(forwarder, interest); |
| 512 | return data; |
| 513 | } |
| 514 | |
| 515 | /** |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 516 | * Build an interest to retrieve a segmented data set from the NFD; for |
| 517 | * details on the DataSet, see |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 518 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/StatusDataset">http://redmine.named-data.net/projects/nfd/wiki/StatusDataset</a> |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 519 | * |
Andrew Brown | c5dda70 | 2015-04-13 09:36:04 -0700 | [diff] [blame] | 520 | * @param forwarder the {@link Face} to an NFD |
| 521 | * @param datasetName the {@link Name} of the dataset to retrieve |
| 522 | * @return the re-assembled {@link Data} packet |
| 523 | * @throws IOException if the request fails |
| 524 | * @throws ManagementException if the returned TLV is not the expected type |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 525 | */ |
| 526 | public static Data retrieveDataSet(Face forwarder, Name datasetName) throws IOException, ManagementException { |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 527 | // build management Interest packet; see <a href="http://redmine.named-data.net/projects/nfd/wiki/StatusDataset">http://redmine.named-data.net/projects/nfd/wiki/StatusDataset</a> |
Andrew Brown | 0e904e1 | 2015-03-17 14:10:29 -0700 | [diff] [blame] | 528 | Interest interest = new Interest(datasetName); |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 529 | interest.setMustBeFresh(true); |
| 530 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 531 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
| 532 | |
| 533 | // send packet |
| 534 | Data data = SegmentedClient.getDefault().getSync(forwarder, interest); |
| 535 | |
| 536 | // check for failed request |
| 537 | if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) { |
| 538 | throw ManagementException.fromResponse(data.getContent()); |
| 539 | } |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 540 | |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 541 | return data; |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 542 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 543 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 544 | /** |
| 545 | * Send an interest as a command to the forwarder; this method will convert |
| 546 | * the interest to a command interest and block until a response is received |
| 547 | * from the forwarder. Ensure the forwarding face is on the local machine |
| 548 | * (management requests are to /localhost/...) and that command signing has |
| 549 | * been set up (e.g. forwarder.setCommandSigningInfo()). |
| 550 | * |
andrewsbrown | 8a32f30 | 2015-03-24 08:42:46 -0700 | [diff] [blame] | 551 | * @param forwarder only a localhost Face, command signing info must be set |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 552 | * @param interest As described at |
andrewsbrown | 9a6d7ba | 2015-03-24 09:53:29 -0700 | [diff] [blame] | 553 | * <a href="http://redmine.named-data.net/projects/nfd/wiki/ControlCommand,">http://redmine.named-data.net/projects/nfd/wiki/ControlCommand,</a> |
| 554 | * the requested interest must have encoded ControlParameters appended to the |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 555 | * interest name |
Andrew Brown | c5dda70 | 2015-04-13 09:36:04 -0700 | [diff] [blame] | 556 | * @return a {@link ControlResponse} |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 557 | * @throws java.io.IOException |
| 558 | * @throws net.named_data.jndn.encoding.EncodingException |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 559 | * @throws com.intel.jndn.management.ManagementException |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 560 | */ |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 561 | public static ControlResponse sendCommand(Face forwarder, Interest interest) throws IOException, EncodingException, ManagementException { |
| 562 | // forwarder must have command signing info set |
| 563 | try { |
| 564 | forwarder.makeCommandInterest(interest); |
| 565 | } catch (SecurityException e) { |
| 566 | throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the face.", e); |
| 567 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 568 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 569 | // send command packet |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 570 | Data data = SimpleClient.getDefault().getSync(forwarder, interest); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 571 | |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 572 | // decode response |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 573 | ControlResponse response = new ControlResponse(); |
| 574 | response.wireDecode(data.getContent().buf()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 575 | |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 576 | // check response for success |
| 577 | if (response.getStatusCode() != OK_STATUS) { |
| 578 | throw ManagementException.fromResponse(response); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 579 | } |
andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 580 | |
| 581 | return response; |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 582 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 583 | } |