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