dhttest.c (2732B)
1 /* 2 * Copy me if you can. 3 * by 20h 4 */ 5 6 #include <unistd.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <strings.h> 10 11 #include "dht.h" 12 13 int 14 main(void) 15 { 16 dht_t *dht; 17 dhtnode_t *node, *node78; 18 char nodename[17]; 19 dhtlist_t *results; 20 21 bzero(nodename, sizeof(nodename)); 22 dht = dht_new("testnetwork"); 23 24 for (int i = 0; i < 4096; i++) { 25 node = dhtnode_new(); 26 dhtnode_mkid(node); 27 snprintf(nodename, sizeof(nodename)-1, "node%d", i); 28 dhtnode_setaddr(node, nodename); 29 30 if(dht_update(dht, node) == NULL) { 31 dhtnode_free(node); 32 break; 33 } 34 35 if (i == 78) 36 node78 = node; 37 } 38 printf("4096 nodes created.\n"); 39 40 printf("node78:\n"); 41 dhtnode_print(node78); 42 printf("nearby node78 is:\n"); 43 results = dht_find(dht, node78, 5); 44 fordhtlist(results, elem) 45 dhtnode_print(elem->node); 46 dhtlist_free(results); 47 48 node = dhtnode_new(); 49 dhtnode_mkid(node); 50 printf("noderand:\n"); 51 dhtnode_print(node); 52 printf("nearby noderand is:\n"); 53 results = dht_find(dht, node, 10); 54 fordhtlist(results, elem) 55 dhtnode_print(elem->node); 56 dhtlist_free(results); 57 dhtnode_free(node); 58 59 dht_free(dht); 60 61 dht = dht_new("localhost"); 62 dhtnode_setid(dht->routing->node, "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00" 63 "\x00\x00\x00\x00\x00\x00\00\x00\x00\x00\x00"); 64 65 node = dhtnode_new(); 66 dhtnode_setid(node, "\xFF\xFF\xFF\xF0\x00\x00\x00\x00\x00" 67 "\x00\x00\x00\x00\x00\x00\00\x00\x00\x00\x00"); 68 dhtnode_setaddr(node, "node1"); 69 dht_update(dht, node); 70 71 node = dhtnode_new(); 72 dhtnode_setid(node, "\x11\x11\x11\x11\x00\x00\x00\x00\x00" 73 "\x00\x00\x00\x00\x00\x00\00\x00\x00\x00\x00"); 74 dhtnode_setaddr(node, "node2"); 75 dht_update(dht, node); 76 77 node = dhtnode_new(); 78 dhtnode_setid(node, "\x22\x22\x22\x22\x00\x00\x00\x00\x00" 79 "\x00\x00\x00\x00\x00\x00\00\x00\x00\x00\x00"); 80 dhtnode_setaddr(node, "nodesearch"); 81 printf("searching for one nearby node\n"); 82 results = dht_find(dht, node, 1); 83 fordhtlist(results, elem) 84 dhtnode_print(elem->node); 85 dhtlist_free(results); 86 dhtnode_free(node); 87 88 node = dhtnode_new(); 89 dhtnode_setid(node, "\xFF\xFF\xFF\xF0\x00\x00\x00\x00\x00" 90 "\x00\x00\x00\x00\x00\x00\00\x00\x00\x00\x00"); 91 dhtnode_setaddr(node, "nodesearch2"); 92 printf("searching for 10 nearby nodes\n"); 93 results = dht_find(dht, node, 10); 94 fordhtlist(results, elem) 95 dhtnode_print(elem->node); 96 dhtlist_free(results); 97 dhtnode_free(node); 98 99 node = dhtnode_new(); 100 dhtnode_setid(node, "\x11\x11\x11\x11\x00\x00\x00\x00\x00" 101 "\x00\x00\x00\x00\x00\x00\00\x00\x00\x00\x00"); 102 dhtnode_setaddr(node, "nodesearch3"); 103 printf("searching for one nearby nodes\n"); 104 results = dht_find(dht, node, 1); 105 fordhtlist(results, elem) 106 dhtnode_print(elem->node); 107 dhtlist_free(results); 108 dhtnode_free(node); 109 dht_free(dht); 110 111 return 0; 112 } 113