NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
connection.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_TRANSPORT_DEBUG_CON_HPP
29 #define WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP
30 
32 
34 
35 #include <websocketpp/uri.hpp>
37 
41 
42 #include <string>
43 #include <vector>
44 
45 namespace websocketpp {
46 namespace transport {
47 namespace debug {
48 
51 struct timer {
52  void cancel() {}
53 };
54 
55 template <typename config>
56 class connection : public lib::enable_shared_from_this< connection<config> > {
57 public:
61  typedef lib::shared_ptr<type> ptr;
62 
66  typedef typename config::alog_type alog_type;
68  typedef typename config::elog_type elog_type;
69 
70  // Concurrency policy types
73 
74  typedef lib::shared_ptr<timer> timer_ptr;
75 
76  explicit connection(bool is_server, const lib::shared_ptr<alog_type> & alog, const lib::shared_ptr<elog_type> & elog)
77  : m_reading(false), m_is_server(is_server), m_alog(alog), m_elog(elog)
78  {
79  m_alog->write(log::alevel::devel,"debug con transport constructor");
80  }
81 
83  ptr get_shared() {
84  return type::shared_from_this();
85  }
86 
88 
95  void set_secure(bool) {}
96 
98 
103  bool is_secure() const {
104  return false;
105  }
106 
108 
119  void set_uri(uri_ptr) {}
120 
122 
135  void set_remote_endpoint(std::string) {}
136 
138 
146  std::string get_remote_endpoint() const {
147  return "unknown (debug transport)";
148  }
149 
151 
155  return connection_hdl();
156  }
157 
159 
168  timer_ptr set_timer(long, timer_handler handler) {
169  m_alog->write(log::alevel::devel,"debug connection set timer");
170  m_timer_handler = handler;
171  return timer_ptr();
172  }
173 
175 
190  size_t read_all(char const * buf, size_t len) {
191  size_t total_read = 0;
192  size_t temp_read = 0;
193 
194  do {
195  temp_read = this->read_some_impl(buf+total_read,len-total_read);
196  total_read += temp_read;
197  } while (temp_read != 0 && total_read < len);
198 
199  return total_read;
200  }
201 
202  // debug stuff to invoke the async handlers
203  void expire_timer(lib::error_code const & ec) {
204  m_timer_handler(ec);
205  }
206 
207  void fullfil_write() {
208  m_write_handler(lib::error_code());
209  }
210 protected:
212 
217  void init(init_handler handler) {
218  m_alog->write(log::alevel::devel,"debug connection init");
219  handler(lib::error_code());
220  }
221 
223 
246  void async_read_at_least(size_t num_bytes, char * buf, size_t len,
247  read_handler handler)
248  {
249  std::stringstream s;
250  s << "debug_con async_read_at_least: " << num_bytes;
251  m_alog->write(log::alevel::devel,s.str());
252 
253  if (num_bytes > len) {
254  handler(make_error_code(error::invalid_num_bytes),size_t(0));
255  return;
256  }
257 
258  if (m_reading == true) {
259  handler(make_error_code(error::double_read),size_t(0));
260  return;
261  }
262 
263  if (num_bytes == 0 || len == 0) {
264  handler(lib::error_code(),size_t(0));
265  return;
266  }
267 
268  m_buf = buf;
269  m_len = len;
270  m_bytes_needed = num_bytes;
271  m_read_handler = handler;
272  m_cursor = 0;
273  m_reading = true;
274  }
275 
277 
288  void async_write(char const *, size_t, write_handler handler) {
289  m_alog->write(log::alevel::devel,"debug_con async_write");
290  m_write_handler = handler;
291  }
292 
294 
304  void async_write(std::vector<buffer> const &, write_handler handler) {
305  m_alog->write(log::alevel::devel,"debug_con async_write buffer list");
306  m_write_handler = handler;
307  }
308 
310 
314 
316 
326  lib::error_code dispatch(dispatch_handler handler) {
327  handler();
328  return lib::error_code();
329  }
330 
332 
336  handler(lib::error_code());
337  }
338 
339  size_t read_some_impl(char const * buf, size_t len) {
340  m_alog->write(log::alevel::devel,"debug_con read_some");
341 
342  if (!m_reading) {
343  m_elog->write(log::elevel::devel,"write while not reading");
344  return 0;
345  }
346 
347  size_t bytes_to_copy = (std::min)(len,m_len-m_cursor);
348 
349  std::copy(buf,buf+bytes_to_copy,m_buf+m_cursor);
350 
351  m_cursor += bytes_to_copy;
352 
353  if (m_cursor >= m_bytes_needed) {
354  complete_read(lib::error_code());
355  }
356 
357  return bytes_to_copy;
358  }
359 
361 
376  void complete_read(lib::error_code const & ec) {
377  m_reading = false;
378 
379  read_handler handler = m_read_handler;
380  m_read_handler = read_handler();
381 
382  handler(ec,m_cursor);
383  }
384 private:
385  timer_handler m_timer_handler;
386 
387  // Read space (Protected by m_read_mutex)
388  char * m_buf;
389  size_t m_len;
390  size_t m_bytes_needed;
391  read_handler m_read_handler;
392  size_t m_cursor;
393 
394  // transport resources
395  connection_hdl m_connection_hdl;
396  write_handler m_write_handler;
397  shutdown_handler m_shutdown_handler;
398 
399  bool m_reading;
400  bool const m_is_server;
401  bool m_is_secure;
402  lib::shared_ptr<alog_type> m_alog;
403  lib::shared_ptr<elog_type> m_elog;
404  std::string m_remote_endpoint;
405 };
406 
407 
408 } // namespace debug
409 } // namespace transport
410 } // namespace websocketpp
411 
412 #endif // WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP
ptr get_shared()
Get a shared pointer to this component.
Definition: connection.hpp:83
config::concurrency_type concurrency_type
transport concurrency policy
Definition: connection.hpp:64
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
Definition: connection.hpp:61
void set_remote_endpoint(std::string)
Set human readable remote endpoint address.
Definition: connection.hpp:135
void set_handle(connection_hdl)
Set Connection Handle.
Definition: connection.hpp:313
void async_read_at_least(size_t num_bytes, char *buf, size_t len, read_handler handler)
Initiate an async_read for at least num_bytes bytes into buf.
Definition: connection.hpp:246
size_t read_some_impl(char const *buf, size_t len)
Definition: connection.hpp:339
lib::function< void(lib::error_code const &)> write_handler
The type and signature of the callback passed to the write method.
Definition: connection.hpp:123
static level const devel
Low level debugging information (warning: very chatty)
Definition: levels.hpp:63
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
void expire_timer(lib::error_code const &ec)
Definition: connection.hpp:203
A fake lock guard implementation that does nothing.
Definition: none.hpp:46
lib::function< void(lib::error_code const &, size_t)> read_handler
The type and signature of the callback passed to the read method.
Definition: connection.hpp:120
config::alog_type alog_type
Type of this transport&#39;s access logging policy.
Definition: connection.hpp:66
A fake mutex implementation that does nothing.
Definition: none.hpp:39
concurrency_type::scoped_lock_type scoped_lock_type
Definition: connection.hpp:71
void complete_read(lib::error_code const &ec)
Signal that a requested read is complete.
Definition: connection.hpp:376
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:141
void async_write(char const *, size_t, write_handler handler)
Asyncronous Transport Write.
Definition: connection.hpp:288
timer_ptr set_timer(long, timer_handler handler)
Call back a function after a period of time.
Definition: connection.hpp:168
connection< config > type
Type of this connection transport component.
Definition: connection.hpp:59
config::elog_type elog_type
Type of this transport&#39;s error logging policy.
Definition: connection.hpp:68
void set_secure(bool)
Set whether or not this connection is secure.
Definition: connection.hpp:95
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
Definition: connection.hpp:135
void set_uri(uri_ptr)
Set uri hook.
Definition: connection.hpp:119
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
Definition: connection.hpp:126
void init(init_handler handler)
Initialize the connection transport.
Definition: connection.hpp:217
lib::function< void(lib::error_code const &)> shutdown_handler
The type and signature of the callback passed to the shutdown method.
Definition: connection.hpp:129
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
Definition: connection.hpp:117
Empty timer class to stub out for timer functionality that stub transport doesn&#39;t support...
Definition: connection.hpp:51
Stub concurrency policy that implements the interface using no-ops.
Definition: none.hpp:60
connection(bool is_server, const lib::shared_ptr< alog_type > &alog, const lib::shared_ptr< elog_type > &elog)
Definition: connection.hpp:76
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:352
concurrency_type::mutex_type mutex_type
Definition: connection.hpp:72
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport&#39;s event system (if present)
Definition: connection.hpp:326
void async_write(std::vector< buffer > const &, write_handler handler)
Asyncronous Transport Write (scatter-gather)
Definition: connection.hpp:304
void async_shutdown(shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
Definition: connection.hpp:335
bool is_secure() const
Tests whether or not the underlying transport is secure.
Definition: connection.hpp:103
lib::error_code make_error_code(error::value e)
Definition: connection.hpp:224
connection_hdl get_handle() const
Get the connection handle.
Definition: connection.hpp:154
lib::shared_ptr< timer > timer_ptr
Definition: connection.hpp:74
Stub logger that ignores all input.
Definition: stub.hpp:41
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
Definition: connection.hpp:146
size_t read_all(char const *buf, size_t len)
Manual input supply (read all)
Definition: connection.hpp:190