| 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 conditions and the following disclaimer.
|
---|
| 17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | // notice, this list of conditions and the following disclaimer in the
|
---|
| 19 | // documentation and/or other materials provided with the distribution.
|
---|
| 20 | //
|
---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
| 22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
| 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
| 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
| 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 32 | //
|
---|
| 33 | // The views and conclusions contained in the software and documentation
|
---|
| 34 | // are those of the authors and should not be interpreted as representing
|
---|
| 35 | // official policies, either expressed or implied, of the Institute of
|
---|
| 36 | // Telematics.
|
---|
| 37 | // [License]
|
---|
| 38 |
|
---|
| 39 | #include "AribaModule.h"
|
---|
| 40 |
|
---|
| 41 | // boost/std includes
|
---|
| 42 | #include <assert.h>
|
---|
| 43 | #include <boost/regex.hpp>
|
---|
| 44 | #include <boost/foreach.hpp>
|
---|
| 45 | #include <boost/algorithm/string.hpp>
|
---|
| 46 |
|
---|
| 47 | // ariba includes
|
---|
| 48 | #include "ariba/SideportListener.h"
|
---|
| 49 | #include "ariba/utility/misc/Helper.h"
|
---|
| 50 | #include "ariba/utility/misc/StringFormat.h"
|
---|
| 51 | #include "ariba/communication/BaseCommunication.h"
|
---|
| 52 | #include "ariba/communication/EndpointDescriptor.h"
|
---|
| 53 |
|
---|
| 54 | using namespace ariba::utility::Helper;
|
---|
| 55 | using ariba::communication::BaseCommunication;
|
---|
| 56 | using ariba::communication::EndpointDescriptor;
|
---|
| 57 |
|
---|
| 58 | namespace ariba {
|
---|
| 59 |
|
---|
| 60 | use_logging_cpp(AribaModule);
|
---|
| 61 | const string AribaModule::BootstrapMechanismNames[5] = {"invalid", "static", "broadcast", "mdns", "sdp"};
|
---|
| 62 |
|
---|
| 63 | AribaModule::AribaModule()
|
---|
| 64 | : started(false), base_comm(NULL), sideport_sniffer(NULL) {
|
---|
| 65 | endpoints = "tcp{41322};rfcomm{10};";
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | AribaModule::~AribaModule() {
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | string AribaModule::getBootstrapHints(const Name& spoVNetName) const {
|
---|
| 72 | std::ostringstream o;
|
---|
| 73 | int i=0;
|
---|
| 74 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
|
---|
| 75 | o << info.spovnetName.toString() << "{";
|
---|
| 76 | if (i!=0) o << ",";
|
---|
| 77 |
|
---|
| 78 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
|
---|
| 79 | if( node.desc != NULL )
|
---|
| 80 | o << node.desc->toString();
|
---|
| 81 | else if(node.mechanism == BootstrapMechanismBroadcast
|
---|
| 82 | || node.mechanism == BootstrapMechanismMulticastDNS
|
---|
| 83 | || node.mechanism == BootstrapMechanismSDP){
|
---|
| 84 | o << BootstrapMechanismNames[node.mechanism];
|
---|
| 85 | if( !node.info.empty() ) o << "{" << node.info << "}";
|
---|
| 86 | o << ";";
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | o << "}";
|
---|
| 90 | i++;
|
---|
| 91 | }
|
---|
| 92 | return o.str();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void AribaModule::addBootstrapHints(string boot_info) {
|
---|
| 96 | using namespace boost::xpressive;
|
---|
| 97 | using namespace ariba::utility::string_format;
|
---|
| 98 | using namespace ariba::utility::Helper;
|
---|
| 99 | using namespace std;
|
---|
| 100 |
|
---|
| 101 | boost::erase_all(boot_info, " ");
|
---|
| 102 | boost::erase_all(boot_info, "\t");
|
---|
| 103 | boost::erase_all(boot_info, "\n");
|
---|
| 104 | boost::erase_all(boot_info, "\r");
|
---|
| 105 |
|
---|
| 106 | smatch match;
|
---|
| 107 | if (regex_search(boot_info, match, robjects)) {
|
---|
| 108 | regex_nav nav = match;
|
---|
| 109 | for (int i = 0; i < nav.size(); i++) {
|
---|
| 110 | string type = nav[i][robject_id].str();
|
---|
| 111 | string data = nav[i][robject_data].str();
|
---|
| 112 | data = data.substr(1, data.size() - 2);
|
---|
| 113 | Name name(type);
|
---|
| 114 |
|
---|
| 115 | // find static bootstrap info --> BootstrapMechanismStatic
|
---|
| 116 | EndpointDescriptor* desc = EndpointDescriptor::fromString(data);
|
---|
| 117 | addBootstrapNode(name, desc, "", BootstrapMechanismStatic);
|
---|
| 118 |
|
---|
| 119 | // find automatic bootstrap info --> {BootstrapMechanismBroadcast,
|
---|
| 120 | // BootstrapMechanismMulticastDNS,BootstrapMechanismSDP}
|
---|
| 121 | typedef vector< string > split_vector_type;
|
---|
| 122 | split_vector_type splitvec;
|
---|
| 123 | boost::split( splitvec, data, boost::is_any_of(";") );
|
---|
| 124 | for(unsigned int i=0; i<splitvec.size(); i++){
|
---|
| 125 | string x = splitvec[i];
|
---|
| 126 | split_vector_type innervec;
|
---|
| 127 |
|
---|
| 128 | boost::split( innervec, x, boost::is_any_of("{}") );
|
---|
| 129 | BootstrapNode node;
|
---|
| 130 | if(innervec.size() < 1) continue;
|
---|
| 131 |
|
---|
| 132 | if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismBroadcast])
|
---|
| 133 | node.mechanism = BootstrapMechanismBroadcast;
|
---|
| 134 | else if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismMulticastDNS])
|
---|
| 135 | node.mechanism = BootstrapMechanismMulticastDNS;
|
---|
| 136 | else if(innervec[0] == BootstrapMechanismNames[BootstrapMechanismSDP])
|
---|
| 137 | node.mechanism = BootstrapMechanismSDP;
|
---|
| 138 | else
|
---|
| 139 | continue;
|
---|
| 140 |
|
---|
| 141 | if(innervec.size() > 1)
|
---|
| 142 | node.info = innervec[1];
|
---|
| 143 |
|
---|
| 144 | this->addBootstrapNode(name, node);
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | logging_info( "Added bootstrap hints: " << getBootstrapHints() );
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | void AribaModule::addBootstrapNode(const Name& spovnet,
|
---|
| 153 | communication::EndpointDescriptor* desc, const string& info,
|
---|
| 154 | const BootstrapMechanism& mechanism) {
|
---|
| 155 | BootstrapNode node(0, desc, mechanism, info);
|
---|
| 156 | addBootstrapNode(spovnet, node);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | void AribaModule::addBootstrapNode(const Name& spovnet, const BootstrapNode& node){
|
---|
| 160 | bool added = false;
|
---|
| 161 |
|
---|
| 162 | // add node to existing bootstrap list
|
---|
| 163 | BOOST_FOREACH( BootstrapInfo& info, bootstrapNodes ){
|
---|
| 164 | if (info.spovnetName == spovnet) {
|
---|
| 165 | info.nodes.push_back(node);
|
---|
| 166 | added = true;
|
---|
| 167 | break;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | // create new entry
|
---|
| 172 | if (!added) {
|
---|
| 173 | BootstrapInfo info;
|
---|
| 174 | info.spovnetName = spovnet;
|
---|
| 175 | info.nodes.push_back(node);
|
---|
| 176 | bootstrapNodes.push_back(info);
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | const communication::EndpointDescriptor* AribaModule::getBootstrapNode(
|
---|
| 181 | const Name& spovnet, const BootstrapMechanism mechanism) const {
|
---|
| 182 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
|
---|
| 183 | if( info.spovnetName == spovnet ) {
|
---|
| 184 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
|
---|
| 185 | if( node.mechanism == mechanism && node.desc != NULL )
|
---|
| 186 | return node.desc;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | return NULL;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | string AribaModule::getBootstrapInfo(
|
---|
| 194 | const Name& spovnet, const BootstrapMechanism mechanism) const {
|
---|
| 195 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
|
---|
| 196 | if( info.spovnetName == spovnet ) {
|
---|
| 197 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
|
---|
| 198 | if( node.mechanism == mechanism && node.desc != NULL )
|
---|
| 199 | return node.info;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | return string();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | vector<AribaModule::BootstrapMechanism> AribaModule::getBootstrapMechanisms(
|
---|
| 208 | const Name& spovnet) const {
|
---|
| 209 | vector<AribaModule::BootstrapMechanism> ret;
|
---|
| 210 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) {
|
---|
| 211 | if( info.spovnetName == spovnet ) {
|
---|
| 212 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) {
|
---|
| 213 | if(std::find(ret.begin(), ret.end(), node.mechanism) == ret.end())
|
---|
| 214 | ret.push_back(node.mechanism);
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | return ret;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | void AribaModule::registerSideportListener(SideportListener* sideport){
|
---|
| 222 | sideport_sniffer = sideport;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | // @see Module.h
|
---|
| 226 | void AribaModule::initialize() {
|
---|
| 227 |
|
---|
| 228 | // preconditions
|
---|
| 229 | assert(!started);
|
---|
| 230 |
|
---|
| 231 | // init variables
|
---|
| 232 | base_comm = NULL;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | // @see Module.h
|
---|
| 236 | void AribaModule::start() {
|
---|
| 237 |
|
---|
| 238 | // preconditions
|
---|
| 239 | assert(base_comm == NULL);
|
---|
| 240 | assert(!started);
|
---|
| 241 |
|
---|
| 242 | // create the base communication component
|
---|
| 243 | started = true;
|
---|
| 244 | base_comm = new BaseCommunication();
|
---|
| 245 | base_comm->setEndpoints(endpoints);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | // @see Module.h
|
---|
| 249 | void AribaModule::stop() {
|
---|
| 250 |
|
---|
| 251 | // preconditions
|
---|
| 252 | assert(base_comm != NULL);
|
---|
| 253 | assert(started);
|
---|
| 254 |
|
---|
| 255 | // delete base communication component
|
---|
| 256 | started = false;
|
---|
| 257 | delete base_comm;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | // @see Module.h
|
---|
| 261 | string AribaModule::getName() const {
|
---|
| 262 | return "ariba";
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | // @see Module.h
|
---|
| 266 | void AribaModule::setProperty(string key, string value) {
|
---|
| 267 | if (key == "endpoints") endpoints = value;
|
---|
| 268 | else if (key == "bootstrap.hints") addBootstrapHints(value);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | // @see Module.h
|
---|
| 272 | const string AribaModule::getProperty(string key) const {
|
---|
| 273 | if (key == "endpoints") return endpoints; // TODO: return local endpoints
|
---|
| 274 | else if (key == "bootstrap.hints") return getBootstrapHints();
|
---|
| 275 | return "";
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | // @see Module.h
|
---|
| 279 | const vector<string> AribaModule::getProperties() const {
|
---|
| 280 | vector<string> properties;
|
---|
| 281 | properties.push_back("endpoints");
|
---|
| 282 | properties.push_back("bootstrap.hints");
|
---|
| 283 | return properties;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | const string AribaModule::getLocalEndpoints() {
|
---|
| 287 | return base_comm->getEndpointDescriptor().toString();
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | } // namespace ariba
|
---|