NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
pool.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_MESSAGE_BUFFER_ALLOC_HPP
29 #define WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP
30 
32 
33 #include <string>
34 
35 namespace websocketpp {
36 namespace message_buffer {
37 
38 /* # message:
39  * object that stores a message while it is being sent or received. Contains
40  * the message payload itself, the message header, the extension data, and the
41  * opcode.
42  *
43  * # connection_message_manager:
44  * An object that manages all of the message_buffers associated with a given
45  * connection. Implements the get_message_buffer(size) method that returns
46  * a message buffer at least size bytes long.
47  *
48  * Message buffers are reference counted with shared ownership semantics. Once
49  * requested from the manager the requester and it's associated downstream code
50  * may keep a pointer to the message indefinitely at a cost of extra resource
51  * usage. Once the reference count drops to the point where the manager is the
52  * only reference the messages is recycled using whatever method is implemented
53  * in the manager.
54  *
55  * # endpoint_message_manager:
56  * An object that manages connection_message_managers. Implements the
57  * get_message_manager() method. This is used once by each connection to
58  * request the message manager that they are supposed to use to manage message
59  * buffers for their own use.
60  *
61  * TYPES OF CONNECTION_MESSAGE_MANAGERS
62  * - allocate a message with the exact size every time one is requested
63  * - maintain a pool of pre-allocated messages and return one when needed.
64  * Recycle previously used messages back into the pool
65  *
66  * TYPES OF ENDPOINT_MESSAGE_MANAGERS
67  * - allocate a new connection manager for each connection. Message pools
68  * become connection specific. This increases memory usage but improves
69  * concurrency.
70  * - allocate a single connection manager and share a pointer to it with all
71  * connections created by this endpoint. The message pool will be shared
72  * among all connections, improving memory usage and performance at the cost
73  * of reduced concurrency
74  */
75 
77 
83 template <typename T>
84 void message_deleter(T* msg) {
85  try {
86  if (!msg->recycle()) {
87  delete msg;
88  }
89  } catch (...) {
90  // TODO: is there a better way to ensure this function doesn't throw?
91  delete msg;
92  }
93 }
94 
96 
100 template <typename con_msg_manager>
101 class message {
102 public:
103  typedef lib::shared_ptr<message> ptr;
104 
105  typedef typename con_msg_manager::weak_ptr con_msg_man_ptr;
106 
107  message(con_msg_man_ptr manager, size_t size = 128)
108  : m_manager(manager)
109  , m_payload(size) {}
110 
112  return m_opcode;
113  }
114  const std::string& get_header() const {
115  return m_header;
116  }
117  const std::string& get_extension_data() const {
118  return m_extension_data;
119  }
120  const std::string& get_payload() const {
121  return m_payload;
122  }
123 
125 
137  bool recycle() {
138  typename con_msg_manager::ptr shared = m_manager.lock();
139 
140  if (shared) {
141  return shared->(recycle(this));
142  } else {
143  return false;
144  }
145  }
146 private:
147  con_msg_man_ptr m_manager;
148 
149  frame::opcode::value m_opcode;
150  std::string m_header;
151  std::string m_extension_data;
152  std::string m_payload;
153 };
154 
155 namespace alloc {
156 
159 template <typename message>
160 class con_msg_manager {
161 public:
162  typedef lib::shared_ptr<con_msg_manager> ptr;
163  typedef lib::weak_ptr<con_msg_manager> weak_ptr;
164 
165  typedef typename message::ptr message_ptr;
166 
168 
173  message_ptr get_message(size_t size) const {
174  return lib::make_shared<message>(size);
175  }
176 
178 
187  bool recycle(message * msg) {
188  return false;
189  }
190 };
191 
194 template <typename con_msg_manager>
195 class endpoint_msg_manager {
196 public:
198 
200 
203  con_msg_man_ptr get_manager() const {
204  return lib::make_shared<con_msg_manager>();
205  }
206 };
207 
208 } // namespace alloc
209 
210 namespace pool {
211 
215 
216 };
217 
221 
222 };
223 
224 } // namespace pool
225 
226 } // namespace message_buffer
227 } // namespace websocketpp
228 
229 #endif // WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP
message(con_msg_man_ptr manager, size_t size=128)
Definition: pool.hpp:107
lib::weak_ptr< con_msg_manager > weak_ptr
Definition: pool.hpp:163
An endpoint manager that maintains a shared pool of connection managers and returns an appropriate on...
Definition: pool.hpp:220
void message_deleter(T *msg)
Custom deleter for use in shared_ptrs to message.
Definition: pool.hpp:84
const std::string & get_header() const
Definition: pool.hpp:114
con_msg_manager::weak_ptr con_msg_man_ptr
Definition: pool.hpp:105
lib::shared_ptr< message > ptr
Definition: pool.hpp:103
con_msg_man_ptr get_manager() const
Get a pointer to a connection message manager.
Definition: pool.hpp:203
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
Represents a buffer for a single WebSocket message.
Definition: message.hpp:84
const std::string & get_payload() const
Definition: pool.hpp:120
message_ptr get_message(size_t size) const
Get a message buffer with specified size.
Definition: pool.hpp:173
frame::opcode::value get_opcode() const
Definition: pool.hpp:111
lib::shared_ptr< con_msg_manager > ptr
Definition: pool.hpp:162
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span-lite.hpp:1535
A connection messages manager that maintains a pool of messages that is used to fulfill get_message r...
Definition: pool.hpp:214
bool recycle(message *msg)
Recycle a message.
Definition: pool.hpp:187
An endpoint message manager that allocates a new manager for each connection.
Definition: alloc.hpp:88
bool recycle()
Recycle the message.
Definition: pool.hpp:137
const std::string & get_extension_data() const
Definition: pool.hpp:117