| 1 |
#ifndef PROTON_CPP_CONTEXTS_H |
| 2 |
#define PROTON_CPP_CONTEXTS_H |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "reconnect_options_impl.hpp" |
| 26 |
|
| 27 |
#include "proton/work_queue.hpp" |
| 28 |
#include "proton/message.hpp" |
| 29 |
|
| 30 |
#include <memory> |
| 31 |
|
| 32 |
struct pn_record_t; |
| 33 |
struct pn_link_t; |
| 34 |
struct pn_session_t; |
| 35 |
struct pn_connection_t; |
| 36 |
struct pn_listener_t; |
| 37 |
|
| 38 |
namespace proton { |
| 39 |
|
| 40 |
class proton_handler; |
| 41 |
class connector; |
| 42 |
|
| 43 |
namespace io {class link_namer;} |
| 44 |
|
| 45 |
// Base class for C++ classes that are used as proton contexts. |
| 46 |
// Contexts are pn_objects managed by pn reference counts, the C++ value is allocated in-place. |
| 47 |
class context { |
| 48 |
public: |
| 49 |
// identifies a context, contains a record pointer and a handle. |
| 50 |
typedef std::pair<pn_record_t*, pn_handle_t> id; |
| 51 |
|
| 52 |
virtual ~context(); |
| 53 |
|
| 54 |
// Allocate a default-constructed T as a proton object. |
| 55 |
// T must be a subclass of context. |
| 56 |
template <class T> static T *create() { return new(alloc(sizeof(T))) T(); } |
| 57 |
|
| 58 |
// The pn_class for a context |
| 59 |
static pn_class_t* pn_class(); |
| 60 |
|
| 61 |
// Get the context identified by id as a C++ T*, return null pointer if not present. |
| 62 |
template <class T> static T* ptr(id id_) { |
| 63 |
return reinterpret_cast<T*>(pn_record_get(id_.first, id_.second)); |
| 64 |
} |
| 65 |
|
| 66 |
// If the context is not present, create it with value x. |
| 67 |
template <class T> static T& ref(id id_) { |
| 68 |
T* ctx = context::ptr<T>(id_); |
| 69 |
if (!ctx) { |
| 70 |
ctx = create<T>(); |
| 71 |
pn_record_def(id_.first, id_.second, pn_class()); |
| 72 |
pn_record_set(id_.first, id_.second, ctx); |
| 73 |
pn_decref(ctx); |
| 74 |
} |
| 75 |
return *ctx; |
| 76 |
} |
| 77 |
|
| 78 |
private: |
| 79 |
static void *alloc(size_t n); |
| 80 |
}; |
| 81 |
|
| 82 |
class listener_context; |
| 83 |
class reconnect_context; |
| 84 |
|
| 85 |
// Connection context used by all connections. |
| 86 |
class connection_context : public context { |
| 87 |
public: |
| 88 |
connection_context(); |
| 89 |
static connection_context& get(pn_connection_t *c); |
| 90 |
|
| 91 |
class container* container; |
| 92 |
pn_session_t *default_session; // Owned by connection. |
| 93 |
message event_message; // re-used by messaging_adapter for performance. |
| 94 |
io::link_namer* link_gen; // Link name generator. |
| 95 |
|
| 96 |
messaging_handler* handler; |
| 97 |
std::string reconnect_url_; |
| 98 |
std::vector<std::string> failover_urls_; |
| 99 |
std::unique_ptr<connection_options> connection_options_; |
| 100 |
std::unique_ptr<reconnect_context> reconnect_context_; |
| 101 |
listener_context* listener_context_; |
| 102 |
work_queue work_queue_; |
| 103 |
}; |
| 104 |
|
| 105 |
class reconnect_options_base; |
| 106 |
|
| 107 |
// This is not a context object on its own, but an optional part of connection |
| 108 |
class reconnect_context { |
| 109 |
public: |
| 110 |
reconnect_context(const reconnect_options_base& ro); |
| 111 |
|
| 112 |
const reconnect_options_base reconnect_options_; |
| 113 |
duration delay_; |
| 114 |
int retries_; |
| 115 |
int current_url_; |
| 116 |
bool stop_reconnect_; |
| 117 |
bool reconnected_; |
| 118 |
}; |
| 119 |
|
| 120 |
class listener_context : public context { |
| 121 |
public: |
| 122 |
listener_context(); |
| 123 |
static listener_context& get(pn_listener_t* c); |
| 124 |
|
| 125 |
listen_handler* listen_handler_; |
| 126 |
std::unique_ptr<const connection_options> connection_options_; |
| 127 |
}; |
| 128 |
|
| 129 |
class link_context : public context { |
| 130 |
public: |
| 131 |
link_context() : handler(0), credit_window(10), pending_credit(0), auto_accept(true), auto_settle(true), draining(false) {} |
| 132 |
static link_context& get(pn_link_t* l); |
| 133 |
|
| 134 |
messaging_handler* handler; |
| 135 |
int credit_window; |
| 136 |
uint32_t pending_credit; |
| 137 |
bool auto_accept; |
| 138 |
bool auto_settle; |
| 139 |
bool draining; |
| 140 |
}; |
| 141 |
|
| 142 |
class session_context : public context { |
| 143 |
public: |
| 144 |
session_context() : handler(0) {} |
| 145 |
static session_context& get(pn_session_t* s); |
| 146 |
|
| 147 |
messaging_handler* handler; |
| 148 |
}; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
#endif /*!PROTON_CPP_CONTEXTS_H*/ |