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

Last change on this file since 5284 was 4934, checked in by Christoph Mayer, 15 years ago
File size: 5.8 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 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;
61const long PeriodicBroadcast::servicetimeout = 3000;
62const unsigned int PeriodicBroadcast::serverport_v4 = 5634;
63const unsigned int PeriodicBroadcast::serverport_v6 = 5636;
64
65PeriodicBroadcast::PeriodicBroadcast(BootstrapInformationCallback* _callback)
66 : BootstrapModule(_callback),
67 server(io_service, &newRemoteServices, &newRemoteServicesMutex) {
68
69}
70
71PeriodicBroadcast::~PeriodicBroadcast(){
72}
73
74void PeriodicBroadcast::threadFunc(PeriodicBroadcast* obj){
75 obj->io_service.run();
76}
77
78string PeriodicBroadcast::getName(){
79 return "PeriodicBroadcast";
80}
81
82string PeriodicBroadcast::getInformation(){
83 return "periodic broadcasting of service information";
84}
85
86bool PeriodicBroadcast::isFunctional(){
87 return true;
88}
89
90void PeriodicBroadcast::start(){
91 io_service_thread = new boost::thread(
92 boost::bind(&PeriodicBroadcast::threadFunc, this) );
93
94 Timer::setInterval( timerinterval );
95 Timer::start();
96}
97
98void PeriodicBroadcast::stop(){
99 io_service.stop();
100 io_service_thread->join();
101 delete io_service_thread;
102 io_service_thread = NULL;
103
104 Timer::stop();
105
106 boost::mutex::scoped_lock lock( localServicesMutex );
107 localServices.clear();
108}
109
110void PeriodicBroadcast::publishService(string name, string info1, string info2, string info3){
111 Service service;
112
113 service.name = name;
114 service.info1 = info1;
115 service.info2 = info2;
116 service.info3 = info3;
117
118 boost::mutex::scoped_lock lock( localServicesMutex );
119 localServices.insert( std::make_pair(name, service) );
120}
121
122void PeriodicBroadcast::revokeService(string name){
123 boost::mutex::scoped_lock lock( localServicesMutex );
124
125 ServiceList::iterator i = localServices.find( name );
126 if( i != localServices.end() ) localServices.erase( name );
127}
128
129void PeriodicBroadcast::eventFunction(){
130 sendLocalServices();
131 updateRemoteServices();
132}
133
134void PeriodicBroadcast::sendLocalServices(){
135 boost::mutex::scoped_lock lock( localServicesMutex );
136
137 ServiceList::iterator i = localServices.begin();
138 ServiceList::iterator iend = localServices.end();
139
140 for( ; i != iend; i++)
141 server.sendservice( i->second );
142}
143
144void PeriodicBroadcast::updateRemoteServices(){
145
146 // cleanup the services that timed out
147 // so they are seen of as new after timeout
148 {
149 boost::mutex::scoped_lock lock( remoteServicesMutex );
150 bool deleted;
151
152 do {
153 deleted = false;
154
155 ServiceList::iterator i = remoteServices.begin();
156 ServiceList::iterator iend = remoteServices.end();
157
158 for( ; i != iend; i++ ){
159
160 if( time(NULL) > (i->second.lastseen + servicetimeout) ){
161 remoteServices.erase( i );
162 deleted = true;
163 break;
164 }
165 }
166
167 }while(deleted);
168 }
169
170 // check if we received new services:
171 // check remoteServices against newRemoteServices
172 {
173 boost::mutex::scoped_lock lock( newRemoteServicesMutex );
174 typedef std::pair<string,Service> mapitem;
175
176 BOOST_FOREACH( mapitem item, newRemoteServices ){
177
178 string name = item.first;
179 Service service = item.second;
180
181 ServiceList::iterator i = remoteServices.find( name );
182 if( i != remoteServices.end() ) {
183 // update the item lastseen time
184 i->second.lastseen = service.lastseen;
185 continue;
186 }
187
188 {
189 // insert the new item as new, lastseen has been set in the
190 // receive function, as this timer only runs in intervals
191 boost::mutex::scoped_lock lock2( remoteServicesMutex );
192 remoteServices.insert( std::make_pair(name, service) );
193 }
194
195 callback->onBootstrapServiceFound(name, service.info1, service.info2, service.info3);
196 }
197
198 // we have checked and transfered all new items
199 newRemoteServices.clear();
200 }
201}
202
203}} //namespace ariba, utility
Note: See TracBrowser for help on using the repository browser.