NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
testee_server.cpp
Go to the documentation of this file.
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 
29 #include <websocketpp/server.hpp>
31 #include <iostream>
32 
34  // pull default settings from our core config
36 
43 
48 
49  static bool const enable_multithreading = true;
50 
57 
58  static bool const enable_multithreading = true;
59  };
60 
63 
68 
71 
74 };
75 
77 
78 using websocketpp::lib::placeholders::_1;
79 using websocketpp::lib::placeholders::_2;
80 using websocketpp::lib::bind;
81 
82 // pull out the type of messages sent by our config
84 
85 // Define a callback to handle incoming messages
87  s->send(hdl, msg->get_payload(), msg->get_opcode());
88 }
89 
91  boost::asio::ip::tcp::no_delay option(true);
92  s.set_option(option);
93 }
94 
95 int main(int argc, char * argv[]) {
96  // Create a server endpoint
97  server testee_server;
98 
99  short port = 9002;
100  size_t num_threads = 1;
101 
102  if (argc == 3) {
103  port = atoi(argv[1]);
104  num_threads = atoi(argv[2]);
105  }
106 
107  try {
108  // Total silence
111 
112  // Initialize ASIO
113  testee_server.init_asio();
114  testee_server.set_reuse_addr(true);
115 
116  // Register our message handler
117  testee_server.set_message_handler(bind(&on_message,&testee_server,::_1,::_2));
118  testee_server.set_socket_init_handler(bind(&on_socket_init,::_1,::_2));
119 
120  // Listen on specified port with extended listen backlog
121  testee_server.set_listen_backlog(8192);
122  testee_server.listen(port);
123 
124  // Start the server accept loop
125  testee_server.start_accept();
126 
127  // Start the ASIO io_service run loop
128  if (num_threads == 1) {
129  testee_server.run();
130  } else {
131  typedef websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_ptr;
132  std::vector<thread_ptr> ts;
133  for (size_t i = 0; i < num_threads; i++) {
134  ts.push_back(websocketpp::lib::make_shared<websocketpp::lib::thread>(&server::run, &testee_server));
135  }
136 
137  for (size_t i = 0; i < num_threads; i++) {
138  ts[i]->join();
139  }
140  }
141 
142  } catch (websocketpp::exception const & e) {
143  std::cout << "exception: " << e.what() << std::endl;
144  }
145 }
static level const all
Special aggregate value representing "all levels".
Definition: levels.hpp:152
Asio based endpoint transport component.
Definition: base.hpp:143
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
static level const none
Special aggregate value representing "no levels".
Definition: levels.hpp:61
websocketpp::server< testee_config > server
core::con_msg_manager_type con_msg_manager_type
void set_reuse_addr(bool value)
Sets whether to use the SO_REUSEADDR flag when opening listening sockets.
Definition: endpoint.hpp:342
Concurrency policy that uses std::mutex / boost::mutex.
Definition: basic.hpp:37
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
Definition: endpoint.hpp:70
static bool const enable_multithreading
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
websocketpp::config::asio core
permessage_compress extension
core::rng_type rng_type
Stores, parses, and manipulates HTTP responses.
Definition: response.hpp:57
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
Definition: endpoint.hpp:181
virtual char const * what() const
Definition: error.hpp:263
server::message_ptr message_ptr
core::alog_type alog_type
core::response_type response_type
core::endpoint_msg_manager_type endpoint_msg_manager_type
static level const none
Special aggregate value representing "no levels".
Definition: levels.hpp:114
core::request_type request_type
Basic logger that outputs to an ostream.
Definition: basic.hpp:59
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: tls.hpp:442
Stub for user supplied base class.
void on_socket_init(websocketpp::connection_hdl, boost::asio::ip::tcp::socket &s)
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.
Thread safe stub "random" integer generator.
Definition: none.hpp:46
static const websocketpp::log::level alog_level
void clear_error_channels(log::level channels)
Clear Error logging channels.
Definition: endpoint.hpp:253
Represents a buffer for a single WebSocket message.
Definition: message.hpp:84
core::endpoint_base endpoint_base
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
void set_listen_backlog(int backlog)
Sets the maximum length of the queue of pending connections.
Definition: endpoint.hpp:324
core::concurrency_type concurrency_type
websocketpp::extensions::permessage_deflate::enabled< permessage_deflate_config > permessage_deflate_type
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition: endpoint.hpp:231
Stores, parses, and manipulates HTTP requests.
Definition: request.hpp:50
std::size_t run()
wraps the run method of the internal io_service object
Definition: endpoint.hpp:613
static const websocketpp::log::level elog_level
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
core::elog_type elog_type
core::concurrency_type concurrency_type
core::response_type response_type
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
Definition: endpoint.hpp:391
int main(int argc, char *argv[])
uint32_t level
Type of a channel package.
Definition: levels.hpp:37
void set_message_handler(message_handler h)
Definition: endpoint.hpp:322
core::message_type message_type
A connection message manager that allocates a new message for each request.
Definition: alloc.hpp:41
An endpoint message manager that allocates a new manager for each connection.
Definition: alloc.hpp:88
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)
websocketpp::transport::asio::endpoint< transport_config > transport_type
Server config with asio transport and TLS disabled.
Definition: asio_no_tls.hpp:38