Debian Codegraph @

cpp/src/proactor_container_impl.hpp

raw
1 #ifndef PROTON_CPP_PROACTOR_CONTAINERIMPL_H
2 #define PROTON_CPP_PROACTOR_CONTAINERIMPL_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 "proton/fwd.hpp"
26 #include "proton/container.hpp"
27 #include "proton/connection.hpp"
28 #include "proton/connection_options.hpp"
29 #include "proton/duration.hpp"
30 #include "proton/error_condition.hpp"
31 #include "proton/messaging_handler.hpp"
32 #include "proton/receiver.hpp"
33 #include "proton/receiver_options.hpp"
34 #include "proton/sender.hpp"
35 #include "proton/sender_options.hpp"
36 #include "proton/work_queue.hpp"
37
38 #include "proton_bits.hpp"
39
40 #include <list>
41 #include <map>
42 #include <set>
43 #include <string>
44 #include <vector>
45
46 #include <mutex>
47 # define MUTEX(x) std::mutex x;
48 # define GUARD(x) std::lock_guard<std::mutex> g(x)
49 # define ONCE_FLAG(x) std::once_flag x;
50 # define CALL_ONCE(x, ...) std::call_once(x, __VA_ARGS__)
51
52 struct pn_proactor_t;
53 struct pn_listener_t;
54 struct pn_event_t;
55
56 namespace proton {
57
58 namespace internal {
59 class connector;
60 }
61
62 class container::impl {
63 public:
64 impl(container& c, const std::string& id, messaging_handler* = 0);
65 ~impl();
66 std::string id() const { return id_; }
67 returned<connection> connect();
68 returned<connection> connect(const std::string&, const connection_options&);
69 returned<sender> open_sender(
70 const std::string&, const proton::sender_options &, const connection_options &);
71 returned<receiver> open_receiver(
72 const std::string&, const proton::receiver_options &, const connection_options &);
73 listener listen(const std::string&);
74 listener listen(const std::string&, const connection_options& lh);
75 listener listen(const std::string&, listen_handler& lh);
76 void client_connection_options(const connection_options &);
77 connection_options client_connection_options() const { return client_connection_options_; }
78 void server_connection_options(const connection_options &);
79 connection_options server_connection_options() const { return server_connection_options_; }
80 void sender_options(const proton::sender_options&);
81 class sender_options sender_options() const { return sender_options_; }
82 void receiver_options(const proton::receiver_options&);
83 class receiver_options receiver_options() const { return receiver_options_; }
84 void run(int threads);
85 void stop(const error_condition& err);
86 void auto_stop(bool set);
87 void schedule(duration, work);
88 template <class T> static void set_handler(T s, messaging_handler* h);
89 template <class T> static messaging_handler* get_handler(T s);
90 messaging_handler* get_handler(pn_event_t *event);
91 static work_queue::impl* make_work_queue(container&);
92
93 private:
94 class common_work_queue;
95 class connection_work_queue;
96 class container_work_queue;
97 pn_listener_t* listen_common_lh(const std::string&);
98 pn_connection_t* make_connection_lh(const url& url, const connection_options&);
99 void start_connection(const url& url, pn_connection_t* c);
100 void reconnect(pn_connection_t* pnc);
101 bool can_reconnect(pn_connection_t* pnc);
102 void setup_reconnect(pn_connection_t* pnc);
103
104 // Event loop to run in each container thread
105 void thread();
106 enum dispatch_result {ContinueLoop, EndBatch, EndLoop};
107 dispatch_result dispatch(pn_event_t*);
108 void run_timer_jobs();
109
110 int threads_;
111 container& container_;
112 MUTEX(lock_)
113
114 ONCE_FLAG(start_once_)
115 ONCE_FLAG(stop_once_)
116 void start_event();
117 void stop_event();
118
119 typedef std::set<container_work_queue*> work_queues;
120 work_queues work_queues_;
121 MUTEX(work_queues_lock_)
122 container_work_queue* add_work_queue();
123 void remove_work_queue(container_work_queue*);
124
125 struct scheduled {
126 timestamp time; // duration from epoch for task
127 work task;
128
129 // We want to get to get the *earliest* first so test is "reversed"
130 bool operator < (const scheduled& r) const { return r.time < time; }
131 };
132 std::vector<scheduled> deferred_; // This vector is kept as a heap
133 MUTEX(deferred_lock_)
134
135 pn_proactor_t* proactor_;
136 messaging_handler* handler_;
137 std::string id_;
138 connection_options client_connection_options_;
139 connection_options server_connection_options_;
140 proton::sender_options sender_options_;
141 proton::receiver_options receiver_options_;
142 error_condition disconnect_error_;
143
144 unsigned reconnecting_;
145 bool auto_stop_;
146 bool stopping_;
147 friend class connector;
148 };
149
150 template <class T>
151 void container::impl::set_handler(T s, messaging_handler* mh) {
152 internal::set_messaging_handler(s, mh);
153 }
154
155 template <class T>
156 messaging_handler* container::impl::get_handler(T s) {
157 return internal::get_messaging_handler(s);
158 }
159
160
161 }
162
163 #endif /*!PROTON_CPP_PROACTOR_CONTAINERIMPL_H*/