| 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 |
#include "proton/session.hpp" |
| 22 |
|
| 23 |
#include "proton/connection.hpp" |
| 24 |
#include "proton/receiver_options.hpp" |
| 25 |
#include "proton/sender_options.hpp" |
| 26 |
#include "proton/session_options.hpp" |
| 27 |
|
| 28 |
#include "contexts.hpp" |
| 29 |
#include "link_namer.hpp" |
| 30 |
#include "proton_bits.hpp" |
| 31 |
|
| 32 |
#include <proton/connection.h> |
| 33 |
#include <proton/session.h> |
| 34 |
|
| 35 |
#include <string> |
| 36 |
|
| 37 |
namespace proton { |
| 38 |
|
| 39 |
session::~session() = default; |
| 40 |
|
| 41 |
void session::open() { |
| 42 |
pn_session_open(pn_object()); |
| 43 |
} |
| 44 |
|
| 45 |
void session::open(const session_options &opts) { |
| 46 |
opts.apply(*this); |
| 47 |
pn_session_open(pn_object()); |
| 48 |
} |
| 49 |
|
| 50 |
void session::close() |
| 51 |
{ |
| 52 |
pn_session_close(pn_object()); |
| 53 |
} |
| 54 |
|
| 55 |
container& session::container() const { |
| 56 |
return connection().container(); |
| 57 |
} |
| 58 |
|
| 59 |
work_queue& session::work_queue() const { |
| 60 |
return connection().work_queue(); |
| 61 |
} |
| 62 |
|
| 63 |
connection session::connection() const { |
| 64 |
return make_wrapper(pn_session_connection(pn_object())); |
| 65 |
} |
| 66 |
|
| 67 |
namespace { |
| 68 |
std::string next_link_name(const connection& c) { |
| 69 |
io::link_namer* ln = connection_context::get(unwrap(c)).link_gen; |
| 70 |
|
| 71 |
return ln ? ln->link_name() : uuid::random().str(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
sender session::open_sender(const std::string &addr) { |
| 76 |
return open_sender(addr, sender_options()); |
| 77 |
} |
| 78 |
|
| 79 |
sender session::open_sender(const std::string &addr, const sender_options &so) { |
| 80 |
std::string name = so.get_name() ? *so.get_name() : next_link_name(connection()); |
| 81 |
pn_link_t *lnk = pn_sender(pn_object(), name.c_str()); |
| 82 |
pn_terminus_set_address(pn_link_target(lnk), addr.c_str()); |
| 83 |
sender snd(make_wrapper<sender>(lnk)); |
| 84 |
snd.open(so); |
| 85 |
return snd; |
| 86 |
} |
| 87 |
|
| 88 |
receiver session::open_receiver(const std::string &addr) { |
| 89 |
return open_receiver(addr, receiver_options()); |
| 90 |
} |
| 91 |
|
| 92 |
receiver session::open_receiver(const std::string &addr, const receiver_options &ro) |
| 93 |
{ |
| 94 |
std::string name = ro.get_name() ? *ro.get_name() : next_link_name(connection()); |
| 95 |
pn_link_t *lnk = pn_receiver(pn_object(), name.c_str()); |
| 96 |
pn_terminus_set_address(pn_link_source(lnk), addr.c_str()); |
| 97 |
receiver rcv(make_wrapper<receiver>(lnk)); |
| 98 |
rcv.open(ro); |
| 99 |
return rcv; |
| 100 |
} |
| 101 |
|
| 102 |
error_condition session::error() const { |
| 103 |
return make_wrapper(pn_session_remote_condition(pn_object())); |
| 104 |
} |
| 105 |
|
| 106 |
size_t session::incoming_bytes() const { |
| 107 |
return pn_session_incoming_bytes(pn_object()); |
| 108 |
} |
| 109 |
|
| 110 |
size_t session::outgoing_bytes() const { |
| 111 |
return pn_session_outgoing_bytes(pn_object()); |
| 112 |
} |
| 113 |
|
| 114 |
sender_range session::senders() const { |
| 115 |
pn_link_t *lnk = pn_link_head(pn_session_connection(pn_object()), 0); |
| 116 |
while (lnk) { |
| 117 |
if (pn_link_is_sender(lnk) && pn_link_session(lnk) == pn_object()) |
| 118 |
break; |
| 119 |
lnk = pn_link_next(lnk, 0); |
| 120 |
} |
| 121 |
return sender_range(sender_iterator(make_wrapper<sender>(lnk), pn_object())); |
| 122 |
} |
| 123 |
|
| 124 |
receiver_range session::receivers() const { |
| 125 |
pn_link_t *lnk = pn_link_head(pn_session_connection(pn_object()), 0); |
| 126 |
while (lnk) { |
| 127 |
if (pn_link_is_receiver(lnk) && pn_link_session(lnk) == pn_object()) |
| 128 |
break; |
| 129 |
lnk = pn_link_next(lnk, 0); |
| 130 |
} |
| 131 |
return receiver_range(receiver_iterator(make_wrapper<receiver>(lnk), pn_object())); |
| 132 |
} |
| 133 |
|
| 134 |
session_iterator session_iterator::operator++() { |
| 135 |
obj_ = pn_session_next(unwrap(obj_), 0); |
| 136 |
return *this; |
| 137 |
} |
| 138 |
|
| 139 |
} // namespace proton |