fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / examples / testee_client / testee_client.cpp
1 /*
2  * Copyright (c) 2015, 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
28 #include <websocketpp/config/asio_no_tls_client.hpp>
29 #include <websocketpp/client.hpp>
30 #include <websocketpp/extensions/permessage_deflate/enabled.hpp>
31
32 #include <iostream>
33
34 struct deflate_config : public websocketpp::config::asio_client {
35     typedef deflate_config type;
36     typedef asio_client base;
37     
38     typedef base::concurrency_type concurrency_type;
39     
40     typedef base::request_type request_type;
41     typedef base::response_type response_type;
42
43     typedef base::message_type message_type;
44     typedef base::con_msg_manager_type con_msg_manager_type;
45     typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
46     
47     typedef base::alog_type alog_type;
48     typedef base::elog_type elog_type;
49     
50     typedef base::rng_type rng_type;
51     
52     struct transport_config : public base::transport_config {
53         typedef type::concurrency_type concurrency_type;
54         typedef type::alog_type alog_type;
55         typedef type::elog_type elog_type;
56         typedef type::request_type request_type;
57         typedef type::response_type response_type;
58         typedef websocketpp::transport::asio::basic_socket::endpoint 
59             socket_type;
60     };
61
62     typedef websocketpp::transport::asio::endpoint<transport_config> 
63         transport_type;
64         
65     /// permessage_compress extension
66     struct permessage_deflate_config {};
67
68     typedef websocketpp::extensions::permessage_deflate::enabled
69         <permessage_deflate_config> permessage_deflate_type;
70 };
71
72 typedef websocketpp::client<deflate_config> client;
73
74 using websocketpp::lib::placeholders::_1;
75 using websocketpp::lib::placeholders::_2;
76 using websocketpp::lib::bind;
77
78 // pull out the type of messages sent by our config
79 typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
80
81 int case_count = 0;
82
83 void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
84     client::connection_ptr con = c->get_con_from_hdl(hdl);
85
86     if (con->get_resource() == "/getCaseCount") {
87         std::cout << "Detected " << msg->get_payload() << " test cases."
88                   << std::endl;
89         case_count = atoi(msg->get_payload().c_str());
90     } else {
91         c->send(hdl, msg->get_payload(), msg->get_opcode());
92     }
93 }
94
95 int main(int argc, char* argv[]) {
96     // Create a server endpoint
97     client c;
98
99     std::string uri = "ws://localhost:9001";
100
101     if (argc == 2) {
102         uri = argv[1];
103     }
104
105     try {
106         // We expect there to be a lot of errors, so suppress them
107         c.clear_access_channels(websocketpp::log::alevel::all);
108         c.clear_error_channels(websocketpp::log::elevel::all);
109
110         // Initialize ASIO
111         c.init_asio();
112
113         // Register our handlers
114         c.set_message_handler(bind(&on_message,&c,::_1,::_2));
115
116         websocketpp::lib::error_code ec;
117         client::connection_ptr con = c.get_connection(uri+"/getCaseCount", ec);
118         c.connect(con);
119
120         // Start the ASIO io_service run loop
121         c.run();
122
123         std::cout << "case count: " << case_count << std::endl;
124
125         for (int i = 1; i <= case_count; i++) {
126             c.reset();
127
128             std::stringstream url;
129
130             url << uri << "/runCase?case=" << i << "&agent="
131                 << websocketpp::user_agent;
132
133             con = c.get_connection(url.str(), ec);
134
135             c.connect(con);
136
137             c.run();
138         }
139
140         std::cout << "done" << std::endl;
141
142     } catch (websocketpp::exception const & e) {
143         std::cout << e.what() << std::endl;
144     }
145 }