NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
debug_server.cpp
Go to the documentation of this file.
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 
33 
34 // Custom logger
36 
37 #include <websocketpp/server.hpp>
38 
39 #include <iostream>
40 
44 
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;
56  typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
57 
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 {
77  };
78 
81 
82  static const long timeout_open_handshake = 0;
83 };
84 
86 
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
95 
97  //sleep(6);
98  return true;
99 }
100 
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 
115 
116  std::cout << "Fail handler: " << con->get_ec() << " " << con->get_ec().message() << std::endl;
117 }
118 
120  std::cout << "Close handler" << std::endl;
121 }
122 
123 // Define a callback to handle incoming messages
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 (websocketpp::exception const & e) {
132  std::cout << "Echo failed because: "
133  << "(" << e.what() << ")" << std::endl;
134  }
135 }
136 
137 int main() {
138  // Create a server endpoint
139  server echo_server;
140 
141  try {
142  // Set logging settings
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 (websocketpp::exception const & e) {
168  std::cout << e.what() << std::endl;
169  } catch (const std::exception & e) {
170  std::cout << e.what() << std::endl;
171  } catch (...) {
172  std::cout << "other exception" << std::endl;
173  }
174 }
static level const all
Special aggregate value representing "all levels".
Definition: levels.hpp:152
base::rng_type rng_type
Asio based endpoint transport component.
Definition: base.hpp:143
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
type::request_type request_type
websocketpp::transport::asio::basic_socket::endpoint socket_type
void set_reuse_addr(bool value)
Sets whether to use the SO_REUSEADDR flag when opening listening sockets.
Definition: endpoint.hpp:364
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
Definition: endpoint.hpp:70
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
Definition: levels.hpp:129
stub_config::con_msg_manager_type con_msg_manager_type
Definition: hybi13.cpp:89
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
websocketpp::transport::asio::endpoint< transport_config > transport_type
debug_custom type
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
Definition: endpoint.hpp:643
void set_close_handler(close_handler h)
Definition: endpoint.hpp:282
base::alog_type alog_type
Custom Logging policies.
base::endpoint_msg_manager_type endpoint_msg_manager_type
type::response_type response_type
int main()
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
Definition: endpoint.hpp:185
base::request_type request_type
void on_fail(server *s, websocketpp::connection_hdl hdl)
base::response_type response_type
debug_asio base
base::elog_type elog_type
void start_accept(lib::error_code &ec)
Starts the server&#39;s async connection acceptance loop (exception free)
Server endpoint role based on the given config.
void set_access_channels(log::level channels)
Set Access logging channel.
Definition: endpoint.hpp:220
server::message_ptr message_ptr
void set_http_handler(http_handler h)
Definition: endpoint.hpp:312
base::con_msg_manager_type con_msg_manager_type
bool validate(server *, websocketpp::connection_hdl)
void set_validate_handler(validate_handler h)
Definition: endpoint.hpp:317
Basic ASIO endpoint socket component.
Definition: none.hpp:318
void set_fail_handler(fail_handler h)
Definition: endpoint.hpp:287
static const long timeout_open_handshake
websocketpp::server< debug_custom > server
type::concurrency_type concurrency_type
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition: endpoint.hpp:231
void on_http(server *s, websocketpp::connection_hdl hdl)
====== WARNING ======== This example is presently used as a scratch space.
Client/Server debug config with asio transport and TLS disabled.
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
base::concurrency_type concurrency_type
std::size_t run()
wraps the run method of the internal io_service object
Definition: endpoint.hpp:641
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
Definition: endpoint.hpp:413
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
void set_message_handler(message_handler h)
Definition: endpoint.hpp:322
void on_close(websocketpp::connection_hdl)
virtual char const * what() const
Definition: error.hpp:263
base::message_type message_type
void send(connection_hdl hdl, std::string const &payload, frame::opcode::value op, lib::error_code &ec)
Create a message and add it to the outgoing send queue (exception free)