| 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/codec/map.hpp" |
| 23 |
#include "proton/receiver_options.hpp" |
| 24 |
#include "proton/messaging_handler.hpp" |
| 25 |
#include "proton/source_options.hpp" |
| 26 |
#include "proton/target_options.hpp" |
| 27 |
|
| 28 |
#include <proton/link.h> |
| 29 |
|
| 30 |
#include "contexts.hpp" |
| 31 |
#include "proactor_container_impl.hpp" |
| 32 |
#include "messaging_adapter.hpp" |
| 33 |
#include "proton_bits.hpp" |
| 34 |
|
| 35 |
namespace proton { |
| 36 |
|
| 37 |
template <class T> struct option { |
| 38 |
T value; |
| 39 |
bool set; |
| 40 |
|
| 41 |
option() : value(), set(false) {} |
| 42 |
option& operator=(const T& x) { value = x; set = true; return *this; } |
| 43 |
void update(const option<T>& x) { if (x.set) *this = x.value; } |
| 44 |
}; |
| 45 |
|
| 46 |
class receiver_options::impl { |
| 47 |
static link_context& get_context(receiver l) { |
| 48 |
return link_context::get(unwrap(l)); |
| 49 |
} |
| 50 |
|
| 51 |
static void set_delivery_mode(receiver l, proton::delivery_mode mode) { |
| 52 |
switch (mode) { |
| 53 |
case delivery_mode::AT_MOST_ONCE: |
| 54 |
pn_link_set_snd_settle_mode(unwrap(l), PN_SND_SETTLED); |
| 55 |
break; |
| 56 |
case delivery_mode::AT_LEAST_ONCE: |
| 57 |
pn_link_set_snd_settle_mode(unwrap(l), PN_SND_UNSETTLED); |
| 58 |
pn_link_set_rcv_settle_mode(unwrap(l), PN_RCV_FIRST); |
| 59 |
break; |
| 60 |
default: |
| 61 |
break; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public: |
| 66 |
option<messaging_handler*> handler; |
| 67 |
option<proton::delivery_mode> delivery_mode; |
| 68 |
option<bool> auto_accept; |
| 69 |
option<bool> auto_settle; |
| 70 |
option<int> credit_window; |
| 71 |
option<bool> dynamic_address; |
| 72 |
option<source_options> source; |
| 73 |
option<target_options> target; |
| 74 |
option<std::string> name; |
| 75 |
option<std::map<symbol, value> > properties; |
| 76 |
|
| 77 |
|
| 78 |
void apply(receiver& r) { |
| 79 |
if (r.uninitialized()) { |
| 80 |
if (delivery_mode.set) set_delivery_mode(r, delivery_mode.value); |
| 81 |
if (handler.set && handler.value) container::impl::set_handler(r, handler.value); |
| 82 |
if (auto_settle.set) get_context(r).auto_settle = auto_settle.value; |
| 83 |
if (auto_accept.set) get_context(r).auto_accept = auto_accept.value; |
| 84 |
if (credit_window.set) get_context(r).credit_window = credit_window.value; |
| 85 |
|
| 86 |
if (source.set) { |
| 87 |
proton::source local_s(make_wrapper<proton::source>(pn_link_source(unwrap(r)))); |
| 88 |
source.value.apply(local_s); |
| 89 |
} |
| 90 |
if (target.set) { |
| 91 |
proton::target local_t(make_wrapper<proton::target>(pn_link_target(unwrap(r)))); |
| 92 |
target.value.apply(local_t); |
| 93 |
} |
| 94 |
if (properties.set) { |
| 95 |
value(pn_link_properties(unwrap(r))) = properties.value; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void update(const impl& x) { |
| 101 |
handler.update(x.handler); |
| 102 |
delivery_mode.update(x.delivery_mode); |
| 103 |
auto_accept.update(x.auto_accept); |
| 104 |
auto_settle.update(x.auto_settle); |
| 105 |
credit_window.update(x.credit_window); |
| 106 |
dynamic_address.update(x.dynamic_address); |
| 107 |
source.update(x.source); |
| 108 |
target.update(x.target); |
| 109 |
name.update(x.name); |
| 110 |
properties.update(x.properties); |
| 111 |
} |
| 112 |
|
| 113 |
}; |
| 114 |
|
| 115 |
receiver_options::receiver_options() : impl_(new impl()) {} |
| 116 |
receiver_options::receiver_options(const receiver_options& x) : impl_(new impl()) { |
| 117 |
*this = x; |
| 118 |
} |
| 119 |
receiver_options::~receiver_options() = default; |
| 120 |
|
| 121 |
receiver_options& receiver_options::operator=(const receiver_options& x) { |
| 122 |
*impl_ = *x.impl_; |
| 123 |
return *this; |
| 124 |
} |
| 125 |
|
| 126 |
void receiver_options::update(const receiver_options& x) { impl_->update(*x.impl_); } |
| 127 |
|
| 128 |
receiver_options& receiver_options::handler(class messaging_handler &h) { impl_->handler = &h; return *this; } |
| 129 |
receiver_options& receiver_options::delivery_mode(proton::delivery_mode m) {impl_->delivery_mode = m; return *this; } |
| 130 |
receiver_options& receiver_options::auto_accept(bool b) {impl_->auto_accept = b; return *this; } |
| 131 |
receiver_options& receiver_options::credit_window(int w) {impl_->credit_window = w; return *this; } |
| 132 |
receiver_options& receiver_options::source(source_options &s) {impl_->source = s; return *this; } |
| 133 |
receiver_options& receiver_options::target(target_options &s) {impl_->target = s; return *this; } |
| 134 |
receiver_options& receiver_options::name(const std::string &s) {impl_->name = s; return *this; } |
| 135 |
receiver_options& receiver_options::properties(const std::map<symbol, value> &props) { impl_->properties = props; return *this; } |
| 136 |
|
| 137 |
void receiver_options::apply(receiver& r) const { impl_->apply(r); } |
| 138 |
|
| 139 |
const std::string* receiver_options::get_name() const { |
| 140 |
return impl_->name.set ? &impl_->name.value : 0; |
| 141 |
} |
| 142 |
|
| 143 |
// No-op, kept for binary compat but auto_settle is not relevant to receiver only sender. |
| 144 |
receiver_options& receiver_options::auto_settle(bool b) { return *this; } |
| 145 |
|
| 146 |
} // namespace proton |