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