| 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/map.hpp" |
| 23 |
#include "proton/source.hpp" |
| 24 |
#include "proton/source_options.hpp" |
| 25 |
#include "proton/target.hpp" |
| 26 |
#include "proton/target_options.hpp" |
| 27 |
#include "proton/types.hpp" |
| 28 |
|
| 29 |
#include "proton_bits.hpp" |
| 30 |
|
| 31 |
#include <limits> |
| 32 |
#include <map> |
| 33 |
#include <vector> |
| 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 |
namespace { |
| 47 |
|
| 48 |
void timeout(terminus &t, duration d) { |
| 49 |
uint32_t seconds = 0; |
| 50 |
if (d == duration::FOREVER) |
| 51 |
seconds = std::numeric_limits<uint32_t>::max(); |
| 52 |
else if (d != duration::IMMEDIATE) { |
| 53 |
uint64_t x = d.milliseconds(); |
| 54 |
if ((std::numeric_limits<uint64_t>::max() - x) <= 500) |
| 55 |
seconds = std::numeric_limits<uint32_t>::max(); |
| 56 |
else { |
| 57 |
x = (x + 500) / 1000; |
| 58 |
seconds = x < std::numeric_limits<uint32_t>::max() ? x : std::numeric_limits<uint32_t>::max(); |
| 59 |
} |
| 60 |
} |
| 61 |
pn_terminus_set_timeout(unwrap(t), seconds); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
namespace { |
| 66 |
|
| 67 |
// Options common to sources and targets |
| 68 |
|
| 69 |
void node_address(terminus &t, option<std::string> &addr, option<bool> &dynamic, option<bool> &anonymous) { |
| 70 |
if (dynamic.set && dynamic.value) { |
| 71 |
pn_terminus_set_dynamic(unwrap(t), true); |
| 72 |
pn_terminus_set_address(unwrap(t), NULL); |
| 73 |
} else if (anonymous.set && anonymous.value) { |
| 74 |
pn_terminus_set_address(unwrap(t), NULL); |
| 75 |
} else if (addr.set) { |
| 76 |
pn_terminus_set_address(unwrap(t), addr.value.c_str()); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void node_durability(terminus &t, option<enum terminus::durability_mode> &mode) { |
| 81 |
if (mode.set) pn_terminus_set_durability(unwrap(t), pn_durability_t(mode.value)); |
| 82 |
} |
| 83 |
|
| 84 |
void node_expiry(terminus &t, option<enum terminus::expiry_policy> &policy, option<duration> &d) { |
| 85 |
if (policy.set) pn_terminus_set_expiry_policy(unwrap(t), pn_expiry_policy_t(policy.value)); |
| 86 |
if (d.set) timeout(t, d.value); |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
class source_options::impl { |
| 93 |
public: |
| 94 |
option<std::string> address; |
| 95 |
option<bool> dynamic; |
| 96 |
option<bool> anonymous; |
| 97 |
option<enum source::durability_mode> durability_mode; |
| 98 |
option<duration> timeout; |
| 99 |
option<enum source::expiry_policy> expiry_policy; |
| 100 |
option<enum source::distribution_mode> distribution_mode; |
| 101 |
option<source::filter_map> filters; |
| 102 |
option<std::vector<symbol> > capabilities; |
| 103 |
option<source::dynamic_property_map> dynamic_properties; |
| 104 |
|
| 105 |
void apply(source& s) { |
| 106 |
node_address(s, address, dynamic, anonymous); |
| 107 |
node_durability(s, durability_mode); |
| 108 |
node_expiry(s, expiry_policy, timeout); |
| 109 |
if (distribution_mode.set) |
| 110 |
pn_terminus_set_distribution_mode(unwrap(s), pn_distribution_mode_t(distribution_mode.value)); |
| 111 |
if (filters.set && !filters.value.empty()) { |
| 112 |
// Applied at most once via source_option. No need to clear. |
| 113 |
value(pn_terminus_filter(unwrap(s))) = filters.value; |
| 114 |
} |
| 115 |
if (capabilities.set) { |
| 116 |
value(pn_terminus_capabilities(unwrap(s))) = capabilities.value; |
| 117 |
} |
| 118 |
if (dynamic_properties.set) { |
| 119 |
map<symbol, value> source_map; |
| 120 |
get(dynamic_properties.value, source_map); |
| 121 |
value(pn_terminus_properties(unwrap(s))) = source_map; |
| 122 |
} |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
source_options::source_options() : impl_(new impl()) {} |
| 127 |
source_options::source_options(const source_options& x) : impl_(new impl()) { |
| 128 |
*this = x; |
| 129 |
} |
| 130 |
source_options::~source_options() = default; |
| 131 |
|
| 132 |
source_options& source_options::operator=(const source_options& x) { |
| 133 |
*impl_ = *x.impl_; |
| 134 |
return *this; |
| 135 |
} |
| 136 |
|
| 137 |
source_options& source_options::address(const std::string &addr) { impl_->address = addr; return *this; } |
| 138 |
source_options& source_options::dynamic(bool b) { impl_->dynamic = b; return *this; } |
| 139 |
source_options& source_options::anonymous(bool b) { impl_->anonymous = b; return *this; } |
| 140 |
source_options& source_options::durability_mode(enum source::durability_mode m) { impl_->durability_mode = m; return *this; } |
| 141 |
source_options& source_options::timeout(duration d) { impl_->timeout = d; return *this; } |
| 142 |
source_options& source_options::expiry_policy(enum source::expiry_policy m) { impl_->expiry_policy = m; return *this; } |
| 143 |
source_options& source_options::distribution_mode(enum source::distribution_mode m) { impl_->distribution_mode = m; return *this; } |
| 144 |
source_options& source_options::filters(const source::filter_map &map) { impl_->filters = map; return *this; } |
| 145 |
source_options& source_options::capabilities(const std::vector<symbol>& c) { impl_->capabilities = c; return *this; } |
| 146 |
source_options& source_options::dynamic_properties(const source::dynamic_property_map& c) { |
| 147 |
impl_->dynamic_properties = c; |
| 148 |
return *this; |
| 149 |
} |
| 150 |
|
| 151 |
void source_options::apply(source& s) const { impl_->apply(s); } |
| 152 |
|
| 153 |
// TARGET |
| 154 |
|
| 155 |
class target_options::impl { |
| 156 |
public: |
| 157 |
option<std::string> address; |
| 158 |
option<bool> dynamic; |
| 159 |
option<bool> anonymous; |
| 160 |
option<enum target::durability_mode> durability_mode; |
| 161 |
option<duration> timeout; |
| 162 |
option<enum target::expiry_policy> expiry_policy; |
| 163 |
option<std::vector<symbol> > capabilities; |
| 164 |
option<target::dynamic_property_map> dynamic_properties; |
| 165 |
|
| 166 |
void apply(target& t) { |
| 167 |
node_address(t, address, dynamic, anonymous); |
| 168 |
node_durability(t, durability_mode); |
| 169 |
node_expiry(t, expiry_policy, timeout); |
| 170 |
if (capabilities.set) { |
| 171 |
value(pn_terminus_capabilities(unwrap(t))) = capabilities.value; |
| 172 |
} |
| 173 |
if (dynamic_properties.set) { |
| 174 |
map<symbol, value> target_map; |
| 175 |
get(dynamic_properties.value, target_map); |
| 176 |
value(pn_terminus_properties(unwrap(t))) = target_map; |
| 177 |
} |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
target_options::target_options() : impl_(new impl()) {} |
| 182 |
target_options::target_options(const target_options& x) : impl_(new impl()) { |
| 183 |
*this = x; |
| 184 |
} |
| 185 |
target_options::~target_options() = default; |
| 186 |
|
| 187 |
target_options& target_options::operator=(const target_options& x) { |
| 188 |
*impl_ = *x.impl_; |
| 189 |
return *this; |
| 190 |
} |
| 191 |
|
| 192 |
target_options& target_options::address(const std::string &addr) { impl_->address = addr; return *this; } |
| 193 |
target_options& target_options::dynamic(bool b) { impl_->dynamic = b; return *this; } |
| 194 |
target_options& target_options::anonymous(bool b) { impl_->anonymous = b; return *this; } |
| 195 |
target_options& target_options::durability_mode(enum target::durability_mode m) { impl_->durability_mode = m; return *this; } |
| 196 |
target_options& target_options::timeout(duration d) { impl_->timeout = d; return *this; } |
| 197 |
target_options& target_options::expiry_policy(enum target::expiry_policy m) { impl_->expiry_policy = m; return *this; } |
| 198 |
target_options& target_options::capabilities(const std::vector<symbol>& c) { impl_->capabilities = c; return *this; } |
| 199 |
target_options& target_options::dynamic_properties(const target::dynamic_property_map& c) { |
| 200 |
impl_->dynamic_properties = c; |
| 201 |
return *this; |
| 202 |
} |
| 203 |
|
| 204 |
void target_options::apply(target& s) const { impl_->apply(s); } |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
} // namespace proton |