forked from vrancurel/dcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
130 lines (114 loc) · 2.35 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "kadsim.h"
using namespace std;
KadConf::KadConf(int n_bits, int k, int alpha, int n_nodes)
{
this->n_bits = n_bits;
this->k = k;
this->alpha = alpha;
this->n_nodes = n_nodes;
}
void
KadConf::save(std::ostream& fout)
{
fout << "n_bits " << n_bits << "\n";
fout << "k " << k << "\n";
fout << "alpha " << alpha << "\n";
fout << "n_nodes " << n_nodes << "\n";
}
void
usage()
{
std::cerr << "usage: kadsim\n";
std::cerr << "\t-b\tn_bits\n";
std::cerr << "\t-k\tKademlia K parameter\n";
std::cerr << "\t-a\tKademlia alpha parameter\n";
std::cerr << "\t-n\tnumber of nodes\n";
std::cerr << "\t-c\tinitial number of connections per node\n";
std::cerr << "\t-N\tnumber of files\n";
std::cerr << "\t-S\trandom seed\n";
exit(1);
}
void
parse_error()
{
std::cerr << "parse error\n";
exit(1);
}
int main(int argc, char **argv)
{
int c;
int n_bits = 64;
int k = 20;
int alpha = 3;
int n_nodes = 1500;
int n_init_conn = 100;
int n_files = 5000;
int rand_seed = 0;
char *fname = NULL;
opterr = 0;
while ((c = getopt (argc, argv, "b:k:a:n:c:S:f:N:")) != -1)
{
switch (c)
{
case 'b':
n_bits = atoi(optarg);
break;
case 'k':
k = atoi(optarg);
break;
case 'a':
alpha = atoi(optarg);
break;
case 'n':
n_nodes = atoi(optarg);
break;
case 'c':
n_init_conn = atoi(optarg);
break;
case 'S':
rand_seed = atoi(optarg);
break ;
case 'f':
fname = strdup(optarg);
break ;
case 'N':
n_files = atoi(optarg);
break ;
case '?':
default:
usage();
}
}
if (fname)
{
std::ifstream fin(fname);
if (!fin.is_open())
{
std::cerr << "unable to open " << fname << "\n";
exit(1);
}
char buf[256], *p;
#define GETLINE() \
fin.getline(buf, sizeof (buf)); \
if (NULL == (p = rindex(buf, ' '))) \
parse_error(); \
p++;
GETLINE(); n_bits = atoi(p);
GETLINE(); k = atoi(p);
GETLINE(); alpha = atoi(p);
GETLINE(); n_nodes = atoi(p);
}
KadConf conf(n_bits, k, alpha, n_nodes);
//conf.save(std::cout);
KadNetwork network(&conf);
Shell shell;
srand(rand_seed);
network.initialize_nodes(n_init_conn);
network.initialize_files(n_files);
network.check_files();
shell.set_cmds(cmd_defs);
shell.set_handle(&network);
shell.set_prompt("kadsim> ");
shell.loop();
return EXIT_SUCCESS;
}