NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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
29
#include <
websocketpp/config/asio_no_tls.hpp
>
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
38
typedef
websocketpp::server<websocketpp::config::asio>
ws_echo_server
;
39
40
// Define a callback to handle incoming messages
41
void
on_message
(
ws_echo_server
* s,
websocketpp::connection_hdl
hdl,
ws_echo_server::message_ptr
msg) {
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::exception
const
& e) {
56
std::cout <<
"Echo failed because: "
57
<<
"("
<< e.
what
() <<
")"
<< 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;
69
ws_server.
set_access_channels
(
websocketpp::log::alevel::all
);
70
ws_server.
clear_access_channels
(
websocketpp::log::alevel::frame_payload
);
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
}
websocketpp::log::alevel::all
static level const all
Special aggregate value representing "all levels".
Definition:
levels.hpp:152
websocketpp::exception
Definition:
error.hpp:251
websocketpp::endpoint< connection< websocketpp::config::asio >, websocketpp::config::asio >::message_ptr
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
Definition:
endpoint.hpp:70
websocketpp::log::alevel::frame_payload
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
Definition:
levels.hpp:129
websocketpp::connection_hdl
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
Definition:
connection_hdl.hpp:48
tcp_echo_server.hpp
websocketpp::transport::asio::endpoint::init_asio
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
Definition:
endpoint.hpp:185
on_message
void on_message(ws_echo_server *s, websocketpp::connection_hdl hdl, ws_echo_server::message_ptr msg)
Definition:
external_io_service.cpp:41
ws_echo_server
websocketpp::server< websocketpp::config::asio > ws_echo_server
Definition:
external_io_service.cpp:38
tcp_echo_server
Definition:
tcp_echo_server.hpp:74
main
int main()
Definition:
external_io_service.cpp:61
websocketpp::server::start_accept
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
Definition:
server_endpoint.hpp:121
websocketpp::server< websocketpp::config::asio >
websocketpp::endpoint::set_access_channels
void set_access_channels(log::level channels)
Set Access logging channel.
Definition:
endpoint.hpp:220
websocketpp::transport::asio::endpoint::stop_listening
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
Definition:
endpoint.hpp:605
asio_no_tls.hpp
websocketpp::endpoint::clear_access_channels
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition:
endpoint.hpp:231
websocketpp::transport::asio::endpoint::listen
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
websocketpp::endpoint::set_message_handler
void set_message_handler(message_handler h)
Definition:
endpoint.hpp:322
websocketpp::exception::what
virtual char const * what() const
Definition:
error.hpp:263
websocketpp::endpoint::send
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)
Definition:
endpoint_impl.hpp:162
server.hpp
ndnSIM
NFD
websocketpp
examples
external_io_service
external_io_service.cpp
Generated on Fri May 6 2022 12:34:14 for ndnSIM by
1.8.13