Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 533c6ef | 2015-03-03 16:08:41 -0800 | [diff] [blame] | 2 | * jndn-mock |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
andrewsbrown | 533c6ef | 2015-03-03 16:08:41 -0800 | [diff] [blame] | 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 | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.mock; |
| 15 | |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 16 | import net.named_data.jndn.ControlParameters; |
| 17 | import net.named_data.jndn.ControlResponse; |
| 18 | import net.named_data.jndn.Data; |
| 19 | import net.named_data.jndn.Face; |
| 20 | import net.named_data.jndn.Interest; |
| 21 | import net.named_data.jndn.Name; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 22 | import net.named_data.jndn.encoding.EncodingException; |
| 23 | import net.named_data.jndn.encoding.TlvWireFormat; |
| 24 | import net.named_data.jndn.encoding.tlv.Tlv; |
| 25 | import net.named_data.jndn.encoding.tlv.TlvDecoder; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 26 | import net.named_data.jndn.security.KeyChain; |
| 27 | import net.named_data.jndn.security.SecurityException; |
| 28 | import net.named_data.jndn.transport.Transport; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 29 | |
andrewsbrown | 1f28bcf | 2015-04-20 13:29:20 -0700 | [diff] [blame] | 30 | import java.nio.ByteBuffer; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 31 | import java.util.ArrayList; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 32 | import java.util.List; |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 33 | import java.util.logging.Level; |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 34 | import java.util.logging.Logger; |
Alexander Afanasyev | 8e9330f | 2016-01-25 19:13:40 -0800 | [diff] [blame] | 35 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 36 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 37 | * A client-side face for unit testing. |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 38 | * |
| 39 | * @author Alexander Afanasyev, <aa@cs.ucla.edu> |
| 40 | * @author Andrew Brown <andrew.brown@intel.com> |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 41 | */ |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 42 | public class MockFace extends Face { |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Interests sent out of this MockFace. |
| 45 | * <p/> |
| 46 | * Sent Interests are appended to this container if options.enablePacketLogger |
| 47 | * is true. User of this class is responsible for cleaning up the container, |
| 48 | * if necessary. After .expressInterest, .processEvents must be called before |
| 49 | * the Interest would show up here. |
| 50 | */ |
| 51 | public final List<Interest> sentInterests = new ArrayList<>(); |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 52 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 53 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 54 | * Data sent out of this MockFace. |
| 55 | * <p/> |
| 56 | * Sent Data are appended to this container if options.enablePacketLogger is |
| 57 | * true. User of this class is responsible for cleaning up the container, if |
| 58 | * necessary. After .put, .processEvents must be called before the Data would |
| 59 | * show up here. |
| 60 | */ |
| 61 | public final List<Data> sentData = new ArrayList<>(); |
| 62 | |
| 63 | /** |
| 64 | * Emits whenever an Interest is sent. |
| 65 | * <p/> |
| 66 | * After .expressInterest, .processEvents must be called before this signal |
| 67 | * would be emitted. |
| 68 | */ |
| 69 | public final List<SignalOnSendInterest> onSendInterest = new ArrayList<>(); |
| 70 | |
| 71 | /** |
| 72 | * Emits whenever a Data packet is sent. |
| 73 | * <p/> |
| 74 | * After .putData, .processEvents must be called before this signal would be |
| 75 | * emitted. |
| 76 | */ |
| 77 | public final List<SignalOnSendData> onSendData = new ArrayList<>(); |
| 78 | |
| 79 | private static final Logger LOGGER = Logger.getLogger(MockFace.class.getName()); |
andrewsbrown | 62c2912 | 2016-05-02 15:58:06 -0700 | [diff] [blame^] | 80 | private MockTransport transport; |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 81 | private KeyChain keyChain; |
| 82 | |
| 83 | ///////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | /** |
| 86 | * API for handling {@link Interest}s. |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 87 | */ |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 88 | public interface SignalOnSendInterest { |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 89 | /** |
| 90 | * Callback called when an Interest is sent out through face (towards NFD). |
| 91 | * @param interest interest being sent out |
| 92 | */ |
| 93 | void emit(final Interest interest); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 94 | } |
| 95 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 96 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 97 | * API for handling {@link Data}s. |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 98 | */ |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 99 | public interface SignalOnSendData { |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Callback called when a Data is sent out through face (towards NFD). |
| 102 | * |
| 103 | * @param data data being sent out |
| 104 | */ |
| 105 | void emit(final Data data); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 109 | * Options for MockFace. |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 110 | */ |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 111 | public static class Options { |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 112 | private boolean enablePacketLogging = false; |
| 113 | private boolean enableRegistrationReply = false; |
| 114 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 115 | /** |
| 116 | * @return true if packet logging is enabled |
| 117 | */ |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 118 | public boolean isEnablePacketLogging() { |
| 119 | return enablePacketLogging; |
| 120 | } |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 121 | |
| 122 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 123 | * Enable/disable packet logging. |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 124 | * |
| 125 | * @param enablePacketLogging If true, packets sent out of MockFace will be appended to a container |
| 126 | * @return this |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 127 | */ |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 128 | public Options setEnablePacketLogging(final boolean enablePacketLogging) { |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 129 | this.enablePacketLogging = enablePacketLogging; |
| 130 | return this; |
| 131 | } |
| 132 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 133 | /** |
| 134 | * @return true if prefix registration mocking is enabled |
| 135 | */ |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 136 | public boolean isEnableRegistrationReply() { |
| 137 | return enableRegistrationReply; |
| 138 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 139 | |
| 140 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 141 | * Enable/disable prefix registration mocking. |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 142 | * |
| 143 | * @param enableRegistrationReply If true, prefix registration command will be automatically replied with a |
| 144 | * successful response |
| 145 | * @return this |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 146 | */ |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 147 | public Options setEnableRegistrationReply(final boolean enableRegistrationReply) { |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 148 | this.enableRegistrationReply = enableRegistrationReply; |
| 149 | return this; |
| 150 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 151 | } |
| 152 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 153 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 154 | * Default options. |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 155 | */ |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame] | 156 | public static final Options DEFAULT_OPTIONS = new Options() |
| 157 | .setEnablePacketLogging(true) |
| 158 | .setEnableRegistrationReply(true); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 161 | * Create MockFace that logs packets in {@link #sentInterests} and |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 162 | * {@link #sentData} and emulates NFD prefix registration. |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 163 | * |
| 164 | * @throws SecurityException should not be thrown by this test class |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 165 | */ |
| 166 | public MockFace() throws SecurityException { |
| 167 | this(DEFAULT_OPTIONS); |
| 168 | } |
| 169 | |
| 170 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 171 | * Create MockFace with the specified options. |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 172 | * <p> |
| 173 | * To create Face that does not log packets: |
| 174 | * <pre> |
| 175 | * new MockFace(new Options()); |
| 176 | * // use onSendInterest.add(handler) and onSendData.add(handler) |
| 177 | * // to add custom logic when Interest or Data packet are sent |
andrewsbrown | 62c2912 | 2016-05-02 15:58:06 -0700 | [diff] [blame^] | 178 | * // from the upper level (to callback) |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 179 | * </pre> |
| 180 | * |
| 181 | * To create Face that just logs packets in sentInterests and sentData: |
| 182 | * <pre> |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 183 | * new MockFace(new Options(){ enablePacketLogging = true; }); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 184 | * </pre> |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 185 | * |
| 186 | * @param options see {@link Options} |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 187 | */ |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 188 | public MockFace(final Options options) { |
andrewsbrown | 62c2912 | 2016-05-02 15:58:06 -0700 | [diff] [blame^] | 189 | super(new MockTransport(), null); |
| 190 | transport = (MockTransport) node_.getTransport(); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 191 | transport.setOnSendBlock(new OnIncomingPacket()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 192 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 193 | try { |
| 194 | keyChain = MockKeyChain.configure(new Name("/mock/key")); |
| 195 | setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName()); |
| 196 | } catch (SecurityException ex) { |
| 197 | LOGGER.log(Level.SEVERE, "Unexpected error in MockKeyChain; this class should never throw", ex); |
| 198 | throw new Error(ex); |
| 199 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 200 | |
Alexander Afanasyev | 4c3de79 | 2016-02-19 20:18:41 -0800 | [diff] [blame] | 201 | if (options.isEnablePacketLogging()) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 202 | onSendInterest.add(new SignalOnSendInterest() { |
| 203 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 204 | public void emit(final Interest interest) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 205 | sentInterests.add(interest); |
| 206 | } |
| 207 | }); |
| 208 | |
| 209 | onSendData.add(new SignalOnSendData() { |
| 210 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 211 | public void emit(final Data data) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 212 | sentData.add(data); |
| 213 | } |
| 214 | }); |
| 215 | } |
| 216 | |
Alexander Afanasyev | 4c3de79 | 2016-02-19 20:18:41 -0800 | [diff] [blame] | 217 | if (options.isEnableRegistrationReply()) { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 218 | onSendInterest.add(new OnPrefixRegistration()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 223 | * Route incoming packets to the correct callbacks. |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 224 | */ |
andrewsbrown | 62c2912 | 2016-05-02 15:58:06 -0700 | [diff] [blame^] | 225 | private class OnIncomingPacket implements MockTransport.OnSendBlockSignal { |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 226 | /** |
| 227 | * {@inheritDoc} |
| 228 | */ |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 229 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 230 | public void emit(final ByteBuffer buffer) { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 231 | // @todo Implement NDNLP processing |
| 232 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 233 | try { |
| 234 | if (buffer.get(0) == Tlv.Interest || buffer.get(0) == Tlv.Data) { |
| 235 | TlvDecoder decoder = new TlvDecoder(buffer); |
| 236 | if (decoder.peekType(Tlv.Interest, buffer.remaining())) { |
| 237 | Interest interest = new Interest(); |
| 238 | interest.wireDecode(buffer, TlvWireFormat.get()); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 239 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 240 | for (SignalOnSendInterest signal : onSendInterest) { |
| 241 | signal.emit(interest); |
| 242 | } |
| 243 | } else if (decoder.peekType(Tlv.Data, buffer.remaining())) { |
| 244 | Data data = new Data(); |
| 245 | data.wireDecode(buffer, TlvWireFormat.get()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 246 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 247 | for (SignalOnSendData signal : onSendData) { |
| 248 | signal.emit(data); |
| 249 | } |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 250 | } |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 251 | } else { |
| 252 | LOGGER.info("Received an unknown packet"); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 253 | } |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 254 | } catch (EncodingException e) { |
andrewsbrown | 62c2912 | 2016-05-02 15:58:06 -0700 | [diff] [blame^] | 255 | LOGGER.log(Level.INFO, "Failed to decodeParameters incoming packet", e); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 261 | * Handle prefix registration requests. |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 262 | */ |
| 263 | private class OnPrefixRegistration implements SignalOnSendInterest { |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 264 | private static final int STATUS_CODE_OK = 200; |
| 265 | private static final int CONTROL_PARAMETERS_NAME_OFFSET = -5; |
| 266 | private static final int CONTROL_COMMAND_NAME_OFFSET = 3; |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 267 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 268 | /** |
| 269 | * {@inheritDoc} |
| 270 | */ |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 271 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 272 | public void emit(final Interest interest) { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 273 | final Name localhostRegistration = new Name("/localhost/nfd/rib"); |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 274 | if (!interest.getName().getPrefix(localhostRegistration.size()).equals(localhostRegistration) || |
| 275 | interest.getName().get(CONTROL_COMMAND_NAME_OFFSET).toString().equals("register")) { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
| 279 | ControlParameters params = new ControlParameters(); |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 280 | try { |
| 281 | params.wireDecode(interest.getName().get(CONTROL_PARAMETERS_NAME_OFFSET).getValue()); |
| 282 | params.setFaceId(1); |
| 283 | params.setOrigin(0); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 284 | params.setCost(0); |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 285 | } catch (EncodingException e) { |
| 286 | throw new IllegalArgumentException("", e); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 287 | } |
| 288 | |
andrewsbrown | 6d70028 | 2016-02-16 18:29:46 -0800 | [diff] [blame] | 289 | ControlResponse response = new ControlResponse(); |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 290 | response.setStatusCode(STATUS_CODE_OK); |
andrewsbrown | 6d70028 | 2016-02-16 18:29:46 -0800 | [diff] [blame] | 291 | response.setStatusText("OK"); |
| 292 | response.setBodyAsControlParameters(params); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 293 | |
| 294 | Data data = new Data(); |
| 295 | data.setName(interest.getName()); |
andrewsbrown | 6d70028 | 2016-02-16 18:29:46 -0800 | [diff] [blame] | 296 | data.setContent(response.wireEncode()); |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 297 | try { |
| 298 | keyChain.sign(data); |
| 299 | } catch (SecurityException e) { |
| 300 | LOGGER.log(Level.FINE, "MockKeyChain signing failed", e); |
| 301 | } |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 302 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 303 | try { |
| 304 | receive(data); |
| 305 | } catch (EncodingException e) { |
| 306 | LOGGER.log(Level.INFO, "Failed to encode ControlReposnse data", e); |
| 307 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
andrewsbrown | 62c2912 | 2016-05-02 15:58:06 -0700 | [diff] [blame^] | 312 | * Mock reception of the Interest packet on the Face (from callback). |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 313 | * |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 314 | * @param interest the mock-remote interest to add to the PIT |
| 315 | * @throws EncodingException if packet encoding fails (it should not) |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 316 | */ |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 317 | public void receive(final Interest interest) throws EncodingException { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 318 | transport.receive(interest.wireEncode().buf()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
andrewsbrown | 62c2912 | 2016-05-02 15:58:06 -0700 | [diff] [blame^] | 322 | * Mock reception of the Data packet on the Face (from callback). |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 323 | * |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 324 | * @param data the mock-remote data to add to the CS |
| 325 | * @throws EncodingException if packet encoding fails (it should not) |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 326 | */ |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame] | 327 | public void receive(final Data data) throws EncodingException { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 328 | transport.receive(data.wireEncode().buf()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 331 | /** |
andrewsbrown | 62c2912 | 2016-05-02 15:58:06 -0700 | [diff] [blame^] | 332 | * @return the callback for this face |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 333 | */ |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 334 | public Transport getTransport() { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 335 | return transport; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 336 | } |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 337 | } |