NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
endpoint.hpp
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  */
27 
28 #ifndef WEBSOCKETPP_ENDPOINT_HPP
29 #define WEBSOCKETPP_ENDPOINT_HPP
30 
32 
34 #include <websocketpp/version.hpp>
35 
36 #include <string>
37 
38 namespace websocketpp {
39 
41 template <typename connection, typename config>
43 public:
44  // Import appropriate types from our helper class
45  // See endpoint_types for more details.
47 
52 
59 
65  typedef typename transport_con_type::ptr transport_con_ptr;
66 
71 
73  typedef typename config::elog_type elog_type;
75  typedef typename config::alog_type alog_type;
76 
81 
83  typedef typename config::rng_type rng_type;
84 
85  // TODO: organize these
87 
88  // This would be ideal. Requires C++11 though
89  //friend connection;
90 
91  explicit endpoint(bool p_is_server)
92  : m_alog(config::alog_level, log::channel_type_hint::access)
93  , m_elog(config::elog_level, log::channel_type_hint::error)
94  , m_user_agent(::websocketpp::user_agent)
95  , m_open_handshake_timeout_dur(config::timeout_open_handshake)
96  , m_close_handshake_timeout_dur(config::timeout_close_handshake)
97  , m_pong_timeout_dur(config::timeout_pong)
98  , m_max_message_size(config::max_message_size)
99  , m_max_http_body_size(config::max_http_body_size)
100  , m_is_server(p_is_server)
101  {
104 
105  m_alog.write(log::alevel::devel, "endpoint constructor");
106 
108  }
109 
110 
113 
114  #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
115  // no copy constructor because endpoints are not copyable
116  endpoint(endpoint &) = delete;
117 
118  // no copy assignment operator because endpoints are not copyable
119  endpoint & operator=(endpoint const &) = delete;
120  #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
121 
122  #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
123  endpoint(endpoint && o)
125  : config::transport_type(std::move(o))
126  , config::endpoint_base(std::move(o))
127  , m_alog(std::move(o.m_alog))
128  , m_elog(std::move(o.m_elog))
129  , m_user_agent(std::move(o.m_user_agent))
130  , m_open_handler(std::move(o.m_open_handler))
131 
132  , m_close_handler(std::move(o.m_close_handler))
133  , m_fail_handler(std::move(o.m_fail_handler))
134  , m_ping_handler(std::move(o.m_ping_handler))
135  , m_pong_handler(std::move(o.m_pong_handler))
136  , m_pong_timeout_handler(std::move(o.m_pong_timeout_handler))
137  , m_interrupt_handler(std::move(o.m_interrupt_handler))
138  , m_http_handler(std::move(o.m_http_handler))
139  , m_validate_handler(std::move(o.m_validate_handler))
140  , m_message_handler(std::move(o.m_message_handler))
141 
142  , m_open_handshake_timeout_dur(o.m_open_handshake_timeout_dur)
143  , m_close_handshake_timeout_dur(o.m_close_handshake_timeout_dur)
144  , m_pong_timeout_dur(o.m_pong_timeout_dur)
145  , m_max_message_size(o.m_max_message_size)
146  , m_max_http_body_size(o.m_max_http_body_size)
147 
148  , m_rng(std::move(o.m_rng))
149  , m_is_server(o.m_is_server)
150  {}
151 
152  #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
153  // no move assignment operator because of const member variables
154  endpoint & operator=(endpoint &&) = delete;
155  #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
156 
157  #endif // _WEBSOCKETPP_MOVE_SEMANTICS_
158 
159 
161 
169  std::string get_user_agent() const {
170  scoped_lock_type guard(m_mutex);
171  return m_user_agent;
172  }
173 
175 
196  void set_user_agent(std::string const & ua) {
197  scoped_lock_type guard(m_mutex);
198  m_user_agent = ua;
199  }
200 
202 
205  bool is_server() const {
206  return m_is_server;
207  }
208 
209  /********************************/
210  /* Pass-through logging adaptor */
211  /********************************/
212 
214 
221  m_alog.set_channels(channels);
222  }
223 
225 
232  m_alog.clear_channels(channels);
233  }
234 
236 
243  m_elog.set_channels(channels);
244  }
245 
247 
254  m_elog.clear_channels(channels);
255  }
256 
258 
261  alog_type & get_alog() {
262  return m_alog;
263  }
264 
266 
269  elog_type & get_elog() {
270  return m_elog;
271  }
272 
273  /*************************/
274  /* Set Handler functions */
275  /*************************/
276 
278  m_alog.write(log::alevel::devel,"set_open_handler");
279  scoped_lock_type guard(m_mutex);
280  m_open_handler = h;
281  }
283  m_alog.write(log::alevel::devel,"set_close_handler");
284  scoped_lock_type guard(m_mutex);
285  m_close_handler = h;
286  }
288  m_alog.write(log::alevel::devel,"set_fail_handler");
289  scoped_lock_type guard(m_mutex);
290  m_fail_handler = h;
291  }
293  m_alog.write(log::alevel::devel,"set_ping_handler");
294  scoped_lock_type guard(m_mutex);
295  m_ping_handler = h;
296  }
298  m_alog.write(log::alevel::devel,"set_pong_handler");
299  scoped_lock_type guard(m_mutex);
300  m_pong_handler = h;
301  }
303  m_alog.write(log::alevel::devel,"set_pong_timeout_handler");
304  scoped_lock_type guard(m_mutex);
305  m_pong_timeout_handler = h;
306  }
308  m_alog.write(log::alevel::devel,"set_interrupt_handler");
309  scoped_lock_type guard(m_mutex);
310  m_interrupt_handler = h;
311  }
313  m_alog.write(log::alevel::devel,"set_http_handler");
314  scoped_lock_type guard(m_mutex);
315  m_http_handler = h;
316  }
318  m_alog.write(log::alevel::devel,"set_validate_handler");
319  scoped_lock_type guard(m_mutex);
320  m_validate_handler = h;
321  }
322  void set_message_handler(message_handler h) {
323  m_alog.write(log::alevel::devel,"set_message_handler");
324  scoped_lock_type guard(m_mutex);
325  m_message_handler = h;
326  }
327 
329  // Connection timeouts and other limits //
331 
333 
352  void set_open_handshake_timeout(long dur) {
353  scoped_lock_type guard(m_mutex);
354  m_open_handshake_timeout_dur = dur;
355  }
356 
358 
378  scoped_lock_type guard(m_mutex);
379  m_close_handshake_timeout_dur = dur;
380  }
381 
383 
399  void set_pong_timeout(long dur) {
400  scoped_lock_type guard(m_mutex);
401  m_pong_timeout_dur = dur;
402  }
403 
405 
415  size_t get_max_message_size() const {
416  return m_max_message_size;
417  }
418 
420 
432  void set_max_message_size(size_t new_value) {
433  m_max_message_size = new_value;
434  }
435 
437 
449  size_t get_max_http_body_size() const {
450  return m_max_http_body_size;
451  }
452 
454 
466  void set_max_http_body_size(size_t new_value) {
467  m_max_http_body_size = new_value;
468  }
469 
470  /*************************************/
471  /* Connection pass through functions */
472  /*************************************/
473 
482  void interrupt(connection_hdl hdl, lib::error_code & ec);
483  void interrupt(connection_hdl hdl);
484 
486 
505  void pause_reading(connection_hdl hdl, lib::error_code & ec);
506 
508  void pause_reading(connection_hdl hdl);
509 
511 
517  void resume_reading(connection_hdl hdl, lib::error_code & ec);
518 
520  void resume_reading(connection_hdl hdl);
521 
523 
535  void send_http_response(connection_hdl hdl, lib::error_code & ec);
536 
538 
550 
552 
560  void send(connection_hdl hdl, std::string const & payload,
561  frame::opcode::value op, lib::error_code & ec);
563 
571  void send(connection_hdl hdl, std::string const & payload,
573 
574  void send(connection_hdl hdl, void const * payload, size_t len,
575  frame::opcode::value op, lib::error_code & ec);
576  void send(connection_hdl hdl, void const * payload, size_t len,
578 
579  void send(connection_hdl hdl, message_ptr msg, lib::error_code & ec);
580  void send(connection_hdl hdl, message_ptr msg);
581 
582  void close(connection_hdl hdl, close::status::value const code,
583  std::string const & reason, lib::error_code & ec);
584  void close(connection_hdl hdl, close::status::value const code,
585  std::string const & reason);
586 
588 
595  void ping(connection_hdl hdl, std::string const & payload,
596  lib::error_code & ec);
598 
606  void ping(connection_hdl hdl, std::string const & payload);
607 
609 
616  void pong(connection_hdl hdl, std::string const & payload,
617  lib::error_code & ec);
619 
627  void pong(connection_hdl hdl, std::string const & payload);
628 
630 
643  connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code & ec) {
644  connection_ptr con = lib::static_pointer_cast<connection_type>(
645  hdl.lock());
646  if (!con) {
648  }
649  return con;
650  }
651 
653  connection_ptr get_con_from_hdl(connection_hdl hdl) {
654  lib::error_code ec;
655  connection_ptr con = this->get_con_from_hdl(hdl,ec);
656  if (ec) {
657  throw exception(ec);
658  }
659  return con;
660  }
661 protected:
662  connection_ptr create_connection();
663 
664  alog_type m_alog;
665  elog_type m_elog;
666 private:
667  // dynamic settings
668  std::string m_user_agent;
669 
670  open_handler m_open_handler;
671  close_handler m_close_handler;
672  fail_handler m_fail_handler;
673  ping_handler m_ping_handler;
674  pong_handler m_pong_handler;
675  pong_timeout_handler m_pong_timeout_handler;
676  interrupt_handler m_interrupt_handler;
677  http_handler m_http_handler;
678  validate_handler m_validate_handler;
679  message_handler m_message_handler;
680 
681  long m_open_handshake_timeout_dur;
682  long m_close_handshake_timeout_dur;
683  long m_pong_timeout_dur;
684  size_t m_max_message_size;
685  size_t m_max_http_body_size;
686 
687  rng_type m_rng;
688 
689  // static settings
690  bool const m_is_server;
691 
692  // endpoint state
693  mutable mutex_type m_mutex;
694 };
695 
696 } // namespace websocketpp
697 
699 
700 #endif // WEBSOCKETPP_ENDPOINT_HPP
void send_http_response(connection_hdl hdl, lib::error_code &ec)
Send deferred HTTP Response.
config::concurrency_type concurrency_type
Type of the concurrency component of this endpoint.
Definition: endpoint.hpp:51
Asio based endpoint transport component.
Definition: base.hpp:143
void write(level, std::string const &)
Write a string message to the given channel.
Definition: stub.hpp:82
uint16_t value
The type of a close code value.
Definition: close.hpp:49
void set_pong_timeout_handler(pong_timeout_handler h)
Definition: endpoint.hpp:302
lib::error_code make_error_code(error::value e)
Definition: error.hpp:235
connection_type::termination_handler termination_handler
Definition: endpoint.hpp:86
size_t get_max_message_size() const
Get default maximum message size.
Definition: endpoint.hpp:415
bool is_server() const
Returns whether or not this endpoint is a server.
Definition: endpoint.hpp:205
lib::function< void(connection_hdl)> close_handler
The type and function signature of a close handler.
Definition: connection.hpp:69
void set_open_handler(open_handler h)
Definition: endpoint.hpp:277
lib::function< void(connection_hdl)> open_handler
The type and function signature of an open handler.
Definition: connection.hpp:59
void ping(connection_hdl hdl, std::string const &payload, lib::error_code &ec)
Send a ping to a specific connection.
lib::function< void(connection_hdl, std::string)> pong_timeout_handler
The type and function signature of a pong timeout handler.
Definition: connection.hpp:116
lib::function< void(ptr)> termination_handler
Definition: connection.hpp:260
lib::function< void(connection_hdl)> interrupt_handler
The type and function signature of an interrupt handler.
Definition: connection.hpp:91
asio::connection< config > transport_con_type
Type of the connection transport component associated with this endpoint transport component...
Definition: endpoint.hpp:74
config::rng_type rng_type
Type of RNG.
Definition: endpoint.hpp:83
lib::function< bool(connection_hdl, std::string)> ping_handler
The type and function signature of a ping handler.
Definition: connection.hpp:101
void set_close_handshake_timeout(long dur)
Set close handshake timeout.
Definition: endpoint.hpp:377
lib::weak_ptr< type > weak_ptr
Type of a weak pointer to this connection.
Definition: connection.hpp:245
Represents an individual WebSocket connection.
Definition: connection.hpp:235
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
Definition: endpoint.hpp:70
void interrupt(connection_hdl hdl, lib::error_code &ec)
These functions act as adaptors to their counterparts in connection.
websocketpp::transport::asio::endpoint< transport_config > transport_type
Definition: integration.cpp:70
connection_type::message_handler message_handler
Type of message_handler.
Definition: endpoint.hpp:68
void set_open_handshake_timeout(long dur)
Set open handshake timeout.
Definition: endpoint.hpp:352
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the transport component of the connections that this endpoint creates...
Definition: endpoint.hpp:65
alog_type & get_alog()
Get reference to access logger.
Definition: endpoint.hpp:261
static const websocketpp::log::level alog_level
Default static access logging channels.
static char const user_agent[]
Default user agent string.
Definition: version.hpp:57
A fake lock guard implementation that does nothing.
Definition: none.hpp:46
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
concurrency_type::scoped_lock_type scoped_lock_type
Type of our concurrency policy&#39;s scoped lock object.
Definition: endpoint.hpp:78
void set_ping_handler(ping_handler h)
Definition: endpoint.hpp:292
A fake mutex implementation that does nothing.
Definition: none.hpp:39
void pause_reading(connection_hdl hdl, lib::error_code &ec)
Pause reading of new data (exception free)
lib::function< void(connection_hdl, message_ptr)> message_handler
Definition: connection.hpp:281
void set_close_handler(close_handler h)
Definition: endpoint.hpp:282
std::string get_user_agent() const
Returns the user agent string that this endpoint will use.
Definition: endpoint.hpp:169
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:141
concurrency_type::mutex_type mutex_type
Type of our concurrency policy&#39;s mutex object.
Definition: endpoint.hpp:80
void resume_reading(connection_hdl hdl, lib::error_code &ec)
Resume reading of new data (exception free)
lib::function< bool(connection_hdl)> validate_handler
The type and function signature of a validate handler.
Definition: connection.hpp:129
message_type::ptr message_ptr
Definition: connection.hpp:269
websocketpp::endpoint_base endpoint_base
User overridable Endpoint base class.
void set_max_http_body_size(size_t new_value)
Set maximum HTTP message body size.
Definition: endpoint.hpp:466
void set_max_message_size(size_t new_value)
Set default maximum message size.
Definition: endpoint.hpp:432
transport_type::transport_con_type transport_con_type
Type of the transport component of the connections that this endpoint creates.
Definition: endpoint.hpp:62
config::alog_type alog_type
Type of access logger.
Definition: endpoint.hpp:75
size_t get_max_http_body_size() const
Get maximum HTTP message body size.
Definition: endpoint.hpp:449
config::transport_type transport_type
Type of the transport component of this endpoint.
Definition: endpoint.hpp:49
connection_type::ptr connection_ptr
Shared pointer to connection_type.
Definition: endpoint.hpp:56
static const websocketpp::log::level elog_level
Default static error logging channels.
endpoint< connection, config > type
Definition: endpoint.hpp:46
An operation was attempted on a connection that did not exist or was already deleted.
Definition: error.hpp:93
lib::function< void(connection_hdl)> http_handler
The type and function signature of a http handler.
Definition: connection.hpp:151
Thread safe stub "random" integer generator.
Definition: none.hpp:46
void set_pong_handler(pong_handler h)
Definition: endpoint.hpp:297
void pong(connection_hdl hdl, std::string const &payload, lib::error_code &ec)
Send a pong to a specific connection.
void clear_error_channels(log::level channels)
Clear Error logging channels.
Definition: endpoint.hpp:253
void set_access_channels(log::level channels)
Set Access logging channel.
Definition: endpoint.hpp:220
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
endpoint(bool p_is_server)
Definition: endpoint.hpp:91
void set_http_handler(http_handler h)
Definition: endpoint.hpp:312
elog_type & get_elog()
Get reference to error logger.
Definition: endpoint.hpp:269
Stub concurrency policy that implements the interface using no-ops.
Definition: none.hpp:60
config::elog_type elog_type
Type of error logger.
Definition: endpoint.hpp:73
Creates and manages connections associated with a WebSocket endpoint.
Definition: endpoint.hpp:42
void set_validate_handler(validate_handler h)
Definition: endpoint.hpp:317
void clear_channels(level)
Dynamically disable the given list of channels.
Definition: stub.hpp:73
connection_type::weak_ptr connection_weak_ptr
Weak pointer to connection type.
Definition: endpoint.hpp:58
void set_fail_handler(fail_handler h)
Definition: endpoint.hpp:287
void set_interrupt_handler(interrupt_handler h)
Definition: endpoint.hpp:307
void init_logging(alog_type *a, elog_type *e)
Initialize logging.
Definition: endpoint.hpp:798
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection.
Definition: connection.hpp:243
connection connection_type
Type of the connections that this endpoint creates.
Definition: endpoint.hpp:54
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition: endpoint.hpp:231
void set_error_channels(log::level channels)
Set Error logging channel.
Definition: endpoint.hpp:242
void set_pong_timeout(long dur)
Set pong timeout.
Definition: endpoint.hpp:399
lib::function< void(connection_hdl, std::string)> pong_handler
The type and function signature of a pong handler.
Definition: connection.hpp:109
void close(connection_hdl hdl, close::status::value const code, std::string const &reason, lib::error_code &ec)
connection_ptr get_con_from_hdl(connection_hdl hdl)
Retrieves a connection_ptr from a connection_hdl (exception version)
Definition: endpoint.hpp:653
void set_user_agent(std::string const &ua)
Sets the user agent string that this endpoint will use.
Definition: endpoint.hpp:196
uint32_t level
Type of a channel package.
Definition: levels.hpp:37
void set_message_handler(message_handler h)
Definition: endpoint.hpp:322
void set_channels(level)
Dynamically enable the given list of channels.
Definition: stub.hpp:64
Stub logger that ignores all input.
Definition: stub.hpp:41
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)
lib::function< void(connection_hdl)> fail_handler
The type and function signature of a fail handler.
Definition: connection.hpp:79
connection_ptr create_connection()