| 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 |  | 
|---|
| 46 | // ariba includes | 
|---|
| 47 | #include "ariba/SideportListener.h" | 
|---|
| 48 | #include "ariba/utility/misc/Helper.h" | 
|---|
| 49 | #include "ariba/utility/misc/StringFormat.h" | 
|---|
| 50 | #include "ariba/communication/BaseCommunication.h" | 
|---|
| 51 | #include "ariba/communication/EndpointDescriptor.h" | 
|---|
| 52 |  | 
|---|
| 53 | using namespace ariba::utility::Helper; | 
|---|
| 54 | using ariba::communication::BaseCommunication; | 
|---|
| 55 | using ariba::communication::EndpointDescriptor; | 
|---|
| 56 |  | 
|---|
| 57 | namespace ariba { | 
|---|
| 58 |  | 
|---|
| 59 | use_logging_cpp(AribaModule); | 
|---|
| 60 |  | 
|---|
| 61 | AribaModule::AribaModule() | 
|---|
| 62 | : base_comm(NULL), sideport_sniffer(NULL), started(false) { | 
|---|
| 63 |  | 
|---|
| 64 | endpoints = "tcp{41322};rfcomm{10};"; | 
|---|
| 65 |  | 
|---|
| 66 | //srand( time(NULL) ); | 
|---|
| 67 | //uint16_t tcpport = (rand() + 1024) % 50000; | 
|---|
| 68 | //uint16_t udpport = (rand() + 1024) % 50000; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | AribaModule::~AribaModule() { | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | string AribaModule::getBootstrapHints(const Name& spoVNetName) const { | 
|---|
| 75 | std::ostringstream o; | 
|---|
| 76 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) { | 
|---|
| 77 | o << info.spovnetName.toString() << "{"; | 
|---|
| 78 | int i=0; | 
|---|
| 79 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) { | 
|---|
| 80 | if (i!=0) o << ","; | 
|---|
| 81 | if( node.desc != NULL ) o << node.desc->toString(); | 
|---|
| 82 | i++; | 
|---|
| 83 | } | 
|---|
| 84 | o << "}"; | 
|---|
| 85 | } | 
|---|
| 86 | return o.str(); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | void AribaModule::addBootstrapHints(string boot_info) { | 
|---|
| 90 | using namespace boost::xpressive; | 
|---|
| 91 | using namespace ariba::utility::string_format; | 
|---|
| 92 | using namespace ariba::utility::Helper; | 
|---|
| 93 | using namespace std; | 
|---|
| 94 |  | 
|---|
| 95 | smatch match; | 
|---|
| 96 | if (regex_search(boot_info, match, robjects)) { | 
|---|
| 97 | regex_nav nav = match; | 
|---|
| 98 | for (int i = 0; i < nav.size(); i++) { | 
|---|
| 99 | string type = nav[i][robject_id].str(); | 
|---|
| 100 | string data = nav[i][robject_data].str(); | 
|---|
| 101 | data = data.substr(1, data.size() - 2); | 
|---|
| 102 | Name name(type); | 
|---|
| 103 | EndpointDescriptor* desc = EndpointDescriptor::fromString(data); | 
|---|
| 104 | logging_debug("Added bootstap info for " << type << ": " << desc->toString() ); | 
|---|
| 105 | addBootstrapNode(name, desc); | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void AribaModule::addBootstrapNode(const Name& spovnet, | 
|---|
| 111 | communication::EndpointDescriptor* desc) { | 
|---|
| 112 |  | 
|---|
| 113 | // set bootstrap node | 
|---|
| 114 | BootstrapNode node; | 
|---|
| 115 | node.timestamp = 0; | 
|---|
| 116 | node.desc = desc; | 
|---|
| 117 | bool added = false; | 
|---|
| 118 |  | 
|---|
| 119 | // add node to existing bootstrap list | 
|---|
| 120 | BOOST_FOREACH( BootstrapInfo& info, bootstrapNodes ){ | 
|---|
| 121 | if (info.spovnetName == spovnet) { | 
|---|
| 122 | info.nodes.push_back(node); | 
|---|
| 123 | added = true; | 
|---|
| 124 | break; | 
|---|
| 125 | } | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | // create new entry | 
|---|
| 129 | if (!added) { | 
|---|
| 130 | BootstrapInfo info; | 
|---|
| 131 | info.spovnetName = spovnet; | 
|---|
| 132 | info.nodes.push_back(node); | 
|---|
| 133 | bootstrapNodes.push_back(info); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | logging_debug( "Added bootstrap info: " << getBootstrapHints() ); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | const communication::EndpointDescriptor* AribaModule::getBootstrapNode( | 
|---|
| 140 | const Name& spovnet) const { | 
|---|
| 141 | BOOST_FOREACH( const BootstrapInfo& info, bootstrapNodes ) { | 
|---|
| 142 | if( info.spovnetName == spovnet ) { | 
|---|
| 143 | BOOST_FOREACH( const BootstrapNode& node, info.nodes ) { | 
|---|
| 144 | if( node.desc != NULL ) return node.desc; | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 | } | 
|---|
| 148 | return NULL; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void AribaModule::registerSideportListener(SideportListener* sideport){ | 
|---|
| 152 | sideport_sniffer = sideport; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | // @see Module.h | 
|---|
| 156 | void AribaModule::initialize() { | 
|---|
| 157 |  | 
|---|
| 158 | // preconditions | 
|---|
| 159 | assert(!started); | 
|---|
| 160 |  | 
|---|
| 161 | // init variables | 
|---|
| 162 | base_comm = NULL; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | // @see Module.h | 
|---|
| 166 | void AribaModule::start() { | 
|---|
| 167 |  | 
|---|
| 168 | // preconditions | 
|---|
| 169 | assert(base_comm == NULL); | 
|---|
| 170 | assert(!started); | 
|---|
| 171 |  | 
|---|
| 172 | // create the base communication component | 
|---|
| 173 | started = true; | 
|---|
| 174 | base_comm = new BaseCommunication(); | 
|---|
| 175 | base_comm->setEndpoints(endpoints); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | // @see Module.h | 
|---|
| 179 | void AribaModule::stop() { | 
|---|
| 180 |  | 
|---|
| 181 | // preconditions | 
|---|
| 182 | assert(base_comm != NULL); | 
|---|
| 183 | assert(started); | 
|---|
| 184 |  | 
|---|
| 185 | // delete base communication component | 
|---|
| 186 | started = false; | 
|---|
| 187 | delete base_comm; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | // @see Module.h | 
|---|
| 191 | string AribaModule::getName() const { | 
|---|
| 192 | return "ariba"; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | // @see Module.h | 
|---|
| 196 | void AribaModule::setProperty(string key, string value) { | 
|---|
| 197 | if (key == "endpoints") endpoints = value; | 
|---|
| 198 | else if (key == "bootstrap.hints") addBootstrapHints(value); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | // @see Module.h | 
|---|
| 202 | const string AribaModule::getProperty(string key) const { | 
|---|
| 203 | if (key == "endpoints") return endpoints; // TODO: return local endpoints | 
|---|
| 204 | else if (key == "bootstrap.hints") return getBootstrapHints(); | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | // @see Module.h | 
|---|
| 208 | const vector<string> AribaModule::getProperties() const { | 
|---|
| 209 | vector<string> properties; | 
|---|
| 210 | properties.push_back("endpoints"); | 
|---|
| 211 | properties.push_back("bootstrap.hints"); | 
|---|
| 212 | return properties; | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | } // namespace ariba | 
|---|