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

Public Types

typedef websocketpp::client< websocketpp::config::asio_clientclient
 
typedef websocketpp::lib::lock_guard< websocketpp::lib::mutex > scoped_lock
 

Public Member Functions

 telemetry_client ()
 
void run (const std::string &uri)
 
void on_open (websocketpp::connection_hdl)
 
void on_close (websocketpp::connection_hdl)
 
void on_fail (websocketpp::connection_hdl)
 
void telemetry_loop ()
 

Detailed Description

The telemetry client connects to a WebSocket server and sends a message every second containing an integer count. This example can be used as the basis for programs where a client connects and pushes data for logging, stress/load testing, etc.

Definition at line 15 of file telemetry_client.cpp.

Member Typedef Documentation

◆ client

◆ scoped_lock

websocketpp::lib::lock_guard<websocketpp::lib::mutex> telemetry_client::scoped_lock

Definition at line 18 of file telemetry_client.cpp.

Constructor & Destructor Documentation

◆ telemetry_client()

telemetry_client::telemetry_client ( )
inline

Definition at line 20 of file telemetry_client.cpp.

20 : m_open(false),m_done(false) {
21 // set up access channels to only log interesting things
26
27 // Initialize the Asio transport policy
28 m_client.init_asio();
29
30 // Bind the handlers we are using
31 using websocketpp::lib::placeholders::_1;
32 using websocketpp::lib::bind;
33 m_client.set_open_handler(bind(&telemetry_client::on_open,this,_1));
34 m_client.set_close_handler(bind(&telemetry_client::on_close,this,_1));
35 m_client.set_fail_handler(bind(&telemetry_client::on_fail,this,_1));
36 }
void on_open(websocketpp::connection_hdl)
void on_close(websocketpp::connection_hdl)
void on_fail(websocketpp::connection_hdl)
void set_fail_handler(fail_handler h)
Definition endpoint.hpp:287
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition endpoint.hpp:231
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 connect
Information about new connections.
Definition levels.hpp:121
static level const app
Special channel for application specific logs. Not used by the library.
Definition levels.hpp:143
static level const disconnect
One line for each closed connection. Includes closing codes and reasons.
Definition levels.hpp:123
Here is the call graph for this function:

Member Function Documentation

◆ on_close()

void telemetry_client::on_close ( websocketpp::connection_hdl )
inline

Definition at line 77 of file telemetry_client.cpp.

77 {
79 "Connection closed, stopping telemetry!");
80
81 scoped_lock guard(m_lock);
82 m_done = true;
83 }
websocketpp::lib::lock_guard< websocketpp::lib::mutex > scoped_lock
alog_type & get_alog()
Get reference to access logger.
Definition endpoint.hpp:261
Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_fail()

void telemetry_client::on_fail ( websocketpp::connection_hdl )
inline

Definition at line 86 of file telemetry_client.cpp.

86 {
88 "Connection failed, stopping telemetry!");
89
90 scoped_lock guard(m_lock);
91 m_done = true;
92 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_open()

void telemetry_client::on_open ( websocketpp::connection_hdl )
inline

Definition at line 68 of file telemetry_client.cpp.

68 {
70 "Connection opened, starting telemetry!");
71
72 scoped_lock guard(m_lock);
73 m_open = true;
74 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

void telemetry_client::run ( const std::string & uri)
inline

Definition at line 39 of file telemetry_client.cpp.

39 {
40 // Create a new connection to the given URI
41 websocketpp::lib::error_code ec;
42 client::connection_ptr con = m_client.get_connection(uri, ec);
43 if (ec) {
45 "Get Connection Error: "+ec.message());
46 return;
47 }
48
49 // Grab a handle for this connection so we can talk to it in a thread
50 // safe manor after the event loop starts.
51 m_hdl = con->get_handle();
52
53 // Queue the connection. No DNS queries or network connections will be
54 // made until the io_service event loop is run.
55 m_client.connect(con);
56
57 // Create a thread to run the ASIO io_service event loop
58 websocketpp::lib::thread asio_thread(&client::run, &m_client);
59
60 // Create a thread to run the telemetry loop
61 websocketpp::lib::thread telemetry_thread(&telemetry_client::telemetry_loop,this);
62
63 asio_thread.join();
64 telemetry_thread.join();
65 }
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ telemetry_loop()

void telemetry_client::telemetry_loop ( )
inline

Definition at line 94 of file telemetry_client.cpp.

94 {
95 uint64_t count = 0;
96 std::stringstream val;
97 websocketpp::lib::error_code ec;
98
99 while(1) {
100 bool wait = false;
101
102 {
103 scoped_lock guard(m_lock);
104 // If the connection has been closed, stop generating telemetry
105 if (m_done) {break;}
106
107 // If the connection hasn't been opened yet wait a bit and retry
108 if (!m_open) {
109 wait = true;
110 }
111 }
112
113 if (wait) {
114 sleep(1);
115 continue;
116 }
117
118 val.str("");
119 val << "count is " << count++;
120
121 m_client.get_alog().write(websocketpp::log::alevel::app, val.str());
122 m_client.send(m_hdl,val.str(),websocketpp::frame::opcode::text,ec);
123
124 // The most likely error that we will get is that the connection is
125 // not in the right state. Usually this means we tried to send a
126 // message to a connection that was closed or in the process of
127 // closing. While many errors here can be easily recovered from,
128 // in this simple example, we'll stop the telemetry loop.
129 if (ec) {
131 "Send Error: "+ec.message());
132 break;
133 }
134
135 sleep(1);
136 }
137 }
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)
int * count
unsigned __int64 uint64_t
Definition stdint.h:136
void wait()
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: