NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
iostream_server.cpp
Go to the documentation of this file.
2 
3 #include <websocketpp/server.hpp>
4 
5 #include <iostream>
6 #include <fstream>
7 
9 
10 using websocketpp::lib::placeholders::_1;
11 using websocketpp::lib::placeholders::_2;
12 using websocketpp::lib::bind;
13 
14 // pull out the type of messages sent by our config
16 
17 // Define a callback to handle incoming messages
19  if (msg->get_opcode() == websocketpp::frame::opcode::text) {
21  "Text Message Received: "+msg->get_payload());
22  } else {
24  "Binary Message Received: "+websocketpp::utility::to_hex(msg->get_payload()));
25  }
26 
27  try {
28  s->send(hdl, msg->get_payload(), msg->get_opcode());
29  } catch (websocketpp::exception const & e) {
31  std::string("Echo Failed: ")+e.what());
32  }
33 }
34 
35 int main() {
36  server s;
37  std::ofstream log;
38 
39  try {
40  // set up access channels to only log interesting things
45 
46  // Log to a file rather than stdout, as we are using stdout for real
47  // output
48  log.open("output.log");
49  s.get_alog().set_ostream(&log);
50  s.get_elog().set_ostream(&log);
51 
52  // print all output to stdout
53  s.register_ostream(&std::cout);
54 
55  // Register our message handler
56  s.set_message_handler(bind(&on_message,&s,::_1,::_2));
57 
59 
60  con->start();
61 
62  // C++ iostream's don't support the idea of asynchronous i/o. As such
63  // there are two input strategies demonstrated here. Buffered I/O will
64  // read from stdin in chunks until EOF. This works very well for
65  // replaying canned connections as would be done in automated testing.
66  //
67  // If the server is being used live however, assuming input is being
68  // piped from elsewhere in realtime, this strategy will result in small
69  // messages being buffered forever. The non-buffered strategy below
70  // reads characters from stdin one at a time. This is inefficient and
71  // for more serious uses should be replaced with a platform specific
72  // asyncronous i/o technique like select, poll, IOCP, etc
73  bool buffered_io = false;
74 
75  if (buffered_io) {
76  std::cin >> *con;
77  con->eof();
78  } else {
79  char a;
80  while(std::cin.get(a)) {
81  con->read_some(&a,1);
82  }
83  con->eof();
84  }
85  } catch (websocketpp::exception const & e) {
86  std::cout << e.what() << std::endl;
87  }
88  log.close();
89 }
static level const all
Special aggregate value representing "all levels".
Definition: levels.hpp:152
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
websocketpp::server< websocketpp::config::core > server
int main()
static level const app
Special channel for application specific logs. Not used by the library.
Definition: levels.hpp:143
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.
alog_type & get_alog()
Get reference to access logger.
Definition: endpoint.hpp:261
std::string to_hex(std::string const &input)
Convert std::string to ascii printed string of hex digits.
static level const disconnect
One line for each closed connection. Includes closing codes and reasons.
Definition: levels.hpp:123
server::message_ptr message_ptr
Server endpoint role based on the given config.
void set_access_channels(log::level channels)
Set Access logging channel.
Definition: endpoint.hpp:220
elog_type & get_elog()
Get reference to error logger.
Definition: endpoint.hpp:269
connection_ptr get_connection()
Create and initialize a new connection.
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition: endpoint.hpp:231
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
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)
static level const connect
Information about new connections.
Definition: levels.hpp:121