| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "proton/io/connection_driver.hpp" |
| 21 |
|
| 22 |
#include "proton/connection.hpp" |
| 23 |
#include "proton/container.hpp" |
| 24 |
#include "proton/error.hpp" |
| 25 |
#include "proton/messaging_handler.hpp" |
| 26 |
#include "proton/transport.hpp" |
| 27 |
#include "proton/uuid.hpp" |
| 28 |
#include "proton/work_queue.hpp" |
| 29 |
|
| 30 |
#include "contexts.hpp" |
| 31 |
#include "messaging_adapter.hpp" |
| 32 |
#include "msg.hpp" |
| 33 |
#include "proton_bits.hpp" |
| 34 |
|
| 35 |
#include <proton/connection.h> |
| 36 |
#include <proton/transport.h> |
| 37 |
#include <proton/event.h> |
| 38 |
|
| 39 |
#include <algorithm> |
| 40 |
|
| 41 |
|
| 42 |
namespace proton { |
| 43 |
namespace io { |
| 44 |
|
| 45 |
void connection_driver::init() { |
| 46 |
if (pn_connection_driver_init(&driver_, pn_connection(), pn_transport()) != 0) { |
| 47 |
this->~connection_driver(); // Dtor won't be called on throw from ctor. |
| 48 |
throw proton::error(std::string("connection_driver allocation failed")); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
connection_driver::connection_driver() : handler_(0) { init(); } |
| 53 |
|
| 54 |
connection_driver::connection_driver(const std::string& id) : container_id_(id), handler_(0) { |
| 55 |
init(); |
| 56 |
} |
| 57 |
|
| 58 |
connection_driver::~connection_driver() { |
| 59 |
pn_connection_driver_destroy(&driver_); |
| 60 |
} |
| 61 |
|
| 62 |
void connection_driver::configure(const connection_options& opts, bool server) { |
| 63 |
proton::connection c(connection()); |
| 64 |
opts.apply_unbound(c); |
| 65 |
if (server) { |
| 66 |
pn_transport_set_server(driver_.transport); |
| 67 |
opts.apply_unbound_server(driver_.transport); |
| 68 |
} else { |
| 69 |
opts.apply_unbound_client(driver_.transport); |
| 70 |
} |
| 71 |
pn_connection_driver_bind(&driver_); |
| 72 |
handler_ = opts.handler(); |
| 73 |
} |
| 74 |
|
| 75 |
void connection_driver::connect(const connection_options& opts) { |
| 76 |
connection_options all; |
| 77 |
all.container_id(container_id_); |
| 78 |
all.update(opts); |
| 79 |
configure(all, false); |
| 80 |
connection().open(); |
| 81 |
} |
| 82 |
|
| 83 |
void connection_driver::accept(const connection_options& opts) { |
| 84 |
connection_options all; |
| 85 |
all.container_id(container_id_); |
| 86 |
all.update(opts); |
| 87 |
configure(all, true); |
| 88 |
} |
| 89 |
|
| 90 |
bool connection_driver::has_events() const { |
| 91 |
return driver_.collector && pn_collector_peek(driver_.collector); |
| 92 |
} |
| 93 |
|
| 94 |
bool connection_driver::dispatch() { |
| 95 |
pn_event_t* c_event; |
| 96 |
while ((c_event = pn_connection_driver_next_event(&driver_)) != NULL) { |
| 97 |
try { |
| 98 |
if (handler_ != 0) { |
| 99 |
messaging_adapter::dispatch(*handler_, c_event); |
| 100 |
} |
| 101 |
} catch (const std::exception& e) { |
| 102 |
pn_condition_t *cond = pn_transport_condition(driver_.transport); |
| 103 |
if (!pn_condition_is_set(cond)) { |
| 104 |
pn_condition_format(cond, "exception", "%s", e.what()); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
return !pn_connection_driver_finished(&driver_); |
| 109 |
} |
| 110 |
|
| 111 |
mutable_buffer connection_driver::read_buffer() { |
| 112 |
pn_rwbytes_t buffer = pn_connection_driver_read_buffer(&driver_); |
| 113 |
return mutable_buffer(buffer.start, buffer.size); |
| 114 |
} |
| 115 |
|
| 116 |
void connection_driver::read_done(size_t n) { |
| 117 |
return pn_connection_driver_read_done(&driver_, n); |
| 118 |
} |
| 119 |
|
| 120 |
void connection_driver::read_close() { |
| 121 |
pn_connection_driver_read_close(&driver_); |
| 122 |
} |
| 123 |
|
| 124 |
const_buffer connection_driver::write_buffer() { |
| 125 |
pn_bytes_t buffer = pn_connection_driver_write_buffer(&driver_); |
| 126 |
return const_buffer(buffer.start, buffer.size); |
| 127 |
} |
| 128 |
|
| 129 |
const_buffer connection_driver::write_done(size_t n) { |
| 130 |
pn_bytes_t buffer = pn_connection_driver_write_done(&driver_, n); |
| 131 |
return const_buffer(buffer.start, buffer.size); |
| 132 |
} |
| 133 |
|
| 134 |
void connection_driver::write_close() { |
| 135 |
pn_connection_driver_write_close(&driver_); |
| 136 |
} |
| 137 |
|
| 138 |
timestamp connection_driver::tick(timestamp now) { |
| 139 |
return timestamp(pn_transport_tick(driver_.transport, now.milliseconds())); |
| 140 |
} |
| 141 |
|
| 142 |
void connection_driver::disconnected(const proton::error_condition& err) { |
| 143 |
pn_condition_t* condition = pn_transport_condition(driver_.transport); |
| 144 |
if (!pn_condition_is_set(condition)) { |
| 145 |
set_error_condition(err, condition); |
| 146 |
} |
| 147 |
pn_connection_driver_close(&driver_); |
| 148 |
} |
| 149 |
|
| 150 |
proton::connection connection_driver::connection() const { |
| 151 |
return make_wrapper(driver_.connection); |
| 152 |
} |
| 153 |
|
| 154 |
proton::transport connection_driver::transport() const { |
| 155 |
return make_wrapper(driver_.transport); |
| 156 |
} |
| 157 |
|
| 158 |
}} |