| 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/delivery.hpp" |
| 23 |
#include "proton/error.hpp" |
| 24 |
#include "proton/link.hpp" |
| 25 |
#include "proton/message.hpp" |
| 26 |
#include "proton/message_id.hpp" |
| 27 |
#include "proton/receiver.hpp" |
| 28 |
#include "proton/sender.hpp" |
| 29 |
#include "proton/timestamp.hpp" |
| 30 |
|
| 31 |
#include "msg.hpp" |
| 32 |
#include "proton_bits.hpp" |
| 33 |
#include "types_internal.hpp" |
| 34 |
|
| 35 |
#include "core/message-internal.h" |
| 36 |
#include "proton/delivery.h" |
| 37 |
|
| 38 |
#include <string> |
| 39 |
#include <algorithm> |
| 40 |
#include <assert.h> |
| 41 |
|
| 42 |
namespace proton { |
| 43 |
|
| 44 |
struct message::impl { |
| 45 |
value body; |
| 46 |
property_map properties; |
| 47 |
annotation_map annotations; |
| 48 |
annotation_map instructions; |
| 49 |
|
| 50 |
impl(pn_message_t *msg) { |
| 51 |
body.reset(pn_message_body(msg)); |
| 52 |
properties.reset(pn_message_properties(msg)); |
| 53 |
annotations.reset(pn_message_annotations(msg)); |
| 54 |
instructions.reset(pn_message_instructions(msg)); |
| 55 |
} |
| 56 |
|
| 57 |
void clear() { |
| 58 |
properties.clear(); |
| 59 |
annotations.clear(); |
| 60 |
instructions.clear(); |
| 61 |
} |
| 62 |
|
| 63 |
// Encode cached maps to the pn_data_t, always used an empty() value for an empty map |
| 64 |
void flush() { |
| 65 |
if (!properties.empty()) properties.value(); |
| 66 |
if (!annotations.empty()) annotations.value(); |
| 67 |
if (!instructions.empty()) instructions.value(); |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
message::message() : pn_msg_(0) {} |
| 72 |
message::message(const message &m) : pn_msg_(0) { *this = m; } |
| 73 |
message::message(message &&m) : pn_msg_(0) { swap(*this, m); } |
| 74 |
message& message::operator=(message&& m) { |
| 75 |
swap(*this, m); |
| 76 |
return *this; |
| 77 |
} |
| 78 |
|
| 79 |
message::message(const value& x) : pn_msg_(0) { body() = x; } |
| 80 |
|
| 81 |
message::~message() { |
| 82 |
if (pn_msg_) { |
| 83 |
impl().~impl(); // destroy in-place |
| 84 |
pn_message_free(pn_msg_); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void swap(message& x, message& y) { |
| 89 |
std::swap(x.pn_msg_, y.pn_msg_); |
| 90 |
} |
| 91 |
|
| 92 |
pn_message_t *message::pn_msg() const { |
| 93 |
if (!pn_msg_) { |
| 94 |
pn_msg_ = pni_message_with_extra(sizeof(struct message::impl)); |
| 95 |
// Construct impl in extra storage allocated with pn_msg_ |
| 96 |
new (pni_message_get_extra(pn_msg_)) struct message::impl(pn_msg_); |
| 97 |
} |
| 98 |
return pn_msg_; |
| 99 |
} |
| 100 |
|
| 101 |
struct message::impl& message::impl() const { |
| 102 |
return *(struct message::impl*)pni_message_get_extra(pn_msg()); |
| 103 |
} |
| 104 |
|
| 105 |
message& message::operator=(const message& m) { |
| 106 |
if (&m != this) { |
| 107 |
// TODO aconway 2015-08-10: more efficient pn_message_copy function |
| 108 |
std::vector<char> data; |
| 109 |
m.encode(data); |
| 110 |
decode(data); |
| 111 |
} |
| 112 |
return *this; |
| 113 |
} |
| 114 |
|
| 115 |
void message::clear() { |
| 116 |
if (pn_msg_) { |
| 117 |
impl().clear(); |
| 118 |
pn_message_clear(pn_msg_); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
namespace { |
| 123 |
void check(int err) { |
| 124 |
if (err) throw error(error_str(err)); |
| 125 |
} |
| 126 |
} // namespace |
| 127 |
|
| 128 |
void message::id(const message_id& id) { pn_message_set_id(pn_msg(), id.atom_); } |
| 129 |
|
| 130 |
message_id message::id() const { |
| 131 |
return pn_message_get_id(pn_msg()); |
| 132 |
} |
| 133 |
|
| 134 |
void message::user(const std::string &id) { |
| 135 |
check(pn_message_set_user_id(pn_msg(), pn_bytes(id))); |
| 136 |
} |
| 137 |
|
| 138 |
std::string message::user() const { |
| 139 |
return str(pn_message_get_user_id(pn_msg())); |
| 140 |
} |
| 141 |
|
| 142 |
void message::to(const std::string &addr) { |
| 143 |
check(pn_message_set_address(pn_msg(), addr.c_str())); |
| 144 |
} |
| 145 |
|
| 146 |
std::string message::to() const { |
| 147 |
const char* addr = pn_message_get_address(pn_msg()); |
| 148 |
return addr ? std::string(addr) : std::string(); |
| 149 |
} |
| 150 |
|
| 151 |
void message::address(const std::string &addr) { |
| 152 |
check(pn_message_set_address(pn_msg(), addr.c_str())); |
| 153 |
} |
| 154 |
|
| 155 |
std::string message::address() const { |
| 156 |
const char* addr = pn_message_get_address(pn_msg()); |
| 157 |
return addr ? std::string(addr) : std::string(); |
| 158 |
} |
| 159 |
|
| 160 |
void message::subject(const std::string &s) { |
| 161 |
check(pn_message_set_subject(pn_msg(), s.c_str())); |
| 162 |
} |
| 163 |
|
| 164 |
std::string message::subject() const { |
| 165 |
const char* s = pn_message_get_subject(pn_msg()); |
| 166 |
return s ? std::string(s) : std::string(); |
| 167 |
} |
| 168 |
|
| 169 |
void message::reply_to(const std::string &s) { |
| 170 |
check(pn_message_set_reply_to(pn_msg(), s.c_str())); |
| 171 |
} |
| 172 |
|
| 173 |
std::string message::reply_to() const { |
| 174 |
const char* s = pn_message_get_reply_to(pn_msg()); |
| 175 |
return s ? std::string(s) : std::string(); |
| 176 |
} |
| 177 |
|
| 178 |
void message::correlation_id(const message_id& id) { |
| 179 |
pn_message_set_correlation_id(pn_msg(), id.atom_); |
| 180 |
} |
| 181 |
|
| 182 |
message_id message::correlation_id() const { |
| 183 |
return pn_message_get_correlation_id(pn_msg()); |
| 184 |
} |
| 185 |
|
| 186 |
void message::content_type(const std::string &s) { |
| 187 |
check(pn_message_set_content_type(pn_msg(), s.c_str())); |
| 188 |
} |
| 189 |
|
| 190 |
std::string message::content_type() const { |
| 191 |
const char* s = pn_message_get_content_type(pn_msg()); |
| 192 |
return s ? std::string(s) : std::string(); |
| 193 |
} |
| 194 |
|
| 195 |
void message::content_encoding(const std::string &s) { |
| 196 |
check(pn_message_set_content_encoding(pn_msg(), s.c_str())); |
| 197 |
} |
| 198 |
|
| 199 |
std::string message::content_encoding() const { |
| 200 |
const char* s = pn_message_get_content_encoding(pn_msg()); |
| 201 |
return s ? std::string(s) : std::string(); |
| 202 |
} |
| 203 |
|
| 204 |
void message::expiry_time(timestamp t) { |
| 205 |
pn_message_set_expiry_time(pn_msg(), t.milliseconds()); |
| 206 |
} |
| 207 |
timestamp message::expiry_time() const { |
| 208 |
return timestamp(pn_message_get_expiry_time(pn_msg())); |
| 209 |
} |
| 210 |
|
| 211 |
void message::creation_time(timestamp t) { |
| 212 |
pn_message_set_creation_time(pn_msg(), t.milliseconds()); |
| 213 |
} |
| 214 |
timestamp message::creation_time() const { |
| 215 |
return timestamp(pn_message_get_creation_time(pn_msg())); |
| 216 |
} |
| 217 |
|
| 218 |
void message::group_id(const std::string &s) { |
| 219 |
check(pn_message_set_group_id(pn_msg(), s.c_str())); |
| 220 |
} |
| 221 |
|
| 222 |
std::string message::group_id() const { |
| 223 |
const char* s = pn_message_get_group_id(pn_msg()); |
| 224 |
return s ? std::string(s) : std::string(); |
| 225 |
} |
| 226 |
|
| 227 |
void message::reply_to_group_id(const std::string &s) { |
| 228 |
check(pn_message_set_reply_to_group_id(pn_msg(), s.c_str())); |
| 229 |
} |
| 230 |
|
| 231 |
std::string message::reply_to_group_id() const { |
| 232 |
const char* s = pn_message_get_reply_to_group_id(pn_msg()); |
| 233 |
return s ? std::string(s) : std::string(); |
| 234 |
} |
| 235 |
|
| 236 |
bool message::inferred() const { return pn_message_is_inferred(pn_msg()); } |
| 237 |
|
| 238 |
void message::inferred(bool b) { pn_message_set_inferred(pn_msg(), b); } |
| 239 |
|
| 240 |
void message::body(const value& x) { body() = x; } |
| 241 |
|
| 242 |
const value& message::body() const { return impl().body; } |
| 243 |
value& message::body() { return impl().body; } |
| 244 |
|
| 245 |
message::property_map& message::properties() { |
| 246 |
return impl().properties; |
| 247 |
} |
| 248 |
|
| 249 |
const message::property_map& message::properties() const { |
| 250 |
return impl().properties; |
| 251 |
} |
| 252 |
|
| 253 |
message::annotation_map& message::message_annotations() { |
| 254 |
return impl().annotations; |
| 255 |
} |
| 256 |
|
| 257 |
const message::annotation_map& message::message_annotations() const { |
| 258 |
return impl().annotations; |
| 259 |
} |
| 260 |
|
| 261 |
message::annotation_map& message::delivery_annotations() { |
| 262 |
return impl().instructions; |
| 263 |
} |
| 264 |
|
| 265 |
const message::annotation_map& message::delivery_annotations() const { |
| 266 |
return impl().instructions; |
| 267 |
} |
| 268 |
|
| 269 |
void message::encode(std::vector<char> &s) const { |
| 270 |
impl().flush(); |
| 271 |
size_t sz = std::max(s.capacity(), size_t(512)); |
| 272 |
while (true) { |
| 273 |
s.resize(sz); |
| 274 |
assert(!s.empty()); |
| 275 |
int err = pn_message_encode(pn_msg(), const_cast<char*>(&s[0]), &sz); |
| 276 |
if (err) { |
| 277 |
if (err != PN_OVERFLOW) |
| 278 |
check(err); |
| 279 |
} else { |
| 280 |
s.resize(sz); |
| 281 |
return; |
| 282 |
} |
| 283 |
sz *= 2; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
std::vector<char> message::encode() const { |
| 288 |
std::vector<char> data; |
| 289 |
encode(data); |
| 290 |
return data; |
| 291 |
} |
| 292 |
|
| 293 |
void message::decode(const std::vector<char> &s) { |
| 294 |
if (s.empty()) |
| 295 |
throw error("message decode: no data"); |
| 296 |
impl().clear(); |
| 297 |
check(pn_message_decode(pn_msg(), &s[0], s.size())); |
| 298 |
} |
| 299 |
|
| 300 |
bool message::durable() const { return pn_message_is_durable(pn_msg()); } |
| 301 |
void message::durable(bool b) { pn_message_set_durable(pn_msg(), b); } |
| 302 |
|
| 303 |
duration message::ttl() const { return duration(pn_message_get_ttl(pn_msg())); } |
| 304 |
void message::ttl(duration d) { pn_message_set_ttl(pn_msg(), d.milliseconds()); } |
| 305 |
|
| 306 |
uint8_t message::priority() const { return pn_message_get_priority(pn_msg()); } |
| 307 |
void message::priority(uint8_t d) { pn_message_set_priority(pn_msg(), d); } |
| 308 |
|
| 309 |
bool message::first_acquirer() const { return pn_message_is_first_acquirer(pn_msg()); } |
| 310 |
void message::first_acquirer(bool b) { pn_message_set_first_acquirer(pn_msg(), b); } |
| 311 |
|
| 312 |
uint32_t message::delivery_count() const { return pn_message_get_delivery_count(pn_msg()); } |
| 313 |
void message::delivery_count(uint32_t d) { pn_message_set_delivery_count(pn_msg(), d); } |
| 314 |
|
| 315 |
int32_t message::group_sequence() const { return pn_message_get_group_sequence(pn_msg()); } |
| 316 |
void message::group_sequence(int32_t d) { pn_message_set_group_sequence(pn_msg(), d); } |
| 317 |
|
| 318 |
const uint8_t message::default_priority = PN_DEFAULT_PRIORITY; |
| 319 |
|
| 320 |
std::ostream& operator<<(std::ostream& o, const message& m) { |
| 321 |
return o << inspectable(m.pn_msg()); |
| 322 |
} |
| 323 |
|
| 324 |
std::string to_string(const message& m) { |
| 325 |
std::ostringstream os; |
| 326 |
os << m; |
| 327 |
return os.str(); |
| 328 |
} |
| 329 |
|
| 330 |
} |