NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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_IOSTREAM_CON_HPP
29 #define WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
30 
32 
34 
35 #include <websocketpp/uri.hpp>
36 
38 
42 
43 #include <algorithm>
44 #include <iostream>
45 #include <sstream>
46 #include <string>
47 #include <vector>
48 
49 namespace websocketpp {
50 namespace transport {
51 namespace iostream {
52 
55 struct timer {
56  void cancel() {}
57 };
58 
59 template <typename config>
60 class connection : public lib::enable_shared_from_this< connection<config> > {
61 public:
65  typedef lib::shared_ptr<type> ptr;
66 
70  typedef typename config::alog_type alog_type;
72  typedef typename config::elog_type elog_type;
73 
74  // Concurrency policy types
77 
78  typedef lib::shared_ptr<timer> timer_ptr;
79 
80  explicit connection(bool is_server, alog_type & alog, elog_type & elog)
81  : m_output_stream(NULL)
82  , m_reading(false)
83  , m_is_server(is_server)
84  , m_is_secure(false)
85  , m_alog(alog)
86  , m_elog(elog)
87  , m_remote_endpoint("iostream transport")
88  {
89  m_alog.write(log::alevel::devel,"iostream con transport constructor");
90  }
91 
93  ptr get_shared() {
94  return type::shared_from_this();
95  }
96 
98 
104  void register_ostream(std::ostream * o) {
105  // TODO: lock transport state?
106  scoped_lock_type lock(m_read_mutex);
107  m_output_stream = o;
108  }
109 
111 
121  void set_uri(uri_ptr) {}
122 
124 
142  friend std::istream & operator>> (std::istream & in, type & t) {
143  // this serializes calls to external read.
144  scoped_lock_type lock(t.m_read_mutex);
145 
146  t.read(in);
147 
148  return in;
149  }
150 
152 
165  size_t read_some(char const * buf, size_t len) {
166  // this serializes calls to external read.
167  scoped_lock_type lock(m_read_mutex);
168 
169  return this->read_some_impl(buf,len);
170  }
171 
173 
188  size_t read_all(char const * buf, size_t len) {
189  // this serializes calls to external read.
190  scoped_lock_type lock(m_read_mutex);
191 
192  size_t total_read = 0;
193  size_t temp_read = 0;
194 
195  do {
196  temp_read = this->read_some_impl(buf+total_read,len-total_read);
197  total_read += temp_read;
198  } while (temp_read != 0 && total_read < len);
199 
200  return total_read;
201  }
202 
204 
208  size_t readsome(char const * buf, size_t len) {
209  return this->read_some(buf,len);
210  }
211 
213 
219  void eof() {
220  // this serializes calls to external read.
221  scoped_lock_type lock(m_read_mutex);
222 
223  if (m_reading) {
224  complete_read(make_error_code(transport::error::eof));
225  }
226  }
227 
229 
235  void fatal_error() {
236  // this serializes calls to external read.
237  scoped_lock_type lock(m_read_mutex);
238 
239  if (m_reading) {
241  }
242  }
243 
245 
257  void set_secure(bool value) {
258  m_is_secure = value;
259  }
260 
262 
271  bool is_secure() const {
272  return m_is_secure;
273  }
274 
276 
289  void set_remote_endpoint(std::string value) {
290  m_remote_endpoint = value;
291  }
292 
294 
305  std::string get_remote_endpoint() const {
306  return m_remote_endpoint;
307  }
308 
310 
314  return m_connection_hdl;
315  }
316 
318 
327  timer_ptr set_timer(long, timer_handler) {
328  return timer_ptr();
329  }
330 
332 
351  m_write_handler = h;
352  }
353 
355 
381  m_vector_write_handler = h;
382  }
383 
385 
401  m_shutdown_handler = h;
402  }
403 protected:
405 
410  void init(init_handler handler) {
411  m_alog.write(log::alevel::devel,"iostream connection init");
412  handler(lib::error_code());
413  }
414 
416 
439  void async_read_at_least(size_t num_bytes, char *buf, size_t len,
440  read_handler handler)
441  {
442  std::stringstream s;
443  s << "iostream_con async_read_at_least: " << num_bytes;
444  m_alog.write(log::alevel::devel,s.str());
445 
446  if (num_bytes > len) {
447  handler(make_error_code(error::invalid_num_bytes),size_t(0));
448  return;
449  }
450 
451  if (m_reading == true) {
452  handler(make_error_code(error::double_read),size_t(0));
453  return;
454  }
455 
456  if (num_bytes == 0 || len == 0) {
457  handler(lib::error_code(),size_t(0));
458  return;
459  }
460 
461  m_buf = buf;
462  m_len = len;
463  m_bytes_needed = num_bytes;
464  m_read_handler = handler;
465  m_cursor = 0;
466  m_reading = true;
467  }
468 
470 
487  void async_write(char const * buf, size_t len, transport::write_handler
488  handler)
489  {
490  m_alog.write(log::alevel::devel,"iostream_con async_write");
491  // TODO: lock transport state?
492 
493  lib::error_code ec;
494 
495  if (m_output_stream) {
496  m_output_stream->write(buf,len);
497 
498  if (m_output_stream->bad()) {
500  }
501  } else if (m_write_handler) {
502  ec = m_write_handler(m_connection_hdl, buf, len);
503  } else {
505  }
506 
507  handler(ec);
508  }
509 
511 
527  void async_write(std::vector<buffer> const & bufs, transport::write_handler
528  handler)
529  {
530  m_alog.write(log::alevel::devel,"iostream_con async_write buffer list");
531  // TODO: lock transport state?
532 
533  lib::error_code ec;
534 
535  if (m_output_stream) {
536  std::vector<buffer>::const_iterator it;
537  for (it = bufs.begin(); it != bufs.end(); it++) {
538  m_output_stream->write((*it).buf,(*it).len);
539 
540  if (m_output_stream->bad()) {
542  break;
543  }
544  }
545  } else if (m_vector_write_handler) {
546  ec = m_vector_write_handler(m_connection_hdl, bufs);
547  } else if (m_write_handler) {
548  std::vector<buffer>::const_iterator it;
549  for (it = bufs.begin(); it != bufs.end(); it++) {
550  ec = m_write_handler(m_connection_hdl, (*it).buf, (*it).len);
551  if (ec) {break;}
552  }
553 
554  } else {
556  }
557 
558  handler(ec);
559  }
560 
562 
566  m_connection_hdl = hdl;
567  }
568 
570 
580  lib::error_code dispatch(dispatch_handler handler) {
581  handler();
582  return lib::error_code();
583  }
584 
586 
594  lib::error_code ec;
595 
596  if (m_shutdown_handler) {
597  ec = m_shutdown_handler(m_connection_hdl);
598  }
599 
600  handler(ec);
601  }
602 private:
603  void read(std::istream &in) {
604  m_alog.write(log::alevel::devel,"iostream_con read");
605 
606  while (in.good()) {
607  if (!m_reading) {
608  m_elog.write(log::elevel::devel,"write while not reading");
609  break;
610  }
611 
612  in.read(m_buf+m_cursor,static_cast<std::streamsize>(m_len-m_cursor));
613 
614  if (in.gcount() == 0) {
615  m_elog.write(log::elevel::devel,"read zero bytes");
616  break;
617  }
618 
619  m_cursor += static_cast<size_t>(in.gcount());
620 
621  // TODO: error handling
622  if (in.bad()) {
623  m_reading = false;
624  complete_read(make_error_code(error::bad_stream));
625  }
626 
627  if (m_cursor >= m_bytes_needed) {
628  m_reading = false;
629  complete_read(lib::error_code());
630  }
631  }
632  }
633 
634  size_t read_some_impl(char const * buf, size_t len) {
635  m_alog.write(log::alevel::devel,"iostream_con read_some");
636 
637  if (!m_reading) {
638  m_elog.write(log::elevel::devel,"write while not reading");
639  return 0;
640  }
641 
642  size_t bytes_to_copy = (std::min)(len,m_len-m_cursor);
643 
644  std::copy(buf,buf+bytes_to_copy,m_buf+m_cursor);
645 
646  m_cursor += bytes_to_copy;
647 
648  if (m_cursor >= m_bytes_needed) {
649  complete_read(lib::error_code());
650  }
651 
652  return bytes_to_copy;
653  }
654 
656 
671  void complete_read(lib::error_code const & ec) {
672  m_reading = false;
673 
674  read_handler handler = m_read_handler;
675  m_read_handler = read_handler();
676 
677  handler(ec,m_cursor);
678  }
679 
680  // Read space (Protected by m_read_mutex)
681  char * m_buf;
682  size_t m_len;
683  size_t m_bytes_needed;
684  read_handler m_read_handler;
685  size_t m_cursor;
686 
687  // transport resources
688  std::ostream * m_output_stream;
689  connection_hdl m_connection_hdl;
690  write_handler m_write_handler;
691  vector_write_handler m_vector_write_handler;
692  shutdown_handler m_shutdown_handler;
693 
694  bool m_reading;
695  bool const m_is_server;
696  bool m_is_secure;
697  alog_type & m_alog;
698  elog_type & m_elog;
699  std::string m_remote_endpoint;
700 
701  // This lock ensures that only one thread can edit read data for this
702  // connection. This is a very coarse lock that is basically locked all the
703  // time. The nature of the connection is such that it cannot be
704  // parallelized, the locking is here to prevent intra-connection concurrency
705  // in order to allow inter-connection concurrency.
706  mutex_type m_read_mutex;
707 };
708 
709 
710 } // namespace iostream
711 } // namespace transport
712 } // namespace websocketpp
713 
714 #endif // WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
An operation that requires an output stream was attempted before setting one.
Definition: base.hpp:78
connection(bool is_server, alog_type &alog, elog_type &elog)
Definition: connection.hpp:80
void register_ostream(std::ostream *o)
Register a std::ostream with the transport for writing output.
Definition: connection.hpp:104
lib::function< lib::error_code(connection_hdl, std::vector< transport::buffer > const &bufs)> vector_write_handler
The type and signature of the callback used by iostream transport to perform vectored writes...
Definition: base.hpp:57
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
Definition: connection.hpp:400
void set_handle(connection_hdl hdl)
Set Connection Handle.
Definition: connection.hpp:565
void async_write(std::vector< buffer > const &bufs, transport::write_handler handler)
Asyncronous Transport Write (scatter-gather)
Definition: connection.hpp:527
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport&#39;s event system (if present)
Definition: connection.hpp:580
Empty timer class to stub out for timer functionality that iostream transport doesn&#39;t support...
Definition: connection.hpp:55
void set_remote_endpoint(std::string value)
Set human readable remote endpoint address.
Definition: connection.hpp:289
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.
bool is_secure() const
Tests whether or not the underlying transport is secure.
Definition: connection.hpp:271
A fake lock guard implementation that does nothing.
Definition: none.hpp:46
void async_shutdown(transport::shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
Definition: connection.hpp:593
size_t readsome(char const *buf, size_t len)
Manual input supply (DEPRECATED)
Definition: connection.hpp:208
async_read called while another async_read was in progress
Definition: base.hpp:74
void set_vector_write_handler(vector_write_handler h)
Sets the vectored write handler.
Definition: connection.hpp:380
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
size_t read_all(char const *buf, size_t len)
Manual input supply (read all)
Definition: connection.hpp:188
A fake mutex implementation that does nothing.
Definition: none.hpp:39
underlying transport pass through
Definition: connection.hpp:153
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
Definition: connection.hpp:305
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:141
lib::function< lib::error_code(connection_hdl)> shutdown_handler
The type and signature of the callback used by iostream transport to signal a transport shutdown...
Definition: base.hpp:61
void async_write(char const *buf, size_t len, transport::write_handler handler)
Asyncronous Transport Write.
Definition: connection.hpp:487
std::istream & operator>>(std::istream &is, Batches &batch)
Read components from input and add them to components.
Definition: batches.cpp:40
concurrency_type::scoped_lock_type scoped_lock_type
Definition: connection.hpp:75
size_t read_some(char const *buf, size_t len)
Manual input supply (read some)
Definition: connection.hpp:165
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
Definition: connection.hpp:135
async_read_at_least call requested more bytes than buffer can store
Definition: base.hpp:71
config::alog_type alog_type
Type of this transport&#39;s access logging policy.
Definition: connection.hpp:70
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
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
config::elog_type elog_type
Type of this transport&#39;s error logging policy.
Definition: connection.hpp:72
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
concurrency_type::mutex_type mutex_type
Definition: connection.hpp:76
Stub concurrency policy that implements the interface using no-ops.
Definition: none.hpp:60
connection< config > type
Type of this connection transport component.
Definition: connection.hpp:63
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:351
ptr get_shared()
Get a shared pointer to this component.
Definition: connection.hpp:93
void set_write_handler(write_handler h)
Sets the write handler.
Definition: connection.hpp:350
void init(init_handler handler)
Initialize the connection transport.
Definition: connection.hpp:410
connection_hdl get_handle() const
Get the connection handle.
Definition: connection.hpp:313
void set_secure(bool value)
Set whether or not this connection is secure.
Definition: connection.hpp:257
timer_ptr set_timer(long, timer_handler)
Call back a function after a period of time.
Definition: connection.hpp:327
lib::error_code make_error_code(error::value e)
Definition: connection.hpp:224
void fatal_error()
Signal transport error.
Definition: connection.hpp:235
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:439
config::concurrency_type concurrency_type
transport concurrency policy
Definition: connection.hpp:68
Stub logger that ignores all input.
Definition: stub.hpp:41
lib::function< lib::error_code(connection_hdl, char const *, size_t)> write_handler
The type and signature of the callback used by iostream transport to write.
Definition: base.hpp:48
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
Definition: connection.hpp:65