-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathexample3.cpp
159 lines (125 loc) · 3.96 KB
/
example3.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
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
#include <stdlib.h>
#include <iostream>
#include <boost/foreach.hpp>
// include libevent's header
#include <event.h>
// include libcage's header
#include <libcage/cage.hpp>
const int max_node = 100;
const int port = 10000;
libcage::cage *cage;
event *ev;
// callback function for get
// dht::value_t --- class {
// public:
// boost::shared_array<char> value;
// int len;
// };
// dht::value_set --- boost::unordered_set<dht::value_t>
// dht::value_set_ptr --- boost::shared_ptr<dht::value_set>
void
get_func(bool result, libcage::dht::value_set_ptr vset)
{
if (result) {
printf("succeeded in getting: n =");
libcage::dht::value_set::iterator it;
BOOST_FOREACH(const libcage::dht::value_t &val, *vset) {
printf(" %d", *(int*)val.value.get());
}
printf("\n");
} else {
printf("failed in getting:\n");
}
timeval tval;
tval.tv_sec = 1;
tval.tv_usec = 0;
evtimer_add(ev, &tval);
}
// callback for timer
void
timer_callback(int fd, short ev, void *arg)
{
int n1, n2;
n1 = abs(mrand48()) % max_node;
for (;;) {
n2 = abs(mrand48()) % max_node;
if (n2 != 0)
break;
}
// get at random
printf("get %d by node %d\n", n2, n1);
cage[n1].get(&n2, sizeof(n2), get_func);
}
class join_callback
{
public:
int n;
void operator() (bool result)
{
// print state
if (result)
std::cout << "join: succeeded, n = "
<< n
<< std::endl;
else
std::cout << "join: failed, n = "
<< n
<< std::endl;
//cage[n].print_state();
// put data
cage[n].put(&n, sizeof(n), &n, sizeof(n), 30000);
n++;
if (n < max_node) {
// start nodes recursively
if (! cage[n].open(PF_INET, port + n)) {
std::cerr << "cannot open port: Port = "
<< port + n
<< std::endl;
return;
}
cage[n].join("localhost", 10000, *this);
} else {
// start timer
timeval tval;
ev = new event;
tval.tv_sec = 1;
tval.tv_usec = 0;
evtimer_set(ev, timer_callback, NULL);
evtimer_add(ev, &tval);
}
}
};
int
main(int argc, char *argv[])
{
#ifdef WIN32
// initialize winsock
WSADATA wsaData;
WSAStartup(MAKEWORD(2,0), &wsaData);
#endif // WIN32
// initialize libevent
event_init();
cage = new libcage::cage[max_node];
// start bootstrap node
if (! cage[0].open(PF_INET, port)) {
std::cerr << "cannot open port: Port = "
<< port
<< std::endl;
return -1;
}
cage[0].set_global();
// start other nodes
join_callback func;
func.n = 1;
if (! cage[1].open(PF_INET, port + func.n)) {
std::cerr << "cannot open port: Port = "
<< port + func.n
<< std::endl;
return -1;
}
cage[1].set_global();
cage[1].join("localhost", 10000, func);
// handle event loop
event_dispatch();
return 0;
}