| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "options.hpp" |
| 21 |
|
| 22 |
#include <proton/connection.hpp> |
| 23 |
#include <proton/connection_options.hpp> |
| 24 |
#include <proton/container.hpp> |
| 25 |
#include <proton/delivery.hpp> |
| 26 |
#include <proton/error_condition.hpp> |
| 27 |
#include <proton/listen_handler.hpp> |
| 28 |
#include <proton/listener.hpp> |
| 29 |
#include <proton/message.hpp> |
| 30 |
#include <proton/messaging_handler.hpp> |
| 31 |
#include <proton/receiver_options.hpp> |
| 32 |
#include <proton/sender_options.hpp> |
| 33 |
#include <proton/source_options.hpp> |
| 34 |
#include <proton/target.hpp> |
| 35 |
#include <proton/target_options.hpp> |
| 36 |
#include <proton/tracker.hpp> |
| 37 |
#include <proton/transport.hpp> |
| 38 |
#include <proton/work_queue.hpp> |
| 39 |
|
| 40 |
#include <deque> |
| 41 |
#include <iostream> |
| 42 |
#include <map> |
| 43 |
#include <string> |
| 44 |
#include <thread> |
| 45 |
|
| 46 |
|
| 47 |
// This is a simplified model for a message broker, that only allows for |
| 48 |
// messages to go to a single receiver. |
| 49 |
// |
| 50 |
// This broker is multithread safe and if compiled with C++11 with a multithreaded Proton |
| 51 |
// binding library will use as many threads as there are thread resources available (usually |
| 52 |
// cores) |
| 53 |
// |
| 54 |
// Queues are only created and never destroyed |
| 55 |
// |
| 56 |
// Broker Entities (that need to be individually serialised) |
| 57 |
// QueueManager - Creates new queues, finds queues |
| 58 |
// Queue - Queues msgs, records subscribers, sends msgs to subscribers |
| 59 |
// Connection - Receives Messages from network, sends messages to network. |
| 60 |
|
| 61 |
// Work |
| 62 |
// FindQueue(queueName, connection) - From a Connection to the QueueManager |
| 63 |
// This will create the queue if it doesn't already exist and send a BoundQueue |
| 64 |
// message back to the connection. |
| 65 |
// BoundQueue(queue) - From the QueueManager to a Connection |
| 66 |
// |
| 67 |
// QueueMsg(msg) - From a Connection (receiver) to a Queue |
| 68 |
// Subscribe(sender) - From a Connection (sender) to a Queue |
| 69 |
// Flow(sender, credit) - From a Connection (sender) to a Queue |
| 70 |
// Unsubscribe(sender) - From a Connection (sender) to a Queue |
| 71 |
// |
| 72 |
// SendMsg(msg) - From a Queue to a Connection (sender) |
| 73 |
// Unsubscribed() - From a Queue to a Connection (sender) |
| 74 |
|
| 75 |
|
| 76 |
// Simple debug output |
| 77 |
bool verbose; |
| 78 |
#define DOUT(x) do {if (verbose) {x};} while (false) |
| 79 |
|
| 80 |
class Queue; |
| 81 |
class Sender; |
| 82 |
|
| 83 |
typedef std::map<proton::sender, Sender*> senders; |
| 84 |
|
| 85 |
class Sender : public proton::messaging_handler { |
| 86 |
friend class connection_handler; |
| 87 |
|
| 88 |
proton::sender sender_; |
| 89 |
senders& senders_; |
| 90 |
proton::work_queue& work_queue_; |
| 91 |
std::string queue_name_; |
| 92 |
Queue* queue_; |
| 93 |
int pending_credit_; |
| 94 |
|
| 95 |
// Messaging handlers |
| 96 |
void on_sendable(proton::sender &sender) override; |
| 97 |
void on_sender_close(proton::sender &sender) override; |
| 98 |
|
| 99 |
public: |
| 100 |
Sender(proton::sender s, senders& ss) : |
| 101 |
sender_(s), senders_(ss), work_queue_(s.work_queue()), queue_(0), pending_credit_(0) |
| 102 |
{} |
| 103 |
|
| 104 |
bool add(proton::work f) { |
| 105 |
return work_queue_.add(f); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
void boundQueue(Queue* q, std::string qn); |
| 110 |
void sendMsg(proton::message m) { |
| 111 |
DOUT(std::cerr << "Sender: " << this << " sending\n";); |
| 112 |
sender_.send(m); |
| 113 |
} |
| 114 |
void unsubscribed() { |
| 115 |
DOUT(std::cerr << "Sender: " << this << " deleting\n";); |
| 116 |
delete this; |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
// Queue - round robin subscriptions |
| 121 |
class Queue { |
| 122 |
proton::work_queue work_queue_; |
| 123 |
const std::string name_; |
| 124 |
std::deque<proton::message> messages_; |
| 125 |
typedef std::map<Sender*, int> subscriptions; // With credit |
| 126 |
subscriptions subscriptions_; |
| 127 |
subscriptions::iterator current_; |
| 128 |
|
| 129 |
void tryToSend() { |
| 130 |
DOUT(std::cerr << "Queue: " << this << " tryToSend: " << subscriptions_.size();); |
| 131 |
// Starting at current_, send messages to subscriptions with credit: |
| 132 |
// After each send try to find another subscription; Wrap around; |
| 133 |
// Finish when we run out of messages or credit. |
| 134 |
size_t outOfCredit = 0; |
| 135 |
while (!messages_.empty() && outOfCredit<subscriptions_.size()) { |
| 136 |
// If we got the end (or haven't started yet) start at the beginning |
| 137 |
if (current_==subscriptions_.end()) { |
| 138 |
current_=subscriptions_.begin(); |
| 139 |
} |
| 140 |
// If we have credit send the message |
| 141 |
DOUT(std::cerr << "(" << current_->second << ") ";); |
| 142 |
if (current_->second>0) { |
| 143 |
DOUT(std::cerr << current_->first << " ";); |
| 144 |
current_->first->add(make_work(&Sender::sendMsg, current_->first, messages_.front())); |
| 145 |
messages_.pop_front(); |
| 146 |
--current_->second; |
| 147 |
++current_; |
| 148 |
} else { |
| 149 |
++outOfCredit; |
| 150 |
} |
| 151 |
} |
| 152 |
DOUT(std::cerr << "\n";); |
| 153 |
} |
| 154 |
|
| 155 |
public: |
| 156 |
Queue(proton::container& c, const std::string& n) : |
| 157 |
work_queue_(c), name_(n), current_(subscriptions_.end()) |
| 158 |
{} |
| 159 |
|
| 160 |
bool add(proton::work f) { |
| 161 |
return work_queue_.add(f); |
| 162 |
} |
| 163 |
|
| 164 |
void queueMsg(proton::message m) { |
| 165 |
DOUT(std::cerr << "Queue: " << this << "(" << name_ << ") queueMsg\n";); |
| 166 |
messages_.push_back(m); |
| 167 |
tryToSend(); |
| 168 |
} |
| 169 |
void flow(Sender* s, int c) { |
| 170 |
DOUT(std::cerr << "Queue: " << this << "(" << name_ << ") flow: " << c << " to " << s << "\n";); |
| 171 |
subscriptions_[s] = c; |
| 172 |
tryToSend(); |
| 173 |
} |
| 174 |
void subscribe(Sender* s) { |
| 175 |
DOUT(std::cerr << "Queue: " << this << "(" << name_ << ") subscribe Sender: " << s << "\n";); |
| 176 |
subscriptions_[s] = 0; |
| 177 |
} |
| 178 |
void unsubscribe(Sender* s) { |
| 179 |
DOUT(std::cerr << "Queue: " << this << "(" << name_ << ") unsubscribe Sender: " << s << "\n";); |
| 180 |
// If we're about to erase the current subscription move on |
| 181 |
if (current_ != subscriptions_.end() && current_->first==s) ++current_; |
| 182 |
subscriptions_.erase(s); |
| 183 |
s->add(make_work(&Sender::unsubscribed, s)); |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
// We have credit to send a message. |
| 188 |
void Sender::on_sendable(proton::sender &sender) { |
| 189 |
if (queue_) { |
| 190 |
queue_->add(make_work(&Queue::flow, queue_, this, sender.credit())); |
| 191 |
} else { |
| 192 |
pending_credit_ = sender.credit(); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
void Sender::on_sender_close(proton::sender &sender) { |
| 197 |
if (queue_) { |
| 198 |
queue_->add(make_work(&Queue::unsubscribe, queue_, this)); |
| 199 |
} else { |
| 200 |
// TODO: Is it possible to be closed before we get the queue allocated? |
| 201 |
// If so, we should have a way to mark the sender deleted, so we can delete |
| 202 |
// on queue binding |
| 203 |
} |
| 204 |
senders_.erase(sender); |
| 205 |
} |
| 206 |
|
| 207 |
void Sender::boundQueue(Queue* q, std::string qn) { |
| 208 |
DOUT(std::cerr << "Sender: " << this << " bound to Queue: " << q <<"(" << qn << ")\n";); |
| 209 |
queue_ = q; |
| 210 |
queue_name_ = qn; |
| 211 |
|
| 212 |
q->add(make_work(&Queue::subscribe, q, this)); |
| 213 |
sender_.open(proton::sender_options() |
| 214 |
.source((proton::source_options().address(queue_name_))) |
| 215 |
.handler(*this)); |
| 216 |
if (pending_credit_>0) { |
| 217 |
queue_->add(make_work(&Queue::flow, queue_, this, pending_credit_)); |
| 218 |
} |
| 219 |
std::cout << "sending from " << queue_name_ << std::endl; |
| 220 |
} |
| 221 |
|
| 222 |
class Receiver : public proton::messaging_handler { |
| 223 |
friend class connection_handler; |
| 224 |
|
| 225 |
proton::receiver receiver_; |
| 226 |
proton::work_queue& work_queue_; |
| 227 |
Queue* queue_; |
| 228 |
std::deque<proton::message> messages_; |
| 229 |
|
| 230 |
// A message is received. |
| 231 |
void on_message(proton::delivery &, proton::message &m) override { |
| 232 |
messages_.push_back(m); |
| 233 |
|
| 234 |
if (queue_) { |
| 235 |
queueMsgs(); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void queueMsgs() { |
| 240 |
DOUT(std::cerr << "Receiver: " << this << " queueing " << messages_.size() << " msgs to: " << queue_ << "\n";); |
| 241 |
while (!messages_.empty()) { |
| 242 |
queue_->add(make_work(&Queue::queueMsg, queue_, messages_.front())); |
| 243 |
messages_.pop_front(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
public: |
| 248 |
Receiver(proton::receiver r) : |
| 249 |
receiver_(r), work_queue_(r.work_queue()), queue_(0) |
| 250 |
{} |
| 251 |
|
| 252 |
bool add(proton::work f) { |
| 253 |
return work_queue_.add(f); |
| 254 |
} |
| 255 |
|
| 256 |
void boundQueue(Queue* q, std::string qn) { |
| 257 |
DOUT(std::cerr << "Receiver: " << this << " bound to Queue: " << q << "(" << qn << ")\n";); |
| 258 |
queue_ = q; |
| 259 |
receiver_.open(proton::receiver_options() |
| 260 |
.source((proton::source_options().address(qn))) |
| 261 |
.handler(*this)); |
| 262 |
std::cout << "receiving to " << qn << std::endl; |
| 263 |
|
| 264 |
queueMsgs(); |
| 265 |
} |
| 266 |
}; |
| 267 |
|
| 268 |
class QueueManager { |
| 269 |
proton::container& container_; |
| 270 |
proton::work_queue work_queue_; |
| 271 |
typedef std::map<std::string, Queue*> queues; |
| 272 |
queues queues_; |
| 273 |
int next_id_; // Use to generate unique queue IDs. |
| 274 |
|
| 275 |
public: |
| 276 |
QueueManager(proton::container& c) : |
| 277 |
container_(c), work_queue_(c), next_id_(0) |
| 278 |
{} |
| 279 |
|
| 280 |
bool add(proton::work f) { |
| 281 |
return work_queue_.add(f); |
| 282 |
} |
| 283 |
|
| 284 |
template <class T> |
| 285 |
void findQueue(T& connection, std::string& qn) { |
| 286 |
if (qn.empty()) { |
| 287 |
// Dynamic queue creation |
| 288 |
std::ostringstream os; |
| 289 |
os << "_dynamic_" << next_id_++; |
| 290 |
qn = os.str(); |
| 291 |
} |
| 292 |
Queue* q = 0; |
| 293 |
queues::iterator i = queues_.find(qn); |
| 294 |
if (i==queues_.end()) { |
| 295 |
q = new Queue(container_, qn); |
| 296 |
queues_[qn] = q; |
| 297 |
} else { |
| 298 |
q = i->second; |
| 299 |
} |
| 300 |
connection.add(make_work(&T::boundQueue, &connection, q, qn)); |
| 301 |
} |
| 302 |
|
| 303 |
void findQueueSender(Sender* s, std::string qn) { |
| 304 |
findQueue(*s, qn); |
| 305 |
} |
| 306 |
|
| 307 |
void findQueueReceiver(Receiver* r, std::string qn) { |
| 308 |
findQueue(*r, qn); |
| 309 |
} |
| 310 |
}; |
| 311 |
|
| 312 |
class connection_handler : public proton::messaging_handler { |
| 313 |
QueueManager& queue_manager_; |
| 314 |
senders senders_; |
| 315 |
|
| 316 |
public: |
| 317 |
connection_handler(QueueManager& qm) : |
| 318 |
queue_manager_(qm) |
| 319 |
{} |
| 320 |
|
| 321 |
void on_connection_open(proton::connection& c) override { |
| 322 |
c.open(); // Accept the connection |
| 323 |
} |
| 324 |
|
| 325 |
// A sender sends messages from a queue to a subscriber. |
| 326 |
void on_sender_open(proton::sender &sender) override { |
| 327 |
std::string qn = sender.source().dynamic() ? "" : sender.source().address(); |
| 328 |
Sender* s = new Sender(sender, senders_); |
| 329 |
senders_[sender] = s; |
| 330 |
queue_manager_.add(make_work(&QueueManager::findQueueSender, &queue_manager_, s, qn)); |
| 331 |
} |
| 332 |
|
| 333 |
// A receiver receives messages from a publisher to a queue. |
| 334 |
void on_receiver_open(proton::receiver &receiver) override { |
| 335 |
std::string qname = receiver.target().address(); |
| 336 |
Receiver* r = new Receiver(receiver); |
| 337 |
queue_manager_.add(make_work(&QueueManager::findQueueReceiver, &queue_manager_, r, qname)); |
| 338 |
} |
| 339 |
|
| 340 |
void on_session_close(proton::session &session) override { |
| 341 |
// Unsubscribe all senders that belong to session. |
| 342 |
for (proton::sender_iterator i = session.senders().begin(); i != session.senders().end(); ++i) { |
| 343 |
senders::iterator j = senders_.find(*i); |
| 344 |
if (j == senders_.end()) continue; |
| 345 |
Sender* s = j->second; |
| 346 |
if (s->queue_) { |
| 347 |
s->queue_->add(make_work(&Queue::unsubscribe, s->queue_, s)); |
| 348 |
} |
| 349 |
senders_.erase(j); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
void on_error(const proton::error_condition& e) override { |
| 354 |
std::cout << "protocol error: " << e.what() << std::endl; |
| 355 |
} |
| 356 |
|
| 357 |
// The container calls on_transport_close() last. |
| 358 |
void on_transport_close(proton::transport& t) override { |
| 359 |
// Unsubscribe all senders. |
| 360 |
for (proton::sender_iterator i = t.connection().senders().begin(); i != t.connection().senders().end(); ++i) { |
| 361 |
senders::iterator j = senders_.find(*i); |
| 362 |
if (j == senders_.end()) continue; |
| 363 |
Sender* s = j->second; |
| 364 |
if (s->queue_) { |
| 365 |
s->queue_->add(make_work(&Queue::unsubscribe, s->queue_, s)); |
| 366 |
} |
| 367 |
} |
| 368 |
delete this; // All done. |
| 369 |
} |
| 370 |
}; |
| 371 |
|
| 372 |
class broker { |
| 373 |
public: |
| 374 |
broker(const std::string addr) : |
| 375 |
container_("broker"), queues_(container_), listener_(queues_) |
| 376 |
{ |
| 377 |
container_.listen(addr, listener_); |
| 378 |
} |
| 379 |
|
| 380 |
void run() { |
| 381 |
container_.run(std::thread::hardware_concurrency()); |
| 382 |
} |
| 383 |
|
| 384 |
private: |
| 385 |
struct listener : public proton::listen_handler { |
| 386 |
listener(QueueManager& c) : queues_(c) {} |
| 387 |
|
| 388 |
proton::connection_options on_accept(proton::listener&) override{ |
| 389 |
return proton::connection_options().handler(*(new connection_handler(queues_))); |
| 390 |
} |
| 391 |
|
| 392 |
void on_open(proton::listener& l) override { |
| 393 |
std::cout << "broker listening on " << l.port() << std::endl; |
| 394 |
} |
| 395 |
|
| 396 |
void on_error(proton::listener&, const std::string& s) override { |
| 397 |
std::cerr << "listen error: " << s << std::endl; |
| 398 |
throw std::runtime_error(s); |
| 399 |
} |
| 400 |
QueueManager& queues_; |
| 401 |
}; |
| 402 |
|
| 403 |
proton::container container_; |
| 404 |
QueueManager queues_; |
| 405 |
listener listener_; |
| 406 |
}; |
| 407 |
|
| 408 |
int main(int argc, char **argv) { |
| 409 |
// Command line options |
| 410 |
std::string address("0.0.0.0"); |
| 411 |
example::options opts(argc, argv); |
| 412 |
|
| 413 |
opts.add_flag(verbose, 'v', "verbose", "verbose (debugging) output"); |
| 414 |
opts.add_value(address, 'a', "address", "listen on URL", "URL"); |
| 415 |
|
| 416 |
try { |
| 417 |
verbose = false; |
| 418 |
opts.parse(); |
| 419 |
broker(address).run(); |
| 420 |
return 0; |
| 421 |
} catch (const example::bad_option& e) { |
| 422 |
std::cout << opts << std::endl << e.what() << std::endl; |
| 423 |
} catch (const std::exception& e) { |
| 424 |
std::cout << "broker shutdown: " << e.what() << std::endl; |
| 425 |
} |
| 426 |
return 1; |
| 427 |
} |