NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
client_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_CLIENT_ENDPOINT_HPP
29 #define WEBSOCKETPP_CLIENT_ENDPOINT_HPP
30 
31 #include <websocketpp/endpoint.hpp>
32 #include <websocketpp/uri.hpp>
33 
35 
37 
38 #include <string>
39 
40 namespace websocketpp {
41 
43 
46 template <typename config>
47 class client : public endpoint<connection<config>,config> {
48 public:
51 
56 
61 
63  typedef typename transport_type::transport_con_type transport_con_type;
65  typedef typename transport_con_type::ptr transport_con_ptr;
66 
69 
70  friend class connection<config>;
71 
72  explicit client() : endpoint_type(false)
73  {
74  endpoint_type::m_alog->write(log::alevel::devel, "client constructor");
75  }
76 
78 
89  connection_ptr get_connection(uri_ptr location, lib::error_code & ec) {
90  if (location->get_secure() && !transport_type::is_secure()) {
92  return connection_ptr();
93  }
94 
95  connection_ptr con = endpoint_type::create_connection();
96 
97  if (!con) {
99  return con;
100  }
101 
102  con->set_uri(location);
103 
104  ec = lib::error_code();
105  return con;
106  }
107 
109 
119  connection_ptr get_connection(std::string const & u, lib::error_code & ec) {
120  uri_ptr location = lib::make_shared<uri>(u);
121 
122  if (!location->get_valid()) {
124  return connection_ptr();
125  }
126 
127  return get_connection(location, ec);
128  }
129 
131 
139  connection_ptr connect(connection_ptr con) {
140  // Ask transport to perform a connection
141  transport_type::async_connect(
142  lib::static_pointer_cast<transport_con_type>(con),
143  con->get_uri(),
144  lib::bind(
145  &type::handle_connect,
146  this,
147  con,
148  lib::placeholders::_1
149  )
150  );
151 
152  return con;
153  }
154 private:
155  // handle_connect
156  void handle_connect(connection_ptr con, lib::error_code const & ec) {
157  if (ec) {
158  con->terminate(ec);
159 
161  "handle_connect error: "+ec.message());
162  } else {
164  "Successful connection");
165 
166  con->start();
167  }
168  }
169 };
170 
171 } // namespace websocketpp
172 
173 #endif //WEBSOCKETPP_CLIENT_ENDPOINT_HPP
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
Asio based endpoint transport component.
Definition: base.hpp:143
lib::error_code make_error_code(error::value e)
Definition: error.hpp:235
connection_ptr get_connection(std::string const &u, lib::error_code &ec)
Get a new connection (string version)
endpoint< connection_type, config > endpoint_type
Type of the endpoint component of this server.
Represents an individual WebSocket connection.
Definition: connection.hpp:235
Client endpoint role based on the given config.
lib::shared_ptr< alog_type > m_alog
Definition: endpoint.hpp:664
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the connection transport component.
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:141
transport_type::transport_con_type transport_con_type
Type of the connection transport component.
lib::shared_ptr< elog_type > m_elog
Definition: endpoint.hpp:665
client< config > type
Type of this endpoint.
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
config::concurrency_type concurrency_type
Type of the endpoint concurrency component.
Stub concurrency policy that implements the interface using no-ops.
Definition: none.hpp:60
Creates and manages connections associated with a WebSocket endpoint.
Definition: endpoint.hpp:42
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:352
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection.
Definition: connection.hpp:243
Attempted to open a secure connection with an insecure endpoint.
Definition: error.hpp:57
connection< config > connection_type
Type of the connections this server will create.
config::transport_type transport_type
Type of the endpoint transport component.
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
static level const rerror
Recoverable error.
Definition: levels.hpp:75
An invalid uri was supplied.
Definition: error.hpp:65
static level const connect
Information about new connections.
Definition: levels.hpp:121
connection_ptr create_connection()
Connection creation attempted failed.
Definition: error.hpp:99