NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
external_io_service.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 #include "tcp_echo_server.hpp"
28 
30 #include <websocketpp/server.hpp>
31 
32 #include <iostream>
33 
34 using websocketpp::lib::placeholders::_1;
35 using websocketpp::lib::placeholders::_2;
36 using websocketpp::lib::bind;
37 
39 
40 // Define a callback to handle incoming messages
42  std::cout << "on_message called with hdl: " << hdl.lock().get()
43  << " and message: " << msg->get_payload()
44  << std::endl;
45 
46  // check for a special command to instruct the server to stop listening so
47  // it can be cleanly exited.
48  if (msg->get_payload() == "stop-listening") {
49  s->stop_listening();
50  return;
51  }
52 
53  try {
54  s->send(hdl, msg->get_payload(), msg->get_opcode());
55  } catch (websocketpp::lib::error_code const & e) {
56  std::cout << "Echo failed because: " << e
57  << "(" << e.message() << ")" << std::endl;
58  }
59 }
60 
61 int main() {
62  asio::io_service service;
63 
64  // Add a TCP echo server on port 9003
65  tcp_echo_server custom_http_server(service, 9003);
66 
67  // Add a WebSocket echo server on port 9002
68  ws_echo_server ws_server;
71 
72  // The only difference in this code between an internal and external
73  // io_service is the different constructor to init_asio
74  ws_server.init_asio(&service);
75 
76  // Register our message handler
77  ws_server.set_message_handler(bind(&on_message,&ws_server,::_1,::_2));
78  ws_server.listen(9002);
79  ws_server.start_accept();
80 
81  // TODO: add a timer?
82 
83  // Start the Asio io_service run loop for all
84  service.run();
85 }
static level const all
Special aggregate value representing "all levels".
Definition: levels.hpp:152
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
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
Definition: endpoint.hpp:181
void on_message(ws_echo_server *s, websocketpp::connection_hdl hdl, ws_echo_server::message_ptr msg)
websocketpp::server< websocketpp::config::asio > ws_echo_server
int main()
void start_accept(lib::error_code &ec)
Starts the server&#39;s async connection acceptance loop (exception free)
void set_access_channels(log::level channels)
Set Access logging channel.
Definition: endpoint.hpp:220
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
Definition: endpoint.hpp:577
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition: endpoint.hpp:231
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
void set_message_handler(message_handler h)
Definition: endpoint.hpp:322
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)