WebSocket++  0.8.3-dev
C++ websocket client/server library
step5.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 // **NOTE:** This file is a snapshot of the WebSocket++ utility client tutorial.
28 // Additional related material can be found in the tutorials/utility_client
29 // directory of the WebSocket++ repository.
30 
31 #include <websocketpp/config/asio_no_tls_client.hpp>
32 #include <websocketpp/client.hpp>
33 
34 #include <websocketpp/common/thread.hpp>
35 #include <websocketpp/common/memory.hpp>
36 
37 #include <cstdlib>
38 #include <iostream>
39 #include <map>
40 #include <string>
41 #include <sstream>
42 
43 typedef websocketpp::client<websocketpp::config::asio_client> client;
44 
45 class connection_metadata {
46 public:
47  typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
48 
49  connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri)
50  : m_id(id)
51  , m_hdl(hdl)
52  , m_status("Connecting")
53  , m_uri(uri)
54  , m_server("N/A")
55  {}
56 
57  void on_open(client * c, websocketpp::connection_hdl hdl) {
58  m_status = "Open";
59 
60  client::connection_ptr con = c->get_con_from_hdl(hdl);
61  m_server = con->get_response_header("Server");
62  }
63 
64  void on_fail(client * c, websocketpp::connection_hdl hdl) {
65  m_status = "Failed";
66 
67  client::connection_ptr con = c->get_con_from_hdl(hdl);
68  m_server = con->get_response_header("Server");
69  m_error_reason = con->get_ec().message();
70  }
71 
72  void on_close(client * c, websocketpp::connection_hdl hdl) {
73  m_status = "Closed";
74  client::connection_ptr con = c->get_con_from_hdl(hdl);
75  std::stringstream s;
76  s << "close code: " << con->get_remote_close_code() << " ("
77  << websocketpp::close::status::get_string(con->get_remote_close_code())
78  << "), close reason: " << con->get_remote_close_reason();
79  m_error_reason = s.str();
80  }
81 
82  websocketpp::connection_hdl get_hdl() const {
83  return m_hdl;
84  }
85 
86  int get_id() const {
87  return m_id;
88  }
89 
90  std::string get_status() const {
91  return m_status;
92  }
93 
94  friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
95 private:
96  int m_id;
97  websocketpp::connection_hdl m_hdl;
98  std::string m_status;
99  std::string m_uri;
100  std::string m_server;
101  std::string m_error_reason;
102 };
103 
104 std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
105  out << "> URI: " << data.m_uri << "\n"
106  << "> Status: " << data.m_status << "\n"
107  << "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
108  << "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason);
109 
110  return out;
111 }
112 
113 class websocket_endpoint {
114 public:
115  websocket_endpoint () : m_next_id(0) {
116  m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
117  m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
118 
119  m_endpoint.init_asio();
120  m_endpoint.start_perpetual();
121 
122  m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&client::run, &m_endpoint);
123  }
124 
125  ~websocket_endpoint() {
126  m_endpoint.stop_perpetual();
127 
128  for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
129  if (it->second->get_status() != "Open") {
130  // Only close open connections
131  continue;
132  }
133 
134  std::cout << "> Closing connection " << it->second->get_id() << std::endl;
135 
136  websocketpp::lib::error_code ec;
137  m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec);
138  if (ec) {
139  std::cout << "> Error closing connection " << it->second->get_id() << ": "
140  << ec.message() << std::endl;
141  }
142  }
143 
144  m_thread->join();
145  }
146 
147  int connect(std::string const & uri) {
148  websocketpp::lib::error_code ec;
149 
150  client::connection_ptr con = m_endpoint.get_connection(uri, ec);
151 
152  if (ec) {
153  std::cout << "> Connect initialization error: " << ec.message() << std::endl;
154  return -1;
155  }
156 
157  int new_id = m_next_id++;
158  connection_metadata::ptr metadata_ptr = websocketpp::lib::make_shared<connection_metadata>(new_id, con->get_handle(), uri);
159  m_connection_list[new_id] = metadata_ptr;
160 
161  con->set_open_handler(websocketpp::lib::bind(
162  &connection_metadata::on_open,
163  metadata_ptr,
164  &m_endpoint,
165  websocketpp::lib::placeholders::_1
166  ));
167  con->set_fail_handler(websocketpp::lib::bind(
168  &connection_metadata::on_fail,
169  metadata_ptr,
170  &m_endpoint,
171  websocketpp::lib::placeholders::_1
172  ));
173  con->set_close_handler(websocketpp::lib::bind(
174  &connection_metadata::on_close,
175  metadata_ptr,
176  &m_endpoint,
177  websocketpp::lib::placeholders::_1
178  ));
179 
180  m_endpoint.connect(con);
181 
182  return new_id;
183  }
184 
185  void close(int id, websocketpp::close::status::value code, std::string reason) {
186  websocketpp::lib::error_code ec;
187 
188  con_list::iterator metadata_it = m_connection_list.find(id);
189  if (metadata_it == m_connection_list.end()) {
190  std::cout << "> No connection found with id " << id << std::endl;
191  return;
192  }
193 
194  m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec);
195  if (ec) {
196  std::cout << "> Error initiating close: " << ec.message() << std::endl;
197  }
198  }
199 
200  connection_metadata::ptr get_metadata(int id) const {
201  con_list::const_iterator metadata_it = m_connection_list.find(id);
202  if (metadata_it == m_connection_list.end()) {
203  return connection_metadata::ptr();
204  } else {
205  return metadata_it->second;
206  }
207  }
208 private:
209  typedef std::map<int,connection_metadata::ptr> con_list;
210 
211  client m_endpoint;
213 
214  con_list m_connection_list;
215  int m_next_id;
216 };
217 
218 int main() {
219  bool done = false;
220  std::string input;
221  websocket_endpoint endpoint;
222 
223  while (!done) {
224  std::cout << "Enter Command: ";
225  std::getline(std::cin, input);
226 
227  if (input == "quit") {
228  done = true;
229  } else if (input == "help") {
230  std::cout
231  << "\nCommand List:\n"
232  << "connect <ws uri>\n"
233  << "close <connection id> [<close code:default=1000>] [<close reason>]\n"
234  << "show <connection id>\n"
235  << "help: Display this help text\n"
236  << "quit: Exit the program\n"
237  << std::endl;
238  } else if (input.substr(0,7) == "connect") {
239  int id = endpoint.connect(input.substr(8));
240  if (id != -1) {
241  std::cout << "> Created connection with id " << id << std::endl;
242  }
243  } else if (input.substr(0,5) == "close") {
244  std::stringstream ss(input);
245 
246  std::string cmd;
247  int id;
248  int close_code = websocketpp::close::status::normal;
249  std::string reason;
250 
251  ss >> cmd >> id >> close_code;
252  std::getline(ss,reason);
253 
254  endpoint.close(id, close_code, reason);
255  } else if (input.substr(0,4) == "show") {
256  int id = atoi(input.substr(5).c_str());
257 
258  connection_metadata::ptr metadata = endpoint.get_metadata(id);
259  if (metadata) {
260  std::cout << *metadata << std::endl;
261  } else {
262  std::cout << "> Unknown connection id " << id << std::endl;
263  }
264  } else {
265  std::cout << "> Unrecognized Command" << std::endl;
266  }
267  }
268 
269  return 0;
270 }
271 
272 /*
273 
274 clang++ -std=c++11 -stdlib=libc++ -I/Users/zaphoyd/software/websocketpp/ -I/Users/zaphoyd/software/boost_1_55_0/ -D_WEBSOCKETPP_CPP11_STL_ step4.cpp /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_system.a
275 
276 clang++ -I/Users/zaphoyd/software/websocketpp/ -I/Users/zaphoyd/software/boost_1_55_0/ step4.cpp /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_system.a /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_thread.a /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_random.a
277 
278 clang++ -std=c++11 -stdlib=libc++ -I/Users/zaphoyd/Documents/websocketpp/ -I/Users/zaphoyd/Documents/boost_1_53_0_libcpp/ -D_WEBSOCKETPP_CPP11_STL_ step4.cpp /Users/zaphoyd/Documents/boost_1_53_0_libcpp/stage/lib/libboost_system.a
279 
280 */
websocket_endpoint
Definition: step3.cpp:42