blob: e1a62dee420dfc06d1790ce65282a4e840771ae3 [file] [log] [blame]
Andrew Brown3831baf2015-01-19 13:38:52 -08001/*
andrewsbrown533c6ef2015-03-03 16:08:41 -08002 * jndn-mock
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 Brown3831baf2015-01-19 13:38:52 -080013 */
14package com.intel.jndn.mock;
15
16import java.io.IOException;
17import java.util.HashMap;
18import java.util.Map.Entry;
Andrew Brownec1b0d02015-02-21 13:11:42 -080019import java.util.logging.Logger;
Andrew Brown3831baf2015-01-19 13:38:52 -080020import net.named_data.jndn.Data;
Andrew Brownb91e6902015-02-12 09:01:50 -080021import net.named_data.jndn.Face;
Andrew Brown3831baf2015-01-19 13:38:52 -080022import net.named_data.jndn.ForwardingFlags;
23import net.named_data.jndn.Interest;
24import net.named_data.jndn.Name;
25import net.named_data.jndn.Node;
26import net.named_data.jndn.OnData;
27import net.named_data.jndn.OnInterest;
28import net.named_data.jndn.OnRegisterFailed;
29import net.named_data.jndn.OnTimeout;
30import net.named_data.jndn.encoding.EncodingException;
31import net.named_data.jndn.encoding.WireFormat;
andrewsbrownebb72a82015-03-31 13:31:18 -070032import net.named_data.jndn.transport.Transport;
Andrew Brown3831baf2015-01-19 13:38:52 -080033
34/**
Andrew Brownb91e6902015-02-12 09:01:50 -080035 * <p>
36 * Use the MockTransport to mock sending data over the network. Allows for
37 * testing NDN applications while simulating network IO. TODO implement longest
38 * prefix match here for comprehensive testing.
39 * </p>
40 * <p>
41 * Usage
42 * </p>
43 * <pre><code>
44 * Face mockFace = new MockFace();
45 * mockFace.registerPrefix(...); // as usual
46 * mockFace.expressInterest(...); // as usual
47 *
48 * // also, simply inject a response that will be returned for an expressed interest
49 * mockFace.addResponse(interestName, data);
50 * </pre></code>
Andrew Brown3831baf2015-01-19 13:38:52 -080051 *
52 * @author Andrew Brown <andrew.brown@intel.com>
53 */
Andrew Brownb91e6902015-02-12 09:01:50 -080054public class MockFace extends Face {
Andrew Brown3831baf2015-01-19 13:38:52 -080055
Andrew Brownec1b0d02015-02-21 13:11:42 -080056 private static final Logger logger = Logger.getLogger(MockFace.class.getName());
Andrew Brown3831baf2015-01-19 13:38:52 -080057 private final Node node_;
58 HashMap<String, Data> responseMap = new HashMap<>();
59 HashMap<Long, MockOnInterestHandler> handlerMap = new HashMap<>();
60 long lastRegisteredId = 0;
61
62 /**
63 * Create a new Face to mock communication over the network; all packets are
64 * maintained in memory
65 */
66 public MockFace() {
67 node_ = new Node(new MockTransport(), null);
68 }
69
70 /**
andrewsbrownebb72a82015-03-31 13:31:18 -070071 * @return a reference to the current MockTransport
72 */
73 public MockTransport getTransport() {
74 return (MockTransport) node_.getTransport();
75 }
76
77 /**
Andrew Brown3831baf2015-01-19 13:38:52 -080078 * Add a response Data packet to send immediately when an Interest with a
79 * matching name is received; will continue to respond with the same packet
80 * over multiple requests. This will preempt any registered OnInterest
81 * handlers.
82 *
83 * @param name
84 * @param data
85 */
86 public void addResponse(Name name, Data data) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080087 logger.fine("Added response for: " + name.toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080088 responseMap.put(name.toUri(), data);
89 }
90
91 /**
92 * Stop sending a response for the given name.
93 *
94 * @param name
95 */
96 public void removeResponse(Name name) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080097 logger.fine("Removed response for: " + name.toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080098 responseMap.remove(name);
99 }
100
101 /**
102 * Handle incoming Interest packets; when an Interest is expressed through
103 * expressInterest(), this will run to determine if: 1) any responses have
Andrew Brown8d8535b2015-01-19 15:22:06 -0800104 * been registered or 2) if any OnInterest handlers have been registered. If
105 * one of these two succeeds, this method then re-directs the Interest from
Andrew Brown3831baf2015-01-19 13:38:52 -0800106 * traveling down the network stack and returns data.
Andrew Brown8d8535b2015-01-19 15:22:06 -0800107 *
Andrew Brown3831baf2015-01-19 13:38:52 -0800108 * @param interest
109 */
110 protected void handleIncomingRequests(Interest interest) {
111 String interestName = interest.getName().toUri();
112 long registeredPrefixId = findRegisteredHandler(interest);
113 // check if response registered
114 if (responseMap.containsKey(interestName)) {
Andrew Brownec1b0d02015-02-21 13:11:42 -0800115 logger.fine("Found response for: " + interestName);
Andrew Brown3831baf2015-01-19 13:38:52 -0800116 Data data = responseMap.get(interestName);
117 ((MockTransport) node_.getTransport()).respondWith(data);
Andrew Brown8d8535b2015-01-19 15:22:06 -0800118 } // check if handler registered
Andrew Brown3831baf2015-01-19 13:38:52 -0800119 else if (registeredPrefixId != -1) {
Andrew Brownec1b0d02015-02-21 13:11:42 -0800120 logger.fine("Found handler for: " + interestName);
Andrew Brown3831baf2015-01-19 13:38:52 -0800121 MockOnInterestHandler handler = handlerMap.get(findRegisteredHandler(interest));
122 handler.onInterest.onInterest(handler.prefix, interest, node_.getTransport(), registeredPrefixId);
Andrew Brown8d8535b2015-01-19 15:22:06 -0800123 } // log failure
Andrew Brown3831baf2015-01-19 13:38:52 -0800124 else {
Andrew Brownec1b0d02015-02-21 13:11:42 -0800125 logger.warning("No response found for interest (aborting): " + interestName);
Andrew Brown3831baf2015-01-19 13:38:52 -0800126 }
127 }
128
129 /**
130 * Find a handler that matches the incoming interest; currently, the only
131 * flags supported are the ChildInherit flags.
Andrew Brown8d8535b2015-01-19 15:22:06 -0800132 *
Andrew Brown3831baf2015-01-19 13:38:52 -0800133 * @param interest
Andrew Brown8d8535b2015-01-19 15:22:06 -0800134 * @return
Andrew Brown3831baf2015-01-19 13:38:52 -0800135 */
136 protected long findRegisteredHandler(Interest interest) {
137 for (Entry<Long, MockOnInterestHandler> entry : handlerMap.entrySet()) {
138 MockOnInterestHandler handler = entry.getValue();
139 if (handler.flags.getChildInherit() && handler.prefix.match(interest.getName())) {
140 return entry.getKey();
141 }
142 if (handler.prefix.equals(interest.getName())) {
143 return entry.getKey();
144 }
145 }
146 return -1;
147 }
148
149 /**
150 * Helper class for holding references to OnInterest handlers
151 */
152 class MockOnInterestHandler {
153
154 Name prefix;
155 OnInterest onInterest;
156 ForwardingFlags flags;
157
158 public MockOnInterestHandler(Name prefix, OnInterest onInterest, ForwardingFlags flags) {
159 this.prefix = prefix;
160 this.onInterest = onInterest;
161 this.flags = flags;
162 }
163 }
164
165 /**
166 * Send the Interest through the transport, read the entire response and call
167 * onData(interest, data).
168 *
169 * @param interest The Interest to send. This copies the Interest.
170 * @param onData When a matching data packet is received, this calls
171 * onData.onData(interest, data) where interest is the interest given to
172 * expressInterest and data is the received Data object. NOTE: You must not
173 * change the interest object - if you need to change it then make a copy.
174 * @param onTimeout If the interest times out according to the interest
175 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
176 * interest given to expressInterest. If onTimeout is null, this does not use
177 * it.
178 * @param wireFormat A WireFormat object used to encode the message.
179 * @return The pending interest ID which can be used with
180 * removePendingInterest.
181 * @throws IOException For I/O error in sending the interest.
182 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800183 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800184 public long expressInterest(Interest interest, OnData onData, OnTimeout onTimeout,
185 WireFormat wireFormat) throws IOException {
186 long id = node_.expressInterest(interest, onData, onTimeout, wireFormat);
187 handleIncomingRequests(interest);
188 return id;
189 }
190
191 /**
192 * Send the Interest through the transport, read the entire response and call
193 * onData(interest, data). This uses the default
194 * WireFormat.getDefaultWireFormat().
195 *
196 * @param interest The Interest to send. This copies the Interest.
197 * @param onData When a matching data packet is received, this calls
198 * onData.onData(interest, data) where interest is the interest given to
199 * expressInterest and data is the received Data object. NOTE: You must not
200 * change the interest object - if you need to change it then make a copy.
201 * @param onTimeout If the interest times out according to the interest
202 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
203 * interest given to expressInterest. If onTimeout is null, this does not use
204 * it.
205 * @return The pending interest ID which can be used with
206 * removePendingInterest.
207 * @throws IOException For I/O error in sending the interest.
208 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800209 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800210 public long expressInterest(Interest interest, OnData onData, OnTimeout onTimeout) throws IOException {
211 return expressInterest(interest, onData, onTimeout, WireFormat.getDefaultWireFormat());
212 }
213
214 /**
215 * Send the Interest through the transport, read the entire response and call
216 * onData(interest, data). Ignore if the interest times out.
217 *
218 * @param interest The Interest to send. This copies the Interest.
219 * @param onData When a matching data packet is received, this calls
220 * onData.onData(interest, data) where interest is the interest given to
221 * expressInterest and data is the received Data object. NOTE: You must not
222 * change the interest object - if you need to change it then make a copy.
223 * @param wireFormat A WireFormat object used to encode the message.
224 * @return The pending interest ID which can be used with
225 * removePendingInterest.
226 * @throws IOException For I/O error in sending the interest.
227 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800228 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800229 public long expressInterest(Interest interest, OnData onData, WireFormat wireFormat) throws IOException {
230 return expressInterest(interest, onData, null, wireFormat);
231 }
232
233 /**
234 * Send the Interest through the transport, read the entire response and call
235 * onData(interest, data). Ignore if the interest times out. This uses the
236 * default WireFormat.getDefaultWireFormat().
237 *
238 * @param interest The Interest to send. This copies the Interest.
239 * @param onData When a matching data packet is received, this calls
240 * onData.onData(interest, data) where interest is the interest given to
241 * expressInterest and data is the received Data object. NOTE: You must not
242 * change the interest object - if you need to change it then make a copy.
243 * @return The pending interest ID which can be used with
244 * removePendingInterest.
245 * @throws IOException For I/O error in sending the interest.
246 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800247 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800248 public long expressInterest(Interest interest, OnData onData) throws IOException {
249 return expressInterest(interest, onData, null, WireFormat.getDefaultWireFormat());
250 }
251
252 /**
253 * Encode name as an Interest. If interestTemplate is not null, use its
254 * interest selectors. Send the interest through the transport, read the
255 * entire response and call onData(interest, data).
256 *
257 * @param name A Name for the interest. This copies the Name.
258 * @param interestTemplate If not null, copy interest selectors from the
259 * template. This does not keep a pointer to the Interest object.
260 * @param onData When a matching data packet is received, this calls
261 * onData.onData(interest, data) where interest is the interest given to
262 * expressInterest and data is the received Data object. NOTE: You must not
263 * change the interest object - if you need to change it then make a copy.
264 * @param onTimeout If the interest times out according to the interest
265 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
266 * interest given to expressInterest. If onTimeout is null, this does not use
267 * it.
268 * @param wireFormat A WireFormat object used to encode the message.
269 * @return The pending interest ID which can be used with
270 * removePendingInterest.
271 * @throws IOException For I/O error in sending the interest.
272 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800273 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800274 public long expressInterest(Name name, Interest interestTemplate, OnData onData, OnTimeout onTimeout,
275 WireFormat wireFormat) throws IOException {
276 Interest interest = new Interest(name);
277 if (interestTemplate != null) {
278 interest.setMinSuffixComponents(interestTemplate.getMinSuffixComponents());
279 interest.setMaxSuffixComponents(interestTemplate.getMaxSuffixComponents());
280 interest.setKeyLocator(interestTemplate.getKeyLocator());
281 interest.setExclude(interestTemplate.getExclude());
282 interest.setChildSelector(interestTemplate.getChildSelector());
283 interest.setMustBeFresh(interestTemplate.getMustBeFresh());
284 interest.setScope(interestTemplate.getScope());
285 interest.setInterestLifetimeMilliseconds(
286 interestTemplate.getInterestLifetimeMilliseconds());
287 // Don't copy the nonce.
288 } else {
289 interest.setInterestLifetimeMilliseconds(4000.0);
290 }
291
292 return expressInterest(interest, onData, onTimeout, wireFormat);
293 }
294
295 /**
296 * Encode name as an Interest. If interestTemplate is not null, use its
297 * interest selectors. Send the interest through the transport, read the
298 * entire response and call onData(interest, data). Use a default interest
299 * lifetime.
300 *
301 * @param name A Name for the interest. This copies the Name.
302 * @param onData When a matching data packet is received, this calls
303 * onData.onData(interest, data) where interest is the interest given to
304 * expressInterest and data is the received Data object. NOTE: You must not
305 * change the interest object - if you need to change it then make a copy.
306 * @param onTimeout If the interest times out according to the interest
307 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
308 * interest given to expressInterest. If onTimeout is null, this does not use
309 * it.
310 * @param wireFormat A WireFormat object used to encode the message.
311 * @return The pending interest ID which can be used with
312 * removePendingInterest.
313 * @throws IOException For I/O error in sending the interest.
314 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800315 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800316 public long expressInterest(Name name, OnData onData, OnTimeout onTimeout,
317 WireFormat wireFormat) throws IOException {
318 return expressInterest(name, null, onData, onTimeout, wireFormat);
319 }
320
321 /**
322 * Encode name as an Interest. If interestTemplate is not null, use its
323 * interest selectors. Send the interest through the transport, read the
324 * entire response and call onData(interest, data). Ignore if the interest
325 * times out.
326 *
327 * @param name A Name for the interest. This copies the Name.
328 * @param interestTemplate If not null, copy interest selectors from the
329 * template. This does not keep a pointer to the Interest object.
330 * @param onData When a matching data packet is received, this calls
331 * onData.onData(interest, data) where interest is the interest given to
332 * expressInterest and data is the received Data object. NOTE: You must not
333 * change the interest object - if you need to change it then make a copy.
334 * @param wireFormat A WireFormat object used to encode the message.
335 * @return The pending interest ID which can be used with
336 * removePendingInterest.
337 * @throws IOException For I/O error in sending the interest.
338 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800339 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800340 public long expressInterest(Name name, Interest interestTemplate, OnData onData,
341 WireFormat wireFormat) throws IOException {
342 return expressInterest(name, interestTemplate, onData, null, wireFormat);
343 }
344
345 /**
346 * Encode name as an Interest. If interestTemplate is not null, use its
347 * interest selectors. Send the interest through the transport, read the
348 * entire response and call onData(interest, data). This uses the default
349 * WireFormat.getDefaultWireFormat().
350 *
351 * @param name A Name for the interest. This copies the Name.
352 * @param interestTemplate If not null, copy interest selectors from the
353 * template. This does not keep a pointer to the Interest object.
354 * @param onData When a matching data packet is received, this calls
355 * onData.onData(interest, data) where interest is the interest given to
356 * expressInterest and data is the received Data object. NOTE: You must not
357 * change the interest object - if you need to change it then make a copy.
358 * @param onTimeout If the interest times out according to the interest
359 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
360 * interest given to expressInterest. If onTimeout is null, this does not use
361 * it.
362 * @return The pending interest ID which can be used with
363 * removePendingInterest.
364 * @throws IOException For I/O error in sending the interest.
365 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800366 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800367 public long expressInterest(Name name, Interest interestTemplate, OnData onData,
368 OnTimeout onTimeout) throws IOException {
369 return expressInterest(name, interestTemplate, onData, onTimeout,
370 WireFormat.getDefaultWireFormat());
371 }
372
373 /**
374 * Encode name as an Interest. If interestTemplate is not null, use its
375 * interest selectors. Send the interest through the transport, read the
376 * entire response and call onData(interest, data). Ignore if the interest
377 * times out. This uses the default WireFormat.getDefaultWireFormat().
378 *
379 * @param name A Name for the interest. This copies the Name.
380 * @param interestTemplate If not null, copy interest selectors from the
381 * template. This does not keep a pointer to the Interest object.
382 * @param onData When a matching data packet is received, this calls
383 * onData.onData(interest, data) where interest is the interest given to
384 * expressInterest and data is the received Data object. NOTE: You must not
385 * change the interest object - if you need to change it then make a copy.
386 * @return The pending interest ID which can be used with
387 * removePendingInterest.
388 * @throws IOException For I/O error in sending the interest.
389 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800390 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800391 public long expressInterest(Name name, Interest interestTemplate, OnData onData) throws IOException {
392 return expressInterest(name, interestTemplate, onData, null, WireFormat.getDefaultWireFormat());
393 }
394
395 /**
396 * Encode name as an Interest. If interestTemplate is not null, use its
397 * interest selectors. Send the interest through the transport, read the
398 * entire response and call onData(interest, data). Use a default interest
399 * lifetime. This uses the default WireFormat.getDefaultWireFormat().
400 *
401 * @param name A Name for the interest. This copies the Name.
402 * @param onData When a matching data packet is received, this calls
403 * onData.onData(interest, data) where interest is the interest given to
404 * expressInterest and data is the received Data object. NOTE: You must not
405 * change the interest object - if you need to change it then make a copy.
406 * @param onTimeout If the interest times out according to the interest
407 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
408 * interest given to expressInterest. If onTimeout is null, this does not use
409 * it.
410 * @return The pending interest ID which can be used with
411 * removePendingInterest.
412 * @throws IOException For I/O error in sending the interest.
413 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800414 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800415 public long expressInterest(Name name, OnData onData, OnTimeout onTimeout) throws IOException {
416 return expressInterest(name, null, onData, onTimeout, WireFormat.getDefaultWireFormat());
417 }
418
419 /**
420 * Encode name as an Interest. If interestTemplate is not null, use its
421 * interest selectors. Send the interest through the transport, read the
422 * entire response and call onData(interest, data). Use a default interest
423 * lifetime. Ignore if the interest times out.
424 *
425 * @param name A Name for the interest. This copies the Name.
426 * @param onData When a matching data packet is received, this calls
427 * onData.onData(interest, data) where interest is the interest given to
428 * expressInterest and data is the received Data object. NOTE: You must not
429 * change the interest object - if you need to change it then make a copy.
430 * @param wireFormat A WireFormat object used to encode the message.
431 * @return The pending interest ID which can be used with
432 * removePendingInterest.
433 * @throws IOException For I/O error in sending the interest.
434 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800435 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800436 public long expressInterest(Name name, OnData onData, WireFormat wireFormat) throws IOException {
437 return expressInterest(name, null, onData, null, wireFormat);
438 }
439
440 /**
441 * Encode name as an Interest. If interestTemplate is not null, use its
442 * interest selectors. Send the interest through the transport, read the
443 * entire response and call onData(interest, data). Use a default interest
444 * lifetime. Ignore if the interest times out. This uses the default
445 * WireFormat.getDefaultWireFormat().
446 *
447 * @param name A Name for the interest. This copies the Name.
448 * @param onData When a matching data packet is received, this calls
449 * onData.onData(interest, data) where interest is the interest given to
450 * expressInterest and data is the received Data object. NOTE: You must not
451 * change the interest object - if you need to change it then make a copy.
452 * @return The pending interest ID which can be used with
453 * removePendingInterest.
454 * @throws IOException For I/O error in sending the interest.
455 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800456 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800457 public long expressInterest(Name name, OnData onData) throws IOException {
458 return expressInterest(name, null, onData, null, WireFormat.getDefaultWireFormat());
459 }
460
461 /**
462 * Remove the pending interest entry with the pendingInterestId from the
463 * pending interest table. This does not affect another pending interest with
464 * a different pendingInterestId, even if it has the same interest name. If
465 * there is no entry with the pendingInterestId, do nothing.
466 *
467 * @param pendingInterestId The ID returned from expressInterest.
468 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800469// public void removePendingInterest(long pendingInterestId) {
470// node_.removePendingInterest(pendingInterestId);
471// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800472 /**
473 * Register prefix with the connected NDN hub and call onInterest when a
474 * matching interest is received. If you have not called
475 * setCommandSigningInfo, this assumes you are connecting to NDNx. If you have
476 * called setCommandSigningInfo, this first sends an NFD registration request,
477 * and if that times out then this sends an NDNx registration request. If you
478 * need to register a prefix with NFD, you must first call
479 * setCommandSigningInfo.
480 *
481 * @param prefix A Name for the prefix to register. This copies the Name.
482 * @param onInterest When an interest is received which matches the name
483 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
484 * registeredPrefixId). NOTE: You must not change the prefix object - if you
485 * need to change it then make a copy.
486 * @param onRegisterFailed If register prefix fails for any reason, this calls
487 * onRegisterFailed.onRegisterFailed(prefix).
488 * @param flags The flags for finer control of which interests are forwarded
489 * to the application.
490 * @param wireFormat A WireFormat object used to encode the message.
491 * @return The lastRegisteredId prefix ID which can be used with
492 * removeRegisteredPrefix.
493 * @throws IOException For I/O error in sending the registration request.
494 * @throws SecurityException If signing a command interest for NFD and cannot
495 * find the private key for the certificateName.
496 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800497 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800498 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
499 ForwardingFlags flags, WireFormat wireFormat) throws IOException, net.named_data.jndn.security.SecurityException {
500 lastRegisteredId++;
501 handlerMap.put(lastRegisteredId, new MockOnInterestHandler(prefix, onInterest, flags));
502 return lastRegisteredId;
503 }
504
505 /**
506 * Register prefix with the connected NDN hub and call onInterest when a
507 * matching interest is received. This uses the default
508 * WireFormat.getDefaultWireFormat().
509 *
510 * @param prefix A Name for the prefix to register. This copies the Name.
511 * @param onInterest When an interest is received which matches the name
512 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
513 * registeredPrefixId). NOTE: You must not change the prefix object - if you
514 * need to change it then make a copy.
515 * @param onRegisterFailed If register prefix fails for any reason, this calls
516 * onRegisterFailed.onRegisterFailed(prefix).
517 * @param flags The flags for finer control of which interests are forwarded
518 * to the application.
519 * @return The lastRegisteredId prefix ID which can be used with
520 * removeRegisteredPrefix.
521 * @throws IOException For I/O error in sending the registration request.
522 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800523 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800524 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
525 ForwardingFlags flags) throws IOException, net.named_data.jndn.security.SecurityException {
526 return registerPrefix(prefix, onInterest, onRegisterFailed, flags,
527 WireFormat.getDefaultWireFormat());
528 }
529
530 /**
531 * Register prefix with the connected NDN hub and call onInterest when a
532 * matching interest is received. Use default ForwardingFlags.
533 *
534 * @param prefix A Name for the prefix to register. This copies the Name.
535 * @param onInterest When an interest is received which matches the name
536 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
537 * registeredPrefixId). NOTE: You must not change the prefix object - if you
538 * need to change it then make a copy.
539 * @param onRegisterFailed If register prefix fails for any reason, this calls
540 * onRegisterFailed.onRegisterFailed(prefix).
541 * @param wireFormat A WireFormat object used to encode the message.
542 * @return The lastRegisteredId prefix ID which can be used with
543 * removeRegisteredPrefix.
544 * @throws IOException For I/O error in sending the registration request.
545 * @throws SecurityException If signing a command interest for NFD and cannot
546 * find the private key for the certificateName.
547 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800548 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800549 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
550 WireFormat wireFormat) throws IOException, net.named_data.jndn.security.SecurityException {
551 return registerPrefix(prefix, onInterest, onRegisterFailed, new ForwardingFlags(), wireFormat);
552 }
553
554 /**
555 * Register prefix with the connected NDN hub and call onInterest when a
556 * matching interest is received. This uses the default
557 * WireFormat.getDefaultWireFormat(). Use default ForwardingFlags.
558 *
559 * @param prefix A Name for the prefix to register. This copies the Name.
560 * @param onInterest When an interest is received which matches the name
561 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
562 * registeredPrefixId). NOTE: You must not change the prefix object - if you
563 * need to change it then make a copy.
564 * @param onRegisterFailed If register prefix fails for any reason, this calls
565 * onRegisterFailed.onRegisterFailed(prefix).
566 * @return The lastRegisteredId prefix ID which can be used with
567 * removeRegisteredPrefix.
568 * @throws IOException For I/O error in sending the registration request.
569 * @throws SecurityException If signing a command interest for NFD and cannot
570 * find the private key for the certificateName.
571 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800572 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800573 public long registerPrefix(Name prefix, OnInterest onInterest,
574 OnRegisterFailed onRegisterFailed) throws IOException, net.named_data.jndn.security.SecurityException {
575 return registerPrefix(prefix, onInterest, onRegisterFailed, new ForwardingFlags(),
576 WireFormat.getDefaultWireFormat());
577 }
578
579 /**
580 * Remove the lastRegisteredId prefix entry with the registeredPrefixId from
581 * the lastRegisteredId prefix table. This does not affect another
582 * lastRegisteredId prefix with a different registeredPrefixId, even if it has
583 * the same prefix name. If there is no entry with the registeredPrefixId, do
584 * nothing.
585 *
586 * @param registeredPrefixId The ID returned from registerPrefix.
587 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800588// public void removeRegisteredPrefix(long registeredPrefixId) {
589// handlerMap.remove(registeredPrefixId);
590// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800591 /**
592 * Process any packets to receive and call callbacks such as onData,
593 * onInterest or onTimeout. This returns immediately if there is no data to
594 * receive. This blocks while calling the callbacks. You should repeatedly
595 * call this from an event loop, with calls to sleep as needed so that the
596 * loop doesn’t use 100% of the CPU. Since processEvents modifies the pending
597 * interest table, your application should make sure that it calls
598 * processEvents in the same thread as expressInterest (which also modifies
599 * the pending interest table). This may throw an exception for reading data
600 * or in the callback for processing the data. If you call this from an main
601 * event loop, you may want to catch and log/disregard all exceptions.
602 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800603 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800604 public void processEvents() throws IOException, EncodingException {
605 // Just call Node's processEvents.
606 node_.processEvents();
607 }
608
609 /**
610 * Shut down and disconnect this Face.
611 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800612// public void shutdown() {
613// node_.shutdown();
614// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800615}