Changeset 4853 for source/ariba
- Timestamp:
- Jul 10, 2009, 3:16:49 PM (15 years ago)
- Location:
- source/ariba/utility/bootstrap/modules/periodicbroadcast
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
source/ariba/utility/bootstrap/modules/periodicbroadcast/PeriodicBroadcast.cpp
r4851 r4853 53 53 54 54 #include "PeriodicBroadcast.h" 55 #include "PeriodicBroadcastMessage.h"56 55 57 56 namespace ariba { … … 60 59 use_logging_cpp(PeriodicBroadcast); 61 60 const long PeriodicBroadcast::timerinterval = 1000; 61 const unsigned int PeriodicBroadcast::serverport_v4 = 5634; 62 const unsigned int PeriodicBroadcast::serverport_v6 = 5636; 62 63 63 PeriodicBroadcast::PeriodicBroadcast(BootstrapInformationCallback* _callback) : BootstrapModule(_callback) { 64 PeriodicBroadcast::PeriodicBroadcast(BootstrapInformationCallback* _callback) 65 : BootstrapModule(_callback), 66 server(io_service, &newRemoteServices, &newRemoteServicesMutex) { 64 67 } 65 68 … … 121 124 ServiceList::iterator iend = localServices.end(); 122 125 123 for( ; i != iend; i++){ 124 125 PeriodicBroadcastMessage msg( i->second.name, i->second.info1, i->second.info2, i->second.info3 ); 126 Data data = data_serialize( msg, DEFAULT_V ); 127 uint8_t* pnt = data.getBuffer(); 128 size_t len = data.getLength(); 129 130 131 // TODO: ÃŒber socket rauspusten 132 133 } 126 for( ; i != iend; i++) 127 server.sendservice( i->second ); 134 128 } 135 129 -
source/ariba/utility/bootstrap/modules/periodicbroadcast/PeriodicBroadcast.h
r4851 r4853 45 45 #include <string> 46 46 #include <iostream> 47 #include <boost/asio.hpp> 47 48 #include <boost/foreach.hpp> 48 49 #include <boost/thread/mutex.hpp> … … 51 52 #include "ariba/utility/logging/Logging.h" 52 53 #include "ariba/utility/system/Timer.h" 54 #include "PeriodicBroadcastMessage.h" 53 55 54 56 using std::map; 55 57 using std::string; 58 using boost::asio::ip::udp; 56 59 57 60 namespace ariba { … … 81 84 82 85 static const long timerinterval; 86 static const unsigned int serverport_v4; 87 static const unsigned int serverport_v6; 83 88 84 89 typedef struct _Service { … … 93 98 boost::mutex localServicesMutex; 94 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 95 200 }; 96 201
Note:
See TracChangeset
for help on using the changeset viewer.