SOIL C++
C++ Unified Device Interface
Broadcast.cpp
Go to the documentation of this file.
1#include "Broadcast.h"
2#include "Exception.h"
3
4
6{
7 threads.reset(new boost::thread_group);
8 work.reset(new boost::asio::io_service::work(io_service));
9 for (int i = 0; i < n_threads; i++)
10 {
11 threads->create_thread(boost::bind(&boost::asio::io_service::run, &io_service));
12 }
13 socket.reset(new boost::asio::ip::udp::socket(io_service));//, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0)));
14 socket->open(boost::asio::ip::udp::v4());
15 boost::asio::socket_base::reuse_address reuse_address(true);
16 socket->set_option(boost::asio::socket_base::reuse_address(true));
17 //socket->set_option(boost::asio::socket_base::broadcast(true));
18
19 endpoints.resize(0);
20}
21
22
24{
25 if (socket.use_count() > 0)
26 {
27 if (socket->is_open())
28 {
29 socket->shutdown(boost::asio::ip::udp::socket::shutdown_both);
30 socket->close();
31 }
32 socket.reset();
33 endpoints.resize(0);
34 }
35 work.reset();
36 threads->join_all();
37 threads.reset();
38}
39
40void UDP::Broadcast::add_client(std::string ip, unsigned int port)
41{
42 std::unique_lock<std::mutex> lock(endpoints_mutex);
43 try
44 {
45 std::vector<boost::asio::ip::udp::endpoint>::iterator it;
46 boost::asio::ip::address address = boost::asio::ip::address::from_string(ip);
47 for (it = endpoints.begin(); it != endpoints.end(); it++)
48 {
49 if ((*it).address() == address)
50 {
51 throw UDP::Exception("Client is already among broadcast recipients!");
52 }
53 }
54 endpoints.push_back(boost::asio::ip::udp::endpoint(address, port));
55 }
56 catch (std::exception& exception)
57 {
58 std::string message = std::string("Error while adding UDP client: ") + exception.what();
59 throw UDP::Exception(message.c_str());
60 }
61}
62
64{
65 std::unique_lock<std::mutex> lock(endpoints_mutex);
66 try
67 {
68 std::vector<boost::asio::ip::udp::endpoint>::iterator it;
69 boost::asio::ip::address address = boost::asio::ip::address::from_string(ip);
70 for (it = endpoints.begin(); it != endpoints.end(); it++)
71 {
72 if ((*it).address() == address)
73 {
74 break;
75 }
76 }
77 if (it == endpoints.end())
78 {
79 throw UDP::Exception("Client is not in broadcast list!");
80 }
81 endpoints.erase(it);
82 }
83 catch (std::exception& exception)
84 {
85 std::string error_message = std::string("Error while removing UDP client: ") + exception.what();
86 throw UDP::Exception(error_message.c_str());
87 }
88}
89
90void UDP::Broadcast::_send(std::string message)
91{
92 if (socket.use_count() == 0)
93 {
94 return;
95 }
96 if (socket->is_open())
97 {
98 std::unique_lock<std::mutex> lock(endpoints_mutex);
99 std::shared_ptr<std::string> local_message(new std::string(message));
100 std::vector<boost::asio::ip::udp::endpoint>::iterator it;
101 for (it = endpoints.begin(); it != endpoints.end(); it++)
102 {
103 socket->send_to(boost::asio::buffer(*local_message), *it);
104 //socket->send_to(boost::asio::buffer(*local_message), *it, boost::bind(&UDP::Broadcast::handle, this, message, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
105 }
106 }
107}
108
109void UDP::Broadcast::send(std::string message)
110{
111 io_service.post(boost::bind(&UDP::Broadcast::_send, this, message + "\r\n"));
112}
113
114void UDP::Broadcast::send(std::vector<std::string> messages)
115{
116 std::string message = "";
117 for (std::vector<std::string>::iterator it = messages.begin(); it != messages.end(); it++)
118 {
119 message += (*it) + "\r\n";
120 }
121 io_service.post(boost::bind(&UDP::Broadcast::_send, this, message));
122}
123
124/*
125void UDP::Broadcast::handle(std::shared_ptr<std::string> message, const boost::system::error_code & error, std::size_t bytes)
126{
127 std::string error_message = std::string("Error while sending UDP: ") + error.message();
128 throw UDP::Exception(error_message.c_str());
129}
130*/
131
133{
134 for (UDP::Configuration::iterator it = config.clients.begin(); it != config.clients.end(); it++)
135 {
136 this->add_client(it->first, it->second);
137 }
138}
void remove_client(std::string ip)
Remove UDP client.
Definition: Broadcast.cpp:63
Broadcast(int n_threads=1)
Constructor.
Definition: Broadcast.cpp:5
void configure(UDP::Configuration config)
Configure Broadcast.
Definition: Broadcast.cpp:132
void send(std::string message)
Send Message.
Definition: Broadcast.cpp:109
~Broadcast()
Destructor.
Definition: Broadcast.cpp:23
void add_client(std::string ip, unsigned int port)
Add UDP client.
Definition: Broadcast.cpp:40
UDP Broadcast Configuration.
Definition: Configuration.h:14
std::map< std::string, int >::iterator iterator
Iterator.
Definition: Configuration.h:21
std::map< std::string, int > clients
List of clients.
Definition: Configuration.h:28
UDP Broadcast Exception.
Definition: Exception.h:13