Kaynağa Gözat

Added -c to search to allow for more exhaustive searches, also sort sessionStats

Caleb James DeLisle 9 yıl önce
ebeveyn
işleme
ec8c4fdd62

+ 1 - 1
dht/dhtcore/Janitor.c

@@ -137,7 +137,7 @@ static void search(uint8_t target[16], struct Janitor* janitor)
 
     struct Allocator* searchAlloc = Allocator_child(janitor->allocator);
     struct RouterModule_Promise* rp =
-        SearchRunner_search(target, janitor->searchRunner, searchAlloc);
+        SearchRunner_search(target, -1, janitor->searchRunner, searchAlloc);
 
     if (!rp) {
         Log_debug(janitor->logger, "SearchRunner_search() returned NULL, probably full.");

+ 1 - 1
dht/dhtcore/Router.c

@@ -59,7 +59,7 @@ void Router_brokenLink(struct Router* r, uint64_t path, uint64_t labelAtErrorHop
 void Router_searchForNode(struct Router* r, uint8_t ip6[16], struct Allocator* alloc)
 {
     struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
-    SearchRunner_search(ip6, rr->searchRunner, alloc);
+    SearchRunner_search(ip6, -1, rr->searchRunner, alloc);
 }
 
 void Router_disconnectedPeer(struct Router* r, uint64_t path)

+ 13 - 6
dht/dhtcore/SearchRunner.c

@@ -28,10 +28,8 @@
 #include "util/events/Timeout.h"
 #include "util/version/Version.h"
 
-
 /** The maximum number of requests to make before calling a search failed. */
-#define MAX_REQUESTS_PER_SEARCH 8
-
+#define DEFAULT_MAX_REQUESTS_PER_SEARCH 8
 
 struct SearchRunner_pvt
 {
@@ -71,6 +69,9 @@ struct SearchRunner_Search
     /** The number of requests which have been sent out so far for this search. */
     uint32_t totalRequests;
 
+    /** Maximum number of requests to make before terminating the search. */
+    uint32_t maxRequests;
+
     /** The address which we are searching for. */
     struct Address target;
 
@@ -194,7 +195,7 @@ static void searchStep(struct SearchRunner_Search* search)
         nextSearchNode = SearchStore_getNextNode(search->search);
 
         // If the number of requests sent has exceeded the max search requests, let's stop there.
-        if (search->totalRequests >= MAX_REQUESTS_PER_SEARCH || nextSearchNode == NULL) {
+        if (search->totalRequests >= search->maxRequests || nextSearchNode == NULL) {
             if (search->pub.callback) {
                 search->pub.callback(&search->pub, 0, NULL, NULL);
             }
@@ -283,6 +284,7 @@ struct SearchRunner_SearchData* SearchRunner_showActiveSearch(struct SearchRunne
 }
 
 struct RouterModule_Promise* SearchRunner_search(uint8_t target[16],
+                                                 int maxRequests,
                                                  struct SearchRunner* searchRunner,
                                                  struct Allocator* allocator)
 {
@@ -294,6 +296,10 @@ struct RouterModule_Promise* SearchRunner_search(uint8_t target[16],
         return NULL;
     }
 
+    if (maxRequests < 1) {
+        maxRequests = DEFAULT_MAX_REQUESTS_PER_SEARCH;
+    }
+
     struct Allocator* alloc = Allocator_child(allocator);
 
     struct Address targetAddr = { .path = 0 };
@@ -302,7 +308,7 @@ struct RouterModule_Promise* SearchRunner_search(uint8_t target[16],
     struct NodeList* nodes =
         NodeStore_getClosestNodes(runner->nodeStore,
                                   &targetAddr,
-                                  MAX_REQUESTS_PER_SEARCH,
+                                  maxRequests,
                                   Version_CURRENT_PROTOCOL,
                                   alloc);
 
@@ -323,7 +329,8 @@ struct RouterModule_Promise* SearchRunner_search(uint8_t target[16],
             .alloc = alloc
         },
         .runner = runner,
-        .search = sss
+        .search = sss,
+        .maxRequests = maxRequests
     }));
     Identity_set(search);
     runner->searches++;

+ 5 - 3
dht/dhtcore/SearchRunner.h

@@ -53,12 +53,14 @@ struct SearchRunner
  * then it will be called with 0 milliseconds lag and NULL response indicating the search is over.
  *
  * @param searchTarget the address to search for.
+ * @param maxRequests the number of requests to make before terminating the search.
  * @param runner the search runner
  * @param alloc an allocator for the search, free this to cancel the search
  */
-struct RouterModule_Promise* SearchRunner_search(uint8_t searchTarget[16],
-                                                 struct SearchRunner* runner,
-                                                 struct Allocator* alloc);
+struct RouterModule_Promise* SearchRunner_search(uint8_t target[16],
+                                                 int maxRequests,
+                                                 struct SearchRunner* searchRunner,
+                                                 struct Allocator* allocator);
 
 /**
  * Show an active search.

+ 16 - 2
dht/dhtcore/SearchRunner_admin.c

@@ -108,6 +108,11 @@ static void search(Dict* args, void* vctx, String* txid, struct Allocator* reqAl
 {
     struct Context* ctx = Identity_check((struct Context*) vctx);
     String* addrStr = Dict_getString(args, String_CONST("ipv6"));
+
+    int maxRequests = -1;
+    uint64_t* maxRequestsPtr = Dict_getInt(args, String_CONST("maxRequests"));
+    if (maxRequestsPtr) { maxRequests = *maxRequestsPtr; }
+
     uint8_t addr[16];
     if (AddrTools_parseIp(addr, (uint8_t*) addrStr->bytes)) {
         Dict* resp = Dict_new(reqAlloc);
@@ -116,12 +121,20 @@ static void search(Dict* args, void* vctx, String* txid, struct Allocator* reqAl
     } else {
         struct Allocator* alloc = Allocator_child(ctx->allocator);
         struct Search* s = Allocator_calloc(alloc, sizeof(struct Search), 1);
-        s->promise = SearchRunner_search(addr, ctx->runner, alloc);
+        s->promise = SearchRunner_search(addr, maxRequests, ctx->runner, alloc);
         s->ctx = ctx;
         s->txid = String_clone(txid, alloc);
         s->alloc = alloc;
         Identity_set(s);
 
+        if (!s->promise) {
+            Dict* resp = Dict_new(reqAlloc);
+            Dict_putString(resp, String_CONST("error"), String_CONST("creating search"), reqAlloc);
+            Admin_sendMessage(resp, txid, ctx->admin);
+            Allocator_free(alloc);
+            return;
+        }
+
         s->promise->userData = s;
         s->promise->callback = searchResponse;
     }
@@ -146,6 +159,7 @@ void SearchRunner_admin_register(struct SearchRunner* runner,
 
     Admin_registerFunction("SearchRunner_search", search, ctx, true,
         ((struct Admin_FunctionArg[]) {
-            { .name = "ipv6", .required = 1, .type = "String" }
+            { .name = "ipv6", .required = 1, .type = "String" },
+            { .name = "maxRequests", .required = 0, .type = "Int" }
         }), admin);
 }

+ 10 - 1
tools/search

@@ -34,6 +34,11 @@ var equalsIp = function (ip, fullAddr) {
 
 var main = function (argv) {
 
+    var count = -1;
+    if (argv.indexOf('-c') !== -1) {
+        count = Number(argv[argv.indexOf('-c')+1]);
+    }
+
     var target = argv[argv.length-1];
 
     var cjdns;
@@ -64,6 +69,10 @@ var main = function (argv) {
         var timeOfFirstFind;
         var handleMessage = function (err, msg) {
             if (err) { throw err; }
+            if (msg.error && msg.error !== 'none') {
+                cjdns.disconnect();
+                console.log(msg);
+            }
             if (msg.complete === "1") {
                 cjdns.disconnect();
                 console.log("Done -- first result in " + (timeOfFirstFind - startTime) + "ms." +
@@ -85,7 +94,7 @@ var main = function (argv) {
         };
 
         cjdns.setDefaultHandler(handleMessage);
-        cjdns.SearchRunner_search(target, handleMessage);
+        cjdns.SearchRunner_search(target, count, handleMessage);
     });
 };
 

+ 10 - 1
tools/sessionStats

@@ -30,6 +30,7 @@ var printSession = function (session) {
 
 var cjdns;
 var handles = [];
+var sessions = [];
 
 nThen(function (waitFor) {
 
@@ -51,7 +52,7 @@ nThen(function (waitFor) {
     var next = function (i) {
         cjdns.SessionManager_sessionStats(Number(handles[i]), waitFor(function (err, ret) {
             if (err) { throw err; }
-            printSession(ret);
+            sessions.push(ret);
             i++
             if (i < handles.length) { next(i); }
         }));
@@ -60,4 +61,12 @@ nThen(function (waitFor) {
 
 }).nThen(function (waitFor) {
     cjdns.disconnect();
+
+    sessions.sort(function (a, b) {
+        return (a.addr.substring(a.addr.indexOf('.')) < b.addr.substring(b.addr.indexOf('.')))
+            ? -1 : 1;
+    });
+    for (var i = 0; i < sessions.length; i++) {
+        printSession(sessions[i]);
+    }
 });