source: source/ariba/AribaModule.cpp@ 3056

Last change on this file since 3056 was 3037, checked in by Christoph Mayer, 16 years ago

-jede Menge fixes und Umstellungen
-angefangen ariba/interface los zu werden, erste dateien sind weg

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