blob: 3b0fd85dbef5dbaf426057a371e6bc611b91db41 [file] [log] [blame]
Jeff Thompson65a11e42013-02-18 17:47:59 -08001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 * This is the closure class for use in expressInterest to re express with exponential falloff.
5 */
6
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07007/**
Jeff Thompson65a11e42013-02-18 17:47:59 -08008 * Create a new ExponentialReExpressClosure where upcall responds to UPCALL_INTEREST_TIMED_OUT
9 * by expressing the interest again with double the interestLifetime. If the interesLifetime goes
10 * over maxInterestLifetime, then call callerClosure.upcall with UPCALL_INTEREST_TIMED_OUT.
11 * When upcall is not UPCALL_INTEREST_TIMED_OUT, just call callerClosure.upcall.
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070012 * @constructor
13 * @param {Closure} callerClosure
14 * @param {Object} settings if not null, an associative array with the following defaults:
Jeff Thompson65a11e42013-02-18 17:47:59 -080015 * {
16 * maxInterestLifetime: 16000 // milliseconds
17 * }
18 */
19var ExponentialReExpressClosure = function ExponentialReExpressClosure
20 (callerClosure, settings) {
21 // Inherit from Closure.
22 Closure.call(this);
23
24 this.callerClosure = callerClosure;
25 settings = (settings || {});
26 this.maxInterestLifetime = (settings.maxInterestLifetime || 16000);
27};
28
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070029/**
30 * Wrap this.callerClosure to responds to UPCALL_INTEREST_TIMED_OUT
31 * by expressing the interest again as described in the constructor.
32 */
Jeff Thompson65a11e42013-02-18 17:47:59 -080033ExponentialReExpressClosure.prototype.upcall = function(kind, upcallInfo) {
34 try {
35 if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
36 var interestLifetime = upcallInfo.interest.interestLifetime;
37 if (interestLifetime == null)
38 return this.callerClosure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, upcallInfo);
39
40 var nextInterestLifetime = interestLifetime * 2;
41 if (nextInterestLifetime > this.maxInterestLifetime)
42 return this.callerClosure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, upcallInfo);
43
44 var nextInterest = upcallInfo.interest.clone();
45 nextInterest.interestLifetime = nextInterestLifetime;
46 upcallInfo.ndn.expressInterest(nextInterest.name, this, nextInterest);
47 return Closure.RESULT_OK;
48 }
49 else
50 return this.callerClosure.upcall(kind, upcallInfo);
51 } catch (ex) {
52 console.log("ExponentialReExpressClosure.upcall exception: " + ex);
53 return Closure.RESULT_ERR;
54 }
55};