-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathexample2.cpp
120 lines (91 loc) · 2.72 KB
/
example2.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
#include <stdlib.h>
#include <iostream>
// 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;
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();
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].set_global();
cage[n].join("localhost", 10000, *this);
}
}
};
void
timer_callback(int fd, short ev, void *arg)
{
timeval tval;
tval.tv_sec = 60;
tval.tv_usec = 0;
evtimer_add((event*)arg, &tval);
for (int i = 0; i < max_node; i++) {
cage[i].print_state();
}
}
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);
// start timer
timeval tval;
event *ev = new event;
tval.tv_sec = 60;
tval.tv_usec = 0;
evtimer_set(ev, timer_callback, ev);
evtimer_add(ev, &tval);
// handle event loop
event_dispatch();
return 0;
}