source: source/ariba/utility/bootstrap/modules/periodicbroadcast/PeriodicBroadcast.cpp@ 4925

Last change on this file since 4925 was 4924, checked in by huebsch, 15 years ago

-bootstrap und ablauf

File size: 5.8 KB
RevLine 
[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 co// [License]
17// The Ariba-Underlay Copyright
18//
19// Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
20//
21// Institute of Telematics
22// UniversitÀt Karlsruhe (TH)
23// Zirkel 2, 76128 Karlsruhe
24// Germany
25//
26// Redistribution and use in source and binary forms, with or without
27// modification, are permitted provided that the following conditions are
28// met:
29//
30// 1. Redistributions of source code must retain the above copyright
31// notice, this list of conditions and the following disclaimer.
32// 2. Redistributions in binary form must reproduce the above copyright
33// notice, this list of conditions and the following disclaimer in the
34// documentation and/or other materials provided with the distribution.
35//
36// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
37// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
40// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
41// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47//
48// The views and conclusions contained in the software and documentation
49// are those of the authors and should not be interpreted as representing
50// official policies, either expressed or implied, of the Institute of
51// Telematics.
52// [License]
53
54#include "PeriodicBroadcast.h"
55
56namespace ariba {
57namespace utility {
58
59use_logging_cpp(PeriodicBroadcast);
60const long PeriodicBroadcast::timerinterval = 1000;
[4866]61const long PeriodicBroadcast::servicetimeout = 3000;
[4853]62const unsigned int PeriodicBroadcast::serverport_v4 = 5634;
63const unsigned int PeriodicBroadcast::serverport_v6 = 5636;
[4850]64
[4853]65PeriodicBroadcast::PeriodicBroadcast(BootstrapInformationCallback* _callback)
66 : BootstrapModule(_callback),
67 server(io_service, &newRemoteServices, &newRemoteServicesMutex) {
[4920]68
[4924]69 io_service_thread = new boost::thread(
70 boost::bind(&PeriodicBroadcast::threadFunc, this) );
[4850]71}
72
73PeriodicBroadcast::~PeriodicBroadcast(){
[4920]74 io_service.stop();
[4924]75 io_service_thread->join();
76 delete io_service_thread;
77 io_service_thread = NULL;
[4850]78}
79
[4924]80void PeriodicBroadcast::threadFunc(PeriodicBroadcast* obj){
81 obj->io_service.run();
82}
83
[4850]84string PeriodicBroadcast::getName(){
85 return "PeriodicBroadcast";
86}
87
88string PeriodicBroadcast::getInformation(){
89 return "periodic broadcasting of service information";
90}
91
92bool PeriodicBroadcast::isFunctional(){
93 return true;
94}
95
96void PeriodicBroadcast::start(){
97 Timer::setInterval( timerinterval );
98 Timer::start();
99}
100
101void PeriodicBroadcast::stop(){
102 Timer::stop();
103
104 boost::mutex::scoped_lock lock( localServicesMutex );
105 localServices.clear();
106}
107
108void PeriodicBroadcast::publishService(string name, string info1, string info2, string info3){
109 Service service;
110
111 service.name = name;
112 service.info1 = info1;
113 service.info2 = info2;
114 service.info3 = info3;
115
116 boost::mutex::scoped_lock lock( localServicesMutex );
117 localServices.insert( std::make_pair(name, service) );
118}
119
120void PeriodicBroadcast::revokeService(string name){
121 boost::mutex::scoped_lock lock( localServicesMutex );
122
123 ServiceList::iterator i = localServices.find( name );
124 if( i != localServices.end() ) localServices.erase( name );
125}
126
127void PeriodicBroadcast::eventFunction(){
128 sendLocalServices();
129 updateRemoteServices();
130}
131
132void PeriodicBroadcast::sendLocalServices(){
[4851]133 boost::mutex::scoped_lock lock( localServicesMutex );
[4850]134
[4851]135 ServiceList::iterator i = localServices.begin();
136 ServiceList::iterator iend = localServices.end();
137
[4853]138 for( ; i != iend; i++)
139 server.sendservice( i->second );
[4850]140}
141
142void PeriodicBroadcast::updateRemoteServices(){
143
[4866]144 // cleanup the services that timed out
145 // so they are seen of as new after timeout
146 {
147 boost::mutex::scoped_lock lock( remoteServicesMutex );
148 bool deleted;
149
150 do {
151 deleted = false;
152
153 ServiceList::iterator i = remoteServices.begin();
154 ServiceList::iterator iend = remoteServices.end();
155
156 for( ; i != iend; i++ ){
157
158 if( time(NULL) > (i->second.lastseen + servicetimeout) ){
159 remoteServices.erase( i );
160 deleted = true;
161 break;
162 }
163 }
164
165 }while(deleted);
166 }
167
168 // check if we received new services:
169 // check remoteServices against newRemoteServices
170 {
171 boost::mutex::scoped_lock lock( newRemoteServicesMutex );
172 typedef std::pair<string,Service> mapitem;
173
174 BOOST_FOREACH( mapitem item, newRemoteServices ){
175
176 string name = item.first;
177 Service service = item.second;
178
179 ServiceList::iterator i = remoteServices.find( name );
180 if( i != remoteServices.end() ) {
181 // update the item lastseen time
182 i->second.lastseen = service.lastseen;
183 continue;
184 }
185
186 {
187 // insert the new item as new, lastseen has been set in the
188 // receive function, as this timer only runs in intervals
189 boost::mutex::scoped_lock lock2( remoteServicesMutex );
190 remoteServices.insert( std::make_pair(name, service) );
191 }
192
193 callback->onBootstrapServiceFound(name, service.info1, service.info2, service.info3);
194 }
195
196 // we have checked and transfered all new items
197 newRemoteServices.clear();
198 }
[4850]199}
200
201}} //namespace ariba, utility
Note: See TracBrowser for help on using the repository browser.