NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
simple_count_server_thread.cpp
Go to the documentation of this file.
1
#include <functional>
2
#include <mutex>
3
#include <set>
4
#include <thread>
5
6
#include <
websocketpp/config/asio_no_tls.hpp
>
7
#include <
websocketpp/server.hpp
>
8
9
typedef
websocketpp::server<websocketpp::config::asio>
server
;
10
11
using
websocketpp::connection_hdl
;
12
13
class
count_server
{
14
public
:
15
count_server
() : m_count(0) {
16
m_server.init_asio();
17
18
m_server.
set_open_handler
(bind(&
count_server::on_open
,
this
,_1));
19
m_server.
set_close_handler
(bind(&
count_server::on_close
,
this
,_1));
20
}
21
22
void
on_open
(
connection_hdl
hdl) {
23
std::lock_guard<std::mutex> lock(m_mutex);
24
m_connections.insert(hdl);
25
}
26
27
void
on_close
(
connection_hdl
hdl) {
28
std::lock_guard<std::mutex> lock(m_mutex);
29
m_connections.erase(hdl);
30
}
31
32
void
count
() {
33
while
(1) {
34
sleep(1);
35
m_count++;
36
37
std::stringstream ss;
38
ss << m_count;
39
40
std::lock_guard<std::mutex> lock(m_mutex);
41
for
(
auto
it : m_connections) {
42
m_server.
send
(it,ss.str(),
websocketpp::frame::opcode::text
);
43
}
44
}
45
}
46
47
void
run
(uint16_t port) {
48
m_server.listen(port);
49
m_server.
start_accept
();
50
m_server.run();
51
}
52
private
:
53
typedef
std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
54
55
int
m_count;
56
server
m_server;
57
con_list m_connections;
58
std::mutex m_mutex;
59
};
60
61
int
main
() {
62
count_server
server
;
63
std::thread t(std::bind(&
count_server::count
,&server));
64
server.
run
(9002);
65
}
count_server::count
void count()
Definition:
simple_count_server_thread.cpp:32
websocketpp::endpoint::set_open_handler
void set_open_handler(open_handler h)
Definition:
endpoint.hpp:277
main
int main()
Definition:
simple_count_server_thread.cpp:61
server
websocketpp::server< websocketpp::config::asio > server
Definition:
simple_count_server_thread.cpp:9
count_server::run
void run(uint16_t port)
Definition:
simple_count_server_thread.cpp:47
websocketpp::connection_hdl
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
Definition:
connection_hdl.hpp:48
count_server::count_server
count_server()
Definition:
simple_count_server_thread.cpp:15
count_server
Definition:
simple_count_server_thread.cpp:13
websocketpp::endpoint::set_close_handler
void set_close_handler(close_handler h)
Definition:
endpoint.hpp:282
count_server::on_close
void on_close(connection_hdl hdl)
Definition:
simple_count_server_thread.cpp:27
websocketpp::server::start_accept
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
Definition:
server_endpoint.hpp:121
websocketpp::server< websocketpp::config::asio >
count_server::on_open
void on_open(connection_hdl hdl)
Definition:
simple_count_server_thread.cpp:22
websocketpp::frame::opcode::text
Definition:
frame.hpp:79
asio_no_tls.hpp
websocketpp::endpoint::send
void send(connection_hdl hdl, std::string const &payload, frame::opcode::value op, lib::error_code &ec)
Create a message and add it to the outgoing send queue (exception free)
Definition:
endpoint_impl.hpp:162
server.hpp
ndnSIM
NFD
websocketpp
docs
simple_count_server_thread.cpp
Generated on Fri May 6 2022 12:34:14 for ndnSIM by
1.8.13