1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
// Copyright Oliver Kowalke 2015.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstddef>
#include <cstdlib>
#include <map>
#include <memory>
#include <set>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/utility.hpp>
#include <boost/fiber/all.hpp>
#include "../round_robin.hpp"
#include "../yield.hpp"
using boost::asio::ip::tcp;
const std::size_t max_length = 1024;
class subscriber_session;
typedef std::shared_ptr< subscriber_session > subscriber_session_ptr;
// a queue has n subscribers (subscriptions)
// this class holds a list of subcribers for one queue
class subscriptions {
public:
~subscriptions();
// subscribe to this queue
void subscribe( subscriber_session_ptr const& s) {
subscribers_.insert( s);
}
// unsubscribe from this queue
void unsubscribe( subscriber_session_ptr const& s) {
subscribers_.erase(s);
}
// publish a message, e.g. push this message to all subscribers
void publish( std::string const& msg);
private:
// list of subscribers
std::set< subscriber_session_ptr > subscribers_;
};
// a class to register queues and to subsribe clients to this queues
class registry : private boost::noncopyable {
private:
typedef std::map< std::string, std::shared_ptr< subscriptions > > queues_cont;
typedef queues_cont::iterator queues_iter;
boost::fibers::mutex mtx_;
queues_cont queues_;
void register_queue_( std::string const& queue) {
if ( queues_.end() != queues_.find( queue) ) {
throw std::runtime_error("queue already exists");
}
queues_[queue] = std::make_shared< subscriptions >();
std::cout << "new queue '" << queue << "' registered" << std::endl;
}
void unregister_queue_( std::string const& queue) {
queues_.erase( queue);
std::cout << "queue '" << queue << "' unregistered" << std::endl;
}
void subscribe_( std::string const& queue, subscriber_session_ptr s) {
queues_iter iter = queues_.find( queue);
if ( queues_.end() == iter ) {
throw std::runtime_error("queue does not exist");
}
iter->second->subscribe( s);
std::cout << "new subscription to queue '" << queue << "'" << std::endl;
}
void unsubscribe_( std::string const& queue, subscriber_session_ptr s) {
queues_iter iter = queues_.find( queue);
if ( queues_.end() != iter ) {
iter->second->unsubscribe( s);
}
}
void publish_( std::string const& queue, std::string const& msg) {
queues_iter iter = queues_.find( queue);
if ( queues_.end() == iter ) {
throw std::runtime_error("queue does not exist");
}
iter->second->publish( msg);
std::cout << "message '" << msg << "' to publish on queue '" << queue << "'" << std::endl;
}
public:
// add a queue to registry
void register_queue( std::string const& queue) {
std::unique_lock< boost::fibers::mutex > lk( mtx_);
register_queue_( queue);
}
// remove a queue from registry
void unregister_queue( std::string const& queue) {
std::unique_lock< boost::fibers::mutex > lk( mtx_);
unregister_queue_( queue);
}
// subscribe to a queue
void subscribe( std::string const& queue, subscriber_session_ptr s) {
std::unique_lock< boost::fibers::mutex > lk( mtx_);
subscribe_( queue, s);
}
// unsubscribe from a queue
void unsubscribe( std::string const& queue, subscriber_session_ptr s) {
std::unique_lock< boost::fibers::mutex > lk( mtx_);
unsubscribe_( queue, s);
}
// publish a message to all subscribers registerd to the queue
void publish( std::string const& queue, std::string const& msg) {
std::unique_lock< boost::fibers::mutex > lk( mtx_);
publish_( queue, msg);
}
};
// a subscriber subscribes to a given queue in order to receive messages published on this queue
class subscriber_session : public std::enable_shared_from_this< subscriber_session > {
public:
explicit subscriber_session( std::shared_ptr< boost::asio::io_context > const& io_context, registry & reg) :
socket_( * io_context),
reg_( reg) {
}
tcp::socket& socket() {
return socket_;
}
// this function is executed inside the fiber
void run() {
std::string queue;
try {
boost::system::error_code ec;
// read first message == queue name
// async_ready() returns if the the complete message is read
// until this the fiber is suspended until the complete message
// is read int the given buffer 'data'
boost::asio::async_read(
socket_,
boost::asio::buffer( data_),
boost::fibers::asio::yield[ec]);
if ( ec) {
throw std::runtime_error("no queue from subscriber");
}
// first message ist equal to the queue name the publisher
// publishes to
queue = data_;
// subscribe to new queue
reg_.subscribe( queue, shared_from_this() );
// read published messages
for (;;) {
// wait for a conditon-variable for new messages
// the fiber will be suspended until the condtion
// becomes true and the fiber is resumed
// published message is stored in buffer 'data_'
std::unique_lock< boost::fibers::mutex > lk( mtx_);
cond_.wait( lk);
std::string data( data_);
lk.unlock();
// message '<fini>' terminates subscription
if ( "<fini>" == data) {
break;
}
// async. write message to socket connected with
// subscriber
// async_write() returns if the complete message was writen
// the fiber is suspended in the meanwhile
boost::asio::async_write(
socket_,
boost::asio::buffer( data, data.size() ),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
std::cout << "subscriber::run(): '" << data << "' written" << std::endl;
}
} catch ( std::exception const& e) {
std::cerr << "subscriber [" << queue << "] failed: " << e.what() << std::endl;
}
// close socket
socket_.close();
// unregister queue
reg_.unsubscribe( queue, shared_from_this() );
}
// called from publisher_session (running in other fiber)
void publish( std::string const& msg) {
std::unique_lock< boost::fibers::mutex > lk( mtx_);
std::memset( data_, '\0', sizeof( data_));
std::memcpy( data_, msg.c_str(), (std::min)(max_length, msg.size()));
cond_.notify_one();
}
private:
tcp::socket socket_;
registry & reg_;
boost::fibers::mutex mtx_;
boost::fibers::condition_variable cond_;
// fixed size message
char data_[max_length];
};
subscriptions::~subscriptions() {
for ( subscriber_session_ptr s : subscribers_) {
s->publish("<fini>");
}
}
void
subscriptions::publish( std::string const& msg) {
for ( subscriber_session_ptr s : subscribers_) {
s->publish( msg);
}
}
// a publisher publishes messages on its queue
// subscriber might register to this queue to get the published messages
class publisher_session : public std::enable_shared_from_this< publisher_session > {
public:
explicit publisher_session( std::shared_ptr< boost::asio::io_context > const& io_context, registry & reg) :
socket_( * io_context),
reg_( reg) {
}
tcp::socket& socket() {
return socket_;
}
// this function is executed inside the fiber
void run() {
std::string queue;
try {
boost::system::error_code ec;
// fixed size message
char data[max_length];
// read first message == queue name
// async_ready() returns if the the complete message is read
// until this the fiber is suspended until the complete message
// is read int the given buffer 'data'
boost::asio::async_read(
socket_,
boost::asio::buffer( data),
boost::fibers::asio::yield[ec]);
if ( ec) {
throw std::runtime_error("no queue from publisher");
}
// first message ist equal to the queue name the publisher
// publishes to
queue = data;
// register the new queue
reg_.register_queue( queue);
// start publishing messages
for (;;) {
// read message from publisher asyncronous
// async_read() suspends this fiber until the complete emssage is read
// and stored in the given buffer 'data'
boost::asio::async_read(
socket_,
boost::asio::buffer( data),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
// publish message to all subscribers
reg_.publish( queue, std::string( data) );
}
} catch ( std::exception const& e) {
std::cerr << "publisher [" << queue << "] failed: " << e.what() << std::endl;
}
// close socket
socket_.close();
// unregister queue
reg_.unregister_queue( queue);
}
private:
tcp::socket socket_;
registry & reg_;
};
typedef std::shared_ptr< publisher_session > publisher_session_ptr;
// function accepts connections requests from clients acting as a publisher
void accept_publisher( std::shared_ptr< boost::asio::io_context > const& io_context,
unsigned short port,
registry & reg) {
// create TCP-acceptor
tcp::acceptor acceptor( * io_context, tcp::endpoint( tcp::v4(), port) );
// loop for accepting connection requests
for (;;) {
boost::system::error_code ec;
// create new publisher-session
// this instance will be associated with one publisher
publisher_session_ptr new_publisher_session =
std::make_shared< publisher_session >( io_context, std::ref( reg) );
// async. accept of new connection request
// this function will suspend this execution context (fiber) until a
// connection was established, after returning from this function a new client (publisher)
// is connected
acceptor.async_accept(
new_publisher_session->socket(),
boost::fibers::asio::yield[ec]);
if ( ! ec) {
// run the new publisher in its own fiber (one fiber for one client)
boost::fibers::fiber(
std::bind( & publisher_session::run, new_publisher_session) ).detach();
}
}
}
// function accepts connections requests from clients acting as a subscriber
void accept_subscriber( std::shared_ptr< boost::asio::io_context > const& io_context,
unsigned short port,
registry & reg) {
// create TCP-acceptor
tcp::acceptor acceptor( * io_context, tcp::endpoint( tcp::v4(), port) );
// loop for accepting connection requests
for (;;) {
boost::system::error_code ec;
// create new subscriber-session
// this instance will be associated with one subscriber
subscriber_session_ptr new_subscriber_session =
std::make_shared< subscriber_session >( io_context, std::ref( reg) );
// async. accept of new connection request
// this function will suspend this execution context (fiber) until a
// connection was established, after returning from this function a new client (subscriber)
// is connected
acceptor.async_accept(
new_subscriber_session->socket(),
boost::fibers::asio::yield[ec]);
if ( ! ec) {
// run the new subscriber in its own fiber (one fiber for one client)
boost::fibers::fiber(
std::bind( & subscriber_session::run, new_subscriber_session) ).detach();
}
}
}
int main( int argc, char* argv[]) {
try {
// create io_context for async. I/O
std::shared_ptr< boost::asio::io_context > io_context = std::make_shared< boost::asio::io_context >();
// register asio scheduler
boost::fibers::use_scheduling_algorithm< boost::fibers::asio::round_robin >( io_context);
// registry for queues and its subscription
registry reg;
// create an acceptor for publishers, run it as fiber
boost::fibers::fiber(
accept_publisher, std::ref( io_context), 9997, std::ref( reg) ).detach();
// create an acceptor for subscribers, run it as fiber
boost::fibers::fiber(
accept_subscriber, std::ref( io_context), 9998, std::ref( reg) ).detach();
// dispatch
io_context->run();
return EXIT_SUCCESS;
} catch ( std::exception const& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return EXIT_FAILURE;
}
|