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