source: source/ariba/utility/bootstrap/modules/periodicbroadcast/PeriodicBroadcast.h@ 4872

Last change on this file since 4872 was 4872, checked in by Christoph Mayer, 15 years ago

ipv4 periodic broadcast

File size: 7.2 KB
Line 
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
44#include <map>
45#include <string>
46#include <ctime>
47#include <iostream>
48#include <boost/asio.hpp>
49#include <boost/foreach.hpp>
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"
55#include "PeriodicBroadcastMessage.h"
56
57using std::map;
58using std::string;
59using std::cout;
60using boost::asio::ip::udp;
61
62namespace ariba {
63namespace utility {
64
65class PeriodicBroadcast : public BootstrapModule, public Timer {
66 use_logging_h(PeriodicBroadcast);
67public:
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
80protected:
81 virtual void eventFunction();
82
83private:
84 void sendLocalServices();
85 void updateRemoteServices();
86
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
89 static const unsigned int serverport_v4;
90 static const unsigned int serverport_v6;
91
92 typedef struct _Service {
93 string name;
94 string info1;
95 string info2;
96 string info3;
97 time_t lastseen;
98
99 _Service()
100 : name(""), info1(""), info2(""), info3(""), lastseen(0){
101 }
102 } Service;
103
104 typedef map<string,Service> ServiceList;
105 ServiceList localServices;
106 boost::mutex localServicesMutex;
107
108 ServiceList remoteServices;
109 boost::mutex remoteServicesMutex;
110
111 ServiceList newRemoteServices;
112 boost::mutex newRemoteServicesMutex;
113
114 boost::asio::io_service io_service;
115
116 class udp_server {
117 private:
118 udp::socket socket_v4;
119 udp::socket socket_v6;
120 udp::endpoint remote_endpoint_;
121 boost::array<char, 1500> recv_buffer_;
122 ServiceList* services;
123 boost::mutex* servicesmutex;
124
125 public:
126 udp_server(boost::asio::io_service& io_service, ServiceList* _services, boost::mutex* _servicesmutex)
127 : services(_services), servicesmutex(_servicesmutex),
128 socket_v4(io_service), socket_v6(io_service) {
129
130 boost::asio::ip::udp::endpoint listen_endpoint_v4(
131 boost::asio::ip::address_v4::broadcast(),
132 PeriodicBroadcast::serverport_v4);
133
134 boost::asio::ip::udp::endpoint listen_endpoint_v6(
135 boost::asio::ip::address_v6::from_string("ff02::1"),
136 PeriodicBroadcast::serverport_v6);
137
138 socket_v4.open( listen_endpoint_v4.protocol() );
139 socket_v6.open( listen_endpoint_v6.protocol() );
140
141 socket_v4.set_option( boost::asio::ip::udp::socket::reuse_address(true) );
142 socket_v6.set_option( boost::asio::ip::udp::socket::reuse_address(true) );
143
144 socket_v4.set_option( boost::asio::socket_base::broadcast(true) );
145 socket_v6.set_option( boost::asio::socket_base::broadcast(true) );
146
147 socket_v4.bind( listen_endpoint_v4 );
148 //socket_v6.bind( listen_endpoint_v6 );
149
150 start_receive();
151 }
152
153 void sendservice(Service service){
154
155 PeriodicBroadcastMessage msg( service.name, service.info1, service.info2, service.info3 );
156 Data data = data_serialize( msg, DEFAULT_V );
157 uint8_t* pnt = data.getBuffer();
158 size_t len = data.getLength() / 8;
159 boost::system::error_code ignored_error;
160
161 cout << "-----------> sending out " << data << std::endl;
162
163 {
164 udp::endpoint endp(udp::v4(), PeriodicBroadcast::serverport_v4);
165 endp.address( boost::asio::ip::address_v4::broadcast() );
166 socket_v4.send_to( boost::asio::buffer(pnt, len), endp, 0, ignored_error );
167 }
168 {
169 udp::endpoint endp(udp::v6(), PeriodicBroadcast::serverport_v6);
170 endp.address( boost::asio::ip::address_v6::from_string("ff02::1") );
171 socket_v6.send_to( boost::asio::buffer(pnt, len), endp, 0, ignored_error );
172 }
173 }
174
175 private:
176 void start_receive(){
177 socket_v4.async_receive_from(
178 boost::asio::buffer(recv_buffer_), remote_endpoint_,
179 boost::bind(&udp_server::handle_receive, this,
180 boost::asio::placeholders::error,
181 boost::asio::placeholders::bytes_transferred));
182
183 socket_v6.async_receive_from(
184 boost::asio::buffer(recv_buffer_), remote_endpoint_,
185 boost::bind(&udp_server::handle_receive, this,
186 boost::asio::placeholders::error,
187 boost::asio::placeholders::bytes_transferred));
188 }
189
190 void handle_receive(const boost::system::error_code& error,
191 std::size_t bytes_transferred){
192
193 if (!error || error == boost::asio::error::message_size){
194
195 PeriodicBroadcastMessage msg;
196 Data data( (uint8_t*)recv_buffer_.data(), bytes_transferred*8 );
197 data_deserialize( msg, data );
198
199 cout << "-----------> received " << data << std::endl;
200
201 { // insert new found service
202 boost::mutex::scoped_lock( *servicesmutex );
203
204 ServiceList::iterator it = services->find( msg.getName() );
205 if( it != services->end() ) services->erase( it );
206
207 Service s;
208 s.info1 = msg.getInfo1();
209 s.info2 = msg.getInfo2();
210 s.info3 = msg.getInfo3();
211 s.lastseen = time(NULL);
212 services->insert( std::make_pair(msg.getName(), s) );
213 }
214
215 start_receive();
216 }
217 }
218
219 void handle_send(boost::shared_ptr<std::string> /*message*/,
220 const boost::system::error_code& /*error*/,
221 std::size_t /*bytes_transferred*/){
222 }
223
224 };
225
226 udp_server server;
227
228};
229
230}} //namespace ariba, utility
231
232#endif // __BLUETOOTH_SDP_H
Note: See TracBrowser for help on using the repository browser.