[4850] | 1 | // [License]
|
---|
| 2 | // The Ariba-Underlay Copyright
|
---|
| 3 | //
|
---|
| 4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
| 5 | //
|
---|
| 6 | // Institute of Telematics
|
---|
| 7 | // UniversitÀt Karlsruhe (TH)
|
---|
| 8 | // Zirkel 2, 76128 Karlsruhe
|
---|
| 9 | // Germany
|
---|
| 10 | //
|
---|
| 11 | // Redistribution and use in source and binary forms, with or without
|
---|
| 12 | // modification, are permitted provided that the following conditions are
|
---|
| 13 | // met:
|
---|
| 14 | //
|
---|
| 15 | // 1. Redistributions of source code must retain the above copyright
|
---|
| 16 | // notice, this list of conditions and the following disclaimer.
|
---|
| 17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | // notice, this list of conditions and the following disclaimer in the
|
---|
| 19 | // documentation and/or other materials provided with the distribution.
|
---|
| 20 | //
|
---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
| 22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
| 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
| 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
| 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 32 | //
|
---|
| 33 | // The views and conclusions contained in the software and documentation
|
---|
| 34 | // are those of the authors and should not be interpreted as representing
|
---|
| 35 | // official policies, either expressed or implied, of the Institute of
|
---|
| 36 | // Telematics.
|
---|
| 37 | // [License]
|
---|
| 38 |
|
---|
| 39 | #ifndef __PERIODIC_BROADCAST_H
|
---|
| 40 | #define __PERIODIC_BROADCAST_H
|
---|
| 41 |
|
---|
| 42 | #include "ariba/config.h"
|
---|
| 43 |
|
---|
[4851] | 44 | #include <map>
|
---|
| 45 | #include <string>
|
---|
[4866] | 46 | #include <ctime>
|
---|
[4850] | 47 | #include <iostream>
|
---|
[4853] | 48 | #include <boost/asio.hpp>
|
---|
[4851] | 49 | #include <boost/foreach.hpp>
|
---|
[4850] | 50 | #include <boost/thread/mutex.hpp>
|
---|
| 51 | #include <boost/thread/thread.hpp>
|
---|
| 52 | #include "ariba/utility/bootstrap/modules/BootstrapModule.h"
|
---|
| 53 | #include "ariba/utility/logging/Logging.h"
|
---|
| 54 | #include "ariba/utility/system/Timer.h"
|
---|
[4853] | 55 | #include "PeriodicBroadcastMessage.h"
|
---|
[4850] | 56 |
|
---|
| 57 | using std::map;
|
---|
| 58 | using std::string;
|
---|
[4866] | 59 | using std::cout;
|
---|
[4853] | 60 | using boost::asio::ip::udp;
|
---|
[4850] | 61 |
|
---|
| 62 | namespace ariba {
|
---|
| 63 | namespace utility {
|
---|
| 64 |
|
---|
| 65 | class PeriodicBroadcast : public BootstrapModule, public Timer {
|
---|
| 66 | use_logging_h(PeriodicBroadcast);
|
---|
| 67 | public:
|
---|
| 68 | PeriodicBroadcast(BootstrapInformationCallback* _callback);
|
---|
| 69 | virtual ~PeriodicBroadcast();
|
---|
| 70 |
|
---|
| 71 | virtual void start();
|
---|
| 72 | virtual void stop();
|
---|
| 73 |
|
---|
| 74 | virtual string getName();
|
---|
| 75 | virtual string getInformation();
|
---|
| 76 | virtual bool isFunctional();
|
---|
| 77 | virtual void publishService(string name, string info1, string info2, string info3);
|
---|
| 78 | virtual void revokeService(string name);
|
---|
| 79 |
|
---|
| 80 | protected:
|
---|
| 81 | virtual void eventFunction();
|
---|
| 82 |
|
---|
| 83 | private:
|
---|
| 84 | void sendLocalServices();
|
---|
| 85 | void updateRemoteServices();
|
---|
| 86 |
|
---|
[4866] | 87 | static const long timerinterval; // used to send out updates on our services and check for new services
|
---|
| 88 | static const long servicetimeout; // timeout after that a service is dead when we did not receive updates
|
---|
[4853] | 89 | static const unsigned int serverport_v4;
|
---|
| 90 | static const unsigned int serverport_v6;
|
---|
[4850] | 91 |
|
---|
| 92 | typedef struct _Service {
|
---|
| 93 | string name;
|
---|
| 94 | string info1;
|
---|
| 95 | string info2;
|
---|
| 96 | string info3;
|
---|
[4866] | 97 | time_t lastseen;
|
---|
| 98 |
|
---|
| 99 | _Service()
|
---|
| 100 | : name(""), info1(""), info2(""), info3(""), lastseen(0){
|
---|
| 101 | }
|
---|
[5414] | 102 |
|
---|
[5420] | 103 | _Service(const string& _name, const string& _info1,
|
---|
| 104 | const string& _info2, const string& _info3, const time_t& _lastseen = 0)
|
---|
| 105 | : name(_name), info1(_info1), info2(_info2), info3(_info3), lastseen(_lastseen){
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[5414] | 108 | _Service(const _Service& rh)
|
---|
| 109 | : name(rh.name), info1(rh.info1), info2(rh.info2),
|
---|
| 110 | info3(rh.info3), lastseen(rh.lastseen){
|
---|
| 111 | }
|
---|
[4850] | 112 | } Service;
|
---|
| 113 |
|
---|
| 114 | typedef map<string,Service> ServiceList;
|
---|
| 115 | ServiceList localServices;
|
---|
| 116 | boost::mutex localServicesMutex;
|
---|
| 117 |
|
---|
[4853] | 118 | ServiceList remoteServices;
|
---|
| 119 | boost::mutex remoteServicesMutex;
|
---|
| 120 |
|
---|
| 121 | ServiceList newRemoteServices;
|
---|
| 122 | boost::mutex newRemoteServicesMutex;
|
---|
| 123 |
|
---|
| 124 | boost::asio::io_service io_service;
|
---|
[4924] | 125 | boost::thread* io_service_thread;
|
---|
| 126 | static void threadFunc(PeriodicBroadcast* obj);
|
---|
[4853] | 127 |
|
---|
| 128 | class udp_server {
|
---|
| 129 | private:
|
---|
[4920] | 130 | udp::socket socket_v4;
|
---|
| 131 | udp::socket socket_v6;
|
---|
[4853] | 132 | udp::endpoint remote_endpoint_;
|
---|
[5464] | 133 | boost::array<char, 1500> recv_buffer_4;
|
---|
| 134 | boost::array<char, 1500> recv_buffer_6;
|
---|
[4853] | 135 | ServiceList* services;
|
---|
| 136 | boost::mutex* servicesmutex;
|
---|
| 137 |
|
---|
| 138 | public:
|
---|
| 139 | udp_server(boost::asio::io_service& io_service, ServiceList* _services, boost::mutex* _servicesmutex)
|
---|
[4872] | 140 | : services(_services), servicesmutex(_servicesmutex),
|
---|
[4920] | 141 | socket_v4(io_service), socket_v6(io_service) {
|
---|
[4853] | 142 |
|
---|
[4872] | 143 | boost::asio::ip::udp::endpoint listen_endpoint_v4(
|
---|
[4920] | 144 | boost::asio::ip::address_v4::any(),
|
---|
[4872] | 145 | PeriodicBroadcast::serverport_v4);
|
---|
[4853] | 146 |
|
---|
[4872] | 147 | boost::asio::ip::udp::endpoint listen_endpoint_v6(
|
---|
[4920] | 148 | boost::asio::ip::address_v6::any(),
|
---|
[4872] | 149 | PeriodicBroadcast::serverport_v6);
|
---|
| 150 |
|
---|
[4896] | 151 | boost::system::error_code err;
|
---|
[4872] | 152 |
|
---|
[4920] | 153 | err = socket_v4.open( listen_endpoint_v4.protocol(), err );
|
---|
| 154 | if(err) logging_warn("failed opening ipv4 socket");
|
---|
[4872] | 155 |
|
---|
[4920] | 156 | err = socket_v6.open( listen_endpoint_v6.protocol(), err );
|
---|
| 157 | if(err) logging_warn("failed opening ipv6 socket");
|
---|
[4872] | 158 |
|
---|
[4920] | 159 | err = socket_v4.set_option( boost::asio::ip::udp::socket::reuse_address(true), err );
|
---|
| 160 | if(err) logging_warn("failed setting reuse address option on ipv4 socket");
|
---|
[4896] | 161 |
|
---|
[4920] | 162 | err = socket_v6.set_option( boost::asio::ip::udp::socket::reuse_address(true), err );
|
---|
| 163 | if(err) logging_warn("failed setting reuse address option on ipv6 socket");
|
---|
[4896] | 164 |
|
---|
[4920] | 165 | err = socket_v4.set_option( boost::asio::socket_base::broadcast(true), err );
|
---|
| 166 | if(err) logging_warn("failed setting broadcast option on ipv4 socket");
|
---|
[4896] | 167 |
|
---|
[4920] | 168 | err = socket_v6.set_option( boost::asio::socket_base::broadcast(true), err );
|
---|
| 169 | if(err) logging_warn("failed setting broadcast option on ipv6 socket");
|
---|
[4896] | 170 |
|
---|
[4920] | 171 | err = socket_v4.bind( listen_endpoint_v4, err );
|
---|
| 172 | if(err) logging_warn("failed binding ipv4 socket");
|
---|
[4872] | 173 |
|
---|
[4921] | 174 | err = socket_v6.bind( listen_endpoint_v6, err );
|
---|
[4920] | 175 | if(err) logging_warn("failed binding ipv6 socket");
|
---|
| 176 |
|
---|
[5465] | 177 | start_receive_4();
|
---|
| 178 | start_receive_6();
|
---|
[4853] | 179 | }
|
---|
| 180 |
|
---|
| 181 | void sendservice(Service service){
|
---|
| 182 |
|
---|
[5479] | 183 | PeriodicBroadcastMessage msg;
|
---|
| 184 |
|
---|
| 185 | msg.setName( service.name );
|
---|
| 186 | msg.setInfo1( service.info1 );
|
---|
| 187 | msg.setInfo2( service.info2 );
|
---|
| 188 | msg.setInfo3( service.info3 );
|
---|
| 189 |
|
---|
[4853] | 190 | Data data = data_serialize( msg, DEFAULT_V );
|
---|
| 191 | uint8_t* pnt = data.getBuffer();
|
---|
[4866] | 192 | size_t len = data.getLength() / 8;
|
---|
[4853] | 193 |
|
---|
[4896] | 194 | boost::system::error_code err;
|
---|
[4866] | 195 |
|
---|
[4853] | 196 | {
|
---|
| 197 | udp::endpoint endp(udp::v4(), PeriodicBroadcast::serverport_v4);
|
---|
| 198 | endp.address( boost::asio::ip::address_v4::broadcast() );
|
---|
[4920] | 199 | socket_v4.send_to( boost::asio::buffer(pnt, len), endp, 0, err );
|
---|
[4896] | 200 | if(err) logging_warn("failed sending message through ipv4 socket");
|
---|
[4853] | 201 | }
|
---|
[4920] | 202 | {
|
---|
[4853] | 203 | udp::endpoint endp(udp::v6(), PeriodicBroadcast::serverport_v6);
|
---|
| 204 | endp.address( boost::asio::ip::address_v6::from_string("ff02::1") );
|
---|
[4933] | 205 | socket_v6.send_to( boost::asio::buffer(pnt, len), endp, 0, err );
|
---|
| 206 | if(err) logging_warn("failed sending message through ipv6 socket");
|
---|
[4920] | 207 | }
|
---|
[4853] | 208 | }
|
---|
| 209 |
|
---|
| 210 | private:
|
---|
[5465] | 211 | void start_receive_4(){
|
---|
[4920] | 212 | socket_v4.async_receive_from(
|
---|
[5464] | 213 | boost::asio::buffer(recv_buffer_4), remote_endpoint_,
|
---|
| 214 | boost::bind(&udp_server::handle_receive_4, this,
|
---|
[4853] | 215 | boost::asio::placeholders::error,
|
---|
| 216 | boost::asio::placeholders::bytes_transferred));
|
---|
[5465] | 217 | }
|
---|
[4920] | 218 |
|
---|
[5465] | 219 | void start_receive_6(){
|
---|
[4853] | 220 | socket_v6.async_receive_from(
|
---|
[5464] | 221 | boost::asio::buffer(recv_buffer_6), remote_endpoint_,
|
---|
| 222 | boost::bind(&udp_server::handle_receive_6, this,
|
---|
[4853] | 223 | boost::asio::placeholders::error,
|
---|
| 224 | boost::asio::placeholders::bytes_transferred));
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[5464] | 227 | void handle_receive_4(const boost::system::error_code& error,
|
---|
[4853] | 228 | std::size_t bytes_transferred){
|
---|
| 229 |
|
---|
[5464] | 230 | if (!error || error == boost::asio::error::message_size)
|
---|
| 231 | handle_info(recv_buffer_4, bytes_transferred);
|
---|
| 232 | else
|
---|
| 233 | logging_warn("failed receiving broadcast data: " << error.message());
|
---|
[4853] | 234 |
|
---|
[5465] | 235 | start_receive_4();
|
---|
[5464] | 236 | }
|
---|
[4853] | 237 |
|
---|
[5464] | 238 | void handle_receive_6(const boost::system::error_code& error,
|
---|
| 239 | std::size_t bytes_transferred){
|
---|
[4853] | 240 |
|
---|
[5464] | 241 | if (!error || error == boost::asio::error::message_size)
|
---|
| 242 | handle_info(recv_buffer_6, bytes_transferred);
|
---|
| 243 | else
|
---|
| 244 | logging_warn("failed receiving broadcast data: " << error.message());
|
---|
[4853] | 245 |
|
---|
[5465] | 246 | start_receive_6();
|
---|
[5464] | 247 | }
|
---|
[5421] | 248 |
|
---|
[5464] | 249 | void handle_info(boost::array<char, 1500>& buffer, std::size_t length){
|
---|
| 250 | PeriodicBroadcastMessage msg;
|
---|
[4853] | 251 |
|
---|
[5464] | 252 | Data data( (uint8_t*)buffer.data(), length*8 );
|
---|
| 253 | data_deserialize( msg, data );
|
---|
[4896] | 254 |
|
---|
[5464] | 255 | { // insert new found service
|
---|
| 256 | boost::mutex::scoped_lock( *servicesmutex );
|
---|
[4896] | 257 |
|
---|
[5464] | 258 | ServiceList::iterator it = services->find( msg.getName() );
|
---|
| 259 | if( it != services->end() ){
|
---|
| 260 |
|
---|
| 261 | it->second.info1 = msg.getInfo1();
|
---|
| 262 | it->second.info2 = msg.getInfo2();
|
---|
| 263 | it->second.info3 = msg.getInfo3();
|
---|
| 264 | it->second.lastseen = time(NULL);
|
---|
| 265 |
|
---|
| 266 | } else {
|
---|
| 267 | Service s( msg.getName(), msg.getInfo1(), msg.getInfo2(), msg.getInfo3(), time(NULL));
|
---|
| 268 | services->insert( std::make_pair(msg.getName(), s) );
|
---|
| 269 | }
|
---|
[4853] | 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | void handle_send(boost::shared_ptr<std::string> /*message*/,
|
---|
[4896] | 274 | const boost::system::error_code& error,
|
---|
[4853] | 275 | std::size_t /*bytes_transferred*/){
|
---|
[4920] | 276 |
|
---|
[4896] | 277 | if(error)
|
---|
| 278 | logging_warn("failed sending out message");
|
---|
[4853] | 279 | }
|
---|
| 280 | };
|
---|
| 281 |
|
---|
| 282 | udp_server server;
|
---|
[4850] | 283 | };
|
---|
| 284 |
|
---|
| 285 | }} //namespace ariba, utility
|
---|
| 286 |
|
---|
| 287 | #endif // __BLUETOOTH_SDP_H
|
---|