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

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

erste version für periodic broadcasting für ipv6 und ipv6

File size: 6.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 <iostream>
47#include <boost/asio.hpp>
48#include <boost/foreach.hpp>
49#include <boost/thread/mutex.hpp>
50#include <boost/thread/thread.hpp>
51#include "ariba/utility/bootstrap/modules/BootstrapModule.h"
52#include "ariba/utility/logging/Logging.h"
53#include "ariba/utility/system/Timer.h"
54#include "PeriodicBroadcastMessage.h"
55
56using std::map;
57using std::string;
58using boost::asio::ip::udp;
59
60namespace ariba {
61namespace utility {
62
63class PeriodicBroadcast : public BootstrapModule, public Timer {
64 use_logging_h(PeriodicBroadcast);
65public:
66 PeriodicBroadcast(BootstrapInformationCallback* _callback);
67 virtual ~PeriodicBroadcast();
68
69 virtual void start();
70 virtual void stop();
71
72 virtual string getName();
73 virtual string getInformation();
74 virtual bool isFunctional();
75 virtual void publishService(string name, string info1, string info2, string info3);
76 virtual void revokeService(string name);
77
78protected:
79 virtual void eventFunction();
80
81private:
82 void sendLocalServices();
83 void updateRemoteServices();
84
85 static const long timerinterval;
86 static const unsigned int serverport_v4;
87 static const unsigned int serverport_v6;
88
89 typedef struct _Service {
90 string name;
91 string info1;
92 string info2;
93 string info3;
94 } Service;
95
96 typedef map<string,Service> ServiceList;
97 ServiceList localServices;
98 boost::mutex localServicesMutex;
99
100 ServiceList remoteServices;
101 boost::mutex remoteServicesMutex;
102
103 ServiceList newRemoteServices;
104 boost::mutex newRemoteServicesMutex;
105
106 boost::asio::io_service io_service;
107
108 class udp_server {
109 private:
110 udp::socket socket_v4;
111 udp::socket socket_v6;
112 udp::endpoint remote_endpoint_;
113 boost::array<char, 1500> recv_buffer_;
114 ServiceList* services;
115 boost::mutex* servicesmutex;
116
117 public:
118 udp_server(boost::asio::io_service& io_service, ServiceList* _services, boost::mutex* _servicesmutex)
119 : socket_v4(io_service, udp::endpoint(udp::v4(), PeriodicBroadcast::serverport_v4)),
120 socket_v6(io_service, udp::endpoint(udp::v6(), PeriodicBroadcast::serverport_v6)),
121 services(_services), servicesmutex(_servicesmutex) {
122
123 boost::asio::socket_base::broadcast option(true);
124 socket_v4.set_option(option);
125 socket_v6.set_option(option);
126
127 start_receive();
128 }
129
130 void sendservice(Service service){
131
132 PeriodicBroadcastMessage msg( service.name, service.info1, service.info2, service.info3 );
133 Data data = data_serialize( msg, DEFAULT_V );
134 uint8_t* pnt = data.getBuffer();
135 size_t len = data.getLength();
136 boost::system::error_code ignored_error;
137
138 {
139 udp::endpoint endp(udp::v4(), PeriodicBroadcast::serverport_v4);
140 endp.address( boost::asio::ip::address_v4::broadcast() );
141 socket_v4.send_to( boost::asio::buffer(pnt, len), endp, 0, ignored_error );
142 }
143 {
144 udp::endpoint endp(udp::v6(), PeriodicBroadcast::serverport_v6);
145 endp.address( boost::asio::ip::address_v6::from_string("ff02::1") );
146 socket_v6.send_to( boost::asio::buffer(pnt, len), endp, 0, ignored_error );
147 }
148 }
149
150 private:
151 void start_receive(){
152 socket_v4.async_receive_from(
153 boost::asio::buffer(recv_buffer_), remote_endpoint_,
154 boost::bind(&udp_server::handle_receive, this,
155 boost::asio::placeholders::error,
156 boost::asio::placeholders::bytes_transferred));
157
158 socket_v6.async_receive_from(
159 boost::asio::buffer(recv_buffer_), remote_endpoint_,
160 boost::bind(&udp_server::handle_receive, this,
161 boost::asio::placeholders::error,
162 boost::asio::placeholders::bytes_transferred));
163 }
164
165 void handle_receive(const boost::system::error_code& error,
166 std::size_t bytes_transferred){
167
168 if (!error || error == boost::asio::error::message_size){
169
170 PeriodicBroadcastMessage msg;
171 Data data( (uint8_t*)recv_buffer_.data(), bytes_transferred*8 );
172 data_deserialize( msg, data );
173
174 { // insert new found service
175 boost::mutex::scoped_lock( *servicesmutex );
176
177 ServiceList::iterator it = services->find( msg.getName() );
178 if( it != services->end() ) services->erase( it );
179
180 Service s;
181 s.info1 = msg.getInfo1();
182 s.info2 = msg.getInfo2();
183 s.info3 = msg.getInfo3();
184 services->insert( std::make_pair(msg.getName(), s) );
185 }
186
187 start_receive();
188 }
189 }
190
191 void handle_send(boost::shared_ptr<std::string> /*message*/,
192 const boost::system::error_code& /*error*/,
193 std::size_t /*bytes_transferred*/){
194 }
195
196 };
197
198 udp_server server;
199
200};
201
202}} //namespace ariba, utility
203
204#endif // __BLUETOOTH_SDP_H
Note: See TracBrowser for help on using the repository browser.