An Overlay-based
Virtual Network Substrate
SpoVNet

source: source/ariba/AribaModule.cpp @ 2454

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

fixed some bugs

File size: 6.0 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::AribaModule() {
61
62        // init with default values
63        this->underlay_abs = NULL;
64        this->ip_addr = NULL;
65        this->tcp_port = 41402;
66        this->udp_port = 41402;
67        this->started = false;
68}
69
70AribaModule::~AribaModule() {
71}
72
73string 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
88void 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
108void 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        std::cout << "added bootstrap info: " << getBootstrapHints() << std::endl;
135}
136
137const 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// @see Module.h
150void AribaModule::initialize() {
151
152        // preconditions
153        assert(!started);
154
155        // init variables
156        underlay_abs = NULL;
157}
158
159// @see Module.h
160void AribaModule::start() {
161
162        // preconditions
163        assert(underlay_abs == NULL);
164        assert(!started);
165
166        // create the base communication component
167        started = true;
168        underlay_abs = new interface::UnderlayAbstraction();
169}
170
171// @see Module.h
172void AribaModule::stop() {
173
174        // preconditions
175        assert(underlay_abs != NULL);
176        assert(started);
177
178        // delete base communication component
179        started = false;
180        delete underlay_abs;
181}
182
183// @see Module.h
184string AribaModule::getName() const {
185        return "ariba";
186}
187
188// @see Module.h
189void AribaModule::setProperty(string key, string value) {
190        if (key == "ip.addr") {
191                if (ip_addr != NULL) delete ip_addr;
192                communication::IPv4Locator* loc = new communication::IPv4Locator();
193                *loc = communication::IPv4Locator::fromString(value);
194                ip_addr = loc;
195        }
196        else if (key == "tcp.port") tcp_port = stoi(value);
197        else if (key == "udp.port") udp_port = stoi(value);
198        else if (key == "bootstrap.hints") addBootstrapHints(value);
199}
200
201// @see Module.h
202const string AribaModule::getProperty(string key) const {
203        if (key == "ip.addr") return ip_addr->toString();
204        else if (key == "tcp.port") return ultos(tcp_port);
205        else if (key == "udp.port") return ultos(udp_port);
206        else if (key == "bootstrap.hints") return getBootstrapHints();
207}
208
209// @see Module.h
210const vector<string> AribaModule::getProperties() const {
211        vector<string> properties;
212        properties.push_back("ip.addr");
213        properties.push_back("tcp.port");
214        properties.push_back("udp.port");
215        properties.push_back("hints");
216        return properties;
217}
218
219} // namespace ariba
Note: See TracBrowser for help on using the repository browser.