blob: 1057f602e9a2ef65b02bb4636fb7d14f193570b1 [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
7/*
8 * 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.
12 *
13 * settings is an associative array with the following defaults:
14 * {
15 * maxInterestLifetime: 16000 // milliseconds
16 * }
17 */
18var ExponentialReExpressClosure = function ExponentialReExpressClosure
19 (callerClosure, settings) {
20 // Inherit from Closure.
21 Closure.call(this);
22
23 this.callerClosure = callerClosure;
24 settings = (settings || {});
25 this.maxInterestLifetime = (settings.maxInterestLifetime || 16000);
26};
27
28ExponentialReExpressClosure.prototype.upcall = function(kind, upcallInfo) {
29 try {
30 if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
31 var interestLifetime = upcallInfo.interest.interestLifetime;
32 if (interestLifetime == null)
33 return this.callerClosure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, upcallInfo);
34
35 var nextInterestLifetime = interestLifetime * 2;
36 if (nextInterestLifetime > this.maxInterestLifetime)
37 return this.callerClosure.upcall(Closure.UPCALL_INTEREST_TIMED_OUT, upcallInfo);
38
39 var nextInterest = upcallInfo.interest.clone();
40 nextInterest.interestLifetime = nextInterestLifetime;
41 upcallInfo.ndn.expressInterest(nextInterest.name, this, nextInterest);
42 return Closure.RESULT_OK;
43 }
44 else
45 return this.callerClosure.upcall(kind, upcallInfo);
46 } catch (ex) {
47 console.log("ExponentialReExpressClosure.upcall exception: " + ex);
48 return Closure.RESULT_ERR;
49 }
50};