Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
telemetry_server Class Reference

Public Types

typedef websocketpp::connection_hdl connection_hdl
 
typedef websocketpp::server< websocketpp::config::asioserver
 

Public Member Functions

 telemetry_server ()
 
void run (std::string docroot, uint16_t port)
 
void set_timer ()
 
void on_timer (websocketpp::lib::error_code const &ec)
 
void on_http (connection_hdl hdl)
 
void on_open (connection_hdl hdl)
 
void on_close (connection_hdl hdl)
 

Detailed Description

The telemetry server accepts connections and sends a message every second to each client containing an integer count. This example can be used as the basis for programs that expose a stream of telemetry data for logging, dashboards, etc.

This example uses the timer based concurrency method and is self contained and singled threaded. Refer to telemetry client for an example of a similar telemetry setup using threads rather than timers.

This example also includes an example simple HTTP server that serves a web dashboard displaying the count. This simple design is suitable for use delivering a small number of files to a small number of clients. It is ideal for cases like embedded dashboards that don't want the complexity of an extra HTTP server to serve static files.

This design will fall over under high traffic or DoS conditions. In such cases you are much better off proxying to a real HTTP server for the http requests.

Definition at line 31 of file telemetry_server.cpp.

Member Typedef Documentation

◆ connection_hdl

◆ server

Constructor & Destructor Documentation

◆ telemetry_server()

telemetry_server::telemetry_server ( )
inline

Definition at line 36 of file telemetry_server.cpp.

36 : m_count(0) {
37 // set up access channels to only log interesting things
41
42 // Initialize the Asio transport policy
43 m_endpoint.init_asio();
44
45 // Bind the handlers we are using
46 using websocketpp::lib::placeholders::_1;
47 using websocketpp::lib::bind;
48 m_endpoint.set_open_handler(bind(&telemetry_server::on_open,this,_1));
49 m_endpoint.set_close_handler(bind(&telemetry_server::on_close,this,_1));
50 m_endpoint.set_http_handler(bind(&telemetry_server::on_http,this,_1));
51 }
void on_open(connection_hdl hdl)
void on_close(connection_hdl hdl)
void on_http(connection_hdl hdl)
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition endpoint.hpp:231
void set_http_handler(http_handler h)
Definition endpoint.hpp:312
void set_open_handler(open_handler h)
Definition endpoint.hpp:277
void set_access_channels(log::level channels)
Set Access logging channel.
Definition endpoint.hpp:220
void set_close_handler(close_handler h)
Definition endpoint.hpp:282
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
static level const app
Special channel for application specific logs. Not used by the library.
Definition levels.hpp:143
static level const access_core
Definition levels.hpp:150
Here is the call graph for this function:

Member Function Documentation

◆ on_close()

void telemetry_server::on_close ( connection_hdl hdl)
inline

Definition at line 160 of file telemetry_server.cpp.

160 {
161 m_connections.erase(hdl);
162 }
Here is the caller graph for this function:

◆ on_http()

void telemetry_server::on_http ( connection_hdl hdl)
inline

Definition at line 109 of file telemetry_server.cpp.

109 {
110 // Upgrade our connection handle to a full connection_ptr
111 server::connection_ptr con = m_endpoint.get_con_from_hdl(hdl);
112
113 std::ifstream file;
114 std::string filename = con->get_resource();
115 std::string response;
116
117 m_endpoint.get_alog().write(websocketpp::log::alevel::app,
118 "http request1: "+filename);
119
120 if (filename == "/") {
121 filename = m_docroot+"index.html";
122 } else {
123 filename = m_docroot+filename.substr(1);
124 }
125
126 m_endpoint.get_alog().write(websocketpp::log::alevel::app,
127 "http request2: "+filename);
128
129 file.open(filename.c_str(), std::ios::in);
130 if (!file) {
131 // 404 error
132 std::stringstream ss;
133
134 ss << "<!doctype html><html><head>"
135 << "<title>Error 404 (Resource not found)</title><body>"
136 << "<h1>Error 404</h1>"
137 << "<p>The requested URL " << filename << " was not found on this server.</p>"
138 << "</body></head></html>";
139
140 con->set_body(ss.str());
142 return;
143 }
144
145 file.seekg(0, std::ios::end);
146 response.reserve(file.tellg());
147 file.seekg(0, std::ios::beg);
148
149 response.assign((std::istreambuf_iterator<char>(file)),
150 std::istreambuf_iterator<char>());
151
152 con->set_body(response);
153 con->set_status(websocketpp::http::status_code::ok);
154 }
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
Definition endpoint.hpp:643
alog_type & get_alog()
Get reference to access logger.
Definition endpoint.hpp:261
static const Segment ss(Segment::ss)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_open()

void telemetry_server::on_open ( connection_hdl hdl)
inline

Definition at line 156 of file telemetry_server.cpp.

156 {
157 m_connections.insert(hdl);
158 }
Here is the caller graph for this function:

◆ on_timer()

void telemetry_server::on_timer ( websocketpp::lib::error_code const & ec)
inline

Definition at line 88 of file telemetry_server.cpp.

88 {
89 if (ec) {
90 // there was an error, stop telemetry
91 m_endpoint.get_alog().write(websocketpp::log::alevel::app,
92 "Timer Error: "+ec.message());
93 return;
94 }
95
96 std::stringstream val;
97 val << "count is " << m_count++;
98
99 // Broadcast count to all connections
100 con_list::iterator it;
101 for (it = m_connections.begin(); it != m_connections.end(); ++it) {
102 m_endpoint.send(*it,val.str(),websocketpp::frame::opcode::text);
103 }
104
105 // set timer for next telemetry check
106 set_timer();
107 }
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)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

void telemetry_server::run ( std::string docroot,
uint16_t port )
inline

Definition at line 53 of file telemetry_server.cpp.

53 {
54 std::stringstream ss;
55 ss << "Running telemetry server on port "<< port <<" using docroot=" << docroot;
56 m_endpoint.get_alog().write(websocketpp::log::alevel::app,ss.str());
57
58 m_docroot = docroot;
59
60 // listen on specified port
61 m_endpoint.listen(port);
62
63 // Start the server accept loop
64 m_endpoint.start_accept();
65
66 // Set the initial timer to start telemetry
67 set_timer();
68
69 // Start the ASIO io_service run loop
70 try {
71 m_endpoint.run();
72 } catch (websocketpp::exception const & e) {
73 std::cout << e.what() << std::endl;
74 }
75 }
virtual char const * what() const
Definition error.hpp:263
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_timer()

void telemetry_server::set_timer ( )
inline

Definition at line 77 of file telemetry_server.cpp.

77 {
78 m_timer = m_endpoint.set_timer(
79 1000,
80 websocketpp::lib::bind(
82 this,
83 websocketpp::lib::placeholders::_1
84 )
85 );
86 }
void on_timer(websocketpp::lib::error_code const &ec)
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: