forked from vrancurel/dcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkad_network.cpp
More file actions
243 lines (201 loc) · 5.19 KB
/
Copy pathkad_network.cpp
File metadata and controls
243 lines (201 loc) · 5.19 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "kadsim.h"
KadNetwork::KadNetwork(KadConf *conf)
{
this->conf = conf;
}
KadNetwork::~KadNetwork()
{
}
/**
* initialize nodes
*
* @param n_init_conn
*/
void
KadNetwork::initialize_nodes(int n_init_conn)
{
std::cout << "initialize nodes\n";
BitMap bitmap = BitMap(conf->n_nodes);
//split the total keyspace equally among nodes
CBigNum keyspace = CBigNum(1);
keyspace=(keyspace<<conf->n_bits);
keyspace/=conf->n_nodes;
//create nodes
for (u_int i = 0;i < conf->n_nodes;i++)
{
KadNode *node = new KadNode(conf, bitmap.get_rand_bit()*keyspace);
nodes.push_back(node);
nodes_map[node->get_id().ToString(16)] = node;
}
//there shall be a responsable for every portion of the keyspace
assert(!bitmap.check());
//continue creating conns for the nodes that dont meet the initial number required
for (u_int i = 0;i < conf->n_nodes;i++)
{
KadNode *node = nodes[i];
if ((i % 1000) == 0)
std::cerr << "node " << i << "/" << conf->n_nodes << " \r";
u_int guard = 0;
while (node->get_n_conns() < n_init_conn)
{
KadNode *other;
if (guard >= (2 * conf->n_nodes))
{
std::cout << "forgiving required conditions for " << node->get_id().ToString(16) << ", it has only " << node->get_n_conns() << " connections\n";
break ;
}
//pick a random node
int x = rand() % nodes.size();
other = nodes[x];
//connect them 2-way
//std::cout << "connecting nodes " << node->get_id().ToString(16) << " (" << node->get_n_conns() << ") and " << other->get_id().ToString(16) << "\n";
node->add_conn(other, false);
other->add_conn(node, false);
guard++;
}
}
std::cout << "\n";
}
void
KadNetwork::initialize_files(int n_files)
{
std::cout << "initialize files\n";
for (int i = 0;i < n_files;i++)
{
if ((i % 1000) == 0)
std::cerr << "file " << i << "/" << n_files << " \r";
//take a random node
int x = rand() % nodes.size();
KadNode *node = nodes[x];
//gen a random identifier for the file
CBigNum bn;
bn.Rand(conf->n_bits);
KadFile *file = new KadFile(bn, node);
files.push_back(file);
//store file at multiple location
std::list<KadNode*> result = node->lookup(*file);
for (std::list<KadNode*>::iterator it = result.begin();it != result.end();++it)
(*it)->store(file);
}
}
/**
* check that files are accessible from random nodes
*/
void
KadNetwork::check_files()
{
std::cout << "checking files\n";
int n_wrong = 0;
for (u_int i = 0;i < files.size();i++)
{
if ((i % 1000) == 0)
std::cerr << "file " << i << "/" << files.size() << " \r";
KadFile *file = files[i];
//take a random node
int x = rand() % nodes.size();
KadNode *node = nodes[x];
//get results
std::list<KadNode*> result = node->lookup(*file);
//check that at least one result has the file
bool found = false;
for (std::list<KadNode*>::iterator it = result.begin();it != result.end();++it)
{
std::vector<KadFile*> node_files = (*it)->get_files();
for (u_int j = 0;j < node_files.size();j++)
{
KadFile *node_file = node_files[j];
if (node_file == file)
{
found = true;
break ;
}
}
}
if (!found)
{
std::cerr << "file " << file->get_id().ToString(16) << " who was referenced by " << file->get_referencer()->get_id().ToString(16) << " was not found\n";
n_wrong++;
}
}
std::cerr << n_wrong << "/" << files.size() << " files wrongly stored\n";
}
void
KadNetwork::rand_node(tnode_callback_func cb_func,
void *cb_arg)
{
int x = rand() % nodes.size();
if (NULL != cb_func)
cb_func(nodes[x], cb_arg);
}
void
KadNetwork::rand_routable(troutable_callback_func cb_func,
void *cb_arg)
{
CBigNum bn;
bn.Rand(conf->n_bits);
KadRoutable routable(bn, KAD_ROUTABLE_FILE);
if (NULL != cb_func)
cb_func(routable, cb_arg);
}
/**
* lookup a node by its id
*
* @param id
*
* @return
*/
KadNode *
KadNetwork::lookup_cheat(std::string id)
{
return nodes_map[id];
}
/**
* find node nearest to specified routable
*
* @param routable
*
* @return
*/
KadNode *
KadNetwork::find_nearest_cheat(KadRoutable routable)
{
KadNode *nearest = NULL;
for (u_int i = 0;i < nodes.size();i++)
{
if (NULL == nearest)
nearest = nodes[i];
else
{
CBigNum d1 = nodes[i]->distance_to(routable);
CBigNum d2 = nearest->distance_to(routable);
if (d1 < d2)
nearest = nodes[i];
}
}
return nearest;
}
void
KadNetwork::save(std::ostream& fout)
{
conf->save(fout);
for (u_int i = 0;i < conf->n_nodes;i++)
{
KadNode *node = nodes[i];
fout << "node " << i << " " << node->get_id().ToString(16) << "\n";
node->save(fout);
}
}
void
KadNetwork::graphviz(std::ostream& fout)
{
fout << "digraph G {\n";
fout << " node [shape=record];\n";
fout << " rankdir=TB;\n";
for (u_int i = 0;i < conf->n_nodes;i++)
{
KadNode *node = nodes[i];
fout << "node_" << node->get_id().ToString(16) << " [color=blue, label=\"" << node->get_id().ToString(16) << "\"];\n";
node->graphviz(fout);
}
fout << "}\n";
}