NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
echo_server.cpp
Go to the documentation of this file.
2 
3 #include <websocketpp/server.hpp>
4 
5 #include <iostream>
6 
8 
9 using websocketpp::lib::placeholders::_1;
10 using websocketpp::lib::placeholders::_2;
11 using websocketpp::lib::bind;
12 
13 // pull out the type of messages sent by our config
15 
16 // Define a callback to handle incoming messages
18  std::cout << "on_message called with hdl: " << hdl.lock().get()
19  << " and message: " << msg->get_payload()
20  << std::endl;
21 
22  // check for a special command to instruct the server to stop listening so
23  // it can be cleanly exited.
24  if (msg->get_payload() == "stop-listening") {
25  s->stop_listening();
26  return;
27  }
28 
29  try {
30  s->send(hdl, msg->get_payload(), msg->get_opcode());
31  } catch (websocketpp::exception const & e) {
32  std::cout << "Echo failed because: "
33  << "(" << e.what() << ")" << std::endl;
34  }
35 }
36 
37 int main() {
38  // Create a server endpoint
39  server echo_server;
40 
41  try {
42  // Set logging settings
45 
46  // Initialize Asio
47  echo_server.init_asio();
48 
49  // Register our message handler
50  echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
51 
52  // Listen on port 9002
53  echo_server.listen(9002);
54 
55  // Start the server accept loop
56  echo_server.start_accept();
57 
58  // Start the ASIO io_service run loop
59  echo_server.run();
60  } catch (websocketpp::exception const & e) {
61  std::cout << e.what() << std::endl;
62  } catch (...) {
63  std::cout << "other exception" << std::endl;
64  }
65 }
static level const all
Special aggregate value representing "all levels".
Definition: levels.hpp:152
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
Definition: echo_server.cpp:17
int main()
Definition: echo_server.cpp:37
server::message_ptr message_ptr
Definition: echo_server.cpp:14
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
Definition: endpoint.hpp:70
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
Definition: levels.hpp:129
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
Definition: endpoint.hpp:185
void start_accept(lib::error_code &ec)
Starts the server&#39;s async connection acceptance loop (exception free)
void set_access_channels(log::level channels)
Set Access logging channel.
Definition: endpoint.hpp:220
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
Definition: endpoint.hpp:605
websocketpp::server< websocketpp::config::asio > server
Definition: echo_server.cpp:7
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition: endpoint.hpp:231
std::size_t run()
wraps the run method of the internal io_service object
Definition: endpoint.hpp:641
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
Definition: endpoint.hpp:413
void set_message_handler(message_handler h)
Definition: endpoint.hpp:322
virtual char const * what() const
Definition: error.hpp:263
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)