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