blob: 6360a722ef9ff768236ca3f66d739ad6addcd5b7 [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;
Andrew Brown3831baf2015-01-19 13:38:52 -080032
33/**
Andrew Brownb91e6902015-02-12 09:01:50 -080034 * <p>
35 * Use the MockTransport to mock sending data over the network. Allows for
36 * testing NDN applications while simulating network IO. TODO implement longest
37 * prefix match here for comprehensive testing.
38 * </p>
39 * <p>
40 * Usage
41 * </p>
42 * <pre><code>
43 * Face mockFace = new MockFace();
44 * mockFace.registerPrefix(...); // as usual
45 * mockFace.expressInterest(...); // as usual
46 *
47 * // also, simply inject a response that will be returned for an expressed interest
48 * mockFace.addResponse(interestName, data);
49 * </pre></code>
Andrew Brown3831baf2015-01-19 13:38:52 -080050 *
51 * @author Andrew Brown <andrew.brown@intel.com>
52 */
Andrew Brownb91e6902015-02-12 09:01:50 -080053public class MockFace extends Face {
Andrew Brown3831baf2015-01-19 13:38:52 -080054
Andrew Brownec1b0d02015-02-21 13:11:42 -080055 private static final Logger logger = Logger.getLogger(MockFace.class.getName());
Andrew Brown3831baf2015-01-19 13:38:52 -080056 private final Node node_;
57 HashMap<String, Data> responseMap = new HashMap<>();
58 HashMap<Long, MockOnInterestHandler> handlerMap = new HashMap<>();
59 long lastRegisteredId = 0;
60
61 /**
62 * Create a new Face to mock communication over the network; all packets are
63 * maintained in memory
64 */
65 public MockFace() {
66 node_ = new Node(new MockTransport(), null);
67 }
68
69 /**
70 * Add a response Data packet to send immediately when an Interest with a
71 * matching name is received; will continue to respond with the same packet
72 * over multiple requests. This will preempt any registered OnInterest
73 * handlers.
74 *
75 * @param name
76 * @param data
77 */
78 public void addResponse(Name name, Data data) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080079 logger.fine("Added response for: " + name.toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080080 responseMap.put(name.toUri(), data);
81 }
82
83 /**
84 * Stop sending a response for the given name.
85 *
86 * @param name
87 */
88 public void removeResponse(Name name) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080089 logger.fine("Removed response for: " + name.toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080090 responseMap.remove(name);
91 }
92
93 /**
94 * Handle incoming Interest packets; when an Interest is expressed through
95 * expressInterest(), this will run to determine if: 1) any responses have
Andrew Brown8d8535b2015-01-19 15:22:06 -080096 * been registered or 2) if any OnInterest handlers have been registered. If
97 * one of these two succeeds, this method then re-directs the Interest from
Andrew Brown3831baf2015-01-19 13:38:52 -080098 * traveling down the network stack and returns data.
Andrew Brown8d8535b2015-01-19 15:22:06 -080099 *
Andrew Brown3831baf2015-01-19 13:38:52 -0800100 * @param interest
101 */
102 protected void handleIncomingRequests(Interest interest) {
103 String interestName = interest.getName().toUri();
104 long registeredPrefixId = findRegisteredHandler(interest);
105 // check if response registered
106 if (responseMap.containsKey(interestName)) {
Andrew Brownec1b0d02015-02-21 13:11:42 -0800107 logger.fine("Found response for: " + interestName);
Andrew Brown3831baf2015-01-19 13:38:52 -0800108 Data data = responseMap.get(interestName);
109 ((MockTransport) node_.getTransport()).respondWith(data);
Andrew Brown8d8535b2015-01-19 15:22:06 -0800110 } // check if handler registered
Andrew Brown3831baf2015-01-19 13:38:52 -0800111 else if (registeredPrefixId != -1) {
Andrew Brownec1b0d02015-02-21 13:11:42 -0800112 logger.fine("Found handler for: " + interestName);
Andrew Brown3831baf2015-01-19 13:38:52 -0800113 MockOnInterestHandler handler = handlerMap.get(findRegisteredHandler(interest));
114 handler.onInterest.onInterest(handler.prefix, interest, node_.getTransport(), registeredPrefixId);
Andrew Brown8d8535b2015-01-19 15:22:06 -0800115 } // log failure
Andrew Brown3831baf2015-01-19 13:38:52 -0800116 else {
Andrew Brownec1b0d02015-02-21 13:11:42 -0800117 logger.warning("No response found for interest (aborting): " + interestName);
Andrew Brown3831baf2015-01-19 13:38:52 -0800118 }
119 }
120
121 /**
122 * Find a handler that matches the incoming interest; currently, the only
123 * flags supported are the ChildInherit flags.
Andrew Brown8d8535b2015-01-19 15:22:06 -0800124 *
Andrew Brown3831baf2015-01-19 13:38:52 -0800125 * @param interest
Andrew Brown8d8535b2015-01-19 15:22:06 -0800126 * @return
Andrew Brown3831baf2015-01-19 13:38:52 -0800127 */
128 protected long findRegisteredHandler(Interest interest) {
129 for (Entry<Long, MockOnInterestHandler> entry : handlerMap.entrySet()) {
130 MockOnInterestHandler handler = entry.getValue();
131 if (handler.flags.getChildInherit() && handler.prefix.match(interest.getName())) {
132 return entry.getKey();
133 }
134 if (handler.prefix.equals(interest.getName())) {
135 return entry.getKey();
136 }
137 }
138 return -1;
139 }
140
141 /**
142 * Helper class for holding references to OnInterest handlers
143 */
144 class MockOnInterestHandler {
145
146 Name prefix;
147 OnInterest onInterest;
148 ForwardingFlags flags;
149
150 public MockOnInterestHandler(Name prefix, OnInterest onInterest, ForwardingFlags flags) {
151 this.prefix = prefix;
152 this.onInterest = onInterest;
153 this.flags = flags;
154 }
155 }
156
157 /**
158 * Send the Interest through the transport, read the entire response and call
159 * onData(interest, data).
160 *
161 * @param interest The Interest to send. This copies the Interest.
162 * @param onData When a matching data packet is received, this calls
163 * onData.onData(interest, data) where interest is the interest given to
164 * expressInterest and data is the received Data object. NOTE: You must not
165 * change the interest object - if you need to change it then make a copy.
166 * @param onTimeout If the interest times out according to the interest
167 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
168 * interest given to expressInterest. If onTimeout is null, this does not use
169 * it.
170 * @param wireFormat A WireFormat object used to encode the message.
171 * @return The pending interest ID which can be used with
172 * removePendingInterest.
173 * @throws IOException For I/O error in sending the interest.
174 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800175 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800176 public long expressInterest(Interest interest, OnData onData, OnTimeout onTimeout,
177 WireFormat wireFormat) throws IOException {
178 long id = node_.expressInterest(interest, onData, onTimeout, wireFormat);
179 handleIncomingRequests(interest);
180 return id;
181 }
182
183 /**
184 * Send the Interest through the transport, read the entire response and call
185 * onData(interest, data). This uses the default
186 * WireFormat.getDefaultWireFormat().
187 *
188 * @param interest The Interest to send. This copies the Interest.
189 * @param onData When a matching data packet is received, this calls
190 * onData.onData(interest, data) where interest is the interest given to
191 * expressInterest and data is the received Data object. NOTE: You must not
192 * change the interest object - if you need to change it then make a copy.
193 * @param onTimeout If the interest times out according to the interest
194 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
195 * interest given to expressInterest. If onTimeout is null, this does not use
196 * it.
197 * @return The pending interest ID which can be used with
198 * removePendingInterest.
199 * @throws IOException For I/O error in sending the interest.
200 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800201 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800202 public long expressInterest(Interest interest, OnData onData, OnTimeout onTimeout) throws IOException {
203 return expressInterest(interest, onData, onTimeout, WireFormat.getDefaultWireFormat());
204 }
205
206 /**
207 * Send the Interest through the transport, read the entire response and call
208 * onData(interest, data). Ignore if the interest times out.
209 *
210 * @param interest The Interest to send. This copies the Interest.
211 * @param onData When a matching data packet is received, this calls
212 * onData.onData(interest, data) where interest is the interest given to
213 * expressInterest and data is the received Data object. NOTE: You must not
214 * change the interest object - if you need to change it then make a copy.
215 * @param wireFormat A WireFormat object used to encode the message.
216 * @return The pending interest ID which can be used with
217 * removePendingInterest.
218 * @throws IOException For I/O error in sending the interest.
219 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800220 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800221 public long expressInterest(Interest interest, OnData onData, WireFormat wireFormat) throws IOException {
222 return expressInterest(interest, onData, null, wireFormat);
223 }
224
225 /**
226 * Send the Interest through the transport, read the entire response and call
227 * onData(interest, data). Ignore if the interest times out. This uses the
228 * default WireFormat.getDefaultWireFormat().
229 *
230 * @param interest The Interest to send. This copies the Interest.
231 * @param onData When a matching data packet is received, this calls
232 * onData.onData(interest, data) where interest is the interest given to
233 * expressInterest and data is the received Data object. NOTE: You must not
234 * change the interest object - if you need to change it then make a copy.
235 * @return The pending interest ID which can be used with
236 * removePendingInterest.
237 * @throws IOException For I/O error in sending the interest.
238 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800239 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800240 public long expressInterest(Interest interest, OnData onData) throws IOException {
241 return expressInterest(interest, onData, null, WireFormat.getDefaultWireFormat());
242 }
243
244 /**
245 * Encode name as an Interest. If interestTemplate is not null, use its
246 * interest selectors. Send the interest through the transport, read the
247 * entire response and call onData(interest, data).
248 *
249 * @param name A Name for the interest. This copies the Name.
250 * @param interestTemplate If not null, copy interest selectors from the
251 * template. This does not keep a pointer to the Interest object.
252 * @param onData When a matching data packet is received, this calls
253 * onData.onData(interest, data) where interest is the interest given to
254 * expressInterest and data is the received Data object. NOTE: You must not
255 * change the interest object - if you need to change it then make a copy.
256 * @param onTimeout If the interest times out according to the interest
257 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
258 * interest given to expressInterest. If onTimeout is null, this does not use
259 * it.
260 * @param wireFormat A WireFormat object used to encode the message.
261 * @return The pending interest ID which can be used with
262 * removePendingInterest.
263 * @throws IOException For I/O error in sending the interest.
264 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800265 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800266 public long expressInterest(Name name, Interest interestTemplate, OnData onData, OnTimeout onTimeout,
267 WireFormat wireFormat) throws IOException {
268 Interest interest = new Interest(name);
269 if (interestTemplate != null) {
270 interest.setMinSuffixComponents(interestTemplate.getMinSuffixComponents());
271 interest.setMaxSuffixComponents(interestTemplate.getMaxSuffixComponents());
272 interest.setKeyLocator(interestTemplate.getKeyLocator());
273 interest.setExclude(interestTemplate.getExclude());
274 interest.setChildSelector(interestTemplate.getChildSelector());
275 interest.setMustBeFresh(interestTemplate.getMustBeFresh());
276 interest.setScope(interestTemplate.getScope());
277 interest.setInterestLifetimeMilliseconds(
278 interestTemplate.getInterestLifetimeMilliseconds());
279 // Don't copy the nonce.
280 } else {
281 interest.setInterestLifetimeMilliseconds(4000.0);
282 }
283
284 return expressInterest(interest, onData, onTimeout, wireFormat);
285 }
286
287 /**
288 * Encode name as an Interest. If interestTemplate is not null, use its
289 * interest selectors. Send the interest through the transport, read the
290 * entire response and call onData(interest, data). Use a default interest
291 * lifetime.
292 *
293 * @param name A Name for the interest. This copies the Name.
294 * @param onData When a matching data packet is received, this calls
295 * onData.onData(interest, data) where interest is the interest given to
296 * expressInterest and data is the received Data object. NOTE: You must not
297 * change the interest object - if you need to change it then make a copy.
298 * @param onTimeout If the interest times out according to the interest
299 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
300 * interest given to expressInterest. If onTimeout is null, this does not use
301 * it.
302 * @param wireFormat A WireFormat object used to encode the message.
303 * @return The pending interest ID which can be used with
304 * removePendingInterest.
305 * @throws IOException For I/O error in sending the interest.
306 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800307 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800308 public long expressInterest(Name name, OnData onData, OnTimeout onTimeout,
309 WireFormat wireFormat) throws IOException {
310 return expressInterest(name, null, onData, onTimeout, wireFormat);
311 }
312
313 /**
314 * Encode name as an Interest. If interestTemplate is not null, use its
315 * interest selectors. Send the interest through the transport, read the
316 * entire response and call onData(interest, data). Ignore if the interest
317 * times out.
318 *
319 * @param name A Name for the interest. This copies the Name.
320 * @param interestTemplate If not null, copy interest selectors from the
321 * template. This does not keep a pointer to the Interest object.
322 * @param onData When a matching data packet is received, this calls
323 * onData.onData(interest, data) where interest is the interest given to
324 * expressInterest and data is the received Data object. NOTE: You must not
325 * change the interest object - if you need to change it then make a copy.
326 * @param wireFormat A WireFormat object used to encode the message.
327 * @return The pending interest ID which can be used with
328 * removePendingInterest.
329 * @throws IOException For I/O error in sending the interest.
330 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800331 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800332 public long expressInterest(Name name, Interest interestTemplate, OnData onData,
333 WireFormat wireFormat) throws IOException {
334 return expressInterest(name, interestTemplate, onData, null, wireFormat);
335 }
336
337 /**
338 * Encode name as an Interest. If interestTemplate is not null, use its
339 * interest selectors. Send the interest through the transport, read the
340 * entire response and call onData(interest, data). This uses the default
341 * WireFormat.getDefaultWireFormat().
342 *
343 * @param name A Name for the interest. This copies the Name.
344 * @param interestTemplate If not null, copy interest selectors from the
345 * template. This does not keep a pointer to the Interest object.
346 * @param onData When a matching data packet is received, this calls
347 * onData.onData(interest, data) where interest is the interest given to
348 * expressInterest and data is the received Data object. NOTE: You must not
349 * change the interest object - if you need to change it then make a copy.
350 * @param onTimeout If the interest times out according to the interest
351 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
352 * interest given to expressInterest. If onTimeout is null, this does not use
353 * it.
354 * @return The pending interest ID which can be used with
355 * removePendingInterest.
356 * @throws IOException For I/O error in sending the interest.
357 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800358 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800359 public long expressInterest(Name name, Interest interestTemplate, OnData onData,
360 OnTimeout onTimeout) throws IOException {
361 return expressInterest(name, interestTemplate, onData, onTimeout,
362 WireFormat.getDefaultWireFormat());
363 }
364
365 /**
366 * Encode name as an Interest. If interestTemplate is not null, use its
367 * interest selectors. Send the interest through the transport, read the
368 * entire response and call onData(interest, data). Ignore if the interest
369 * times out. This uses the default WireFormat.getDefaultWireFormat().
370 *
371 * @param name A Name for the interest. This copies the Name.
372 * @param interestTemplate If not null, copy interest selectors from the
373 * template. This does not keep a pointer to the Interest object.
374 * @param onData When a matching data packet is received, this calls
375 * onData.onData(interest, data) where interest is the interest given to
376 * expressInterest and data is the received Data object. NOTE: You must not
377 * change the interest object - if you need to change it then make a copy.
378 * @return The pending interest ID which can be used with
379 * removePendingInterest.
380 * @throws IOException For I/O error in sending the interest.
381 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800382 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800383 public long expressInterest(Name name, Interest interestTemplate, OnData onData) throws IOException {
384 return expressInterest(name, interestTemplate, onData, null, WireFormat.getDefaultWireFormat());
385 }
386
387 /**
388 * Encode name as an Interest. If interestTemplate is not null, use its
389 * interest selectors. Send the interest through the transport, read the
390 * entire response and call onData(interest, data). Use a default interest
391 * lifetime. This uses the default WireFormat.getDefaultWireFormat().
392 *
393 * @param name A Name for the interest. This copies the Name.
394 * @param onData When a matching data packet is received, this calls
395 * onData.onData(interest, data) where interest is the interest given to
396 * expressInterest and data is the received Data object. NOTE: You must not
397 * change the interest object - if you need to change it then make a copy.
398 * @param onTimeout If the interest times out according to the interest
399 * lifetime, this calls onTimeout.onTimeout(interest) where interest is the
400 * interest given to expressInterest. If onTimeout is null, this does not use
401 * it.
402 * @return The pending interest ID which can be used with
403 * removePendingInterest.
404 * @throws IOException For I/O error in sending the interest.
405 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800406 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800407 public long expressInterest(Name name, OnData onData, OnTimeout onTimeout) throws IOException {
408 return expressInterest(name, null, onData, onTimeout, WireFormat.getDefaultWireFormat());
409 }
410
411 /**
412 * Encode name as an Interest. If interestTemplate is not null, use its
413 * interest selectors. Send the interest through the transport, read the
414 * entire response and call onData(interest, data). Use a default interest
415 * lifetime. Ignore if the interest times out.
416 *
417 * @param name A Name for the interest. This copies the Name.
418 * @param onData When a matching data packet is received, this calls
419 * onData.onData(interest, data) where interest is the interest given to
420 * expressInterest and data is the received Data object. NOTE: You must not
421 * change the interest object - if you need to change it then make a copy.
422 * @param wireFormat A WireFormat object used to encode the message.
423 * @return The pending interest ID which can be used with
424 * removePendingInterest.
425 * @throws IOException For I/O error in sending the interest.
426 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800427 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800428 public long expressInterest(Name name, OnData onData, WireFormat wireFormat) throws IOException {
429 return expressInterest(name, null, onData, null, wireFormat);
430 }
431
432 /**
433 * Encode name as an Interest. If interestTemplate is not null, use its
434 * interest selectors. Send the interest through the transport, read the
435 * entire response and call onData(interest, data). Use a default interest
436 * lifetime. Ignore if the interest times out. This uses the default
437 * WireFormat.getDefaultWireFormat().
438 *
439 * @param name A Name for the interest. This copies the Name.
440 * @param onData When a matching data packet is received, this calls
441 * onData.onData(interest, data) where interest is the interest given to
442 * expressInterest and data is the received Data object. NOTE: You must not
443 * change the interest object - if you need to change it then make a copy.
444 * @return The pending interest ID which can be used with
445 * removePendingInterest.
446 * @throws IOException For I/O error in sending the interest.
447 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800448 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800449 public long expressInterest(Name name, OnData onData) throws IOException {
450 return expressInterest(name, null, onData, null, WireFormat.getDefaultWireFormat());
451 }
452
453 /**
454 * Remove the pending interest entry with the pendingInterestId from the
455 * pending interest table. This does not affect another pending interest with
456 * a different pendingInterestId, even if it has the same interest name. If
457 * there is no entry with the pendingInterestId, do nothing.
458 *
459 * @param pendingInterestId The ID returned from expressInterest.
460 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800461// public void removePendingInterest(long pendingInterestId) {
462// node_.removePendingInterest(pendingInterestId);
463// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800464 /**
465 * Register prefix with the connected NDN hub and call onInterest when a
466 * matching interest is received. If you have not called
467 * setCommandSigningInfo, this assumes you are connecting to NDNx. If you have
468 * called setCommandSigningInfo, this first sends an NFD registration request,
469 * and if that times out then this sends an NDNx registration request. If you
470 * need to register a prefix with NFD, you must first call
471 * setCommandSigningInfo.
472 *
473 * @param prefix A Name for the prefix to register. This copies the Name.
474 * @param onInterest When an interest is received which matches the name
475 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
476 * registeredPrefixId). NOTE: You must not change the prefix object - if you
477 * need to change it then make a copy.
478 * @param onRegisterFailed If register prefix fails for any reason, this calls
479 * onRegisterFailed.onRegisterFailed(prefix).
480 * @param flags The flags for finer control of which interests are forwarded
481 * to the application.
482 * @param wireFormat A WireFormat object used to encode the message.
483 * @return The lastRegisteredId prefix ID which can be used with
484 * removeRegisteredPrefix.
485 * @throws IOException For I/O error in sending the registration request.
486 * @throws SecurityException If signing a command interest for NFD and cannot
487 * find the private key for the certificateName.
488 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800489 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800490 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
491 ForwardingFlags flags, WireFormat wireFormat) throws IOException, net.named_data.jndn.security.SecurityException {
492 lastRegisteredId++;
493 handlerMap.put(lastRegisteredId, new MockOnInterestHandler(prefix, onInterest, flags));
494 return lastRegisteredId;
495 }
496
497 /**
498 * Register prefix with the connected NDN hub and call onInterest when a
499 * matching interest is received. This uses the default
500 * WireFormat.getDefaultWireFormat().
501 *
502 * @param prefix A Name for the prefix to register. This copies the Name.
503 * @param onInterest When an interest is received which matches the name
504 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
505 * registeredPrefixId). NOTE: You must not change the prefix object - if you
506 * need to change it then make a copy.
507 * @param onRegisterFailed If register prefix fails for any reason, this calls
508 * onRegisterFailed.onRegisterFailed(prefix).
509 * @param flags The flags for finer control of which interests are forwarded
510 * to the application.
511 * @return The lastRegisteredId prefix ID which can be used with
512 * removeRegisteredPrefix.
513 * @throws IOException For I/O error in sending the registration request.
514 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800515 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800516 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
517 ForwardingFlags flags) throws IOException, net.named_data.jndn.security.SecurityException {
518 return registerPrefix(prefix, onInterest, onRegisterFailed, flags,
519 WireFormat.getDefaultWireFormat());
520 }
521
522 /**
523 * Register prefix with the connected NDN hub and call onInterest when a
524 * matching interest is received. Use default ForwardingFlags.
525 *
526 * @param prefix A Name for the prefix to register. This copies the Name.
527 * @param onInterest When an interest is received which matches the name
528 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
529 * registeredPrefixId). NOTE: You must not change the prefix object - if you
530 * need to change it then make a copy.
531 * @param onRegisterFailed If register prefix fails for any reason, this calls
532 * onRegisterFailed.onRegisterFailed(prefix).
533 * @param wireFormat A WireFormat object used to encode the message.
534 * @return The lastRegisteredId prefix ID which can be used with
535 * removeRegisteredPrefix.
536 * @throws IOException For I/O error in sending the registration request.
537 * @throws SecurityException If signing a command interest for NFD and cannot
538 * find the private key for the certificateName.
539 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800540 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800541 public long registerPrefix(Name prefix, OnInterest onInterest, OnRegisterFailed onRegisterFailed,
542 WireFormat wireFormat) throws IOException, net.named_data.jndn.security.SecurityException {
543 return registerPrefix(prefix, onInterest, onRegisterFailed, new ForwardingFlags(), wireFormat);
544 }
545
546 /**
547 * Register prefix with the connected NDN hub and call onInterest when a
548 * matching interest is received. This uses the default
549 * WireFormat.getDefaultWireFormat(). Use default ForwardingFlags.
550 *
551 * @param prefix A Name for the prefix to register. This copies the Name.
552 * @param onInterest When an interest is received which matches the name
553 * prefix, this calls onInterest.onInterest(prefix, interest, transport,
554 * registeredPrefixId). NOTE: You must not change the prefix object - if you
555 * need to change it then make a copy.
556 * @param onRegisterFailed If register prefix fails for any reason, this calls
557 * onRegisterFailed.onRegisterFailed(prefix).
558 * @return The lastRegisteredId prefix ID which can be used with
559 * removeRegisteredPrefix.
560 * @throws IOException For I/O error in sending the registration request.
561 * @throws SecurityException If signing a command interest for NFD and cannot
562 * find the private key for the certificateName.
563 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800564 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800565 public long registerPrefix(Name prefix, OnInterest onInterest,
566 OnRegisterFailed onRegisterFailed) throws IOException, net.named_data.jndn.security.SecurityException {
567 return registerPrefix(prefix, onInterest, onRegisterFailed, new ForwardingFlags(),
568 WireFormat.getDefaultWireFormat());
569 }
570
571 /**
572 * Remove the lastRegisteredId prefix entry with the registeredPrefixId from
573 * the lastRegisteredId prefix table. This does not affect another
574 * lastRegisteredId prefix with a different registeredPrefixId, even if it has
575 * the same prefix name. If there is no entry with the registeredPrefixId, do
576 * nothing.
577 *
578 * @param registeredPrefixId The ID returned from registerPrefix.
579 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800580// public void removeRegisteredPrefix(long registeredPrefixId) {
581// handlerMap.remove(registeredPrefixId);
582// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800583 /**
584 * Process any packets to receive and call callbacks such as onData,
585 * onInterest or onTimeout. This returns immediately if there is no data to
586 * receive. This blocks while calling the callbacks. You should repeatedly
587 * call this from an event loop, with calls to sleep as needed so that the
588 * loop doesn’t use 100% of the CPU. Since processEvents modifies the pending
589 * interest table, your application should make sure that it calls
590 * processEvents in the same thread as expressInterest (which also modifies
591 * the pending interest table). This may throw an exception for reading data
592 * or in the callback for processing the data. If you call this from an main
593 * event loop, you may want to catch and log/disregard all exceptions.
594 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800595 @Override
Andrew Brown3831baf2015-01-19 13:38:52 -0800596 public void processEvents() throws IOException, EncodingException {
597 // Just call Node's processEvents.
598 node_.processEvents();
599 }
600
601 /**
602 * Shut down and disconnect this Face.
603 */
Andrew Brownb91e6902015-02-12 09:01:50 -0800604// public void shutdown() {
605// node_.shutdown();
606// }
Andrew Brown3831baf2015-01-19 13:38:52 -0800607}