Debian Codegraph @

cpp/examples/multithreaded_client.cpp

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 //
21 // C++11 or greater
22 //
23 // A multi-threaded client that calls proton::container::run() in one thread, sends
24 // messages in another and receives messages in a third.
25 //
26 // Note this client does not deal with flow-control. If the sender is faster
27 // than the receiver, messages will build up in memory on the sending side.
28 // See @ref multithreaded_client_flow_control.cpp for a more complex example with
29 // flow control.
30 //
31 // NOTE: no proper error handling
32
33 #include <proton/connection.hpp>
34 #include <proton/connection_options.hpp>
35 #include <proton/container.hpp>
36 #include <proton/message.hpp>
37 #include <proton/messaging_handler.hpp>
38 #include <proton/receiver.hpp>
39 #include <proton/sender.hpp>
40 #include <proton/work_queue.hpp>
41
42 #include <condition_variable>
43 #include <iostream>
44 #include <mutex>
45 #include <queue>
46 #include <sstream>
47 #include <string>
48 #include <thread>
49
50 // Lock output from threads to avoid scrambling
51 std::mutex out_lock;
52 #define OUT(x) do { std::lock_guard<std::mutex> l(out_lock); x; } while (false)
53
54 // Handler for a single thread-safe sending and receiving connection.
55 class client : public proton::messaging_handler {
56 // Invariant
57 const std::string url_;
58 const std::string address_;
59
60 // Only used in proton handler thread
61 proton::sender sender_;
62
63 // Shared by proton and user threads, protected by lock_
64 std::mutex lock_;
65 proton::work_queue *work_queue_;
66 std::condition_variable sender_ready_;
67 std::queue<proton::message> messages_;
68 std::condition_variable messages_ready_;
69
70 public:
71 client(const std::string& url, const std::string& address) : url_(url), address_(address), work_queue_(0) {}
72
73 // Thread safe
74 void send(const proton::message& msg) {
75 // Use [=] to copy the message, we cannot pass it by reference since it
76 // will be used in another thread.
77 work_queue()->add([=]() { sender_.send(msg); });
78 }
79
80 // Thread safe
81 proton::message receive() {
82 std::unique_lock<std::mutex> l(lock_);
83 while (messages_.empty()) messages_ready_.wait(l);
84 auto msg = std::move(messages_.front());
85 messages_.pop();
86 return msg;
87 }
88
89 // Thread safe
90 void close() {
91 work_queue()->add([=]() { sender_.connection().close(); });
92 }
93
94 private:
95
96 proton::work_queue* work_queue() {
97 // Wait till work_queue_ and sender_ are initialized.
98 std::unique_lock<std::mutex> l(lock_);
99 while (!work_queue_) sender_ready_.wait(l);
100 return work_queue_;
101 }
102
103 // == messaging_handler overrides, only called in proton handler thread
104
105 // Note: this example creates a connection when the container starts.
106 // To create connections after the container has started, use
107 // container::connect().
108 // See @ref multithreaded_client_flow_control.cpp for an example.
109 void on_container_start(proton::container& cont) override {
110 cont.connect(url_);
111 }
112
113 void on_connection_open(proton::connection& conn) override {
114 conn.open_sender(address_);
115 conn.open_receiver(address_);
116 }
117
118 void on_sender_open(proton::sender& s) override {
119 // sender_ and work_queue_ must be set atomically
120 std::lock_guard<std::mutex> l(lock_);
121 sender_ = s;
122 work_queue_ = &s.work_queue();
123 sender_ready_.notify_all();
124 }
125
126 void on_message(proton::delivery& dlv, proton::message& msg) override {
127 std::lock_guard<std::mutex> l(lock_);
128 messages_.push(msg);
129 messages_ready_.notify_all();
130 }
131
132 void on_error(const proton::error_condition& e) override {
133 OUT(std::cerr << "unexpected error: " << e << std::endl);
134 exit(1);
135 }
136 };
137
138 int main(int argc, const char** argv) {
139 try {
140 if (argc != 4) {
141 std ::cerr <<
142 "Usage: " << argv[0] << " CONNECTION-URL AMQP-ADDRESS MESSAGE-COUNT\n"
143 "CONNECTION-URL: connection address, e.g.'amqp://127.0.0.1'\n"
144 "AMQP-ADDRESS: AMQP node address, e.g. 'examples'\n"
145 "MESSAGE-COUNT: number of messages to send\n";
146 return 1;
147 }
148 const char *url = argv[1];
149 const char *address = argv[2];
150 int n_messages = atoi(argv[3]);
151
152 client cl(url, address);
153 proton::container container(cl);
154 std::thread container_thread([&]() { container.run(); });
155
156 std::thread sender([&]() {
157 for (int i = 0; i < n_messages; ++i) {
158 proton::message msg(std::to_string(i + 1));
159 cl.send(msg);
160 OUT(std::cout << "sent \"" << msg.body() << '"' << std::endl);
161 }
162 });
163
164 int received = 0;
165 std::thread receiver([&]() {
166 for (int i = 0; i < n_messages; ++i) {
167 auto msg = cl.receive();
168 OUT(std::cout << "received \"" << msg.body() << '"' << std::endl);
169 ++received;
170 }
171 });
172
173 sender.join();
174 receiver.join();
175 cl.close();
176 container_thread.join();
177 std::cout << received << " messages sent and received" << std::endl;
178
179 return 0;
180 } catch (const std::exception& e) {
181 std::cerr << e.what() << std::endl;
182 }
183
184 return 1;
185 }