Debian Codegraph @

cpp/src/connection.cpp

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "proton_bits.hpp"
23
24 #include "proton/codec/map.hpp"
25 #include "proton/codec/vector.hpp"
26 #include "proton/connection.hpp"
27 #include "proton/connection_options.hpp"
28 #include "proton/container.hpp"
29 #include "proton/error.hpp"
30 #include "proton/receiver_options.hpp"
31 #include "proton/sender_options.hpp"
32 #include "proton/session.hpp"
33 #include "proton/session_options.hpp"
34 #include "proton/transport.hpp"
35 #include "proton/work_queue.hpp"
36
37 #include "contexts.hpp"
38 #include "msg.hpp"
39 #include "proton_bits.hpp"
40
41 #include <proton/connection.h>
42 #include <proton/session.h>
43 #include <proton/transport.h>
44 #include <proton/object.h>
45 #include <proton/proactor.h>
46
47 namespace proton {
48
49 connection::~connection() = default;
50
51 transport connection::transport() const {
52 return make_wrapper(pn_connection_transport(pn_object()));
53 }
54
55 void connection::open() {
56 open(connection_options());
57 }
58
59 void connection::open(const connection_options &opts) {
60 opts.apply_unbound(*this);
61 pn_connection_open(pn_object());
62 }
63
64 void connection::close() {
65 pn_connection_close(pn_object());
66 reconnect_context* rctx = connection_context::get(pn_object()).reconnect_context_.get();
67 if (rctx) rctx->stop_reconnect_ = true;
68 }
69
70 std::string connection::virtual_host() const {
71 return str(pn_connection_remote_hostname(pn_object()));
72 }
73
74 std::string connection::container_id() const {
75 return str(pn_connection_remote_container(pn_object()));
76 }
77
78 std::string connection::user() const {
79 return str(pn_transport_get_user(pn_connection_transport(pn_object())));
80 }
81
82 container& connection::container() const {
83 class container* c = connection_context::get(pn_object()).container;
84 if (!c) throw proton::error("No container");
85 return *c;
86 }
87
88 work_queue& connection::work_queue() const {
89 return connection_context::get(pn_object()).work_queue_;
90 }
91
92 session_range connection::sessions() const {
93 return session_range(session_iterator(make_wrapper(pn_session_head(pn_object(), 0))));
94 }
95
96 receiver_range connection::receivers() const {
97 pn_link_t *lnk = pn_link_head(pn_object(), 0);
98 while (lnk) {
99 if (pn_link_is_receiver(lnk))
100 break;
101 lnk = pn_link_next(lnk, 0);
102 }
103 return receiver_range(receiver_iterator(make_wrapper<receiver>(lnk)));
104 }
105
106 sender_range connection::senders() const {
107 pn_link_t *lnk = pn_link_head(pn_object(), 0);
108 while (lnk) {
109 if (pn_link_is_sender(lnk))
110 break;
111 lnk = pn_link_next(lnk, 0);
112 }
113 return sender_range(sender_iterator(make_wrapper<sender>(lnk)));
114 }
115
116 session connection::open_session() {
117 return open_session(session_options());
118 }
119
120 session connection::open_session(const session_options &opts) {
121 session s(make_wrapper<session>(pn_session(pn_object())));
122 // TODO: error check, too many sessions, no mem...
123 if (!!s) s.open(opts);
124 return s;
125 }
126
127 session connection::default_session() {
128 connection_context& ctx = connection_context::get(pn_object());
129 if (!ctx.default_session) {
130 // Note we can't use a proton::session here because we don't want to own
131 // a session reference. The connection owns the session, owning it here as well
132 // would create a circular ownership.
133 ctx.default_session = pn_session(pn_object());
134 pn_session_open(ctx.default_session);
135 }
136 return make_wrapper(ctx.default_session);
137 }
138
139 sender connection::open_sender(const std::string &addr) {
140 return open_sender(addr, sender_options());
141 }
142
143 sender connection::open_sender(const std::string &addr, const class sender_options &opts) {
144 return default_session().open_sender(addr, opts);
145 }
146
147 receiver connection::open_receiver(const std::string &addr) {
148 return open_receiver(addr, receiver_options());
149 }
150
151 receiver connection::open_receiver(const std::string &addr, const class receiver_options &opts)
152 {
153 return default_session().open_receiver(addr, opts);
154 }
155
156 class sender_options connection::sender_options() const {
157 connection_context& ctx = connection_context::get(pn_object());
158 return ctx.container ?
159 ctx.container->sender_options() :
160 proton::sender_options();
161 }
162
163 class receiver_options connection::receiver_options() const {
164 connection_context& ctx = connection_context::get(pn_object());
165 return ctx.container ?
166 ctx.container->receiver_options() :
167 proton::receiver_options();
168 }
169
170 error_condition connection::error() const {
171 return make_wrapper(pn_connection_remote_condition(pn_object()));
172 }
173
174 uint32_t connection::max_frame_size() const {
175 return pn_transport_get_remote_max_frame(pn_connection_transport(pn_object()));
176 }
177
178 uint16_t connection::max_sessions() const {
179 return pn_transport_remote_channel_max(pn_connection_transport(pn_object()));
180 }
181
182 uint32_t connection::idle_timeout() const {
183 return pn_transport_get_remote_idle_timeout(pn_connection_transport(pn_object()));
184 }
185
186 void connection::wake() const {
187 pn_connection_wake(pn_object());
188 }
189
190 std::vector<symbol> connection::offered_capabilities() const {
191 value caps(pn_connection_remote_offered_capabilities(pn_object()));
192 return get_multiple<std::vector<symbol> >(caps);
193 }
194
195 std::vector<symbol> connection::desired_capabilities() const {
196 value caps(pn_connection_remote_desired_capabilities(pn_object()));
197 return get_multiple<std::vector<symbol> >(caps);
198 }
199
200 std::map<symbol, value> connection::properties() const {
201 std::map<symbol, value> props_ret;
202 value props(pn_connection_remote_properties(pn_object()));
203 if (!props.empty()) {
204 get(props, props_ret);
205 }
206 return props_ret;
207 }
208
209 bool connection::reconnected() const {
210 connection_context& cc = connection_context::get(pn_object());
211 reconnect_context* rc = cc.reconnect_context_.get();
212 return (rc && rc->reconnected_);
213 }
214
215 void connection::update_options(const connection_options& options) {
216 connection_context& cc = connection_context::get(pn_object());
217 cc.connection_options_->update(options);
218 }
219
220 } // namespace proton