fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / examples / debug_client / debug_client.cpp
1 /*
2  * Copyright (c) 2014, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of the WebSocket++ Project nor the
12  *       names of its contributors may be used to endorse or promote products
13  *       derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /** ====== WARNING ========
28  * This example is presently used as a scratch space. It may or may not be broken
29  * at any given time.
30  */
31
32 #include <websocketpp/config/asio_client.hpp>
33
34 #include <websocketpp/client.hpp>
35
36 #include <iostream>
37 #include <chrono>
38
39 typedef websocketpp::client<websocketpp::config::asio_client> client;
40
41 using websocketpp::lib::placeholders::_1;
42 using websocketpp::lib::placeholders::_2;
43 using websocketpp::lib::bind;
44
45 // pull out the type of messages sent by our config
46 typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
47 typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
48 typedef client::connection_ptr connection_ptr;
49
50
51
52 class perftest {
53 public:
54     typedef perftest type;
55     typedef std::chrono::duration<int,std::micro> dur_type;
56
57     perftest () {
58         m_endpoint.set_access_channels(websocketpp::log::alevel::all);
59         m_endpoint.set_error_channels(websocketpp::log::elevel::all);
60
61         // Initialize ASIO
62         m_endpoint.init_asio();
63
64         // Register our handlers
65         m_endpoint.set_socket_init_handler(bind(&type::on_socket_init,this,::_1));
66         //m_endpoint.set_tls_init_handler(bind(&type::on_tls_init,this,::_1));
67         m_endpoint.set_message_handler(bind(&type::on_message,this,::_1,::_2));
68         m_endpoint.set_open_handler(bind(&type::on_open,this,::_1));
69         m_endpoint.set_close_handler(bind(&type::on_close,this,::_1));
70         m_endpoint.set_fail_handler(bind(&type::on_fail,this,::_1));
71     }
72
73     void start(std::string uri) {
74         websocketpp::lib::error_code ec;
75         client::connection_ptr con = m_endpoint.get_connection(uri, ec);
76
77         if (ec) {
78             m_endpoint.get_alog().write(websocketpp::log::alevel::app,ec.message());
79             return;
80         }
81
82         //con->set_proxy("http://humupdates.uchicago.edu:8443");
83
84         m_endpoint.connect(con);
85
86         // Start the ASIO io_service run loop
87         m_start = std::chrono::high_resolution_clock::now();
88         m_endpoint.run();
89     }
90
91     void on_socket_init(websocketpp::connection_hdl) {
92         m_socket_init = std::chrono::high_resolution_clock::now();
93     }
94
95     context_ptr on_tls_init(websocketpp::connection_hdl) {
96         m_tls_init = std::chrono::high_resolution_clock::now();
97         context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv1);
98
99         try {
100             ctx->set_options(boost::asio::ssl::context::default_workarounds |
101                              boost::asio::ssl::context::no_sslv2 |
102                              boost::asio::ssl::context::no_sslv3 |
103                              boost::asio::ssl::context::single_dh_use);
104         } catch (std::exception& e) {
105             std::cout << e.what() << std::endl;
106         }
107         return ctx;
108     }
109
110     void on_fail(websocketpp::connection_hdl hdl) {
111         client::connection_ptr con = m_endpoint.get_con_from_hdl(hdl);
112         
113         std::cout << "Fail handler" << std::endl;
114         std::cout << con->get_state() << std::endl;
115         std::cout << con->get_local_close_code() << std::endl;
116         std::cout << con->get_local_close_reason() << std::endl;
117         std::cout << con->get_remote_close_code() << std::endl;
118         std::cout << con->get_remote_close_reason() << std::endl;
119         std::cout << con->get_ec() << " - " << con->get_ec().message() << std::endl;
120     }
121
122     void on_open(websocketpp::connection_hdl hdl) {
123         m_open = std::chrono::high_resolution_clock::now();
124         m_endpoint.send(hdl, "", websocketpp::frame::opcode::text);
125     }
126     void on_message(websocketpp::connection_hdl hdl, message_ptr) {
127         m_message = std::chrono::high_resolution_clock::now();
128         m_endpoint.close(hdl,websocketpp::close::status::going_away,"");
129     }
130     void on_close(websocketpp::connection_hdl) {
131         m_close = std::chrono::high_resolution_clock::now();
132
133         std::cout << "Socket Init: " << std::chrono::duration_cast<dur_type>(m_socket_init-m_start).count() << std::endl;
134         std::cout << "TLS Init: " << std::chrono::duration_cast<dur_type>(m_tls_init-m_start).count() << std::endl;
135         std::cout << "Open: " << std::chrono::duration_cast<dur_type>(m_open-m_start).count() << std::endl;
136         std::cout << "Message: " << std::chrono::duration_cast<dur_type>(m_message-m_start).count() << std::endl;
137         std::cout << "Close: " << std::chrono::duration_cast<dur_type>(m_close-m_start).count() << std::endl;
138     }
139 private:
140     client m_endpoint;
141
142     std::chrono::high_resolution_clock::time_point m_start;
143     std::chrono::high_resolution_clock::time_point m_socket_init;
144     std::chrono::high_resolution_clock::time_point m_tls_init;
145     std::chrono::high_resolution_clock::time_point m_open;
146     std::chrono::high_resolution_clock::time_point m_message;
147     std::chrono::high_resolution_clock::time_point m_close;
148 };
149
150 int main(int argc, char* argv[]) {
151     std::string uri = "wss://echo.websocket.org";
152
153     if (argc == 2) {
154         uri = argv[1];
155     }
156
157     try {
158         perftest endpoint;
159         endpoint.start(uri);
160     } catch (const std::exception & e) {
161         std::cout << e.what() << std::endl;
162     } catch (websocketpp::lib::error_code e) {
163         std::cout << e.message() << std::endl;
164     } catch (...) {
165         std::cout << "other exception" << std::endl;
166     }
167 }