Qpid Proton C++  0.13.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 "./link_namer.hpp"
26 #include "../container.hpp"
27 
28 #include <mutex>
29 #include <sstream>
30 
31 namespace proton {
32 namespace io {
33 
43  public:
46  store(client_copts_, opts);
47  }
48 
51  return load(client_copts_);
52  }
53 
56  store(server_copts_, opts);
57  }
58 
61  return load(server_copts_);
62  }
63 
65  void sender_options(const class sender_options & opts) {
66  store(sender_opts_, opts);
67  }
68 
71  return load(sender_opts_);
72  }
73 
75  void receiver_options(const class receiver_options & opts) {
76  store(receiver_opts_, opts);
77  }
78 
81  return load(receiver_opts_);
82  }
83 
85  returned<sender> open_sender(
86  const std::string &url, const class sender_options &opts, const connection_options &copts)
87  {
88  return open_link<sender, class sender_options>(url, opts, copts, &connection::open_sender);
89  }
90 
92  returned<receiver> open_receiver(
93  const std::string &url, const class receiver_options &opts, const connection_options &copts)
94  {
95  return open_link<receiver>(url, opts, copts, &connection::open_receiver);
96  }
97 
98  private:
99  template<class T, class Opts>
100  returned<T> open_link(
101  const std::string &url_str, const Opts& opts, const connection_options& copts,
102  T (connection::*open_fn)(const std::string&, const Opts&))
103  {
104  std::string addr = url(url_str).path();
105  std::shared_ptr<thread_safe<connection> > ts_connection = connect(url_str, copts);
106  std::promise<returned<T> > result_promise;
107  auto do_open = [ts_connection, addr, opts, open_fn, &result_promise]() {
108  try {
109  connection c = ts_connection->unsafe();
110  returned<T> s = make_thread_safe((c.*open_fn)(addr, opts));
111  result_promise.set_value(s);
112  } catch (...) {
113  result_promise.set_exception(std::current_exception());
114  }
115  };
116  ts_connection->event_loop()->inject(do_open);
117  std::future<returned<T> > result_future = result_promise.get_future();
118  if (!result_future.valid())
119  throw error(url_str+": connection closed");
120  return result_future.get();
121  }
122 
123  mutable std::mutex lock_;
124  template <class T> T load(const T& v) const {
125  std::lock_guard<std::mutex> g(lock_);
126  return v;
127  }
128  template <class T> void store(T& v, const T& x) const {
129  std::lock_guard<std::mutex> g(lock_);
130  v = x;
131  }
132  connection_options client_copts_, server_copts_;
133  class receiver_options receiver_opts_;
134  class sender_options sender_opts_;
135 };
136 
137 } // io
138 } // proton
139 
140 #endif // PROTON_IO_CONTAINER_IMPL_BASE_HPP
virtual returned< connection > connect(const std::string &url, const connection_options &)=0
Connect to url and send an open request to the remote peer.
std::string path() const
path is everything after the final &quot;/&quot;.
A top-level container of connections, sessions, senders, and receivers.
Definition: container.hpp:59
class receiver_options receiver_options() const
Receiver options applied to receivers created by this container.
Definition: container_impl_base.hpp:80
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:45
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:60
A Proton URL.
Definition: url.hpp:55
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)
Open a connection and sender for url.
Definition: container_impl_base.hpp:85
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:65
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:75
connection_options client_connection_options() const
Connection options that will be to outgoing connections.
Definition: container_impl_base.hpp:50
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:92
void server_connection_options(const connection_options &opts)
Connection options that will be applied to incoming connections.
Definition: container_impl_base.hpp:55
class sender_options sender_options() const
Sender options applied to senders created by this container.
Definition: container_impl_base.hpp:70
returned< T > make_thread_safe(const T &obj)
Make a thread-safe wrapper for obj.
Definition: thread_safe.hpp:162