fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / examples / debug_server / debug_server.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/debug_asio_no_tls.hpp>
33
34 // Custom logger
35 #include <websocketpp/logger/syslog.hpp>
36
37 #include <websocketpp/server.hpp>
38
39 #include <iostream>
40
41 ////////////////////////////////////////////////////////////////////////////////
42 ///////////////// Custom Config for debugging custom policies //////////////////
43 ////////////////////////////////////////////////////////////////////////////////
44
45 struct debug_custom : public websocketpp::config::debug_asio {
46     typedef debug_custom type;
47     typedef debug_asio base;
48
49     typedef base::concurrency_type concurrency_type;
50
51     typedef base::request_type request_type;
52     typedef base::response_type response_type;
53
54     typedef base::message_type message_type;
55     typedef base::con_msg_manager_type con_msg_manager_type;
56     typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
57
58     /// Custom Logging policies
59     /*typedef websocketpp::log::syslog<concurrency_type,
60         websocketpp::log::elevel> elog_type;
61     typedef websocketpp::log::syslog<concurrency_type,
62         websocketpp::log::alevel> alog_type;
63     */
64     typedef base::alog_type alog_type;
65     typedef base::elog_type elog_type;
66
67     typedef base::rng_type rng_type;
68
69     struct transport_config : public base::transport_config {
70         typedef type::concurrency_type concurrency_type;
71         typedef type::alog_type alog_type;
72         typedef type::elog_type elog_type;
73         typedef type::request_type request_type;
74         typedef type::response_type response_type;
75         typedef websocketpp::transport::asio::basic_socket::endpoint
76             socket_type;
77     };
78
79     typedef websocketpp::transport::asio::endpoint<transport_config>
80         transport_type;
81     
82     static const long timeout_open_handshake = 0;
83 };
84
85 ////////////////////////////////////////////////////////////////////////////////
86
87 typedef websocketpp::server<debug_custom> server;
88
89 using websocketpp::lib::placeholders::_1;
90 using websocketpp::lib::placeholders::_2;
91 using websocketpp::lib::bind;
92
93 // pull out the type of messages sent by our config
94 typedef server::message_ptr message_ptr;
95
96 bool validate(server *, websocketpp::connection_hdl) {
97     //sleep(6);
98     return true;
99 }
100
101 void on_http(server* s, websocketpp::connection_hdl hdl) {
102     server::connection_ptr con = s->get_con_from_hdl(hdl);
103
104     std::string res = con->get_request_body();
105
106     std::stringstream ss;
107     ss << "got HTTP request with " << res.size() << " bytes of body data.";
108
109     con->set_body(ss.str());
110     con->set_status(websocketpp::http::status_code::ok);
111 }
112
113 void on_fail(server* s, websocketpp::connection_hdl hdl) {
114     server::connection_ptr con = s->get_con_from_hdl(hdl);
115     
116     std::cout << "Fail handler: " << con->get_ec() << " " << con->get_ec().message()  << std::endl;
117 }
118
119 void on_close(websocketpp::connection_hdl) {
120     std::cout << "Close handler" << std::endl;
121 }
122
123 // Define a callback to handle incoming messages
124 void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
125     std::cout << "on_message called with hdl: " << hdl.lock().get()
126               << " and message: " << msg->get_payload()
127               << std::endl;
128
129     try {
130         s->send(hdl, msg->get_payload(), msg->get_opcode());
131     } catch (const websocketpp::lib::error_code& e) {
132         std::cout << "Echo failed because: " << e
133                   << "(" << e.message() << ")" << std::endl;
134     }
135 }
136
137 int main() {
138     // Create a server endpoint
139     server echo_server;
140
141     try {
142         // Set logging settings
143         echo_server.set_access_channels(websocketpp::log::alevel::all);
144         echo_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
145
146         // Initialize ASIO
147         echo_server.init_asio();
148         echo_server.set_reuse_addr(true);
149
150         // Register our message handler
151         echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
152
153         echo_server.set_http_handler(bind(&on_http,&echo_server,::_1));
154         echo_server.set_fail_handler(bind(&on_fail,&echo_server,::_1));
155         echo_server.set_close_handler(&on_close);
156
157         echo_server.set_validate_handler(bind(&validate,&echo_server,::_1));
158
159         // Listen on port 9012
160         echo_server.listen(9012);
161
162         // Start the server accept loop
163         echo_server.start_accept();
164
165         // Start the ASIO io_service run loop
166         echo_server.run();
167     } catch (const std::exception & e) {
168         std::cout << e.what() << std::endl;
169     } catch (websocketpp::lib::error_code e) {
170         std::cout << e.message() << std::endl;
171     } catch (...) {
172         std::cout << "other exception" << std::endl;
173     }
174 }