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