Qpid Proton C++  0.14.0
container_impl_base.hpp
1 #ifndef PROTON_IO_CONTAINER_IMPL_BASE_HPP
2 #define PROTON_IO_CONTAINER_IMPL_BASE_HPP
3 
4 /*
5  *
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  *
23  */
24 
25 #include "../container.hpp"
26 
27 #include <mutex>
28 #include <sstream>
29 
30 namespace proton {
31 namespace io {
32 
41 class container_impl_base : public standard_container {
42  public:
43  // Pull in base class functions here so that name search finds all the overloads
44  using standard_container::open_receiver;
45  using standard_container::open_sender;
46 
49  store(client_copts_, opts);
50  }
51 
54  return load(client_copts_);
55  }
56 
59  store(server_copts_, opts);
60  }
61 
64  return load(server_copts_);
65  }
66 
68  void sender_options(const class sender_options & opts) {
69  store(sender_opts_, opts);
70  }
71 
74  return load(sender_opts_);
75  }
76 
78  void receiver_options(const class receiver_options & opts) {
79  store(receiver_opts_, opts);
80  }
81 
84  return load(receiver_opts_);
85  }
86 
88  returned<sender> open_sender(
89  const std::string &url, const class sender_options &opts, const connection_options &copts)
90  {
91  return open_link<sender, class sender_options>(url, opts, copts, &connection::open_sender);
92  }
93 
95  returned<receiver> open_receiver(
96  const std::string &url, const class receiver_options &opts, const connection_options &copts)
97  {
98  return open_link<receiver>(url, opts, copts, &connection::open_receiver);
99  }
100 
101  private:
102  template<class T, class Opts>
103  returned<T> open_link(
104  const std::string &url_str, const Opts& opts, const connection_options& copts,
105  T (connection::*open_fn)(const std::string&, const Opts&))
106  {
107  std::string addr = url(url_str).path();
108  std::shared_ptr<thread_safe<connection> > ts_connection = connect(url_str, copts);
109  std::promise<returned<T> > result_promise;
110  auto do_open = [ts_connection, addr, opts, open_fn, &result_promise]() {
111  try {
112  connection c = ts_connection->unsafe();
113  returned<T> s = make_thread_safe((c.*open_fn)(addr, opts));
114  result_promise.set_value(s);
115  } catch (...) {
116  result_promise.set_exception(std::current_exception());
117  }
118  };
119  ts_connection->event_loop()->inject(do_open);
120  std::future<returned<T> > result_future = result_promise.get_future();
121  if (!result_future.valid())
122  throw error(url_str+": connection closed");
123  return result_future.get();
124  }
125 
126  mutable std::mutex lock_;
127  template <class T> T load(const T& v) const {
128  std::lock_guard<std::mutex> g(lock_);
129  return v;
130  }
131  template <class T> void store(T& v, const T& x) const {
132  std::lock_guard<std::mutex> g(lock_);
133  v = x;
134  }
135  connection_options client_copts_, server_copts_;
136  class receiver_options receiver_opts_;
137  class sender_options sender_opts_;
138 };
139 
140 } // io
141 } // proton
142 
143 #endif // PROTON_IO_CONTAINER_IMPL_BASE_HPP
std::string path() const
path is everything after the final "/".
receiver open_receiver(const std::string &addr)
Open a receiver for addr on default_session().
Options for creating a sender.
Definition: sender_options.hpp:64
void client_connection_options(const connection_options &opts)
Connection options that will be to outgoing connections.
Definition: container_impl_base.hpp:48
A connection to a remote AMQP peer.
Definition: connection.hpp:48
Options for creating a connection.
Definition: connection_options.hpp:67
connection_options server_connection_options() const
Connection options that will be applied to incoming connections.
Definition: container_impl_base.hpp:63
A Proton URL.
Definition: url.hpp:55
Experimental - A base container implementation.
Definition: container_impl_base.hpp:41
returned< sender > open_sender(const std::string &url, const class sender_options &opts, const connection_options &copts)
Open a connection and sender for url.
Definition: container_impl_base.hpp:88
Options for creating a receiver.
Definition: receiver_options.hpp:62
void sender_options(const class sender_options &opts)
Sender options applied to senders created by this container.
Definition: container_impl_base.hpp:68
sender open_sender(const std::string &addr)
Open a sender for addr on default_session().
void receiver_options(const class receiver_options &opts)
Receiver options applied to receivers created by this container.
Definition: container_impl_base.hpp:78
connection_options client_connection_options() const
Connection options that will be to outgoing connections.
Definition: container_impl_base.hpp:53
returned< receiver > open_receiver(const std::string &url, const class receiver_options &opts, const connection_options &copts)
Open a connection and receiver for url.
Definition: container_impl_base.hpp:95
void server_connection_options(const connection_options &opts)
Connection options that will be applied to incoming connections.
Definition: container_impl_base.hpp:58
The base Proton error.
Definition: error.hpp:37
returned< T > make_thread_safe(const T &obj)
Make a thread-safe wrapper for obj.
Definition: thread_safe.hpp:165
Type traits for mapping between AMQP and C++ types.
Definition: annotation_key.hpp:28