NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
simple_broadcast_server.cpp
Go to the documentation of this file.
1 #include <set>
3 #include <websocketpp/server.hpp>
4 
6 
8 using websocketpp::lib::placeholders::_1;
9 using websocketpp::lib::placeholders::_2;
10 using websocketpp::lib::bind;
11 
12 class broadcast_server {
13 public:
15  m_server.init_asio();
16 
17  m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
18  m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
19  m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
20  }
21 
22  void on_open(connection_hdl hdl) {
23  m_connections.insert(hdl);
24  }
25 
27  m_connections.erase(hdl);
28  }
29 
31  for (auto it : m_connections) {
32  m_server.send(it,msg);
33  }
34  }
35 
36  void run(uint16_t port) {
37  m_server.listen(port);
38  m_server.start_accept();
39  m_server.run();
40  }
41 private:
42  typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
43 
44  server m_server;
45  con_list m_connections;
46 };
47 
48 int main() {
50  server.run(9002);
51 }
void on_close(connection_hdl hdl)
websocketpp::server< websocketpp::config::asio > server
void set_open_handler(open_handler h)
Definition: endpoint.hpp:277
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
Definition: endpoint.hpp:70
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
void on_message(connection_hdl hdl, server::message_ptr msg)
void on_open(connection_hdl hdl)
void set_close_handler(close_handler h)
Definition: endpoint.hpp:282
void start_accept(lib::error_code &ec)
Starts the server&#39;s async connection acceptance loop (exception free)
void run(uint16_t port)
void set_message_handler(message_handler h)
Definition: endpoint.hpp:322
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)