An Overlay-based
Virtual Network Substrate
SpoVNet

source: source/ariba/AribaModule.cpp @ 2452

Last change on this file since 2452 was 2452, checked in by mies, 15 years ago

implemented bootstrap info parser

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