fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / examples / iostream_server / iostream_server.cpp
1 #include <websocketpp/config/core.hpp>
2
3 #include <websocketpp/server.hpp>
4
5 #include <iostream>
6 #include <fstream>
7
8 typedef websocketpp::server<websocketpp::config::core> server;
9
10 using websocketpp::lib::placeholders::_1;
11 using websocketpp::lib::placeholders::_2;
12 using websocketpp::lib::bind;
13
14 // pull out the type of messages sent by our config
15 typedef server::message_ptr message_ptr;
16
17 // Define a callback to handle incoming messages
18 void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
19     if (msg->get_opcode() == websocketpp::frame::opcode::text) {
20         s->get_alog().write(websocketpp::log::alevel::app,
21                     "Text Message Received: "+msg->get_payload());
22     } else {
23         s->get_alog().write(websocketpp::log::alevel::app,
24                     "Binary Message Received: "+websocketpp::utility::to_hex(msg->get_payload()));
25     }
26
27     try {
28         s->send(hdl, msg->get_payload(), msg->get_opcode());
29     } catch (const websocketpp::lib::error_code& e) {
30         s->get_alog().write(websocketpp::log::alevel::app,
31                     "Echo Failed: "+e.message());
32     }
33 }
34
35 int main() {
36     server s;
37     std::ofstream log;
38
39     try {
40         // set up access channels to only log interesting things
41         s.clear_access_channels(websocketpp::log::alevel::all);
42         s.set_access_channels(websocketpp::log::alevel::connect);
43         s.set_access_channels(websocketpp::log::alevel::disconnect);
44         s.set_access_channels(websocketpp::log::alevel::app);
45
46         // Log to a file rather than stdout, as we are using stdout for real
47         // output
48         log.open("output.log");
49         s.get_alog().set_ostream(&log);
50         s.get_elog().set_ostream(&log);
51
52         // print all output to stdout
53         s.register_ostream(&std::cout);
54
55         // Register our message handler
56         s.set_message_handler(bind(&on_message,&s,::_1,::_2));
57
58         server::connection_ptr con = s.get_connection();
59
60         con->start();
61
62         // C++ iostream's don't support the idea of asynchronous i/o. As such
63         // there are two input strategies demonstrated here. Buffered I/O will
64         // read from stdin in chunks until EOF. This works very well for
65         // replaying canned connections as would be done in automated testing.
66         //
67         // If the server is being used live however, assuming input is being
68         // piped from elsewhere in realtime, this strategy will result in small
69         // messages being buffered forever. The non-buffered strategy below
70         // reads characters from stdin one at a time. This is inefficient and
71         // for more serious uses should be replaced with a platform specific
72         // asyncronous i/o technique like select, poll, IOCP, etc
73         bool buffered_io = false;
74
75         if (buffered_io) {
76             std::cin >> *con;
77             con->eof();
78         } else {
79             char a;
80             while(std::cin.get(a)) {
81                 con->read_some(&a,1);
82             }
83             con->eof();
84         }
85     } catch (websocketpp::exception const & e) {
86         std::cout << e.what() << std::endl;
87     }
88     log.close();
89 }