Qpid Proton C++  0.17.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 <future>
28 #include <mutex>
29 #include <sstream>
30 
31 namespace proton {
32 namespace io {
33 
42 class container_impl_base : public standard_container {
43  public:
44  // Pull in base class functions here so that name search finds all the overloads
45  using standard_container::open_receiver;
46  using standard_container::open_sender;
47 
50  store(client_copts_, opts);
51  }
52 
55  return load(client_copts_);
56  }
57 
60  store(server_copts_, opts);
61  }
62 
65  return load(server_copts_);
66  }
67 
69  void sender_options(const class sender_options & opts) {
70  store(sender_opts_, opts);
71  }
72 
75  return load(sender_opts_);
76  }
77 
79  void receiver_options(const class receiver_options & opts) {
80  store(receiver_opts_, opts);
81  }
82 
85  return load(receiver_opts_);
86  }
87 
89  returned<sender> open_sender(
90  const std::string &url, const class sender_options &opts, const connection_options &copts)
91  {
92  return open_link<sender, class sender_options>(url, opts, copts, &connection::open_sender);
93  }
94 
96  returned<receiver> open_receiver(
97  const std::string &url, const class receiver_options &opts, const connection_options &copts)
98  {
99  return open_link<receiver>(url, opts, copts, &connection::open_receiver);
100  }
101 
102  private:
103  template<class T, class Opts>
104  returned<T> open_link(
105  const std::string &url_str, const Opts& opts, const connection_options& copts,
106  T (connection::*open_fn)(const std::string&, const Opts&))
107  {
108  std::string addr = url(url_str).path();
109  std::shared_ptr<thread_safe<connection> > ts_connection = connect(url_str, copts);
110  std::promise<returned<T> > result_promise;
111  auto do_open = [ts_connection, addr, opts, open_fn, &result_promise]() {
112  try {
113  connection c = ts_connection->unsafe();
114  returned<T> s = make_thread_safe((c.*open_fn)(addr, opts));
115  result_promise.set_value(s);
116  } catch (...) {
117  result_promise.set_exception(std::current_exception());
118  }
119  };
120  ts_connection->event_loop()->inject(do_open);
121  std::future<returned<T> > result_future = result_promise.get_future();
122  if (!result_future.valid())
123  throw error(url_str+": connection closed");
124  return result_future.get();
125  }
126 
127  mutable std::mutex lock_;
128  template <class T> T load(const T& v) const {
129  std::lock_guard<std::mutex> g(lock_);
130  return v;
131  }
132  template <class T> void store(T& v, const T& x) const {
133  std::lock_guard<std::mutex> g(lock_);
134  v = x;
135  }
136  connection_options client_copts_, server_copts_;
137  class receiver_options receiver_opts_;
138  class sender_options sender_opts_;
139 };
140 
141 } // io
142 } // proton
143 
144 #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:54
void client_connection_options(const connection_options &opts)
Definition: container_impl_base.hpp:49
A connection to a remote AMQP peer.
Definition: connection.hpp:40
Options for creating a connection.
Definition: connection_options.hpp:62
connection_options server_connection_options() const
Definition: container_impl_base.hpp:64
A URL parser.
Definition: url.hpp:56
Experimental - A base container implementation.
Definition: container_impl_base.hpp:42
returned< sender > open_sender(const std::string &url, const class sender_options &opts, const connection_options &copts)
Definition: container_impl_base.hpp:89
Options for creating a receiver.
Definition: receiver_options.hpp:52
void sender_options(const class sender_options &opts)
Definition: container_impl_base.hpp:69
sender open_sender(const std::string &addr)
Open a sender for addr on default_session().
void receiver_options(const class receiver_options &opts)
Definition: container_impl_base.hpp:79
connection_options client_connection_options() const
Definition: container_impl_base.hpp:54
returned< receiver > open_receiver(const std::string &url, const class receiver_options &opts, const connection_options &copts)
Definition: container_impl_base.hpp:96
void server_connection_options(const connection_options &opts)
Definition: container_impl_base.hpp:59
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:160
The main Proton namespace.
Definition: annotation_key.hpp:30