blob: 686e15af471908a5a87b3832b465cca060f62293 [file] [log] [blame]
Andrew Brown3831baf2015-01-19 13:38:52 -08001/*
2 * File name: MockTransport.java
3 *
4 * Purpose: Use the MockTransport to mock sending data over the network.
5 *
6 * © Copyright Intel Corporation. All rights reserved.
7 * Intel Corporation, 2200 Mission College Boulevard,
8 * Santa Clara, CA 95052-8119, USA
9 */
10package com.intel.jndn.mock;
11
12import java.io.IOException;
13import java.util.HashMap;
14import java.util.Map.Entry;
15import net.named_data.jndn.Data;
Andrew Brownb91e6902015-02-12 09:01:50 -080016import net.named_data.jndn.Face;
Andrew Brown3831baf2015-01-19 13:38:52 -080017import net.named_data.jndn.ForwardingFlags;
18import net.named_data.jndn.Interest;
19import net.named_data.jndn.Name;
20import net.named_data.jndn.Node;
21import net.named_data.jndn.OnData;
22import net.named_data.jndn.OnInterest;
23import net.named_data.jndn.OnRegisterFailed;
24import net.named_data.jndn.OnTimeout;
25import net.named_data.jndn.encoding.EncodingException;
26import net.named_data.jndn.encoding.WireFormat;
27import org.apache.logging.log4j.LogManager;
28import org.apache.logging.log4j.Logger;
29
30/**
Andrew Brownb91e6902015-02-12 09:01:50 -080031 * <p>
32 * Use the MockTransport to mock sending data over the network. Allows for
33 * testing NDN applications while simulating network IO. TODO implement longest
34 * prefix match here for comprehensive testing.
35 * </p>
36 * <p>
37 * Usage
38 * </p>
39 * <pre><code>
40 * Face mockFace = new MockFace();
41 * mockFace.registerPrefix(...); // as usual
42 * mockFace.expressInterest(...); // as usual
43 *
44 * // also, simply inject a response that will be returned for an expressed interest
45 * mockFace.addResponse(interestName, data);
46 * </pre></code>
Andrew Brown3831baf2015-01-19 13:38:52 -080047 *
48 * @author Andrew Brown <andrew.brown@intel.com>
49 */
Andrew Brownb91e6902015-02-12 09:01:50 -080050public class MockFace extends Face {
Andrew Brown3831baf2015-01-19 13:38:52 -080051
52 private static final Logger logger = LogManager.getLogger();
53 private final Node node_;
54 HashMap<String, Data> responseMap = new HashMap<>();
55 HashMap<Long, MockOnInterestHandler> handlerMap = new HashMap<>();
56 long lastRegisteredId = 0;
57
58 /**
59 * Create a new Face to mock communication over the network; all packets are
60 * maintained in memory
61 */
62 public MockFace() {
63 node_ = new Node(new MockTransport(), null);
64 }
65
66 /**
67 * Add a response Data packet to send immediately when an Interest with a
68 * matching name is received; will continue to respond with the same packet
69 * over multiple requests. This will preempt any registered OnInterest
70 * handlers.
71 *
72 * @param name
73 * @param data
74 */
75 public void addResponse(Name name, Data data) {
76 logger.debug("Added response for: " + name.toUri());
77 responseMap.put(name.toUri(), data);
78 }
79
80 /**
81 * Stop sending a response for the given name.
82 *
83 * @param name
84 */
85 public void removeResponse(Name name) {
86 logger.debug("Removed response for: " + name.toUri());
87 responseMap.remove(name);
88 }
89
90 /**
91 * Handle incoming Interest packets; when an Interest is expressed through
92 * expressInterest(), this will run to determine if: 1) any responses have
Andrew Brown8d8535b2015-01-19 15:22:06 -080093 * been registered or 2) if any OnInterest handlers have been registered. If
94 * one of these two succeeds, this method then re-directs the Interest from
Andrew Brown3831baf2015-01-19 13:38:52 -080095 * traveling down the network stack and returns data.
Andrew Brown8d8535b2015-01-19 15:22:06 -080096 *
Andrew Brown3831baf2015-01-19 13:38:52 -080097 * @param interest
98 */
99 protected void handleIncomingRequests(Interest interest) {
100 String interestName = interest.getName().toUri();
101 long registeredPrefixId = findRegisteredHandler(interest);
102 // check if response registered
103 if (responseMap.containsKey(interestName)) {
104 logger.debug("Found response for: " + interestName);
105 Data data = responseMap.get(interestName);
106 ((MockTransport) node_.getTransport()).respondWith(data);
Andrew Brown8d8535b2015-01-19 15:22:06 -0800107 } // check if handler registered
Andrew Brown3831baf2015-01-19 13:38:52 -0800108 else if (registeredPrefixId != -1) {
109 logger.debug("Found handler for: " + interestName);
110 MockOnInterestHandler handler = handlerMap.get(findRegisteredHandler(interest));
111 handler.onInterest.onInterest(handler.prefix, interest, node_.getTransport(), registeredPrefixId);
Andrew Brown8d8535b2015-01-19 15:22:06 -0800112 } // log failure
Andrew Brown3831baf2015-01-19 13:38:52 -0800113 else {
114 logger.warn("No response found for interest (aborting): " + interestName);
115 }
116 }
117
118 /**
119 * Find a handler that matches the incoming interest; currently, the only
120 * flags supported are the ChildInherit flags.
Andrew Brown8d8535b2015-01-19 15:22:06 -0800121 *
Andrew Brown3831baf2015-01-19 13:38:52 -0800122 * @param interest
Andrew Brown8d8535b2015-01-19 15:22:06 -0800123 * @return
Andrew Brown3831baf2015-01-19 13:38:52 -0800124 */
125 protected long findRegisteredHandler(Interest interest) {
126 for (Entry<Long, MockOnInterestHandler> entry : handlerMap.entrySet()) {
127 MockOnInterestHandler handler = entry.getValue();
128 if (handler.flags.getChildInherit() && handler.prefix.match(interest.getName())) {
129 return entry.getKey();
130 }
131 if (handler.prefix.equals(interest.getName())) {
132 return entry.getKey();
133 }
134 }
135 return -1;
136 }
137
138 /**
139 * Helper class for holding references to OnInterest handlers
140 */
141 class MockOnInterestHandler {
142
143 Name prefix;
144 OnInterest onInterest;
145 ForwardingFlags flags;
146
147 public MockOnInterestHandler(Name prefix, OnInterest onInterest, ForwardingFlags flags) {
148 this.prefix = prefix;
149 this.onInterest = onInterest;
150 this.flags = flags;
151 }
152 }
153
154 /**
155 * Send the Interest through the transport, read the entire response and call
156 * onData(interest, data).
157 *
158 * @param interest The Interest to send. This copies the Interest.
159 * @param onData When a matching data packet is received, this calls
160 * onData.onData(interest, data) where interest is the interest given to
161 * expressInterest and data is the received Data object. NOTE: You must not
162 * change the interest object - if you need to change it then make a copy.
163 * @param onTimeout If the interest times out according to the interest
164 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
165 * interest given to expressInterest. If onTimeout is null, this does not use
166 * it.
167 * @param wireFormat A WireFormat object used to encode the message.
168 * @return The pending interest ID which can be used with
169 * removePendingInterest.
170 * @throws IOException For I/O error in sending the interest.
171 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800172 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800173 public long expressInterest(Interest interest, OnData onData, OnTimeout onTimeout,
174 WireFormat wireFormat) throws IOException {
175 long id = node_.expressInterest(interest, onData, onTimeout, wireFormat);
176 handleIncomingRequests(interest);
177 return id;
178 }
179
180 /**
181 * Send the Interest through the transport, read the entire response and call
182 * onData(interest, data). This uses the default
183 * WireFormat.getDefaultWireFormat().
184 *
185 * @param interest The Interest to send. This copies the Interest.
186 * @param onData When a matching data packet is received, this calls
187 * onData.onData(interest, data) where interest is the interest given to
188 * expressInterest and data is the received Data object. NOTE: You must not
189 * change the interest object - if you need to change it then make a copy.
190 * @param onTimeout If the interest times out according to the interest
191 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
192 * interest given to expressInterest. If onTimeout is null, this does not use
193 * it.
194 * @return The pending interest ID which can be used with
195 * removePendingInterest.
196 * @throws IOException For I/O error in sending the interest.
197 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800198 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800199 public long expressInterest(Interest interest, OnData onData, OnTimeout onTimeout) throws IOException {
200 return expressInterest(interest, onData, onTimeout, WireFormat.getDefaultWireFormat());
201 }
202
203 /**
204 * Send the Interest through the transport, read the entire response and call
205 * onData(interest, data). Ignore if the interest times out.
206 *
207 * @param interest The Interest to send. This copies the Interest.
208 * @param onData When a matching data packet is received, this calls
209 * onData.onData(interest, data) where interest is the interest given to
210 * expressInterest and data is the received Data object. NOTE: You must not
211 * change the interest object - if you need to change it then make a copy.
212 * @param wireFormat A WireFormat object used to encode the message.
213 * @return The pending interest ID which can be used with
214 * removePendingInterest.
215 * @throws IOException For I/O error in sending the interest.
216 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800217 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800218 public long expressInterest(Interest interest, OnData onData, WireFormat wireFormat) throws IOException {
219 return expressInterest(interest, onData, null, wireFormat);
220 }
221
222 /**
223 * Send the Interest through the transport, read the entire response and call
224 * onData(interest, data). Ignore if the interest times out. This uses the
225 * default WireFormat.getDefaultWireFormat().
226 *
227 * @param interest The Interest to send. This copies the Interest.
228 * @param onData When a matching data packet is received, this calls
229 * onData.onData(interest, data) where interest is the interest given to
230 * expressInterest and data is the received Data object. NOTE: You must not
231 * change the interest object - if you need to change it then make a copy.
232 * @return The pending interest ID which can be used with
233 * removePendingInterest.
234 * @throws IOException For I/O error in sending the interest.
235 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800236 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800237 public long expressInterest(Interest interest, OnData onData) throws IOException {
238 return expressInterest(interest, onData, null, WireFormat.getDefaultWireFormat());
239 }
240
241 /**
242 * Encode name as an Interest. If interestTemplate is not null, use its
243 * interest selectors. Send the interest through the transport, read the
244 * entire response and call onData(interest, data).
245 *
246 * @param name A Name for the interest. This copies the Name.
247 * @param interestTemplate If not null, copy interest selectors from the
248 * template. This does not keep a pointer to the Interest object.
249 * @param onData When a matching data packet is received, this calls
250 * onData.onData(interest, data) where interest is the interest given to
251 * expressInterest and data is the received Data object. NOTE: You must not
252 * change the interest object - if you need to change it then make a copy.
253 * @param onTimeout If the interest times out according to the interest
254 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
255 * interest given to expressInterest. If onTimeout is null, this does not use
256 * it.
257 * @param wireFormat A WireFormat object used to encode the message.
258 * @return The pending interest ID which can be used with
259 * removePendingInterest.
260 * @throws IOException For I/O error in sending the interest.
261 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800262 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800263 public long expressInterest(Name name, Interest interestTemplate, OnData onData, OnTimeout onTimeout,
264 WireFormat wireFormat) throws IOException {
265 Interest interest = new Interest(name);
266 if (interestTemplate != null) {
267 interest.setMinSuffixComponents(interestTemplate.getMinSuffixComponents());
268 interest.setMaxSuffixComponents(interestTemplate.getMaxSuffixComponents());
269 interest.setKeyLocator(interestTemplate.getKeyLocator());
270 interest.setExclude(interestTemplate.getExclude());
271 interest.setChildSelector(interestTemplate.getChildSelector());
272 interest.setMustBeFresh(interestTemplate.getMustBeFresh());
273 interest.setScope(interestTemplate.getScope());
274 interest.setInterestLifetimeMilliseconds(
275 interestTemplate.getInterestLifetimeMilliseconds());
276 // Don't copy the nonce.
277 } else {
278 interest.setInterestLifetimeMilliseconds(4000.0);
279 }
280
281 return expressInterest(interest, onData, onTimeout, wireFormat);
282 }
283
284 /**
285 * Encode name as an Interest. If interestTemplate is not null, use its
286 * interest selectors. Send the interest through the transport, read the
287 * entire response and call onData(interest, data). Use a default interest
288 * lifetime.
289 *
290 * @param name A Name for the interest. This copies the Name.
291 * @param onData When a matching data packet is received, this calls
292 * onData.onData(interest, data) where interest is the interest given to
293 * expressInterest and data is the received Data object. NOTE: You must not
294 * change the interest object - if you need to change it then make a copy.
295 * @param onTimeout If the interest times out according to the interest
296 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
297 * interest given to expressInterest. If onTimeout is null, this does not use
298 * it.
299 * @param wireFormat A WireFormat object used to encode the message.
300 * @return The pending interest ID which can be used with
301 * removePendingInterest.
302 * @throws IOException For I/O error in sending the interest.
303 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800304 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800305 public long expressInterest(Name name, OnData onData, OnTimeout onTimeout,
306 WireFormat wireFormat) throws IOException {
307 return expressInterest(name, null, onData, onTimeout, wireFormat);
308 }
309
310 /**
311 * Encode name as an Interest. If interestTemplate is not null, use its
312 * interest selectors. Send the interest through the transport, read the
313 * entire response and call onData(interest, data). Ignore if the interest
314 * times out.
315 *
316 * @param name A Name for the interest. This copies the Name.
317 * @param interestTemplate If not null, copy interest selectors from the
318 * template. This does not keep a pointer to the Interest object.
319 * @param onData When a matching data packet is received, this calls
320 * onData.onData(interest, data) where interest is the interest given to
321 * expressInterest and data is the received Data object. NOTE: You must not
322 * change the interest object - if you need to change it then make a copy.
323 * @param wireFormat A WireFormat object used to encode the message.
324 * @return The pending interest ID which can be used with
325 * removePendingInterest.
326 * @throws IOException For I/O error in sending the interest.
327 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800328 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800329 public long expressInterest(Name name, Interest interestTemplate, OnData onData,
330 WireFormat wireFormat) throws IOException {
331 return expressInterest(name, interestTemplate, onData, null, wireFormat);
332 }
333
334 /**
335 * Encode name as an Interest. If interestTemplate is not null, use its
336 * interest selectors. Send the interest through the transport, read the
337 * entire response and call onData(interest, data). This uses the default
338 * WireFormat.getDefaultWireFormat().
339 *
340 * @param name A Name for the interest. This copies the Name.
341 * @param interestTemplate If not null, copy interest selectors from the
342 * template. This does not keep a pointer to the Interest object.
343 * @param onData When a matching data packet is received, this calls
344 * onData.onData(interest, data) where interest is the interest given to
345 * expressInterest and data is the received Data object. NOTE: You must not
346 * change the interest object - if you need to change it then make a copy.
347 * @param onTimeout If the interest times out according to the interest
348 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
349 * interest given to expressInterest. If onTimeout is null, this does not use
350 * it.
351 * @return The pending interest ID which can be used with
352 * removePendingInterest.
353 * @throws IOException For I/O error in sending the interest.
354 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800355 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800356 public long expressInterest(Name name, Interest interestTemplate, OnData onData,
357 OnTimeout onTimeout) throws IOException {
358 return expressInterest(name, interestTemplate, onData, onTimeout,
359 WireFormat.getDefaultWireFormat());
360 }
361
362 /**
363 * Encode name as an Interest. If interestTemplate is not null, use its
364 * interest selectors. Send the interest through the transport, read the
365 * entire response and call onData(interest, data). Ignore if the interest
366 * times out. This uses the default WireFormat.getDefaultWireFormat().
367 *
368 * @param name A Name for the interest. This copies the Name.
369 * @param interestTemplate If not null, copy interest selectors from the
370 * template. This does not keep a pointer to the Interest object.
371 * @param onData When a matching data packet is received, this calls
372 * onData.onData(interest, data) where interest is the interest given to
373 * expressInterest and data is the received Data object. NOTE: You must not
374 * change the interest object - if you need to change it then make a copy.
375 * @return The pending interest ID which can be used with
376 * removePendingInterest.
377 * @throws IOException For I/O error in sending the interest.
378 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800379 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800380 public long expressInterest(Name name, Interest interestTemplate, OnData onData) throws IOException {
381 return expressInterest(name, interestTemplate, onData, null, WireFormat.getDefaultWireFormat());
382 }
383
384 /**
385 * Encode name as an Interest. If interestTemplate is not null, use its
386 * interest selectors. Send the interest through the transport, read the
387 * entire response and call onData(interest, data). Use a default interest
388 * lifetime. This uses the default WireFormat.getDefaultWireFormat().
389 *
390 * @param name A Name for the interest. This copies the Name.
391 * @param onData When a matching data packet is received, this calls
392 * onData.onData(interest, data) where interest is the interest given to
393 * expressInterest and data is the received Data object. NOTE: You must not
394 * change the interest object - if you need to change it then make a copy.
395 * @param onTimeout If the interest times out according to the interest
396 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
397 * interest given to expressInterest. If onTimeout is null, this does not use
398 * it.
399 * @return The pending interest ID which can be used with
400 * removePendingInterest.
401 * @throws IOException For I/O error in sending the interest.
402 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800403 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800404 public long expressInterest(Name name, OnData onData, OnTimeout onTimeout) throws IOException {
405 return expressInterest(name, null, onData, onTimeout, WireFormat.getDefaultWireFormat());
406 }
407
408 /**
409 * Encode name as an Interest. If interestTemplate is not null, use its
410 * interest selectors. Send the interest through the transport, read the
411 * entire response and call onData(interest, data). Use a default interest
412 * lifetime. Ignore if the interest times out.
413 *
414 * @param name A Name for the interest. This copies the Name.
415 * @param onData When a matching data packet is received, this calls
416 * onData.onData(interest, data) where interest is the interest given to
417 * expressInterest and data is the received Data object. NOTE: You must not
418 * change the interest object - if you need to change it then make a copy.
419 * @param wireFormat A WireFormat object used to encode the message.
420 * @return The pending interest ID which can be used with
421 * removePendingInterest.
422 * @throws IOException For I/O error in sending the interest.
423 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800424 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800425 public long expressInterest(Name name, OnData onData, WireFormat wireFormat) throws IOException {
426 return expressInterest(name, null, onData, null, wireFormat);
427 }
428
429 /**
430 * Encode name as an Interest. If interestTemplate is not null, use its
431 * interest selectors. Send the interest through the transport, read the
432 * entire response and call onData(interest, data). Use a default interest
433 * lifetime. Ignore if the interest times out. This uses the default
434 * WireFormat.getDefaultWireFormat().
435 *
436 * @param name A Name for the interest. This copies the Name.
437 * @param onData When a matching data packet is received, this calls
438 * onData.onData(interest, data) where interest is the interest given to
439 * expressInterest and data is the received Data object. NOTE: You must not
440 * change the interest object - if you need to change it then make a copy.
441 * @return The pending interest ID which can be used with
442 * removePendingInterest.
443 * @throws IOException For I/O error in sending the interest.
444 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800445 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800446 public long expressInterest(Name name, OnData onData) throws IOException {
447 return expressInterest(name, null, onData, null, WireFormat.getDefaultWireFormat());
448 }
449
450 /**
451 * Remove the pending interest entry with the pendingInterestId from the
452 * pending interest table. This does not affect another pending interest with
453 * a different pendingInterestId, even if it has the same interest name. If
454 * there is no entry with the pendingInterestId, do nothing.
455 *
456 * @param pendingInterestId The ID returned from expressInterest.
457 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800458// public void removePendingInterest(long pendingInterestId) {
459// node_.removePendingInterest(pendingInterestId);
460// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800461 /**
462 * Register prefix with the connected NDN hub and call onInterest when a
463 * matching interest is received. If you have not called
464 * setCommandSigningInfo, this assumes you are connecting to NDNx. If you have
465 * called setCommandSigningInfo, this first sends an NFD registration request,
466 * and if that times out then this sends an NDNx registration request. If you
467 * need to register a prefix with NFD, you must first call
468 * setCommandSigningInfo.
469 *
470 * @param prefix A Name for the prefix to register. This copies the Name.
471 * @param onInterest When an interest is received which matches the name
472 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
473 * registeredPrefixId). NOTE: You must not change the prefix object - if you
474 * need to change it then make a copy.
475 * @param onRegisterFailed If register prefix fails for any reason, this calls
476 * onRegisterFailed.onRegisterFailed(prefix).
477 * @param flags The flags for finer control of which interests are forwarded
478 * to the application.
479 * @param wireFormat A WireFormat object used to encode the message.
480 * @return The lastRegisteredId prefix ID which can be used with
481 * removeRegisteredPrefix.
482 * @throws IOException For I/O error in sending the registration request.
483 * @throws SecurityException If signing a command interest for NFD and cannot
484 * find the private key for the certificateName.
485 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800486 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800487 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
488 ForwardingFlags flags, WireFormat wireFormat) throws IOException, net.named_data.jndn.security.SecurityException {
489 lastRegisteredId++;
490 handlerMap.put(lastRegisteredId, new MockOnInterestHandler(prefix, onInterest, flags));
491 return lastRegisteredId;
492 }
493
494 /**
495 * Register prefix with the connected NDN hub and call onInterest when a
496 * matching interest is received. This uses the default
497 * WireFormat.getDefaultWireFormat().
498 *
499 * @param prefix A Name for the prefix to register. This copies the Name.
500 * @param onInterest When an interest is received which matches the name
501 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
502 * registeredPrefixId). NOTE: You must not change the prefix object - if you
503 * need to change it then make a copy.
504 * @param onRegisterFailed If register prefix fails for any reason, this calls
505 * onRegisterFailed.onRegisterFailed(prefix).
506 * @param flags The flags for finer control of which interests are forwarded
507 * to the application.
508 * @return The lastRegisteredId prefix ID which can be used with
509 * removeRegisteredPrefix.
510 * @throws IOException For I/O error in sending the registration request.
511 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800512 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800513 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
514 ForwardingFlags flags) throws IOException, net.named_data.jndn.security.SecurityException {
515 return registerPrefix(prefix, onInterest, onRegisterFailed, flags,
516 WireFormat.getDefaultWireFormat());
517 }
518
519 /**
520 * Register prefix with the connected NDN hub and call onInterest when a
521 * matching interest is received. Use default ForwardingFlags.
522 *
523 * @param prefix A Name for the prefix to register. This copies the Name.
524 * @param onInterest When an interest is received which matches the name
525 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
526 * registeredPrefixId). NOTE: You must not change the prefix object - if you
527 * need to change it then make a copy.
528 * @param onRegisterFailed If register prefix fails for any reason, this calls
529 * onRegisterFailed.onRegisterFailed(prefix).
530 * @param wireFormat A WireFormat object used to encode the message.
531 * @return The lastRegisteredId prefix ID which can be used with
532 * removeRegisteredPrefix.
533 * @throws IOException For I/O error in sending the registration request.
534 * @throws SecurityException If signing a command interest for NFD and cannot
535 * find the private key for the certificateName.
536 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800537 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800538 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
539 WireFormat wireFormat) throws IOException, net.named_data.jndn.security.SecurityException {
540 return registerPrefix(prefix, onInterest, onRegisterFailed, new ForwardingFlags(), wireFormat);
541 }
542
543 /**
544 * Register prefix with the connected NDN hub and call onInterest when a
545 * matching interest is received. This uses the default
546 * WireFormat.getDefaultWireFormat(). Use default ForwardingFlags.
547 *
548 * @param prefix A Name for the prefix to register. This copies the Name.
549 * @param onInterest When an interest is received which matches the name
550 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
551 * registeredPrefixId). NOTE: You must not change the prefix object - if you
552 * need to change it then make a copy.
553 * @param onRegisterFailed If register prefix fails for any reason, this calls
554 * onRegisterFailed.onRegisterFailed(prefix).
555 * @return The lastRegisteredId prefix ID which can be used with
556 * removeRegisteredPrefix.
557 * @throws IOException For I/O error in sending the registration request.
558 * @throws SecurityException If signing a command interest for NFD and cannot
559 * find the private key for the certificateName.
560 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800561 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800562 public long registerPrefix(Name prefix, OnInterest onInterest,
563 OnRegisterFailed onRegisterFailed) throws IOException, net.named_data.jndn.security.SecurityException {
564 return registerPrefix(prefix, onInterest, onRegisterFailed, new ForwardingFlags(),
565 WireFormat.getDefaultWireFormat());
566 }
567
568 /**
569 * Remove the lastRegisteredId prefix entry with the registeredPrefixId from
570 * the lastRegisteredId prefix table. This does not affect another
571 * lastRegisteredId prefix with a different registeredPrefixId, even if it has
572 * the same prefix name. If there is no entry with the registeredPrefixId, do
573 * nothing.
574 *
575 * @param registeredPrefixId The ID returned from registerPrefix.
576 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800577// public void removeRegisteredPrefix(long registeredPrefixId) {
578// handlerMap.remove(registeredPrefixId);
579// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800580 /**
581 * Process any packets to receive and call callbacks such as onData,
582 * onInterest or onTimeout. This returns immediately if there is no data to
583 * receive. This blocks while calling the callbacks. You should repeatedly
584 * call this from an event loop, with calls to sleep as needed so that the
585 * loop doesn’t use 100% of the CPU. Since processEvents modifies the pending
586 * interest table, your application should make sure that it calls
587 * processEvents in the same thread as expressInterest (which also modifies
588 * the pending interest table). This may throw an exception for reading data
589 * or in the callback for processing the data. If you call this from an main
590 * event loop, you may want to catch and log/disregard all exceptions.
591 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800592 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800593 public void processEvents() throws IOException, EncodingException {
594 // Just call Node's processEvents.
595 node_.processEvents();
596 }
597
598 /**
599 * Shut down and disconnect this Face.
600 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800601// public void shutdown() {
602// node_.shutdown();
603// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800604}