From f86e6ca98c24aaf7a49705a1c3bf34bc29ba0886 Mon Sep 17 00:00:00 2001 From: Stephen Vickers Date: Mon, 29 Feb 2016 13:58:05 +0000 Subject: [PATCH] #7 Allow users to specify the initial TTL for a `traceRoute()` method call, #6 Permit users to control the number of permitted hop timeouts for the `traceRoute()` method --- index.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 75b0132..fdd0a62 100644 --- a/index.js +++ b/index.js @@ -462,7 +462,7 @@ Session.prototype.traceRouteCallback = function (trace, req, error, target, return; } - if ((error instanceof RequestTimedOutError) && ++trace.timeouts >= 3) { + if ((error instanceof RequestTimedOutError) && ++trace.timeouts >= trace.maxHopTimeouts) { trace.doneCallback (new Error ("Too many timeouts"), target); return; } @@ -485,12 +485,28 @@ Session.prototype.traceRouteCallback = function (trace, req, error, target, } } -Session.prototype.traceRoute = function (target, ttl, feedCallback, +Session.prototype.traceRoute = function (target, ttlOrOptions, feedCallback, doneCallback) { + // signature was (target, feedCallback, doneCallback) if (! doneCallback) { doneCallback = feedCallback; - feedCallback = ttl; - ttl = this.ttl; + feedCallback = ttlOrOptions; + ttlOrOptions = {ttl: this.ttl}; + } + + var maxHopTimeouts = 3; + var startTtl = 1; + var ttl = this.ttl; + + if (typeof ttlOrOptions == "object") { + if (ttlOrOptions.ttl) + ttl = ttlOrOptions.ttl; + if (ttlOrOptions.maxHopTimeouts) + maxHopTimeouts = ttlOrOptions.maxHopTimeouts; + if (ttlOrOptions.startTtl) + startTtl = ttlOrOptions.startTtl; + } else { + ttl = ttlOrOptions; } var id = this._generateId (); @@ -505,6 +521,7 @@ Session.prototype.traceRoute = function (target, ttl, feedCallback, feedCallback: feedCallback, doneCallback: doneCallback, ttl: ttl, + maxHopTimeouts: maxHopTimeouts, timeouts: 0 }; @@ -514,7 +531,7 @@ Session.prototype.traceRoute = function (target, ttl, feedCallback, id: id, retries: this.retries, timeout: this.timeout, - ttl: 1, + ttl: startTtl, target: target }; req.callback = me.traceRouteCallback.bind (me, trace, req);