http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/connection.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/connection.cpp b/proton-c/bindings/cpp/src/connection.cpp deleted file mode 100644 index 113932c..0000000 --- a/proton-c/bindings/cpp/src/connection.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "proton_bits.hpp" - -#include "proton/connection.hpp" -#include "proton/container.hpp" -#include "proton/error.hpp" -#include "proton/event_loop.hpp" -#include "proton/receiver_options.hpp" -#include "proton/sender_options.hpp" -#include "proton/session.hpp" -#include "proton/session_options.hpp" -#include "proton/transport.hpp" - -#include "connector.hpp" -#include "contexts.hpp" -#include "msg.hpp" -#include "proton_bits.hpp" - -#include <proton/connection.h> -#include <proton/session.h> -#include <proton/transport.h> -#include <proton/reactor.h> -#include <proton/object.h> - -namespace proton { - -transport connection::transport() const { - return make_wrapper(pn_connection_transport(pn_object())); -} - -void connection::open() { - open(connection_options()); -} - -void connection::open(const connection_options &opts) { - opts.apply_unbound(*this); - pn_connection_open(pn_object()); -} - -void connection::close() { pn_connection_close(pn_object()); } - -std::string connection::virtual_host() const { - return str(pn_connection_remote_hostname(pn_object())); -} - -std::string connection::container_id() const { - return str(pn_connection_get_container(pn_object())); -} - -std::string connection::user() const { - return str(pn_transport_get_user(pn_connection_transport(pn_object()))); -} - -container& connection::container() const { - class container* c = connection_context::get(pn_object()).container; - if (!c) { - pn_reactor_t *r = pn_object_reactor(pn_object()); - if (r) - c = &container_context::get(r); - } - if (!c) - throw proton::error("connection does not have a container"); - return *c; -} - -session_range connection::sessions() const { - return session_range(session_iterator(make_wrapper(pn_session_head(pn_object(), 0)))); -} - -receiver_range connection::receivers() const { - pn_link_t *lnk = pn_link_head(pn_object(), 0); - while (lnk) { - if (pn_link_is_receiver(lnk)) - break; - lnk = pn_link_next(lnk, 0); - } - return receiver_range(receiver_iterator(make_wrapper<receiver>(lnk))); -} - -sender_range connection::senders() const { - pn_link_t *lnk = pn_link_head(pn_object(), 0); - while (lnk) { - if (pn_link_is_sender(lnk)) - break; - lnk = pn_link_next(lnk, 0); - } - return sender_range(sender_iterator(make_wrapper<sender>(lnk))); -} - -session connection::open_session() { - return open_session(session_options()); -} - -session connection::open_session(const session_options &opts) { - session s(make_wrapper<session>(pn_session(pn_object()))); - // TODO: error check, too many sessions, no mem... - if (!!s) s.open(opts); - return s; -} - -session connection::default_session() { - connection_context& ctx = connection_context::get(pn_object()); - if (!ctx.default_session) { - // Note we can't use a proton::session here because we don't want to own - // a session reference. The connection owns the session, owning it here as well - // would create a circular ownership. - ctx.default_session = pn_session(pn_object()); - pn_session_open(ctx.default_session); - } - return make_wrapper(ctx.default_session); -} - -sender connection::open_sender(const std::string &addr) { - return open_sender(addr, sender_options()); -} - -sender connection::open_sender(const std::string &addr, const sender_options &opts) { - return default_session().open_sender(addr, opts); -} - -receiver connection::open_receiver(const std::string &addr) { - return open_receiver(addr, receiver_options()); -} - -receiver connection::open_receiver(const std::string &addr, const receiver_options &opts) -{ - return default_session().open_receiver(addr, opts); -} - -error_condition connection::error() const { - return make_wrapper(pn_connection_remote_condition(pn_object())); -} - -uint32_t connection::max_frame_size() const { - return pn_transport_get_remote_max_frame(pn_connection_transport(pn_object())); -} - -uint16_t connection::max_sessions() const { - return pn_transport_remote_channel_max(pn_connection_transport(pn_object())); -} - -uint32_t connection::idle_timeout() const { - return pn_transport_get_remote_idle_timeout(pn_connection_transport(pn_object())); -} - -}
http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/connection_options.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/connection_options.cpp b/proton-c/bindings/cpp/src/connection_options.cpp deleted file mode 100644 index c30b98d..0000000 --- a/proton-c/bindings/cpp/src/connection_options.cpp +++ /dev/null @@ -1,210 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -#include "proton/connection_options.hpp" -#include "proton/messaging_handler.hpp" -#include "proton/reconnect_timer.hpp" -#include "proton/transport.hpp" -#include "proton/ssl.hpp" -#include "proton/sasl.hpp" - -#include "acceptor.hpp" -#include "contexts.hpp" -#include "connector.hpp" -#include "messaging_adapter.hpp" -#include "msg.hpp" -#include "proton_bits.hpp" - -#include <proton/connection.h> -#include <proton/transport.h> - -namespace proton { - -template <class T> struct option { - T value; - bool set; - - option() : value(), set(false) {} - option& operator=(const T& x) { value = x; set = true; return *this; } - void update(const option<T>& x) { if (x.set) *this = x.value; } -}; - -class connection_options::impl { - public: - option<messaging_handler*> handler; - option<uint32_t> max_frame_size; - option<uint16_t> max_sessions; - option<duration> idle_timeout; - option<std::string> container_id; - option<std::string> virtual_host; - option<std::string> user; - option<std::string> password; - option<reconnect_timer> reconnect; - option<class ssl_client_options> ssl_client_options; - option<class ssl_server_options> ssl_server_options; - option<bool> sasl_enabled; - option<std::string> sasl_allowed_mechs; - option<bool> sasl_allow_insecure_mechs; - option<std::string> sasl_config_name; - option<std::string> sasl_config_path; - - /* - * There are three types of connection options: the handler - * (required at creation, so too late to apply here), open frame - * options (that never change after the original open), and - * transport options (set once per transport over the life of the - * connection). - */ - void apply_unbound(connection& c) { - pn_connection_t *pnc = unwrap(c); - container::impl::connector *outbound = dynamic_cast<container::impl::connector*>( - connection_context::get(c).handler.get()); - - // Only apply connection options if uninit. - bool uninit = c.uninitialized(); - if (!uninit) return; - - if (reconnect.set && outbound) - outbound->reconnect_timer(reconnect.value); - if (container_id.set) - pn_connection_set_container(pnc, container_id.value.c_str()); - if (virtual_host.set) - pn_connection_set_hostname(pnc, virtual_host.value.c_str()); - if (user.set) - pn_connection_set_user(pnc, user.value.c_str()); - if (password.set) - pn_connection_set_password(pnc, password.value.c_str()); - } - - void apply_bound(connection& c) { - // Transport options. pnt is NULL between reconnect attempts - // and if there is a pipelined open frame. - pn_connection_t *pnc = unwrap(c); - container::impl::connector *outbound = dynamic_cast<container::impl::connector*>( - connection_context::get(c).handler.get()); - - pn_transport_t *pnt = pn_connection_transport(pnc); - if (!pnt) return; - - // SSL - if (outbound && outbound->address().scheme() == url::AMQPS) { - // A side effect of pn_ssl() is to set the ssl peer - // hostname to the connection hostname, which has - // already been adjusted for the virtual_host option. - pn_ssl_t *ssl = pn_ssl(pnt); - if (pn_ssl_init(ssl, ssl_client_options.value.pn_domain(), NULL)) - throw error(MSG("client SSL/TLS initialization error")); - } else if (!outbound) { - // TODO aconway 2016-05-13: reactor only - pn_acceptor_t *pnp = pn_connection_acceptor(pnc); - if (pnp) { - listener_context &lc(listener_context::get(pnp)); - if (lc.ssl) { - pn_ssl_t *ssl = pn_ssl(pnt); - if (pn_ssl_init(ssl, ssl_server_options.value.pn_domain(), NULL)) - throw error(MSG("server SSL/TLS initialization error")); - } - } - } - - // SASL - if (!sasl_enabled.set || sasl_enabled.value) { - if (sasl_enabled.set) // Explicitly set, not just default behaviour. - pn_sasl(pnt); // Force a sasl instance. Lazily create one otherwise. - if (sasl_allow_insecure_mechs.set) - pn_sasl_set_allow_insecure_mechs(pn_sasl(pnt), sasl_allow_insecure_mechs.value); - if (sasl_allowed_mechs.set) - pn_sasl_allowed_mechs(pn_sasl(pnt), sasl_allowed_mechs.value.c_str()); - if (sasl_config_name.set) - pn_sasl_config_name(pn_sasl(pnt), sasl_config_name.value.c_str()); - if (sasl_config_path.set) - pn_sasl_config_path(pn_sasl(pnt), sasl_config_path.value.c_str()); - } - - if (max_frame_size.set) - pn_transport_set_max_frame(pnt, max_frame_size.value); - if (max_sessions.set) - pn_transport_set_channel_max(pnt, max_sessions.value); - if (idle_timeout.set) - pn_transport_set_idle_timeout(pnt, idle_timeout.value.milliseconds()); - } - - void update(const impl& x) { - handler.update(x.handler); - max_frame_size.update(x.max_frame_size); - max_sessions.update(x.max_sessions); - idle_timeout.update(x.idle_timeout); - container_id.update(x.container_id); - virtual_host.update(x.virtual_host); - user.update(x.user); - password.update(x.password); - reconnect.update(x.reconnect); - ssl_client_options.update(x.ssl_client_options); - ssl_server_options.update(x.ssl_server_options); - sasl_enabled.update(x.sasl_enabled); - sasl_allow_insecure_mechs.update(x.sasl_allow_insecure_mechs); - sasl_allowed_mechs.update(x.sasl_allowed_mechs); - sasl_config_name.update(x.sasl_config_name); - sasl_config_path.update(x.sasl_config_path); - } - -}; - -connection_options::connection_options() : impl_(new impl()) {} - -connection_options::connection_options(class messaging_handler& h) : impl_(new impl()) { handler(h); } - -connection_options::connection_options(const connection_options& x) : impl_(new impl()) { - *this = x; -} - -connection_options::~connection_options() {} - -connection_options& connection_options::operator=(const connection_options& x) { - *impl_ = *x.impl_; - return *this; -} - -connection_options& connection_options::update(const connection_options& x) { - impl_->update(*x.impl_); - return *this; -} - -connection_options& connection_options::handler(class messaging_handler &h) { impl_->handler = &h; return *this; } -connection_options& connection_options::max_frame_size(uint32_t n) { impl_->max_frame_size = n; return *this; } -connection_options& connection_options::max_sessions(uint16_t n) { impl_->max_sessions = n; return *this; } -connection_options& connection_options::idle_timeout(duration t) { impl_->idle_timeout = t; return *this; } -connection_options& connection_options::container_id(const std::string &id) { impl_->container_id = id; return *this; } -connection_options& connection_options::virtual_host(const std::string &id) { impl_->virtual_host = id; return *this; } -connection_options& connection_options::user(const std::string &user) { impl_->user = user; return *this; } -connection_options& connection_options::password(const std::string &password) { impl_->password = password; return *this; } -connection_options& connection_options::reconnect(const reconnect_timer &rc) { impl_->reconnect = rc; return *this; } -connection_options& connection_options::ssl_client_options(const class ssl_client_options &c) { impl_->ssl_client_options = c; return *this; } -connection_options& connection_options::ssl_server_options(const class ssl_server_options &c) { impl_->ssl_server_options = c; return *this; } -connection_options& connection_options::sasl_enabled(bool b) { impl_->sasl_enabled = b; return *this; } -connection_options& connection_options::sasl_allow_insecure_mechs(bool b) { impl_->sasl_allow_insecure_mechs = b; return *this; } -connection_options& connection_options::sasl_allowed_mechs(const std::string &s) { impl_->sasl_allowed_mechs = s; return *this; } -connection_options& connection_options::sasl_config_name(const std::string &n) { impl_->sasl_config_name = n; return *this; } -connection_options& connection_options::sasl_config_path(const std::string &p) { impl_->sasl_config_path = p; return *this; } - -void connection_options::apply_unbound(connection& c) const { impl_->apply_unbound(c); } -void connection_options::apply_bound(connection& c) const { impl_->apply_bound(c); } -messaging_handler* connection_options::handler() const { return impl_->handler.value; } -} // namespace proton http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/connector.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/connector.cpp b/proton-c/bindings/cpp/src/connector.cpp deleted file mode 100644 index 0467d60..0000000 --- a/proton-c/bindings/cpp/src/connector.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "connector.hpp" - -#include "proton/connection.hpp" -#include "proton/transport.hpp" -#include "proton/container.hpp" -#include "proton/reconnect_timer.hpp" -#include "proton/sasl.hpp" -#include "proton/url.hpp" - -#include "container_impl.hpp" -#include "proton_bits.hpp" -#include "proton_event.hpp" - -#include <proton/connection.h> -#include <proton/transport.h> - -namespace proton { - -container::impl::connector::connector(connection&c, const connection_options& options, const url& a) : - connection_(c), options_(options), address_(a), reconnect_timer_(0) -{} - -container::impl::connector::~connector() { delete reconnect_timer_; } - -void container::impl::connector::reconnect_timer(const class reconnect_timer &rt) { - delete reconnect_timer_; - reconnect_timer_ = new class reconnect_timer(rt); -} - -void container::impl::connector::connect() { - pn_transport_t *pnt = pn_transport(); - transport t(make_wrapper(pnt)); - pn_transport_bind(pnt, unwrap(connection_)); - pn_decref(pnt); - // Apply options to the new transport. - options_.apply_bound(connection_); -} - -void container::impl::connector::on_connection_local_open(proton_event &) { - connect(); -} - -void container::impl::connector::on_connection_remote_open(proton_event &) { - if (reconnect_timer_) { - reconnect_timer_->reset(); - } -} - -void container::impl::connector::on_connection_init(proton_event &) { -} - -void container::impl::connector::on_transport_tail_closed(proton_event &e) { - on_transport_closed(e); -} - -void container::impl::connector::on_transport_closed(proton_event &) { - if (!connection_) return; - if (connection_.active()) { - if (reconnect_timer_) { - pn_transport_unbind(unwrap(connection_.transport())); - int delay = reconnect_timer_->next_delay(timestamp::now()); - if (delay >= 0) { - if (delay == 0) { - // log "Disconnected, reconnecting..." - connect(); - return; - } - else { - // log "Disconnected, reconnecting in " << delay << " milliseconds" - container::impl::schedule(connection_.container(), delay, this); - return; - } - } - } - } - pn_connection_release(unwrap(connection_)); - connection_ = 0; -} - -void container::impl::connector::on_timer_task(proton_event &) { - connect(); -} - -} http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/container.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/container.cpp b/proton-c/bindings/cpp/src/container.cpp deleted file mode 100644 index 3daa925..0000000 --- a/proton-c/bindings/cpp/src/container.cpp +++ /dev/null @@ -1,138 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "proton/container.hpp" -#include "proton/error_condition.hpp" - -#include "proton/error_condition.hpp" -#include "proton/listen_handler.hpp" -#include "proton/listener.hpp" -#include "proton/thread_safe.hpp" - -#include "container_impl.hpp" - -namespace proton { - -container::container(messaging_handler& h, const std::string& id) : - impl_(new impl(*this, id, &h)) {} -container::container(const std::string& id) : - impl_(new impl(*this, id)) {} -container::~container() {} - -returned<connection> container::connect(const std::string &url) { - return connect(url, connection_options()); -} - -returned<sender> container::open_sender(const std::string &url) { - return open_sender(url, proton::sender_options(), connection_options()); -} - -returned<sender> container::open_sender(const std::string &url, const proton::sender_options &lo) { - return open_sender(url, lo, connection_options()); -} - -returned<sender> container::open_sender(const std::string &url, const proton::connection_options &co) { - return open_sender(url, sender_options(), co); -} - -returned<receiver> container::open_receiver(const std::string &url) { - return open_receiver(url, proton::receiver_options(), connection_options()); -} - -returned<receiver> container::open_receiver(const std::string &url, const proton::receiver_options &lo) { - return open_receiver(url, lo, connection_options()); -} - -returned<receiver> container::open_receiver(const std::string &url, const proton::connection_options &co) { - return open_receiver(url, receiver_options(), co); -} - -namespace{ - struct listen_opts : public listen_handler { - connection_options opts; - listen_opts(const connection_options& o) : opts(o) {} - connection_options on_accept() { return opts; } - void on_close() { delete this; } - }; -} - -listener container::listen(const std::string& url, const connection_options& opts) { - // Note: listen_opts::on_close() calls delete(this) so this is not a leak. - // The container will always call on_closed() even if there are errors or exceptions. - listen_opts* lh = new listen_opts(opts); - return listen(url, *lh); -} - -listener container::listen(const std::string &url) { - return listen(url, connection_options()); -} - -void container::stop() { stop(error_condition()); } - -returned<connection> container::connect(const std::string& url, const connection_options& opts) { - return impl_->connect(url, opts); -} - -listener container::listen(const std::string& url, listen_handler& l) { return impl_->listen(url, l); } - -void container::stop_listening(const std::string& url) { impl_->stop_listening(url); } - -void container::run() { impl_->run(); } - -void container::auto_stop(bool set) { impl_->auto_stop(set); } - -void container::stop(const error_condition& err) { impl_->stop(err); } - -returned<sender> container::open_sender( - const std::string &url, - const class sender_options &o, - const connection_options &c) { - return impl_->open_sender(url, o, c); -} - -returned<receiver> container::open_receiver( - const std::string&url, - const class receiver_options &o, - const connection_options &c) { - return impl_->open_receiver(url, o, c); -} - -std::string container::id() const { return impl_->id(); } - -void container::schedule(duration d, void_function0& f) { return impl_->schedule(d, f); } - -#if PN_CPP_HAS_STD_FUNCTION -void container::schedule(duration d, std::function<void()> f) { return impl_->schedule(d, f); } -#endif - -void container::client_connection_options(const connection_options& c) { impl_->client_connection_options(c); } -connection_options container::client_connection_options() const { return impl_->client_connection_options(); } - -void container::server_connection_options(const connection_options &o) { impl_->server_connection_options(o); } -connection_options container::server_connection_options() const { return impl_->server_connection_options(); } - -void container::sender_options(const class sender_options &o) { impl_->sender_options(o); } -class sender_options container::sender_options() const { return impl_->sender_options(); } - -void container::receiver_options(const class receiver_options & o) { impl_->receiver_options(o); } -class receiver_options container::receiver_options() const { return impl_->receiver_options(); } - -} // namespace proton http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/container_impl.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/container_impl.cpp b/proton-c/bindings/cpp/src/container_impl.cpp deleted file mode 100644 index 9cec831..0000000 --- a/proton-c/bindings/cpp/src/container_impl.cpp +++ /dev/null @@ -1,365 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "proton/connection_options.hpp" -#include "proton/connection.hpp" -#include "proton/error.hpp" -#include "proton/event_loop.hpp" -#include "proton/listener.hpp" -#include "proton/receiver.hpp" -#include "proton/sender.hpp" -#include "proton/session.hpp" -#include "proton/ssl.hpp" -#include "proton/sasl.hpp" -#include "proton/thread_safe.hpp" -#include "proton/transport.hpp" -#include "proton/url.hpp" -#include "proton/uuid.hpp" - -#include "acceptor.hpp" -#include "connector.hpp" -#include "container_impl.hpp" -#include "contexts.hpp" -#include "event_loop_impl.hpp" -#include "messaging_adapter.hpp" -#include "msg.hpp" -#include "proton_bits.hpp" -#include "proton_event.hpp" - -#include <proton/connection.h> -#include <proton/handlers.h> -#include <proton/reactor.h> -#include <proton/session.h> - -namespace proton { - -class container::impl::handler_context { - public: - static handler_context& get(pn_handler_t* h) { - return *reinterpret_cast<handler_context*>(pn_handler_mem(h)); - } - static void cleanup(pn_handler_t*) {} - - /* - * NOTE: this call, at the transition from C to C++ is possibly - * the biggest performance bottleneck. "Average" clients ignore - * 90% of these events. Current strategy is to create the - * messaging_event on the stack. For success, the messaging_event - * should be small and free of indirect malloc/free/new/delete. - */ - - static void dispatch(pn_handler_t *c_handler, pn_event_t *c_event, pn_event_type_t) - { - handler_context& hc(handler_context::get(c_handler)); - proton_event pevent(c_event, hc.container_); - pevent.dispatch(*hc.handler_); - return; - } - - container *container_; - proton_handler *handler_; -}; - -// Used to sniff for connector events before the reactor's global handler sees them. -class container::impl::override_handler : public proton_handler -{ - public: - internal::pn_ptr<pn_handler_t> base_handler; - container::impl &container_impl_; - - override_handler(pn_handler_t *h, container::impl &c) : base_handler(h), container_impl_(c) {} - - virtual void on_unhandled(proton_event &pe) { - proton_event::event_type type = pe.type(); - if (type==proton_event::EVENT_NONE) return; // Also not from the reactor - - pn_event_t *cevent = pe.pn_event(); - pn_connection_t *conn = pn_event_connection(cevent); - if (conn) { - proton_handler *oh = connection_context::get(conn).handler.get(); - if (oh && type != proton_event::CONNECTION_INIT) { - // Send event to connector - pe.dispatch(*oh); - } - else if (!oh && type == proton_event::CONNECTION_INIT) { - // Newly accepted connection from lister socket - connection c(make_wrapper(conn)); - container_impl_.configure_server_connection(c); - } - } - pn_handler_dispatch(base_handler.get(), cevent, pn_event_type_t(type)); - } -}; - -internal::pn_ptr<pn_handler_t> container::impl::cpp_handler(proton_handler *h) { - pn_handler_t *handler = h ? pn_handler_new(&handler_context::dispatch, - sizeof(class handler_context), - &handler_context::cleanup) : 0; - if (handler) { - handler_context &hc = handler_context::get(handler); - hc.container_ = &container_; - hc.handler_ = h; - } - return internal::take_ownership(handler); -} - -container::impl::impl(container& c, const std::string& id, messaging_handler *mh) : - container_(c), - reactor_(reactor::create()), - id_(id.empty() ? uuid::random().str() : id), - auto_stop_(true) -{ - container_context::set(reactor_, container_); - - // Set our own global handler that "subclasses" the existing one - pn_handler_t *global_handler = reactor_.pn_global_handler(); - proton_handler* oh = new override_handler(global_handler, *this); - handlers_.push_back(oh); - reactor_.pn_global_handler(cpp_handler(oh).get()); - if (mh) { - proton_handler* h = new messaging_adapter(*mh); - handlers_.push_back(h); - reactor_.pn_handler(cpp_handler(h).get()); - } - - // Note: we have just set up the following handlers that see - // events in this order: messaging_adapter, connector override, - // the reactor's default globalhandler (pn_iohandler) -} - -namespace { -void close_acceptor(acceptor a) { - listen_handler*& lh = listener_context::get(unwrap(a)).listen_handler_; - if (lh) { - lh->on_close(); - lh = 0; - } - a.close(); -} -} - -container::impl::~impl() { - for (acceptors::iterator i = acceptors_.begin(); i != acceptors_.end(); ++i) - close_acceptor(i->second); -} - -// FIXME aconway 2016-06-07: this is not thread safe. It is sufficient for using -// default_container::schedule() inside a handler but not for inject() from -// another thread. -bool event_loop::impl::inject(void_function0& f) { - try { f(); } catch(...) {} - return true; -} - -#if PN_CPP_HAS_STD_FUNCTION -bool event_loop::impl::inject(std::function<void()> f) { - try { f(); } catch(...) {} - return true; -} -#endif - -returned<connection> container::impl::connect(const std::string &urlstr, const connection_options &user_opts) { - connection_options opts = client_connection_options(); // Defaults - opts.update(user_opts); - messaging_handler* mh = opts.handler(); - internal::pn_ptr<pn_handler_t> chandler; - if (mh) { - proton_handler* h = new messaging_adapter(*mh); - handlers_.push_back(h); - chandler = cpp_handler(h); - } - - proton::url url(urlstr); - connection conn(reactor_.connection_to_host(url.host(), url.port(), chandler.get())); - internal::pn_unique_ptr<connector> ctor(new connector(conn, opts, url)); - connection_context& cc(connection_context::get(conn)); - cc.handler.reset(ctor.release()); - cc.event_loop_ = new event_loop::impl; - - pn_connection_t *pnc = unwrap(conn); - pn_connection_set_container(pnc, id_.c_str()); - pn_connection_set_hostname(pnc, url.host().c_str()); - if (!url.user().empty()) - pn_connection_set_user(pnc, url.user().c_str()); - if (!url.password().empty()) - pn_connection_set_password(pnc, url.password().c_str()); - - conn.open(opts); - return make_thread_safe(conn); -} - -returned<sender> container::impl::open_sender(const std::string &url, const proton::sender_options &o1, const connection_options &o2) { - proton::sender_options lopts(sender_options_); - lopts.update(o1); - connection_options copts(client_connection_options_); - copts.update(o2); - connection conn = connect(url, copts); - return make_thread_safe(conn.default_session().open_sender(proton::url(url).path(), lopts)); -} - -returned<receiver> container::impl::open_receiver(const std::string &url, const proton::receiver_options &o1, const connection_options &o2) { - proton::receiver_options lopts(receiver_options_); - lopts.update(o1); - connection_options copts(client_connection_options_); - copts.update(o2); - connection conn = connect(url, copts); - return make_thread_safe( - conn.default_session().open_receiver(proton::url(url).path(), lopts)); -} - -listener container::impl::listen(const std::string& url, listen_handler& lh) { - if (acceptors_.find(url) != acceptors_.end()) - throw error("already listening on " + url); - connection_options opts = server_connection_options(); // Defaults - - messaging_handler* mh = opts.handler(); - internal::pn_ptr<pn_handler_t> chandler; - if (mh) { - proton_handler* h = new messaging_adapter(*mh); - handlers_.push_back(h); - chandler = cpp_handler(h); - } - - proton::url u(url); - pn_acceptor_t *acptr = pn_reactor_acceptor( - unwrap(reactor_), u.host().c_str(), u.port().c_str(), chandler.get()); - if (!acptr) { - std::string err(pn_error_text(pn_reactor_error(unwrap(reactor_)))); - lh.on_error(err); - lh.on_close(); - throw error(err); - } - // Do not use pn_acceptor_set_ssl_domain(). Manage the incoming connections ourselves for - // more flexibility (i.e. ability to change the server cert for a long running listener). - listener_context& lc(listener_context::get(acptr)); - lc.listen_handler_ = &lh; - lc.ssl = u.scheme() == url::AMQPS; - listener_context::get(acptr).listen_handler_ = &lh; - acceptors_[url] = make_wrapper(acptr); - return listener(container_, url); -} - -void container::impl::stop_listening(const std::string& url) { - acceptors::iterator i = acceptors_.find(url); - if (i != acceptors_.end()) - close_acceptor(i->second); -} - -void container::impl::schedule(impl& ci, int delay, proton_handler *h) { - internal::pn_ptr<pn_handler_t> task_handler; - if (h) - task_handler = ci.cpp_handler(h); - ci.reactor_.schedule(delay, task_handler.get()); -} - -void container::impl::schedule(container& c, int delay, proton_handler *h) { - schedule(*c.impl_.get(), delay, h); -} - -namespace { -// Abstract base for timer_handler_std and timer_handler_03 -struct timer_handler : public proton_handler, public void_function0 { - void on_timer_task(proton_event& ) PN_CPP_OVERRIDE { - (*this)(); - delete this; - } - void on_reactor_final(proton_event&) PN_CPP_OVERRIDE { - delete this; - } -}; - -struct timer_handler_03 : public timer_handler { - void_function0& func; - timer_handler_03(void_function0& f): func(f) {} - void operator()() PN_CPP_OVERRIDE { func(); } -}; -} - -void container::impl::schedule(duration delay, void_function0& f) { - schedule(*this, delay.milliseconds(), new timer_handler_03(f)); -} - -#if PN_CPP_HAS_STD_FUNCTION -namespace { -struct timer_handler_std : public timer_handler { - std::function<void()> func; - timer_handler_std(std::function<void()> f): func(f) {} - void operator()() PN_CPP_OVERRIDE { func(); } -}; -} - -void container::impl::schedule(duration delay, std::function<void()> f) { - schedule(*this, delay.milliseconds(), new timer_handler_std(f)); -} -#endif - -void container::impl::client_connection_options(const connection_options &opts) { - client_connection_options_ = opts; -} - -void container::impl::server_connection_options(const connection_options &opts) { - server_connection_options_ = opts; -} - -void container::impl::sender_options(const proton::sender_options &opts) { - sender_options_ = opts; -} - -void container::impl::receiver_options(const proton::receiver_options &opts) { - receiver_options_ = opts; -} - -void container::impl::configure_server_connection(connection &c) { - pn_acceptor_t *pnp = pn_connection_acceptor(unwrap(c)); - listener_context &lc(listener_context::get(pnp)); - pn_connection_set_container(unwrap(c), id_.c_str()); - connection_options opts = server_connection_options_; - opts.update(lc.get_options()); - // Unbound options don't apply to server connection - opts.apply_bound(c); - // Handler applied separately - messaging_handler* mh = opts.handler(); - if (mh) { - proton_handler* h = new messaging_adapter(*mh); - handlers_.push_back(h); - internal::pn_ptr<pn_handler_t> chandler = cpp_handler(h); - pn_record_t *record = pn_connection_attachments(unwrap(c)); - pn_record_set_handler(record, chandler.get()); - } - connection_context::get(c).event_loop_ = new event_loop::impl; -} - -void container::impl::run() { - do { - reactor_.run(); - } while (!auto_stop_); -} - -void container::impl::stop(const error_condition&) { - reactor_.stop(); - auto_stop_ = true; -} - -void container::impl::auto_stop(bool set) { - auto_stop_ = set; -} - -} http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/container_test.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/container_test.cpp b/proton-c/bindings/cpp/src/container_test.cpp deleted file mode 100644 index 564a4ba..0000000 --- a/proton-c/bindings/cpp/src/container_test.cpp +++ /dev/null @@ -1,204 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - - -#include "test_bits.hpp" -#include "proton/connection.hpp" -#include "proton/connection_options.hpp" -#include "proton/container.hpp" -#include "proton/default_container.hpp" -#include "proton/messaging_handler.hpp" -#include "proton/listener.hpp" -#include "proton/listen_handler.hpp" -#include "proton/thread_safe.hpp" - -#include <cstdlib> -#include <ctime> -#include <string> -#include <cstdio> -#include <sstream> - -namespace { - -static std::string int2string(int n) { - std::ostringstream strm; - strm << n; - return strm.str(); -} - -int listen_on_random_port(proton::container& c, proton::listener& l) { - int port; - // I'm going to hell for this: - std::srand((unsigned int)time(0)); - while (true) { - port = 20000 + (std::rand() % 30000); - try { - l = c.listen("0.0.0.0:" + int2string(port)); - break; - } catch (...) { - // keep trying - } - } - return port; -} - -class test_handler : public proton::messaging_handler { - public: - const std::string host; - proton::connection_options opts; - bool closing; - bool done; - - std::string peer_vhost; - proton::listener listener; - - test_handler(const std::string h, const proton::connection_options& c_opts) - : host(h), opts(c_opts), closing(false), done(false) - {} - - void on_container_start(proton::container &c) PN_CPP_OVERRIDE { - int port = listen_on_random_port(c, listener); - proton::connection conn = c.connect(host + ":" + int2string(port), opts); - } - - void on_connection_open(proton::connection &c) PN_CPP_OVERRIDE { - if (peer_vhost.empty() && !c.virtual_host().empty()) - peer_vhost = c.virtual_host(); - if (!closing) c.close(); - closing = true; - } - - void on_connection_close(proton::connection &) PN_CPP_OVERRIDE { - if (!done) listener.stop(); - done = true; - } -}; - -int test_container_vhost() { - proton::connection_options opts; - opts.virtual_host(std::string("a.b.c")); - test_handler th(std::string("127.0.0.1"), opts); - proton::default_container(th).run(); - ASSERT_EQUAL(th.peer_vhost, std::string("a.b.c")); - return 0; -} - -int test_container_default_vhost() { - proton::connection_options opts; - test_handler th(std::string("127.0.0.1"), opts); - proton::default_container(th).run(); - ASSERT_EQUAL(th.peer_vhost, std::string("127.0.0.1")); - return 0; -} - -int test_container_no_vhost() { - // explicitly setting an empty virtual-host will cause the Open - // performative to be sent without a hostname field present. - // Sadly whether or not a 'hostname' field was received cannot be - // determined from here, so just exercise the code - proton::connection_options opts; - opts.virtual_host(std::string("")); - test_handler th(std::string("127.0.0.1"), opts); - proton::default_container(th).run(); - ASSERT_EQUAL(th.peer_vhost, std::string("")); - return 0; -} - -struct test_listener : public proton::listen_handler { - bool on_accept_, on_close_; - std::string on_error_; - test_listener() : on_accept_(false), on_close_(false) {} - proton::connection_options on_accept() PN_CPP_OVERRIDE { - on_accept_ = true; - return proton::connection_options(); - } - void on_close() PN_CPP_OVERRIDE { on_close_ = true; } - void on_error(const std::string& e) PN_CPP_OVERRIDE { on_error_ = e; } -}; - -int test_container_bad_address() { - // Listen on a bad address, check for leaks - // Regression test for https://issues.apache.org/jira/browse/PROTON-1217 - - proton::default_container c; - // Default fixed-option listener. Valgrind for leaks. - try { c.listen("999.666.999.666:0"); } catch (const proton::error&) {} - // Dummy listener. - test_listener l; - test_handler h2(std::string("999.999.999.666"), proton::connection_options()); - try { c.listen("999.666.999.666:0", l); } catch (const proton::error&) {} - ASSERT(!l.on_accept_); - ASSERT(l.on_close_); - ASSERT(!l.on_error_.empty()); - return 0; -} - -class stop_tester : public proton::messaging_handler { - proton::listener listener; - - // Set up a listener which would block forever - void on_container_start(proton::container& c) PN_CPP_OVERRIDE { - ASSERT(state==0); - int port = listen_on_random_port(c, listener); - c.connect("127.0.0.1:" + int2string(port)); - c.auto_stop(false); - state = 1; - } - - // Get here twice - once for listener, once for connector - void on_connection_open(proton::connection &c) PN_CPP_OVERRIDE { - c.close(); - state++; - } - - void on_connection_close(proton::connection &c) PN_CPP_OVERRIDE { - ASSERT(state==3); - c.container().stop(); - state = 4; - } - void on_container_stop(proton::container & ) PN_CPP_OVERRIDE { - ASSERT(state==4); - state = 5; - } - -public: - stop_tester(): state(0) {} - - int state; -}; - -int test_container_stop() { - stop_tester t; - proton::default_container(t).run(); - ASSERT(t.state==5); - return 0; -} - -} - -int main(int, char**) { - int failed = 0; - RUN_TEST(failed, test_container_vhost()); - RUN_TEST(failed, test_container_default_vhost()); - RUN_TEST(failed, test_container_no_vhost()); - RUN_TEST(failed, test_container_bad_address()); - RUN_TEST(failed, test_container_stop()); - return failed; -} - http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/contexts.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/contexts.cpp b/proton-c/bindings/cpp/src/contexts.cpp deleted file mode 100644 index 231506f..0000000 --- a/proton-c/bindings/cpp/src/contexts.cpp +++ /dev/null @@ -1,120 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "contexts.hpp" -#include "msg.hpp" -#include "reactor.hpp" -#include "proton_bits.hpp" - -#include "proton/error.hpp" - -#include <proton/connection.h> -#include <proton/object.h> -#include <proton/link.h> -#include <proton/message.h> -#include <proton/reactor.h> -#include <proton/session.h> - -#include <typeinfo> - -namespace proton { - -namespace { -void cpp_context_finalize(void* v) { reinterpret_cast<context*>(v)->~context(); } -#define CID_cpp_context CID_pn_object -#define cpp_context_initialize NULL -#define cpp_context_finalize cpp_context_finalize -#define cpp_context_hashcode NULL -#define cpp_context_compare NULL -#define cpp_context_inspect NULL -pn_class_t cpp_context_class = PN_CLASS(cpp_context); - -// Handles -PN_HANDLE(CONNECTION_CONTEXT) -PN_HANDLE(CONTAINER_CONTEXT) -PN_HANDLE(LISTENER_CONTEXT) -PN_HANDLE(LINK_CONTEXT) - -void set_context(pn_record_t* record, pn_handle_t handle, const pn_class_t *clazz, void* value) -{ - pn_record_def(record, handle, clazz); - pn_record_set(record, handle, value); -} - -template <class T> -T* get_context(pn_record_t* record, pn_handle_t handle) { - return reinterpret_cast<T*>(pn_record_get(record, handle)); -} - -} - -context::~context() {} - -void *context::alloc(size_t n) { return pn_object_new(&cpp_context_class, n); } - -pn_class_t* context::pn_class() { return &cpp_context_class; } - - -context::id connection_context::id(pn_connection_t* c) { - return context::id(pn_connection_attachments(c), CONNECTION_CONTEXT); -} - -context::id connection_context::id(const connection& c) { - return id(unwrap(c)); -} - -void container_context::set(const reactor& r, container& c) { - set_context(pn_reactor_attachments(unwrap(r)), CONTAINER_CONTEXT, PN_VOID, &c); -} - -container &container_context::get(pn_reactor_t *pn_reactor) { - container *ctx = get_context<container>(pn_reactor_attachments(pn_reactor), CONTAINER_CONTEXT); - if (!ctx) throw error(MSG("Reactor has no C++ container context")); - return *ctx; -} - -listener_context& listener_context::get(pn_acceptor_t* a) { - // TODO aconway 2016-05-13: reactor only - // A Proton C pn_acceptor_t is really just a selectable - pn_selectable_t *sel = reinterpret_cast<pn_selectable_t*>(a); - - listener_context* ctx = - get_context<listener_context>(pn_selectable_attachments(sel), LISTENER_CONTEXT); - if (!ctx) { - ctx = context::create<listener_context>(); - set_context(pn_selectable_attachments(sel), LISTENER_CONTEXT, context::pn_class(), ctx); - pn_decref(ctx); - } - return *ctx; -} - -link_context& link_context::get(pn_link_t* l) { - link_context* ctx = - get_context<link_context>(pn_link_attachments(l), LINK_CONTEXT); - if (!ctx) { - ctx = context::create<link_context>(); - set_context(pn_link_attachments(l), LINK_CONTEXT, context::pn_class(), ctx); - pn_decref(ctx); - } - return *ctx; -} - -} http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/data.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/data.cpp b/proton-c/bindings/cpp/src/data.cpp deleted file mode 100644 index 0f6e7f5..0000000 --- a/proton-c/bindings/cpp/src/data.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "proton/internal/data.hpp" - -#include "proton/binary.hpp" -#include "proton/codec/encoder.hpp" -#include "proton/decimal.hpp" -#include "proton/message_id.hpp" -#include "proton/symbol.hpp" -#include "proton/timestamp.hpp" -#include "proton/value.hpp" - -#include <proton/codec.h> - -#include <ostream> - -#include "proton_bits.hpp" - -namespace proton { -namespace internal { - -data data::create() { return internal::take_ownership(pn_data(0)).get(); } - -void data::copy(const data& x) { ::pn_data_copy(pn_object(), x.pn_object()); } - -void data::clear() { ::pn_data_clear(pn_object()); } - -void data::rewind() { ::pn_data_rewind(pn_object()); } - -bool data::empty() const { return ::pn_data_size(pn_object()) == 0; } - -void* data::point() const { return pn_data_point(pn_object()); } - -void data::restore(void* h) { pn_data_restore(pn_object(), pn_handle_t(h)); } - -void data::narrow() { pn_data_narrow(pn_object()); } - -void data::widen() { pn_data_widen(pn_object()); } - -int data::append(data src) { return pn_data_append(pn_object(), src.pn_object());} - -int data::appendn(data src, int limit) { return pn_data_appendn(pn_object(), src.pn_object(), limit);} - -bool data::next() { return pn_data_next(pn_object()); } - -std::ostream& operator<<(std::ostream& o, const data& d) { - state_guard sg(const_cast<data&>(d)); - const_cast<data&>(d).rewind(); - return o << inspectable(d.pn_object()); -} - -} // internal -} // proton http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/decimal.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/decimal.cpp b/proton-c/bindings/cpp/src/decimal.cpp deleted file mode 100644 index 0cadb19..0000000 --- a/proton-c/bindings/cpp/src/decimal.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "proton/decimal.hpp" -#include <iostream> - -namespace proton { - -std::ostream& operator<<(std::ostream& o, const decimal32& d) { - return o << "decimal32(" <<static_cast<byte_array<sizeof(d)> >(d)<< ")"; -} - -std::ostream& operator<<(std::ostream& o, const decimal64& d) { - return o << "decimal64(" <<static_cast<byte_array<sizeof(d)> >(d)<< ")"; -} - -std::ostream& operator<<(std::ostream& o, const decimal128& d) { - return o << "decimal128(" <<static_cast<byte_array<sizeof(d)> >(d)<< ")"; -} - -} http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/decoder.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/decoder.cpp b/proton-c/bindings/cpp/src/decoder.cpp deleted file mode 100644 index 9c941c8..0000000 --- a/proton-c/bindings/cpp/src/decoder.cpp +++ /dev/null @@ -1,334 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "proton/codec/decoder.hpp" - -#include "proton/annotation_key.hpp" -#include "proton/binary.hpp" -#include "proton/decimal.hpp" -#include "proton/message_id.hpp" -#include "proton/scalar.hpp" -#include "proton/symbol.hpp" -#include "proton/timestamp.hpp" -#include "proton/value.hpp" - -#include "proton_bits.hpp" -#include "types_internal.hpp" -#include "msg.hpp" - -#include <proton/codec.h> - -namespace proton { -namespace codec { - -/**@file - * - * Note the pn_data_t "current" node is always pointing *before* the next value - * to be returned by the decoder. - */ -decoder::decoder(const internal::value_base& v, bool exact) - : data(const_cast<internal::value_base&>(v).data()), exact_(exact) -{ - rewind(); -} - -namespace { -template <class T> T check(T result) { - if (result < 0) - throw conversion_error(error_str(result)); - return result; -} -} - -void decoder::decode(const char* i, size_t size) { - internal::state_guard sg(*this); - const char* end = i + size; - while (i < end) - i += check(pn_data_decode(pn_object(), i, size_t(end - i))); -} - -void decoder::decode(const std::string& s) { decode(s.data(), s.size()); } - -bool decoder::more() { - internal::state_guard sg(*this); - return next(); -} - -type_id decoder::pre_get() { - if (!next()) throw conversion_error("no more data"); - type_id t = type_id(pn_data_type(pn_object())); - if (t < 0) throw conversion_error("invalid data"); - return t; -} - -namespace { - -template <class T, class U> void assign(T& x, const U& y) { x = y; } -void assign(uuid& x, const pn_uuid_t y) { byte_copy(x, y); } -void assign(decimal32& x, const pn_decimal32_t y) { byte_copy(x, y); } -void assign(decimal64& x, const pn_decimal64_t y) { byte_copy(x, y); } -void assign(decimal128& x, const pn_decimal128_t y) { byte_copy(x, y); } -void assign(symbol& x, const pn_bytes_t y) { x = str(y); } -void assign(binary& x, const pn_bytes_t y) { x = bin(y); } - -} // namespace - - -// Simple extract with no type conversion. -template <class T, class U> decoder& decoder::extract(T& x, U (*get)(pn_data_t*)) { - internal::state_guard sg(*this); - assert_type_equal(internal::type_id_of<T>::value, pre_get()); - assign(x, get(pn_object())); - sg.cancel(); // No error, cancel the reset. - return *this; -} - -type_id decoder::next_type() { - internal::state_guard sg(*this); - return pre_get(); -} - -decoder& decoder::operator>>(start& s) { - internal::state_guard sg(*this); - s.type = pre_get(); - switch (s.type) { - case ARRAY: - s.size = pn_data_get_array(pn_object()); - s.element = type_id(pn_data_get_array_type(pn_object())); s.is_described = pn_data_is_array_described(pn_object()); - break; - case LIST: - s.size = pn_data_get_list(pn_object()); - break; - case MAP: - s.size = pn_data_get_map(pn_object()); - break; - case DESCRIBED: - s.is_described = true; - s.size = 1; - break; - default: - throw conversion_error(MSG("" << s.type << " is not a container type")); - } - pn_data_enter(pn_object()); - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(const finish&) { - pn_data_exit(pn_object()); - return *this; -} - -decoder& decoder::operator>>(null&) { - internal::state_guard sg(*this); - assert_type_equal(NULL_TYPE, pre_get()); - return *this; -} - -decoder& decoder::operator>>(internal::value_base& x) { - if (*this == x.data_) - throw conversion_error("extract into self"); - data d = x.data(); - d.clear(); - narrow(); - try { - check(d.appendn(*this, 1)); - widen(); - } catch(...) { - widen(); - throw; - } - next(); - return *this; -} - -decoder& decoder::operator>>(message_id& x) { - internal::state_guard sg(*this); - type_id got = pre_get(); - if (got != ULONG && got != UUID && got != BINARY && got != STRING) - throw conversion_error( - msg() << "expected one of ulong, uuid, binary or string but found " << got); - x.set(pn_data_get_atom(pn_object())); - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(annotation_key& x) { - internal::state_guard sg(*this); - type_id got = pre_get(); - if (got != ULONG && got != SYMBOL) - throw conversion_error(msg() << "expected one of ulong or symbol but found " << got); - x.set(pn_data_get_atom(pn_object())); - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(scalar& x) { - internal::state_guard sg(*this); - type_id got = pre_get(); - if (!type_id_is_scalar(got)) - throw conversion_error("expected scalar, found "+type_name(got)); - x.set(pn_data_get_atom(pn_object())); - sg.cancel(); // No error, no rewind - return *this; -} - -decoder& decoder::operator>>(bool &x) { return extract(x, pn_data_get_bool); } - -decoder& decoder::operator>>(uint8_t &x) { return extract(x, pn_data_get_ubyte); } - -decoder& decoder::operator>>(int8_t &x) { return extract(x, pn_data_get_byte); } - -decoder& decoder::operator>>(uint16_t &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(USHORT, tid); - switch (tid) { - case UBYTE: x = pn_data_get_ubyte(pn_object()); break; - case USHORT: x = pn_data_get_ushort(pn_object()); break; - default: assert_type_equal(USHORT, tid); - } - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(int16_t &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(SHORT, tid); - switch (tid) { - case BYTE: x = pn_data_get_byte(pn_object()); break; - case SHORT: x = pn_data_get_short(pn_object()); break; - default: assert_type_equal(SHORT, tid); - } - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(uint32_t &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(UINT, tid); - switch (tid) { - case UBYTE: x = pn_data_get_ubyte(pn_object()); break; - case USHORT: x = pn_data_get_ushort(pn_object()); break; - case UINT: x = pn_data_get_uint(pn_object()); break; - default: assert_type_equal(UINT, tid); - } - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(int32_t &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(INT, tid); - switch (tid) { - case BYTE: x = pn_data_get_byte(pn_object()); break; - case SHORT: x = pn_data_get_short(pn_object()); break; - case INT: x = pn_data_get_int(pn_object()); break; - default: assert_type_equal(INT, tid); - } - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(uint64_t &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(ULONG, tid); - switch (tid) { - case UBYTE: x = pn_data_get_ubyte(pn_object()); break; - case USHORT: x = pn_data_get_ushort(pn_object()); break; - case UINT: x = pn_data_get_uint(pn_object()); break; - case ULONG: x = pn_data_get_ulong(pn_object()); break; - default: assert_type_equal(ULONG, tid); - } - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(int64_t &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(LONG, tid); - switch (tid) { - case BYTE: x = pn_data_get_byte(pn_object()); break; - case SHORT: x = pn_data_get_short(pn_object()); break; - case INT: x = pn_data_get_int(pn_object()); break; - case LONG: x = pn_data_get_long(pn_object()); break; - default: assert_type_equal(LONG, tid); - } - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(wchar_t &x) { return extract(x, pn_data_get_char); } - -decoder& decoder::operator>>(timestamp &x) { return extract(x, pn_data_get_timestamp); } - -decoder& decoder::operator>>(float &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(FLOAT, tid); - switch (tid) { - case FLOAT: x = pn_data_get_float(pn_object()); break; - case DOUBLE: x = float(pn_data_get_double(pn_object())); break; - default: assert_type_equal(FLOAT, tid); - } - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(double &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(DOUBLE, tid); - switch (tid) { - case FLOAT: x = static_cast<double>(pn_data_get_float(pn_object())); break; - case DOUBLE: x = pn_data_get_double(pn_object()); break; - default: assert_type_equal(DOUBLE, tid); - } - sg.cancel(); - return *this; -} - -decoder& decoder::operator>>(decimal32 &x) { return extract(x, pn_data_get_decimal32); } -decoder& decoder::operator>>(decimal64 &x) { return extract(x, pn_data_get_decimal64); } -decoder& decoder::operator>>(decimal128 &x) { return extract(x, pn_data_get_decimal128); } - -decoder& decoder::operator>>(uuid &x) { return extract(x, pn_data_get_uuid); } -decoder& decoder::operator>>(binary &x) { return extract(x, pn_data_get_binary); } -decoder& decoder::operator>>(symbol &x) { return extract(x, pn_data_get_symbol); } - -decoder& decoder::operator>>(std::string &x) { - internal::state_guard sg(*this); - type_id tid = pre_get(); - if (exact_) assert_type_equal(STRING, tid); - switch (tid) { - case STRING: x = str(pn_data_get_string(pn_object())); break; - case SYMBOL: x = str(pn_data_get_symbol(pn_object())); break; - default: assert_type_equal(STRING, tid); - } - sg.cancel(); - return *this; -} - -} // codec -} // proton http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/delivery.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/delivery.cpp b/proton-c/bindings/cpp/src/delivery.cpp deleted file mode 100644 index 0562304..0000000 --- a/proton-c/bindings/cpp/src/delivery.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "proton/delivery.hpp" - -#include "proton/receiver.hpp" - -#include "proton_bits.hpp" - -#include <proton/delivery.h> - -namespace { - -void settle_delivery(pn_delivery_t* o, uint64_t state) { - pn_delivery_update(o, state); - pn_delivery_settle(o); -} - -} - -namespace proton { - -delivery::delivery(pn_delivery_t* d): transfer(make_wrapper(d)) {} -receiver delivery::receiver() const { return make_wrapper<class receiver>(pn_delivery_link(pn_object())); } -void delivery::accept() { settle_delivery(pn_object(), ACCEPTED); } -void delivery::reject() { settle_delivery(pn_object(), REJECTED); } -void delivery::release() { settle_delivery(pn_object(), RELEASED); } -void delivery::modify() { settle_delivery(pn_object(), MODIFIED); } - -} http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/duration.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/duration.cpp b/proton-c/bindings/cpp/src/duration.cpp deleted file mode 100644 index 9918da1..0000000 --- a/proton-c/bindings/cpp/src/duration.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -#include "proton/duration.hpp" -#include "proton/timestamp.hpp" - -#include <limits> -#include <iostream> - -namespace proton { - -const duration duration::FOREVER(std::numeric_limits<duration::numeric_type>::max()); -const duration duration::IMMEDIATE(0); -const duration duration::SECOND(1000); -const duration duration::MINUTE(SECOND * 60); - -std::ostream& operator<<(std::ostream& o, duration d) { return o << d.milliseconds(); } - -} http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/encoder.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/encoder.cpp b/proton-c/bindings/cpp/src/encoder.cpp deleted file mode 100644 index 89ac8c4..0000000 --- a/proton-c/bindings/cpp/src/encoder.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "proton/codec/encoder.hpp" - -#include "proton_bits.hpp" -#include "types_internal.hpp" -#include "msg.hpp" - -#include "proton/annotation_key.hpp" -#include "proton/binary.hpp" -#include "proton/decimal.hpp" -#include "proton/message_id.hpp" -#include "proton/scalar_base.hpp" -#include "proton/symbol.hpp" -#include "proton/timestamp.hpp" -#include "proton/value.hpp" - -#include <proton/codec.h> - -#include <algorithm> -#include <assert.h> - -namespace proton { -namespace codec { - -void encoder::check(long result) { - if (result < 0) - throw conversion_error(error_str(pn_data_error(pn_object()), result)); -} - - -encoder::encoder(internal::value_base& v) : data(v.data()) { - clear(); -} - -bool encoder::encode(char* buffer, size_t& size) { - internal::state_guard sg(*this); // In case of error - ssize_t result = pn_data_encode(pn_object(), buffer, size); - if (result == PN_OVERFLOW) { - result = pn_data_encoded_size(pn_object()); - if (result >= 0) { - size = size_t(result); - return false; - } - } - check(result); - size = size_t(result); - sg.cancel(); // Don't restore state, all is well. - pn_data_clear(pn_object()); - return true; -} - -void encoder::encode(std::string& s) { - s.resize(std::max(s.capacity(), size_t(1))); // Use full capacity, ensure not empty - size_t size = s.size(); - assert(!s.empty()); - if (!encode(&s[0], size)) { - s.resize(size); - assert(!s.empty()); - encode(&s[0], size); - } -} - -std::string encoder::encode() { - std::string s; - encode(s); - return s; -} - -encoder& encoder::operator<<(const start& s) { - switch (s.type) { - case ARRAY: pn_data_put_array(pn_object(), s.is_described, pn_type_t(s.element)); break; - case MAP: pn_data_put_map(pn_object()); break; - case LIST: pn_data_put_list(pn_object()); break; - case DESCRIBED: pn_data_put_described(pn_object()); break; - default: - throw conversion_error(MSG("" << s.type << " is not a container type")); - } - pn_data_enter(pn_object()); - return *this; -} - -encoder& encoder::operator<<(const finish&) { - pn_data_exit(pn_object()); - return *this; -} - -namespace { - -template <class T, class U> T coerce(const U &x) { return x; } -template <> pn_uuid_t coerce(const uuid& x) { pn_uuid_t y; byte_copy(y, x); return y; } -template <> pn_decimal32_t coerce(const decimal32 &x) { pn_decimal32_t y; byte_copy(y, x); return y; } -template <> pn_decimal64_t coerce(const decimal64 &x) { pn_decimal64_t y; byte_copy(y, x); return y; } -template <> pn_decimal128_t coerce(const decimal128 &x) { pn_decimal128_t y; byte_copy(y, x); return y; } - -int pn_data_put_amqp_string(pn_data_t *d, const std::string& x) { return pn_data_put_string(d, pn_bytes(x)); } -int pn_data_put_amqp_binary(pn_data_t *d, const binary& x) { return pn_data_put_binary(d, pn_bytes(x)); } -int pn_data_put_amqp_symbol(pn_data_t *d, const symbol& x) { return pn_data_put_symbol(d, pn_bytes(x)); } -} // namespace - -template <class T, class U> -encoder& encoder::insert(const T& x, int (*put)(pn_data_t*, U)) { - internal::state_guard sg(*this); // Save state in case of error. - check(put(pn_object(), coerce<U>(x))); - sg.cancel(); // Don't restore state, all is good. - return *this; -} - -encoder& encoder::operator<<(bool x) { return insert(x, pn_data_put_bool); } -encoder& encoder::operator<<(uint8_t x) { return insert(x, pn_data_put_ubyte); } -encoder& encoder::operator<<(int8_t x) { return insert(x, pn_data_put_byte); } -encoder& encoder::operator<<(uint16_t x) { return insert(x, pn_data_put_ushort); } -encoder& encoder::operator<<(int16_t x) { return insert(x, pn_data_put_short); } -encoder& encoder::operator<<(uint32_t x) { return insert(x, pn_data_put_uint); } -encoder& encoder::operator<<(int32_t x) { return insert(x, pn_data_put_int); } -encoder& encoder::operator<<(wchar_t x) { return insert(x, pn_data_put_char); } -encoder& encoder::operator<<(uint64_t x) { return insert(x, pn_data_put_ulong); } -encoder& encoder::operator<<(int64_t x) { return insert(x, pn_data_put_long); } -encoder& encoder::operator<<(timestamp x) { return insert(x.milliseconds(), pn_data_put_timestamp); } -encoder& encoder::operator<<(float x) { return insert(x, pn_data_put_float); } -encoder& encoder::operator<<(double x) { return insert(x, pn_data_put_double); } -encoder& encoder::operator<<(decimal32 x) { return insert(x, pn_data_put_decimal32); } -encoder& encoder::operator<<(decimal64 x) { return insert(x, pn_data_put_decimal64); } -encoder& encoder::operator<<(decimal128 x) { return insert(x, pn_data_put_decimal128); } -encoder& encoder::operator<<(const uuid& x) { return insert(x, pn_data_put_uuid); } -encoder& encoder::operator<<(const std::string& x) { return insert(x, pn_data_put_amqp_string); } -encoder& encoder::operator<<(const symbol& x) { return insert(x, pn_data_put_amqp_symbol); } -encoder& encoder::operator<<(const binary& x) { return insert(x, pn_data_put_amqp_binary); } -encoder& encoder::operator<<(const null&) { pn_data_put_null(pn_object()); return *this; } - -encoder& encoder::operator<<(const scalar_base& x) { return insert(x.atom_, pn_data_put_atom); } - -encoder& encoder::operator<<(const internal::value_base& x) { - data d = x.data_; - if (*this == d) - throw conversion_error("cannot insert into self"); - if (!d || d.empty()) - return *this << null(); - d.rewind(); - check(append(d)); - return *this; -} - -} // codec -} // proton http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/endpoint.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/endpoint.cpp b/proton-c/bindings/cpp/src/endpoint.cpp deleted file mode 100644 index b99ab46..0000000 --- a/proton-c/bindings/cpp/src/endpoint.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -#include "proton_bits.hpp" - -#include "proton/connection.hpp" -#include "proton/endpoint.hpp" -#include "proton/error_condition.hpp" -#include "proton/link.hpp" -#include "proton/session.hpp" - -#include <proton/connection.h> -#include <proton/session.h> -#include <proton/link.h> - -namespace { - -inline bool uninitialized(int state) { return state & PN_LOCAL_UNINIT; } -inline bool active(int state) { return state & PN_LOCAL_ACTIVE; } -inline bool closed(int state) { return (state & PN_LOCAL_CLOSED) && (state & PN_REMOTE_CLOSED); } -} - -namespace proton { - -bool connection::uninitialized() const { return ::uninitialized(pn_connection_state(pn_object())); } -bool connection::active() const { return ::active(pn_connection_state(pn_object())); } -bool connection::closed() const { return ::closed(pn_connection_state(pn_object())); } - -void connection::close(const error_condition& condition) { - set_error_condition(condition, pn_connection_condition(pn_object())); - close(); -} - -bool session::uninitialized() const { return ::uninitialized(pn_session_state(pn_object())); } -bool session::active() const { return ::active(pn_session_state(pn_object())); } -bool session::closed() const { return ::closed(pn_session_state(pn_object())); } - -void session::close(const error_condition& condition) { - set_error_condition(condition, pn_session_condition(pn_object())); - close(); -} - -bool link::uninitialized() const { return ::uninitialized(pn_link_state(pn_object())); } -bool link::active() const { return ::active(pn_link_state(pn_object())); } -bool link::closed() const { return ::closed(pn_link_state(pn_object())); } - -void link::close(const error_condition& condition) { - set_error_condition(condition, pn_link_condition(pn_object())); - close(); -} - -endpoint::~endpoint() {} - -} http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/engine_test.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/engine_test.cpp b/proton-c/bindings/cpp/src/engine_test.cpp deleted file mode 100644 index 991836d..0000000 --- a/proton-c/bindings/cpp/src/engine_test.cpp +++ /dev/null @@ -1,268 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - - -#include "test_bits.hpp" -#include "test_dummy_container.hpp" -#include "proton_bits.hpp" - -#include "proton/container.hpp" -#include "proton/uuid.hpp" -#include "proton/io/connection_driver.hpp" -#include "proton/io/link_namer.hpp" -#include "proton/messaging_handler.hpp" -#include "proton/types_fwd.hpp" -#include "proton/link.hpp" -#include <deque> -#include <algorithm> - -namespace { - -using namespace std; -using namespace proton; - -using proton::io::connection_driver; -using proton::io::const_buffer; -using proton::io::mutable_buffer; - -using test::dummy_container; - -typedef std::deque<char> byte_stream; - -/// In memory connection_driver that reads and writes from byte_streams -struct in_memory_engine : public connection_driver { - - byte_stream& reads; - byte_stream& writes; - - in_memory_engine(byte_stream& rd, byte_stream& wr, class container& cont) : - connection_driver(cont), reads(rd), writes(wr) {} - - void do_read() { - mutable_buffer rbuf = read_buffer(); - size_t size = std::min(reads.size(), rbuf.size); - if (size) { - copy(reads.begin(), reads.begin()+size, static_cast<char*>(rbuf.data)); - read_done(size); - reads.erase(reads.begin(), reads.begin()+size); - } - } - - void do_write() { - const_buffer wbuf = write_buffer(); - if (wbuf.size) { - writes.insert(writes.begin(), - static_cast<const char*>(wbuf.data), - static_cast<const char*>(wbuf.data) + wbuf.size); - write_done(wbuf.size); - } - } - - void process() { - if (!dispatch()) - throw std::runtime_error("unexpected close: "+connection().error().what()); - do_read(); - do_write(); - dispatch(); - } -}; - -/// A pair of engines that talk to each other in-memory, simulating a connection. -struct engine_pair { - dummy_container conta, contb; - byte_stream ab, ba; - in_memory_engine a, b; - - engine_pair(const connection_options& oa, const connection_options& ob, - const std::string& name="" - ) : - conta(name+"a"), contb(name+"b"), a(ba, ab, conta), b(ab, ba, contb) - { - a.connect(oa); - b.accept(ob); - } - - void process() { a.process(); b.process(); } -}; - -template <class S> typename S::value_type quick_pop(S& s) { - ASSERT(!s.empty()); - typename S::value_type x = s.front(); - s.pop_front(); - return x; -} - -/// A handler that records incoming endpoints, errors etc. -struct record_handler : public messaging_handler { - std::deque<proton::receiver> receivers; - std::deque<proton::sender> senders; - std::deque<proton::session> sessions; - std::deque<std::string> unhandled_errors, transport_errors, connection_errors; - - void on_receiver_open(receiver &l) PN_CPP_OVERRIDE { - receivers.push_back(l); - } - - void on_sender_open(sender &l) PN_CPP_OVERRIDE { - senders.push_back(l); - } - - void on_session_open(session &s) PN_CPP_OVERRIDE { - sessions.push_back(s); - } - - void on_transport_error(transport& t) PN_CPP_OVERRIDE { - transport_errors.push_back(t.error().what()); - } - - void on_connection_error(connection& c) PN_CPP_OVERRIDE { - connection_errors.push_back(c.error().what()); - } - - void on_error(const proton::error_condition& c) PN_CPP_OVERRIDE { - unhandled_errors.push_back(c.what()); - } -}; - -struct namer : public io::link_namer { - char name; - namer(char c) : name(c) {} - std::string link_name() { return std::string(1, name++); } -}; - -void test_engine_container_link_id() { - record_handler ha, hb; - engine_pair e(ha, hb, "ids-"); - e.a.connect(ha); - e.b.accept(hb); - - namer na('x'); - namer nb('b'); - connection ca = e.a.connection(); - connection cb = e.b.connection(); - set_link_namer(ca, na); - set_link_namer(cb, nb); - - ASSERT_EQUAL("ids-a", e.a.connection().container_id()); - e.b.connection().open(); - ASSERT_EQUAL("ids-b", e.b.connection().container_id()); - - e.a.connection().open_sender("foo"); - while (ha.senders.empty() || hb.receivers.empty()) e.process(); - sender s = quick_pop(ha.senders); - ASSERT_EQUAL("x", s.name()); - - ASSERT_EQUAL("x", quick_pop(hb.receivers).name()); - - e.a.connection().open_receiver("bar"); - while (ha.receivers.empty() || hb.senders.empty()) e.process(); - ASSERT_EQUAL("y", quick_pop(ha.receivers).name()); - ASSERT_EQUAL("y", quick_pop(hb.senders).name()); - - e.b.connection().open_receiver(""); - while (ha.senders.empty() || hb.receivers.empty()) e.process(); - ASSERT_EQUAL("b", quick_pop(ha.senders).name()); - ASSERT_EQUAL("b", quick_pop(hb.receivers).name()); -} - -void test_endpoint_close() { - record_handler ha, hb; - engine_pair e(ha, hb); - e.a.connection().open_sender("x"); - e.a.connection().open_receiver("y"); - while (ha.senders.size()+ha.receivers.size() < 2 || - hb.senders.size()+hb.receivers.size() < 2) e.process(); - proton::link ax = quick_pop(ha.senders), ay = quick_pop(ha.receivers); - proton::link bx = quick_pop(hb.receivers), by = quick_pop(hb.senders); - - // Close a link - ax.close(proton::error_condition("err", "foo bar")); - while (!bx.closed()) e.process(); - proton::error_condition c = bx.error(); - ASSERT_EQUAL("err", c.name()); - ASSERT_EQUAL("foo bar", c.description()); - ASSERT_EQUAL("err: foo bar", c.what()); - - // Close a link with an empty condition - ay.close(proton::error_condition()); - while (!by.closed()) e.process(); - ASSERT(by.error().empty()); - - // Close a connection - connection ca = e.a.connection(), cb = e.b.connection(); - ca.close(proton::error_condition("conn", "bad connection")); - while (!cb.closed()) e.process(); - ASSERT_EQUAL("conn: bad connection", cb.error().what()); - ASSERT_EQUAL(1u, hb.connection_errors.size()); - ASSERT_EQUAL("conn: bad connection", hb.connection_errors.front()); -} - -void test_engine_disconnected() { - // engine.disconnected() aborts the connection and calls the local on_transport_error() - record_handler ha, hb; - engine_pair e(ha, hb, "disconnected"); - e.a.connect(ha); - e.b.accept(hb); - while (!e.a.connection().active() || !e.b.connection().active()) - e.process(); - - // Close a with an error condition. The AMQP connection is still open. - e.a.disconnected(proton::error_condition("oops", "engine failure")); - ASSERT(!e.a.dispatch()); - ASSERT(!e.a.connection().closed()); - ASSERT(e.a.connection().error().empty()); - ASSERT_EQUAL(0u, ha.connection_errors.size()); - ASSERT_EQUAL("oops: engine failure", e.a.transport().error().what()); - ASSERT_EQUAL(1u, ha.transport_errors.size()); - ASSERT_EQUAL("oops: engine failure", ha.transport_errors.front()); - - // In a real app the IO code would detect the abort and do this: - e.b.disconnected(proton::error_condition("broken", "it broke")); - ASSERT(!e.b.dispatch()); - ASSERT(!e.b.connection().closed()); - ASSERT(e.b.connection().error().empty()); - ASSERT_EQUAL(0u, hb.connection_errors.size()); - // Proton-C adds (connection aborted) if transport closes too early, - // and provides a default message if there is no user message. - ASSERT_EQUAL("broken: it broke (connection aborted)", e.b.transport().error().what()); - ASSERT_EQUAL(1u, hb.transport_errors.size()); - ASSERT_EQUAL("broken: it broke (connection aborted)", hb.transport_errors.front()); -} - -void test_no_container() { - // An engine with no container should throw, not crash. - connection_driver e; - try { - e.connection().container(); - FAIL("expected error"); - } catch (proton::error) {} - ASSERT(make_thread_safe<connection>(e.connection()).get()); - ASSERT(!make_thread_safe<connection>(e.connection()).get()->event_loop()); -} - -} - -int main(int, char**) { - int failed = 0; - RUN_TEST(failed, test_engine_container_link_id()); - RUN_TEST(failed, test_endpoint_close()); - RUN_TEST(failed, test_engine_disconnected()); - RUN_TEST(failed, test_no_container()); - return failed; -} http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2f85988e/proton-c/bindings/cpp/src/error.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/error.cpp b/proton-c/bindings/cpp/src/error.cpp deleted file mode 100644 index 28fd9f5..0000000 --- a/proton-c/bindings/cpp/src/error.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "proton/error.hpp" - -namespace proton { - -error::error(const std::string& msg) : std::runtime_error(msg) {} - -timeout_error::timeout_error(const std::string& msg) : error(msg) {} - -conversion_error::conversion_error(const std::string& msg) : error(msg) {} - -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
