-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathexample1.cpp
78 lines (57 loc) · 1.63 KB
/
example1.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
#include <stdlib.h>
#include <iostream>
// include libevent's header
#include <event.h>
// include libcage's header
#include <libcage/cage.hpp>
libcage::cage *cage;
void
join_callback(bool result)
{
if (result)
std::cout << "join: succeeded" << std::endl;
else
std::cout << "join: failed" << std::endl;
cage->print_state();
}
int
main(int argc, char *argv[])
{
if (argc < 2) {
std::cerr << "usage: ./sample1 port [host port]\n\n"
<< "example:\n"
<< " $ ./sample1 10000 &\n"
<< " $ ./sample1 10001 localhost 10000\n"
<< std::endl;
return -1;
}
// initialize
#ifdef WIN32
// initialize winsock
WSADATA wsaData;
WSAStartup(MAKEWORD(2,0), &wsaData);
#endif // WIN32
// initialize libevent
event_init();
// create cage instance after initialize
cage = new libcage::cage;
int port;
port = atoi(argv[1]);
// open UDP
if (! cage->open(PF_INET, port)) {
std::cerr << "cannot open port: Port = "
<< port
<< std::endl;
return -1;
}
// set as global node
cage->set_global();
if (argc >= 4) {
// join to the network
int dst_port = atoi(argv[3]);
cage->join(argv[2], dst_port, &join_callback);
}
// handle event loop
event_dispatch();
return 0;
}